KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > MemberDescriptorSpecializer


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 library 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 library 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 Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.optimize;
22
23 import proguard.optimize.evaluation.StoringInvocationUnit;
24 import proguard.classfile.util.*;
25 import proguard.classfile.visitor.MemberVisitor;
26 import proguard.classfile.attribute.visitor.AttributeVisitor;
27 import proguard.classfile.attribute.Attribute;
28 import proguard.classfile.attribute.annotation.*;
29 import proguard.classfile.editor.*;
30 import proguard.classfile.*;
31 import proguard.evaluation.value.Value;
32
33 /**
34  * This MemberVisitor removes unused parameters descriptors of the methods that
35  * it visits.
36  *
37  * @see StoringInvocationUnit
38  * @see ClassReferenceFixer
39  * @author Eric Lafortune
40  */

41 public class MemberDescriptorSpecializer
42 extends SimplifiedVisitor
43 implements MemberVisitor
44 {
45     private static final boolean DEBUG = true;
46
47
48     private MemberVisitor extraParameterMemberVisitor;
49
50
51     /**
52      * Creates a new MethodDescriptorShrinker.
53      */

54     public MemberDescriptorSpecializer()
55     {
56         this(null);
57     }
58
59
60     /**
61      * Creates a new MethodDescriptorShrinker with an extra visitor.
62      * @param extraParameterMemberVisitor an optional extra visitor for all
63      * class members whose parameters have
64      * been specialized.
65      */

66     public MemberDescriptorSpecializer(MemberVisitor extraParameterMemberVisitor)
67     {
68         this.extraParameterMemberVisitor = extraParameterMemberVisitor;
69     }
70
71
72     // Implementations for MemberVisitor.
73

74     public void visitProgramField(ProgramClass programClass, ProgramField programField)
75     {
76         Value parameterValue = StoringInvocationUnit.getFieldValue(programField);
77         if (parameterValue.computationalType() == Value.TYPE_REFERENCE)
78         {
79             Clazz referencedClass = parameterValue.referenceValue().getReferencedClass();
80             if (referencedClass != null)
81             {
82                 programField.referencedClass = referencedClass;
83             }
84         }
85     }
86
87
88     public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
89     {
90         // All parameters of non-static methods are shifted by one in the local
91
// variable frame.
92
int firstParameterIndex =
93             (programMethod.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
94                 0 : 1;
95
96         int parameterCount =
97             ClassUtil.internalMethodParameterCount(programMethod.getDescriptor(programClass));
98
99         int classIndex = 0;
100
101         // Go over the parameters.
102
for (int parameterIndex = firstParameterIndex; parameterIndex < parameterCount; parameterIndex++)
103         {
104             Value parameterValue = StoringInvocationUnit.getMethodParameterValue(programMethod, parameterIndex);
105              if (parameterValue.computationalType() == Value.TYPE_REFERENCE)
106              {
107                  Clazz referencedClass = parameterValue.referenceValue().getReferencedClass();
108                  if (referencedClass != null)
109                  {
110                      if (DEBUG)
111                      {
112                          if (!referencedClass.equals(programMethod.referencedClasses[classIndex]))
113                          {
114                              System.out.println("MemberDescriptorSpecializer: "+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass));
115                              System.out.println(" "+programMethod.referencedClasses[classIndex].getName()+" -> "+referencedClass.getName());
116                          }
117                      }
118
119                      programMethod.referencedClasses[classIndex] = referencedClass;
120                  }
121
122                  classIndex++;
123              }
124         }
125     }
126 }
127
Popular Tags