dynamicmemberreferenceinitializer.java

来自「proguard 一个java的混淆器」· Java 代码 · 共 591 行 · 第 1/2 页

JAVA
591
字号
/* * 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.CodeAttribute;import proguard.classfile.attribute.visitor.AttributeVisitor;import proguard.classfile.constant.*;import proguard.classfile.constant.visitor.ConstantVisitor;import proguard.classfile.instruction.*;import proguard.classfile.instruction.visitor.InstructionVisitor;import proguard.classfile.visitor.*;import proguard.util.StringMatcher;/** * This InstructionVisitor initializes any constant * <code>Class.get[Declared]{Field,Method}</code> references of all instructions * it visits. More specifically, it fills out the references of string constant * pool entries that refer to a class member in the program class pool or in the * library class pool. * <p> * It optionally prints notes if on usage of * <code>(SomeClass)Class.forName(variable).newInstance()</code>. * <p> * The class hierarchy and references must be initialized before using this * visitor. * * @see ClassSuperHierarchyInitializer * @see ClassReferenceInitializer * * @author Eric Lafortune */public class DynamicMemberReferenceInitializerextends      SimplifiedVisitorimplements   InstructionVisitor,             ConstantVisitor,             AttributeVisitor,             MemberVisitor{    public static final int X = InstructionSequenceMatcher.X;    public static final int Y = InstructionSequenceMatcher.Y;    public static final int Z = InstructionSequenceMatcher.Z;    public static final int A = InstructionSequenceMatcher.A;    public static final int B = InstructionSequenceMatcher.B;    public static final int C = InstructionSequenceMatcher.C;    public static final int D = InstructionSequenceMatcher.D;    private final Constant[] GET_FIELD_CONSTANTS = new Constant[]    {        new MethodrefConstant(1, 2, null, null),        new ClassConstant(3, null),        new NameAndTypeConstant(4, 5),        new Utf8Constant(ClassConstants.INTERNAL_CLASS_NAME_JAVA_LANG_CLASS),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_NAME_CLASS_GET_FIELD),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_CLASS_GET_FIELD),    };    private final Constant[] GET_DECLARED_FIELD_CONSTANTS = new Constant[]    {        new MethodrefConstant(1, 2, null, null),        new ClassConstant(3, null),        new NameAndTypeConstant(4, 5),        new Utf8Constant(ClassConstants.INTERNAL_CLASS_NAME_JAVA_LANG_CLASS),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_NAME_CLASS_GET_DECLARED_FIELD),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_CLASS_GET_DECLARED_FIELD),    };    private final Constant[] GET_METHOD_CONSTANTS = new Constant[]    {        new MethodrefConstant(1, 2, null, null),        new ClassConstant(3, null),        new NameAndTypeConstant(4, 5),        new Utf8Constant(ClassConstants.INTERNAL_CLASS_NAME_JAVA_LANG_CLASS),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_NAME_CLASS_GET_METHOD),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_CLASS_GET_METHOD),    };    private final Constant[] GET_DECLARED_METHOD_CONSTANTS = new Constant[]    {        new MethodrefConstant(1, 2, null, null),        new ClassConstant(3, null),        new NameAndTypeConstant(4, 5),        new Utf8Constant(ClassConstants.INTERNAL_CLASS_NAME_JAVA_LANG_CLASS),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_NAME_CLASS_GET_DECLARED_METHOD),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_CLASS_GET_DECLARED_METHOD),    };    // SomeClass.class.get[Declared]Field("someField").    private final Instruction[] CONSTANT_GET_FIELD_INSTRUCTIONS = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, X),        new ConstantInstruction(InstructionConstants.OP_LDC, Y),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 0),    };    // SomeClass.class.get[Declared]Method("someMethod", new Class[] {}).    private final Instruction[] CONSTANT_GET_METHOD_INSTRUCTIONS0 = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, X),        new ConstantInstruction(InstructionConstants.OP_LDC, Y),        new SimpleInstruction(InstructionConstants.OP_ICONST_0),        new ConstantInstruction(InstructionConstants.OP_ANEWARRAY, 1),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 0),    };    // SomeClass.class.get[Declared]Method("someMethod", new Class[] { A.class }).    private final Instruction[] CONSTANT_GET_METHOD_INSTRUCTIONS1 = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, X),        new ConstantInstruction(InstructionConstants.OP_LDC, Y),        new SimpleInstruction(InstructionConstants.OP_ICONST_1),        new ConstantInstruction(InstructionConstants.OP_ANEWARRAY, 1),        new SimpleInstruction(InstructionConstants.OP_DUP),        new SimpleInstruction(InstructionConstants.OP_ICONST_0),        new ConstantInstruction(InstructionConstants.OP_LDC, A),        new SimpleInstruction(InstructionConstants.OP_AASTORE),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 0),    };    // SomeClass.class.get[Declared]Method("someMethod", new Class[] { A.class, B.class }).    private final Instruction[] CONSTANT_GET_METHOD_INSTRUCTIONS2 = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, X),        new ConstantInstruction(InstructionConstants.OP_LDC, Y),        new SimpleInstruction(InstructionConstants.OP_ICONST_2),        new ConstantInstruction(InstructionConstants.OP_ANEWARRAY, 1),        new SimpleInstruction(InstructionConstants.OP_DUP),        new SimpleInstruction(InstructionConstants.OP_ICONST_0),        new ConstantInstruction(InstructionConstants.OP_LDC, A),        new SimpleInstruction(InstructionConstants.OP_AASTORE),        new SimpleInstruction(InstructionConstants.OP_DUP),        new SimpleInstruction(InstructionConstants.OP_ICONST_1),        new ConstantInstruction(InstructionConstants.OP_LDC, B),        new SimpleInstruction(InstructionConstants.OP_AASTORE),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 0),    };    // get[Declared]Field("someField").    private final Instruction[] GET_FIELD_INSTRUCTIONS = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, Y),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 0),    };    // get[Declared]Method("someMethod", new Class[] {}).    private final Instruction[] GET_METHOD_INSTRUCTIONS0 = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, Y),        new SimpleInstruction(InstructionConstants.OP_ICONST_0),        new ConstantInstruction(InstructionConstants.OP_ANEWARRAY, 1),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 0),    };    // get[Declared]Method("someMethod", new Class[] { A.class }).    private final Instruction[] GET_METHOD_INSTRUCTIONS1 = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, Y),        new SimpleInstruction(InstructionConstants.OP_ICONST_1),        new ConstantInstruction(InstructionConstants.OP_ANEWARRAY, 1),        new SimpleInstruction(InstructionConstants.OP_DUP),        new SimpleInstruction(InstructionConstants.OP_ICONST_0),        new ConstantInstruction(InstructionConstants.OP_LDC, A),        new SimpleInstruction(InstructionConstants.OP_AASTORE),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 0),    };    // get[Declared]Method("someMethod", new Class[] { A.class, B.class }).    private final Instruction[] GET_METHOD_INSTRUCTIONS2 = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, Y),        new SimpleInstruction(InstructionConstants.OP_ICONST_2),        new ConstantInstruction(InstructionConstants.OP_ANEWARRAY, 1),        new SimpleInstruction(InstructionConstants.OP_DUP),        new SimpleInstruction(InstructionConstants.OP_ICONST_0),        new ConstantInstruction(InstructionConstants.OP_LDC, A),        new SimpleInstruction(InstructionConstants.OP_AASTORE),        new SimpleInstruction(InstructionConstants.OP_DUP),        new SimpleInstruction(InstructionConstants.OP_ICONST_1),        new ConstantInstruction(InstructionConstants.OP_LDC, B),        new SimpleInstruction(InstructionConstants.OP_AASTORE),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 0),    };    private final ClassPool      programClassPool;    private final ClassPool      libraryClassPool;    private final WarningPrinter notePrinter;    private final StringMatcher  noteFieldExceptionMatcher;    private final StringMatcher  noteMethodExceptionMatcher;    private final InstructionSequenceMatcher constantGetFieldMatcher =        new InstructionSequenceMatcher(GET_FIELD_CONSTANTS,                                       CONSTANT_GET_FIELD_INSTRUCTIONS);    private final InstructionSequenceMatcher constantGetDeclaredFieldMatcher =        new InstructionSequenceMatcher(GET_DECLARED_FIELD_CONSTANTS,                                       CONSTANT_GET_FIELD_INSTRUCTIONS);    private final InstructionSequenceMatcher constantGetMethodMatcher0 =        new InstructionSequenceMatcher(GET_METHOD_CONSTANTS,                                       CONSTANT_GET_METHOD_INSTRUCTIONS0);    private final InstructionSequenceMatcher constantGetDeclaredMethodMatcher0 =        new InstructionSequenceMatcher(GET_DECLARED_METHOD_CONSTANTS,                                       CONSTANT_GET_METHOD_INSTRUCTIONS0);    private final InstructionSequenceMatcher constantGetMethodMatcher1 =        new InstructionSequenceMatcher(GET_METHOD_CONSTANTS,                                       CONSTANT_GET_METHOD_INSTRUCTIONS1);    private final InstructionSequenceMatcher constantGetDeclaredMethodMatcher1 =        new InstructionSequenceMatcher(GET_DECLARED_METHOD_CONSTANTS,                                       CONSTANT_GET_METHOD_INSTRUCTIONS1);    private final InstructionSequenceMatcher constantGetMethodMatcher2 =        new InstructionSequenceMatcher(GET_METHOD_CONSTANTS,                                       CONSTANT_GET_METHOD_INSTRUCTIONS2);    private final InstructionSequenceMatcher constantGetDeclaredMethodMatcher2 =        new InstructionSequenceMatcher(GET_DECLARED_METHOD_CONSTANTS,                                       CONSTANT_GET_METHOD_INSTRUCTIONS2);    private final InstructionSequenceMatcher getFieldMatcher =        new InstructionSequenceMatcher(GET_FIELD_CONSTANTS,                                       GET_FIELD_INSTRUCTIONS);    private final InstructionSequenceMatcher getDeclaredFieldMatcher =        new InstructionSequenceMatcher(GET_DECLARED_FIELD_CONSTANTS,                                       GET_FIELD_INSTRUCTIONS);    private final InstructionSequenceMatcher getMethodMatcher0 =        new InstructionSequenceMatcher(GET_METHOD_CONSTANTS,                                       GET_METHOD_INSTRUCTIONS0);    private final InstructionSequenceMatcher getDeclaredMethodMatcher0 =        new InstructionSequenceMatcher(GET_DECLARED_METHOD_CONSTANTS,                                       GET_METHOD_INSTRUCTIONS0);    private final InstructionSequenceMatcher getMethodMatcher1 =        new InstructionSequenceMatcher(GET_METHOD_CONSTANTS,                                       GET_METHOD_INSTRUCTIONS1);    private final InstructionSequenceMatcher getDeclaredMethodMatcher1 =        new InstructionSequenceMatcher(GET_DECLARED_METHOD_CONSTANTS,                                       GET_METHOD_INSTRUCTIONS1);    private final InstructionSequenceMatcher getMethodMatcher2 =        new InstructionSequenceMatcher(GET_METHOD_CONSTANTS,                                       GET_METHOD_INSTRUCTIONS2);    private final InstructionSequenceMatcher getDeclaredMethodMatcher2 =        new InstructionSequenceMatcher(GET_DECLARED_METHOD_CONSTANTS,                                       GET_METHOD_INSTRUCTIONS2);    private final MemberFinder memberFinder = new MemberFinder();    // Fields acting as parameters for the visitors.    private Clazz   referencedClass;    private boolean isDeclared;    private boolean isField;    /**     * Creates a new DynamicMemberReferenceInitializer.     */    public DynamicMemberReferenceInitializer(ClassPool      programClassPool,                                             ClassPool      libraryClassPool,                                             WarningPrinter notePrinter,                                             StringMatcher  noteFieldExceptionMatcher,                                             StringMatcher  noteMethodExceptionMatcher)    {        this.programClassPool           = programClassPool;        this.libraryClassPool           = libraryClassPool;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?