KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > obfuscate > AttributeShrinker


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 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.attribute.annotation.*;
27 import proguard.classfile.visitor.*;
28 import proguard.classfile.util.SimplifiedVisitor;
29
30 /**
31  * This ClassVisitor removes attributes that are not marked as being used or
32  * required.
33  *
34  * @see AttributeUsageMarker
35  *
36  * @author Eric Lafortune
37  */

38 public class AttributeShrinker
39 extends SimplifiedVisitor
40 implements ClassVisitor,
41              MemberVisitor,
42              AttributeVisitor
43 {
44     // Implementations for ClassVisitor.
45

46     public void visitProgramClass(ProgramClass programClass)
47     {
48         // Compact the array for class attributes.
49
programClass.u2attributesCount =
50             shrinkArray(programClass.attributes,
51                         programClass.u2attributesCount);
52
53         // Compact the attributes in fields, methods, and class attributes,
54
programClass.fieldsAccept(this);
55         programClass.methodsAccept(this);
56         programClass.attributesAccept(this);
57     }
58
59
60     public void visitLibraryClass(LibraryClass libraryClass)
61     {
62         // Library classes are left unchanged.
63
}
64
65
66     // Implementations for MemberVisitor.
67

68     public void visitProgramMember(ProgramClass programClass, ProgramMember programMember)
69     {
70         // Compact the attributes array.
71
programMember.u2attributesCount =
72             shrinkArray(programMember.attributes,
73                         programMember.u2attributesCount);
74
75         // Compact any attributes of the remaining attributes.
76
programMember.attributesAccept(programClass, this);
77     }
78
79
80     // Implementations for AttributeVisitor.
81

82     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
83
84
85     public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
86     {
87         // Compact the attributes array.
88
codeAttribute.u2attributesCount =
89             shrinkArray(codeAttribute.attributes,
90                         codeAttribute.u2attributesCount);
91     }
92
93
94     // Small utility methods.
95

96     /**
97      * Removes all VisitorAccepter objects that are not marked as being used
98      * from the given array.
99      * @return the new number of VisitorAccepter objects.
100      */

101     private static int shrinkArray(VisitorAccepter[] array, int length)
102     {
103         int counter = 0;
104
105         // Shift the used objects together.
106
for (int index = 0; index < length; index++)
107         {
108             if (AttributeUsageMarker.isUsed(array[index]))
109             {
110                 array[counter++] = array[index];
111             }
112         }
113
114         // Clear the remaining array elements.
115
for (int index = counter; index < length; index++)
116         {
117             array[index] = null;
118         }
119
120         return counter;
121     }
122 }
123
Popular Tags