📄 attributeusagemarker.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.obfuscate;import proguard.classfile.*;import proguard.classfile.attribute.*;import proguard.classfile.attribute.annotation.*;import proguard.classfile.attribute.preverification.*;import proguard.classfile.attribute.visitor.AttributeVisitor;import proguard.classfile.util.SimplifiedVisitor;import proguard.classfile.visitor.*;import proguard.util.*;import java.util.List;/** * This ClassVisitor marks all attributes that should be kept in the classes * it visits. * * @see AttributeShrinker * * @author Eric Lafortune */public class AttributeUsageMarkerextends SimplifiedVisitorimplements ClassVisitor, MemberVisitor, AttributeVisitor{ // A visitor info flag to indicate the attribute is being used. private static final Object USED = new Object(); // Flags to specify whether optional attributes should be kept anyway. private boolean keepAllAttributes; private boolean keepAllUnknownAttributes; private boolean keepAllKnownAttributes; private StringMatcher keepAttributes; private boolean keepSourceFileAttribute; private boolean keepSourceDirAttribute; private boolean keepInnerClassesAttribute; private boolean keepEnclosingMethodAttribute; private boolean keepDeprecatedAttribute; private boolean keepSyntheticAttribute; private boolean keepSignatureAttribute; private boolean keepLineNumberTableAttribute; private boolean keepLocalVariableTableAttribute; private boolean keepLocalVariableTypeTableAttribute; private boolean keepRuntimeVisibleAnnotationsAttribute; private boolean keepRuntimeInvisibleAnnotationsAttribute; private boolean keepRuntimeVisibleParameterAnnotationsAttribute; private boolean keepRuntimeInvisibleParameterAnnotationsAttribute; private boolean keepAnnotationDefaultAttribute; /** * Specifies to keep all optional attributes. */ public void setKeepAllAttributes() { keepAllAttributes = true; } /** * Specifies to keep all unknown attributes. */ public void setKeepAllUnknownAttributes() { keepAllUnknownAttributes = true; } /** * Specifies to keep all known attributes. */ public void setKeepAllKnownAttributes() { keepAllKnownAttributes = true; } /** * Specifies to keep optional attributes with the given names. The attribute * names may contain "*" or "?" wildcards, and they may be preceded by the * "!" negator. */ public void setKeepAttributes(List attributeNames) { keepAttributes = new BasicListMatcher(attributeNames); // Precompute whether the list of attribute names matches the supported // attributes. keepSourceFileAttribute = keepAttributes.matches(ClassConstants.ATTR_SourceFile); keepSourceDirAttribute = keepAttributes.matches(ClassConstants.ATTR_SourceDir); keepInnerClassesAttribute = keepAttributes.matches(ClassConstants.ATTR_InnerClasses); keepEnclosingMethodAttribute = keepAttributes.matches(ClassConstants.ATTR_EnclosingMethod); keepDeprecatedAttribute = keepAttributes.matches(ClassConstants.ATTR_Deprecated); keepSyntheticAttribute = keepAttributes.matches(ClassConstants.ATTR_Synthetic); keepSignatureAttribute = keepAttributes.matches(ClassConstants.ATTR_Signature); keepLineNumberTableAttribute = keepAttributes.matches(ClassConstants.ATTR_LineNumberTable); keepLocalVariableTableAttribute = keepAttributes.matches(ClassConstants.ATTR_LocalVariableTable); keepLocalVariableTypeTableAttribute = keepAttributes.matches(ClassConstants.ATTR_LocalVariableTypeTable); keepRuntimeVisibleAnnotationsAttribute = keepAttributes.matches(ClassConstants.ATTR_RuntimeVisibleAnnotations); keepRuntimeInvisibleAnnotationsAttribute = keepAttributes.matches(ClassConstants.ATTR_RuntimeInvisibleAnnotations); keepRuntimeVisibleParameterAnnotationsAttribute = keepAttributes.matches(ClassConstants.ATTR_RuntimeVisibleParameterAnnotations); keepRuntimeInvisibleParameterAnnotationsAttribute = keepAttributes.matches(ClassConstants.ATTR_RuntimeInvisibleParameterAnnotations); keepAnnotationDefaultAttribute = keepAttributes.matches(ClassConstants.ATTR_AnnotationDefault); } // Implementations for ClassVisitor. public void visitProgramClass(ProgramClass programClass) { // Mark the class member attributes that should be kept. programClass.fieldsAccept(this); programClass.methodsAccept(this); // Mark the class attributes that should be kept. programClass.attributesAccept(this); } // Implementations for MemberVisitor. public void visitProgramField(ProgramClass programClass, ProgramField programField) { visitMember(programClass, programField); } public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) { visitMember(programClass, programMethod); } private void visitMember(ProgramClass programClass, ProgramMember programMember) { // Mark the class member attributes that should be kept. programMember.attributesAccept(programClass, this); } // Implementations for AttributeVisitor. public void visitUnknownAttribute(Clazz clazz, UnknownAttribute unknownAttribute) { if (keepAllAttributes || keepAllUnknownAttributes || (keepAttributes != null && keepAttributes.matches(unknownAttribute.getAttributeName(clazz)))) { markAsUsed(unknownAttribute); } } public void visitSourceFileAttribute(Clazz clazz, SourceFileAttribute sourceFileAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepSourceFileAttribute) { markAsUsed(sourceFileAttribute); } } public void visitSourceDirAttribute(Clazz clazz, SourceDirAttribute sourceDirAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepSourceDirAttribute) { markAsUsed(sourceDirAttribute); } } public void visitInnerClassesAttribute(Clazz clazz, InnerClassesAttribute innerClassesAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepInnerClassesAttribute) { markAsUsed(innerClassesAttribute); } } public void visitEnclosingMethodAttribute(Clazz clazz, EnclosingMethodAttribute enclosingMethodAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepEnclosingMethodAttribute) { markAsUsed(enclosingMethodAttribute); } } public void visitDeprecatedAttribute(Clazz clazz, DeprecatedAttribute deprecatedAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepDeprecatedAttribute) { markAsUsed(deprecatedAttribute); } } public void visitSyntheticAttribute(Clazz clazz, SyntheticAttribute syntheticAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepSyntheticAttribute) { markAsUsed(syntheticAttribute); } } public void visitSignatureAttribute(Clazz clazz, SignatureAttribute signatureAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepSignatureAttribute) { markAsUsed(signatureAttribute); } } public void visitConstantValueAttribute(Clazz clazz, Field field, ConstantValueAttribute constantValueAttribute) { markAsUsed(constantValueAttribute); } public void visitExceptionsAttribute(Clazz clazz, Method method, ExceptionsAttribute exceptionsAttribute) { markAsUsed(exceptionsAttribute); } public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) { markAsUsed(codeAttribute); // Mark the code attributes that should be kept. codeAttribute.attributesAccept(clazz, method, this); } public void visitStackMapAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapAttribute stackMapAttribute) { markAsUsed(stackMapAttribute); } public void visitStackMapTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapTableAttribute stackMapTableAttribute) { markAsUsed(stackMapTableAttribute); } public void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepLineNumberTableAttribute) { markAsUsed(lineNumberTableAttribute); } } public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepLocalVariableTableAttribute) { markAsUsed(localVariableTableAttribute); } } public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepLocalVariableTypeTableAttribute) { markAsUsed(localVariableTypeTableAttribute); } } public void visitRuntimeVisibleAnnotationsAttribute(Clazz clazz, RuntimeVisibleAnnotationsAttribute runtimeVisibleAnnotationsAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepRuntimeVisibleAnnotationsAttribute) { markAsUsed(runtimeVisibleAnnotationsAttribute); } } public void visitRuntimeInvisibleAnnotationsAttribute(Clazz clazz, RuntimeInvisibleAnnotationsAttribute runtimeInvisibleAnnotationsAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepRuntimeInvisibleAnnotationsAttribute) { markAsUsed(runtimeInvisibleAnnotationsAttribute); } } public void visitRuntimeVisibleParameterAnnotationsAttribute(Clazz clazz, Method method, RuntimeVisibleParameterAnnotationsAttribute runtimeVisibleParameterAnnotationsAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepRuntimeVisibleParameterAnnotationsAttribute) { markAsUsed(runtimeVisibleParameterAnnotationsAttribute); } } public void visitRuntimeInvisibleParameterAnnotationsAttribute(Clazz clazz, Method method, RuntimeInvisibleParameterAnnotationsAttribute runtimeInvisibleParameterAnnotationsAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepRuntimeInvisibleParameterAnnotationsAttribute) { markAsUsed(runtimeInvisibleParameterAnnotationsAttribute); } } public void visitAnnotationDefaultAttribute(Clazz clazz, Method method, AnnotationDefaultAttribute annotationDefaultAttribute) { if (keepAllAttributes || keepAllKnownAttributes || keepAnnotationDefaultAttribute) { markAsUsed(annotationDefaultAttribute); } } // Small utility methods. /** * Marks the given VisitorAccepter as being used (or useful). * In this context, the VisitorAccepter will be an Attribute object. */ private static void markAsUsed(VisitorAccepter visitorAccepter) { visitorAccepter.setVisitorInfo(USED); } /** * Returns whether the given VisitorAccepter has been marked as being used. * In this context, the VisitorAccepter will be an Attribute object. */ static boolean isUsed(VisitorAccepter visitorAccepter) { return visitorAccepter.getVisitorInfo() == USED; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -