📄 parameterusagemarker.java
字号:
/* * ProGuard -- shrinking, optimization, obfuscation, and preverification * of Java bytecode. * * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package proguard.optimize.info;import proguard.classfile.*;import proguard.classfile.util.*;import proguard.classfile.visitor.*;import proguard.optimize.info.MethodOptimizationInfo;import proguard.optimize.MethodDescriptorShrinker;/** * This MemberVisitor counts the parameters and marks the used parameters * of the methods that it visits. It also marks the 'this' parameters of * methods that have a hierarchy. * * @author Eric Lafortune */public class ParameterUsageMarkerextends SimplifiedVisitorimplements MemberVisitor{ private VariableUsageMarker variableUsageMarker = new VariableUsageMarker(); // Implementations for MemberVisitor. public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) { int parameterSize = parameterSize(programClass, programMethod); if (parameterSize > 0) { long usedParameters = 0L; if ((programMethod.getAccessFlags() & ClassConstants.INTERNAL_ACC_ABSTRACT) == 0) { // Figure out the local variables that are used by the code. programMethod.attributesAccept(programClass, variableUsageMarker); // Mark the parameters that are used by the code. for (int index = 0; index < parameterSize; index++) { if (variableUsageMarker.isVariableUsed(index)) { usedParameters |= 1L << index; } } } // Mark the 'this' parameter if the method can have other implementations. if (programClass.mayHaveImplementations(programMethod)) { // All implementations must at least keep the 'this' parameter too. usedParameters |= 1L; } // Is it an <init> method? else if (programMethod.getName(programClass).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT)) { // Instance initializers always require the 'this' parameter. usedParameters |= 1L; // Mark the parameters preliminarily. markUsedParameters(programMethod, usedParameters); // Is there a name clash with some existing <init> method? if (programClass.findMethod(ClassConstants.INTERNAL_METHOD_NAME_INIT, MethodDescriptorShrinker.shrinkDescriptor(programClass, programMethod)) != null) { // Cancel the shrinking. Mark all parameters. usedParameters |= parameterMask(parameterSize); } } // Mark the parameters. markUsedParameters(programMethod, usedParameters); } // Set the parameter size. setParameterSize(programMethod, parameterSize); } public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod) { // Can the method have other implementations? if (libraryClass.mayHaveImplementations(libraryMethod)) { // All implementations must keep all parameters of this method, // including the 'this' parameter. long usedParameters = parameterMask(parameterSize(libraryClass, libraryMethod)); // Mark it. markUsedParameters(libraryMethod, usedParameters); } } // Small utility methods. /** * Sets the total size of the parameters. */ private static void setParameterSize(Method method, int parameterSize) { MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); if (info != null) { info.setParameterSize(parameterSize); } } /** * Returns the total size of the parameters. */ public static int getParameterSize(Method method) { MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); return info != null ? info.getParameterSize() : 0; } /** * Marks the given parameter as being used. */ private static void markParameterUsed(Method method, int variableIndex) { MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); if (info != null) { info.setParameterUsed(variableIndex); } } /** * Marks the given parameters as being used. */ private void markUsedParameters(Method method, long usedParameters) { MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); if (info != null) { info.setUsedParameters(info.getUsedParameters() | usedParameters); } } /** * Returns whether the given parameter is being used. */ public static boolean isParameterUsed(Method method, int variableIndex) { MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); return info == null || info.isParameterUsed(variableIndex); } /** * Returns which parameters are being used. */ public static long getUsedParameters(Method method) { MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method); return info != null ? info.getUsedParameters() : -1L; } /** * Returns a bit mask of 1-bits of the given size. */ private int parameterMask(int parameterSize) { return (1 << parameterSize) - 1; } /** * Returns the parameter size of the given method, including the 'this' * parameter, if any. */ private int parameterSize(Clazz clazz, Method method) { int parameterSize = ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz)); if ((method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) == 0) { parameterSize++; } return parameterSize; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -