1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 package proguard.obfuscate;
22 
23 import proguard.classfile.*;
24 import proguard.classfile.attribute.*;
25 import proguard.classfile.attribute.visitor.AttributeVisitor;
26 import proguard.classfile.util.SimplifiedVisitor;
27 import proguard.classfile.visitor.*;
28 
29 import java.util.Arrays;
30 
31 /**
32  * This ClassVisitor removes attributes that are not marked as being used or
33  * required.
34  *
35  * @see AttributeUsageMarker
36  *
37  * @author Eric Lafortune
38  */
39 public class AttributeShrinker
40 extends      SimplifiedVisitor
41 implements   ClassVisitor,
42              MemberVisitor,
43              AttributeVisitor
44 {
45     // Implementations for ClassVisitor.
46 
visitProgramClass(ProgramClass programClass)47     public void visitProgramClass(ProgramClass programClass)
48     {
49         // Compact the array for class attributes.
50         programClass.u2attributesCount =
51             shrinkArray(programClass.attributes,
52                         programClass.u2attributesCount);
53 
54         // Compact the attributes in fields, methods, and class attributes,
55         programClass.fieldsAccept(this);
56         programClass.methodsAccept(this);
57         programClass.attributesAccept(this);
58     }
59 
60 
visitLibraryClass(LibraryClass libraryClass)61     public void visitLibraryClass(LibraryClass libraryClass)
62     {
63         // Library classes are left unchanged.
64     }
65 
66 
67     // Implementations for MemberVisitor.
68 
visitProgramMember(ProgramClass programClass, ProgramMember programMember)69     public void visitProgramMember(ProgramClass programClass, ProgramMember programMember)
70     {
71         // Compact the attributes array.
72         programMember.u2attributesCount =
73             shrinkArray(programMember.attributes,
74                         programMember.u2attributesCount);
75 
76         // Compact any attributes of the remaining attributes.
77         programMember.attributesAccept(programClass, this);
78     }
79 
80 
81     // Implementations for AttributeVisitor.
82 
visitAnyAttribute(Clazz clazz, Attribute attribute)83     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
84 
85 
visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)86     public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
87     {
88         // Compact the attributes array.
89         codeAttribute.u2attributesCount =
90             shrinkArray(codeAttribute.attributes,
91                         codeAttribute.u2attributesCount);
92     }
93 
94 
95     // Small utility methods.
96 
97     /**
98      * Removes all VisitorAccepter objects that are not marked as being used
99      * from the given array.
100      * @return the new number of VisitorAccepter objects.
101      */
shrinkArray(VisitorAccepter[] array, int length)102     private static int shrinkArray(VisitorAccepter[] array, int length)
103     {
104         int counter = 0;
105 
106         // Shift the used objects together.
107         for (int index = 0; index < length; index++)
108         {
109             if (AttributeUsageMarker.isUsed(array[index]))
110             {
111                 array[counter++] = array[index];
112             }
113         }
114 
115         // Clear the remaining array elements.
116         Arrays.fill(array, counter, length, null);
117 
118         return counter;
119     }
120 }
121