📄 classfornamechecker.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.classfile.util;import proguard.classfile.*;import proguard.classfile.attribute.*;import proguard.classfile.attribute.visitor.AttributeVisitor;import proguard.classfile.attribute.annotation.*;import proguard.classfile.constant.*;import proguard.classfile.constant.visitor.ConstantVisitor;import proguard.classfile.instruction.*;import proguard.classfile.instruction.visitor.InstructionVisitor;import proguard.classfile.visitor.*;/** * This class can check whether method references point to <code>Class.forName</code> * or <code>.class</code>. * * @author Eric Lafortune */class ClassForNameCheckerextends SimplifiedVisitorimplements ConstantVisitor, MemberVisitor, AttributeVisitor, InstructionVisitor{ // These fields acts as a return variables and status variables for the visitors. private boolean isClassForNameInvocation; private boolean insideDotClassMethod; private boolean firstInstructionOk; /** * Creates a new ClassForNameChecker. */ public ClassForNameChecker() { } /** * Checks whether the specified method reference points to * <code>Class.forName</code> or <code>.class</code>. * @param clazz the class in which the method reference * is located. * @param methodrefConstantIndex the index of the method reference in the * constant pool. * @return a boolean that indicates whether the reference points to either * method. */ public boolean check(Clazz clazz, int methodrefConstantIndex) { isClassForNameInvocation = false; clazz.constantPoolEntryAccept(methodrefConstantIndex, this); return isClassForNameInvocation; } // Implementations for ConstantVisitor. /** * Checks whether the given method reference points to * <code>Class.forName</code> or <code>.class</code>. */ public void visitMethodrefConstant(Clazz clazz, MethodrefConstant methodrefConstant) { String className = methodrefConstant.getClassName(clazz); String methodName = methodrefConstant.getName(clazz); String methodType = methodrefConstant.getType(clazz); // Is it a reference to "Class Class.forName(String)"? isClassForNameInvocation = className .equals(ClassConstants.INTERNAL_CLASS_NAME_JAVA_LANG_CLASS) && methodName.equals(ClassConstants.INTERNAL_METHOD_NAME_CLASS_FOR_NAME) && methodType.equals(ClassConstants.INTERNAL_METHOD_TYPE_CLASS_FOR_NAME); if (isClassForNameInvocation || insideDotClassMethod) { return; } // Is it a reference to .class? This construct is typically implemented // as (static) "Class class$(String)" or "Class class$(String, boolean)". // First check the type, which has to be right to start with. isClassForNameInvocation = methodType.equals(ClassConstants.INTERNAL_METHOD_TYPE_DOT_CLASS_JAVAC) || methodType.equals(ClassConstants.INTERNAL_METHOD_TYPE_DOT_CLASS_JIKES); if (!isClassForNameInvocation) { return; } // Then check if the method perhaps still has its original "class$" name. isClassForNameInvocation = methodName.equals(ClassConstants.INTERNAL_METHOD_NAME_DOT_CLASS); if (isClassForNameInvocation) { return; } // We're still not sure it's not a reference to .class, since the // method name may have been changed or obfuscated. // Perform a more detailed analysis by looking at the referenced method // itself. Make sure we don't do this recursively. insideDotClassMethod = true; methodrefConstant.referencedMemberAccept(this); insideDotClassMethod = false; } // Implementations for MemberVisitor. /** * Checks whether the given method is a .class implementation. */ public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) { programMethod.attributesAccept(programClass, this); } // Implementations for AttributeVisitor. /** * Checks whether the given code is an implementation of class$(String) or * class$(String, boolean). */ public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) { // Check whether the first instruction recalls the first argument of // this method. firstInstructionOk = false; codeAttribute.instructionAccept(clazz, method, 0, this); // Continue checking whether the second instruction invokes // Class.forName. if (firstInstructionOk) { codeAttribute.instructionAccept(clazz, method, 1, this); } } // Implementations for InstructionVisitor. /** * Checks whether this is a valid first instruction for a .class implementation. */ public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction) { firstInstructionOk = variableInstruction.opcode == InstructionConstants.OP_ALOAD_0; } /** * Checks whether this is a valid second instruction for a .class implementation. */ public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction) { if (firstInstructionOk && constantInstruction.opcode == InstructionConstants.OP_INVOKESTATIC) { clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -