dynamicclassreferenceinitializer.java

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

JAVA
441
字号
/* * 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.*;import proguard.classfile.constant.*;import proguard.classfile.constant.visitor.ConstantVisitor;import proguard.classfile.instruction.*;import proguard.classfile.instruction.visitor.InstructionVisitor;import proguard.util.StringMatcher;/** * This InstructionVisitor initializes any constant <code>Class.forName</code> or * <code>.class</code> references of all classes it visits. More specifically, * it fills out the references of string constant pool entries that refer to a * class 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 must be initialized before using this visitor. * * @see ClassSuperHierarchyInitializer * * @author Eric Lafortune */public class DynamicClassReferenceInitializerextends      SimplifiedVisitorimplements   InstructionVisitor,             ConstantVisitor,             AttributeVisitor{    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[] CLASS_FOR_NAME_CONSTANTS = new Constant[]    {        // 0        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_FOR_NAME),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_CLASS_FOR_NAME),        // 6        new MethodrefConstant(1, 7, null, null),        new NameAndTypeConstant(8, 9),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_NAME_NEW_INSTANCE),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_NEW_INSTANCE),        // 10        new MethodrefConstant(1, 11, null, null),        new NameAndTypeConstant(12, 13),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_NAME_CLASS_GET_COMPONENT_TYPE),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_CLASS_GET_COMPONENT_TYPE),    };    // Class.forName("SomeClass").    private final Instruction[] CONSTANT_CLASS_FOR_NAME_INSTRUCTIONS = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, X),        new ConstantInstruction(InstructionConstants.OP_INVOKESTATIC, 0),    };    // (SomeClass)Class.forName(someName).newInstance().    private final Instruction[] CLASS_FOR_NAME_CAST_INSTRUCTIONS = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_INVOKESTATIC, 0),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 6),        new ConstantInstruction(InstructionConstants.OP_CHECKCAST, X),    };//    private Constant[] DOT_CLASS_JAVAC_CONSTANTS = new Constant[]//    {//        new MethodrefConstant(A, 1, null, null),//        new NameAndTypeConstant(2, 3),//        new Utf8Constant(ClassConstants.INTERNAL_METHOD_NAME_DOT_CLASS_JAVAC),//        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_DOT_CLASS_JAVAC),//    };    private final Constant[] DOT_CLASS_JAVAC_CONSTANTS = new Constant[]    {        new MethodrefConstant(A, 1, null, null),        new NameAndTypeConstant(B, 2),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_DOT_CLASS_JAVAC),    };    // SomeClass.class = class$("SomeClass") (javac).    private final Instruction[] DOT_CLASS_JAVAC_INSTRUCTIONS = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, X),        new ConstantInstruction(InstructionConstants.OP_INVOKESTATIC, 0),    };//    private Constant[] DOT_CLASS_JIKES_CONSTANTS = new Constant[]//    {//        new MethodrefConstant(A, 1, null, null),//        new NameAndTypeConstant(2, 3),//        new Utf8Constant(ClassConstants.INTERNAL_METHOD_NAME_DOT_CLASS_JIKES),//        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_DOT_CLASS_JIKES),//    };    private final Constant[] DOT_CLASS_JIKES_CONSTANTS = new Constant[]    {        new MethodrefConstant(A, 1, null, null),        new NameAndTypeConstant(B, 2),        new Utf8Constant(ClassConstants.INTERNAL_METHOD_TYPE_DOT_CLASS_JIKES),    };    // SomeClass.class = class("SomeClass", false) (jikes).    private final Instruction[] DOT_CLASS_JIKES_INSTRUCTIONS = new Instruction[]    {        new ConstantInstruction(InstructionConstants.OP_LDC, X),        new SimpleInstruction(InstructionConstants.OP_ICONST_0),        new ConstantInstruction(InstructionConstants.OP_INVOKESTATIC, 0),    };    // return Class.forName(v0).    private final Instruction[] DOT_CLASS_JAVAC_IMPLEMENTATION_INSTRUCTIONS = new Instruction[]    {        new VariableInstruction(InstructionConstants.OP_ALOAD_0),        new ConstantInstruction(InstructionConstants.OP_INVOKESTATIC, 0),        new SimpleInstruction(InstructionConstants.OP_ARETURN),    };    // return Class.forName(v0), if (!v1) .getComponentType().    private final Instruction[] DOT_CLASS_JIKES_IMPLEMENTATION_INSTRUCTIONS = new Instruction[]    {        new VariableInstruction(InstructionConstants.OP_ALOAD_0),        new ConstantInstruction(InstructionConstants.OP_INVOKESTATIC, 0),        new VariableInstruction(InstructionConstants.OP_ALOAD_1),        new BranchInstruction(InstructionConstants.OP_IFNE, +6),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 10),        new SimpleInstruction(InstructionConstants.OP_ARETURN),    };    // return Class.forName(v0).getComponentType().    private final Instruction[] DOT_CLASS_JIKES_IMPLEMENTATION_INSTRUCTIONS2 = new Instruction[]    {        new VariableInstruction(InstructionConstants.OP_ALOAD_0),        new ConstantInstruction(InstructionConstants.OP_INVOKESTATIC, 0),        new ConstantInstruction(InstructionConstants.OP_INVOKEVIRTUAL, 10),        new SimpleInstruction(InstructionConstants.OP_ARETURN),    };    private final ClassPool      programClassPool;    private final ClassPool      libraryClassPool;    private final WarningPrinter notePrinter;    private final StringMatcher  noteExceptionMatcher;    private final InstructionSequenceMatcher constantClassForNameMatcher =        new InstructionSequenceMatcher(CLASS_FOR_NAME_CONSTANTS,                                       CONSTANT_CLASS_FOR_NAME_INSTRUCTIONS);    private final InstructionSequenceMatcher classForNameCastMatcher =        new InstructionSequenceMatcher(CLASS_FOR_NAME_CONSTANTS,                                       CLASS_FOR_NAME_CAST_INSTRUCTIONS);    private final InstructionSequenceMatcher dotClassJavacMatcher =        new InstructionSequenceMatcher(DOT_CLASS_JAVAC_CONSTANTS,                                       DOT_CLASS_JAVAC_INSTRUCTIONS);    private final InstructionSequenceMatcher dotClassJikesMatcher =        new InstructionSequenceMatcher(DOT_CLASS_JIKES_CONSTANTS,                                       DOT_CLASS_JIKES_INSTRUCTIONS);    private final InstructionSequenceMatcher dotClassJavacImplementationMatcher =        new InstructionSequenceMatcher(CLASS_FOR_NAME_CONSTANTS,                                       DOT_CLASS_JAVAC_IMPLEMENTATION_INSTRUCTIONS);    private final InstructionSequenceMatcher dotClassJikesImplementationMatcher =        new InstructionSequenceMatcher(CLASS_FOR_NAME_CONSTANTS,                                       DOT_CLASS_JIKES_IMPLEMENTATION_INSTRUCTIONS);    private final InstructionSequenceMatcher dotClassJikesImplementationMatcher2 =        new InstructionSequenceMatcher(CLASS_FOR_NAME_CONSTANTS,                                       DOT_CLASS_JIKES_IMPLEMENTATION_INSTRUCTIONS2);    // A field acting as a return variable for the visitors.    private boolean isClassForNameInvocation;    /**     * Creates a new DynamicClassReferenceInitializer that optionally prints     * notes, with optional class specifications for which never to print notes.     */    public DynamicClassReferenceInitializer(ClassPool      programClassPool,                                            ClassPool      libraryClassPool,

⌨️ 快捷键说明

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