1 21 package proguard.optimize.info; 22 23 import proguard.classfile.*; 24 import proguard.classfile.util.*; 25 import proguard.classfile.visitor.MemberVisitor; 26 import proguard.optimize.MethodDescriptorShrinker; 27 28 35 public class ParameterUsageMarker 36 extends SimplifiedVisitor 37 implements MemberVisitor 38 { 39 private static final boolean DEBUG = false; 40 41 42 private VariableUsageMarker variableUsageMarker = new VariableUsageMarker(); 43 44 45 47 public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) 48 { 49 int parameterSize = parameterSize(programClass, programMethod); 50 if (parameterSize > 0) 51 { 52 int accessFlags = programMethod.getAccessFlags(); 54 if ((accessFlags & ClassConstants.INTERNAL_ACC_ABSTRACT) == 0) 55 { 56 markUsedParameters(programMethod, -1L); 58 } 59 60 else if ((accessFlags & ClassConstants.INTERNAL_ACC_ABSTRACT) != 0) 62 { 63 markParameterUsed(programMethod, 0); 65 } 66 67 else 69 { 70 programMethod.attributesAccept(programClass, variableUsageMarker); 72 73 for (int index = 0; index < parameterSize; index++) 75 { 76 if (variableUsageMarker.isVariableUsed(index)) 77 { 78 markParameterUsed(programMethod, index); 79 } 80 } 81 82 if (programClass.mayHaveImplementations(programMethod) || 85 programMethod.getName(programClass).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT)) 86 { 87 markParameterUsed(programMethod, 0); 89 } 90 } 91 92 if (DEBUG) 93 { 94 System.out.print("ParameterUsageMarker: ["+programClass.getName() +"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"]: "); 95 for (int index = 0; index < parameterSize; index++) 96 { 97 System.out.print(isParameterUsed(programMethod, index) ? '+' : '-'); 98 } 99 System.out.println(); 100 } 101 102 } 103 104 setParameterSize(programMethod, parameterSize); 106 } 107 108 109 public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod) 110 { 111 if (libraryClass.mayHaveImplementations(libraryMethod)) 113 { 114 long usedParameters = -1L; 117 118 markUsedParameters(libraryMethod, usedParameters); 120 } 121 } 122 123 124 126 129 private static void setParameterSize(Method method, int parameterSize) 130 { 131 MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); 132 if (info != null) 133 { 134 info.setParameterSize(parameterSize); 135 } 136 } 137 138 139 142 public static int getParameterSize(Method method) 143 { 144 MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); 145 return info != null ? info.getParameterSize() : 0; 146 } 147 148 149 152 public static void markParameterUsed(Method method, int variableIndex) 153 { 154 MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); 155 if (info != null) 156 { 157 info.setParameterUsed(variableIndex); 158 } 159 } 160 161 162 165 public static void markUsedParameters(Method method, long usedParameters) 166 { 167 MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); 168 if (info != null) 169 { 170 info.setUsedParameters(info.getUsedParameters() | usedParameters); 171 } 172 } 173 174 175 178 public static boolean isParameterUsed(Method method, int variableIndex) 179 { 180 MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); 181 return info == null || 182 info.isParameterUsed(variableIndex); 183 } 184 185 186 189 public static long getUsedParameters(Method method) 190 { 191 MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); 192 return info != null ? info.getUsedParameters() : -1L; 193 } 194 195 196 199 private int parameterMask(int parameterSize) 200 { 201 return (1 << parameterSize) - 1; 202 } 203 204 205 209 private int parameterSize(Clazz clazz, Method method) 210 { 211 int parameterSize = ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz)); 212 if ((method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) == 0) 213 { 214 parameterSize++; 215 } 216 217 return parameterSize; 218 } 219 } 220 | Popular Tags |