📄 usagemarker.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.shrink;import proguard.classfile.*;import proguard.classfile.attribute.*;import proguard.classfile.attribute.annotation.*;import proguard.classfile.attribute.annotation.visitor.*;import proguard.classfile.attribute.preverification.*;import proguard.classfile.attribute.preverification.visitor.*;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.classfile.util.SimplifiedVisitor;import proguard.classfile.visitor.*;/** * This ClassVisitor and MemberVisitor recursively marks all * classes and class elements that are being used. * * @see ClassShrinker * * @author Eric Lafortune */class UsageMarkerextends SimplifiedVisitorimplements ClassVisitor, MemberVisitor, ConstantVisitor, AttributeVisitor, InnerClassesInfoVisitor, ExceptionInfoVisitor, StackMapFrameVisitor, VerificationTypeVisitor, LocalVariableInfoVisitor, LocalVariableTypeInfoVisitor, AnnotationVisitor, ElementValueVisitor, InstructionVisitor{ // A visitor info flag to indicate the ProgramMember object is being used, // if its Clazz can be determined as being used as well. private static final Object POSSIBLY_USED = new Object(); // A visitor info flag to indicate the visitor accepter is being used. private static final Object USED = new Object(); private MyInterfaceUsageMarker interfaceUsageMarker = new MyInterfaceUsageMarker(); private MyPossiblyUsedMethodUsageMarker possiblyUsedMethodUsageMarker = new MyPossiblyUsedMethodUsageMarker();// private ClassVisitor dynamicClassMarker =// new MultiClassVisitor(// new ClassVisitor[]// {// this,// new NamedMethodVisitor(ClassConstants.INTERNAL_METHOD_NAME_INIT,// ClassConstants.INTERNAL_METHOD_TYPE_INIT,// this)// }); // Implementations for ClassVisitor. public void visitProgramClass(ProgramClass programClass) { if (shouldBeMarkedAsUsed(programClass)) { // Mark this class. markAsUsed(programClass); markProgramClassBody(programClass); } } protected void markProgramClassBody(ProgramClass programClass) { // Mark this class's name. markConstant(programClass, programClass.u2thisClass); // Mark the superclass. if (programClass.u2superClass != 0) { markConstant(programClass, programClass.u2superClass); } // Give the interfaces preliminary marks. programClass.hierarchyAccept(false, false, true, false, interfaceUsageMarker); // Explicitly mark the <clinit> method. programClass.methodAccept(ClassConstants.INTERNAL_METHOD_NAME_CLINIT, ClassConstants.INTERNAL_METHOD_TYPE_CLINIT, this); // Explicitly mark the parameterless <init> method. programClass.methodAccept(ClassConstants.INTERNAL_METHOD_NAME_INIT, ClassConstants.INTERNAL_METHOD_TYPE_INIT, this); // Process all methods that have already been marked as possibly used. programClass.methodsAccept(possiblyUsedMethodUsageMarker); // Mark the attributes. programClass.attributesAccept(this); } public void visitLibraryClass(LibraryClass libraryClass) { if (shouldBeMarkedAsUsed(libraryClass)) { markAsUsed(libraryClass); // We're not going to analyze all library code. We're assuming that // if this class is being used, all of its methods will be used as // well. We'll mark them as such (here and in all subclasses). // Mark the superclass. Clazz superClass = libraryClass.superClass; if (superClass != null) { superClass.accept(this); } // Mark the interfaces. Clazz[] interfaceClasses = libraryClass.interfaceClasses; if (interfaceClasses != null) { for (int index = 0; index < interfaceClasses.length; index++) { if (interfaceClasses[index] != null) { interfaceClasses[index].accept(this); } } } // Mark all methods. libraryClass.methodsAccept(this); } } /** * This ClassVisitor marks ProgramClass objects as possibly used, * and it visits LibraryClass objects with its outer UsageMarker. */ private class MyInterfaceUsageMarker implements ClassVisitor { public void visitProgramClass(ProgramClass programClass) { if (shouldBeMarkedAsPossiblyUsed(programClass)) { // We can't process the interface yet, because it might not // be required. Give it a preliminary mark. markAsPossiblyUsed(programClass); } } public void visitLibraryClass(LibraryClass libraryClass) { // Make sure all library interface methods are marked. UsageMarker.this.visitLibraryClass(libraryClass); } } private class MyPossiblyUsedMethodUsageMarker extends SimplifiedVisitor implements MemberVisitor { // Implementations for MemberVisitor. public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) { // Has the method already been referenced? if (isPossiblyUsed(programMethod)) { markAsUsed(programMethod); // Mark the method body. markProgramMethodBody(programClass, programMethod); // Note that, if the method has been marked as possibly used, // the method hierarchy has already been marked (cfr. below). } } } // Implementations for MemberVisitor. public void visitProgramField(ProgramClass programClass, ProgramField programField) { if (shouldBeMarkedAsUsed(programField)) { markAsUsed(programField); // Mark the name and descriptor. markConstant(programClass, programField.u2nameIndex); markConstant(programClass, programField.u2descriptorIndex); // Mark the attributes. programField.attributesAccept(programClass, this); // Mark the classes referenced in the descriptor string. programField.referencedClassesAccept(this); } } public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) { if (shouldBeMarkedAsUsed(programMethod)) { // Is the method's class used? if (isUsed(programClass)) { markAsUsed(programMethod); // Mark the method body. markProgramMethodBody(programClass, programMethod); // Mark the method hierarchy. markMethodHierarchy(programClass, programMethod); } // Hasn't the method been marked as possibly being used yet? else if (shouldBeMarkedAsPossiblyUsed(programMethod)) { // We can't process the method yet, because the class isn't // marked as being used (yet). Give it a preliminary mark. markAsPossiblyUsed(programMethod); // Mark the method hierarchy. markMethodHierarchy(programClass, programMethod); } } } public void visitLibraryField(LibraryClass programClass, LibraryField programField) {} public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod) { if (shouldBeMarkedAsUsed(libraryMethod)) { markAsUsed(libraryMethod); // Mark the method hierarchy. markMethodHierarchy(libraryClass, libraryMethod); } } protected void markProgramMethodBody(ProgramClass programClass, ProgramMethod programMethod) { // Mark the name and descriptor. markConstant(programClass, programMethod.u2nameIndex); markConstant(programClass, programMethod.u2descriptorIndex); // Mark the attributes. programMethod.attributesAccept(programClass, this); // Mark the classes referenced in the descriptor string. programMethod.referencedClassesAccept(this); } /** * Marks the hierarchy of implementing or overriding methods corresponding * to the given method, if any. */ protected void markMethodHierarchy(Clazz clazz, Method method) { clazz.methodImplementationsAccept(method, false, this); } // Implementations for ConstantVisitor. public void visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant) { if (shouldBeMarkedAsUsed(integerConstant)) { markAsUsed(integerConstant); } } public void visitLongConstant(Clazz clazz, LongConstant longConstant) { if (shouldBeMarkedAsUsed(longConstant)) { markAsUsed(longConstant); } } public void visitFloatConstant(Clazz clazz, FloatConstant floatConstant) { if (shouldBeMarkedAsUsed(floatConstant)) { markAsUsed(floatConstant); } } public void visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant) { if (shouldBeMarkedAsUsed(doubleConstant)) { markAsUsed(doubleConstant); } } public void visitStringConstant(Clazz clazz, StringConstant stringConstant) { if (shouldBeMarkedAsUsed(stringConstant)) { markAsUsed(stringConstant); markConstant(clazz, stringConstant.u2stringIndex); // Mark the referenced class and its parameterless constructor, // if the string is being used in a Class.forName construct. //stringConstant.referencedClassAccept(dynamicClassMarker); // Mark the referenced class. stringConstant.referencedClassAccept(this); } } public void visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant) { if (shouldBeMarkedAsUsed(utf8Constant)) { markAsUsed(utf8Constant); } } public void visitFieldrefConstant(Clazz clazz, FieldrefConstant fieldrefConstant) { visitRefConstant(clazz, fieldrefConstant); } public void visitInterfaceMethodrefConstant(Clazz clazz, InterfaceMethodrefConstant interfaceMethodrefConstant) { visitRefConstant(clazz, interfaceMethodrefConstant); } public void visitMethodrefConstant(Clazz clazz, MethodrefConstant methodrefConstant) { visitRefConstant(clazz, methodrefConstant); } private void visitRefConstant(Clazz clazz, RefConstant methodrefConstant) { if (shouldBeMarkedAsUsed(methodrefConstant)) { markAsUsed(methodrefConstant); markConstant(clazz, methodrefConstant.u2classIndex); markConstant(clazz, methodrefConstant.u2nameAndTypeIndex); // When compiled with "-target 1.2" or higher, the class or // interface actually containing the referenced method may be // higher up the hierarchy. Make sure it's marked, in case it // isn't used elsewhere. methodrefConstant.referencedClassAccept(this); // Mark the referenced method itself. methodrefConstant.referencedMemberAccept(this); } } public void visitClassConstant(Clazz clazz, ClassConstant classConstant) { if (shouldBeMarkedAsUsed(classConstant)) { markAsUsed(classConstant); markConstant(clazz, classConstant.u2nameIndex); // Mark the referenced class itself. classConstant.referencedClassAccept(this); } } public void visitNameAndTypeConstant(Clazz clazz, NameAndTypeConstant nameAndTypeConstant) { if (shouldBeMarkedAsUsed(nameAndTypeConstant)) { markAsUsed(nameAndTypeConstant); markConstant(clazz, nameAndTypeConstant.u2nameIndex); markConstant(clazz, nameAndTypeConstant.u2descriptorIndex); } } // Implementations for AttributeVisitor. // Note that attributes are typically only referenced once, so we don't // test if they have been marked already. public void visitUnknownAttribute(Clazz clazz, UnknownAttribute unknownAttribute) { // This is the best we can do for unknown attributes. markAsUsed(unknownAttribute); markConstant(clazz, unknownAttribute.u2attributeNameIndex); } public void visitSourceFileAttribute(Clazz clazz, SourceFileAttribute sourceFileAttribute) { markAsUsed(sourceFileAttribute);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -