001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
015: * more details.
016: *
017: * You should have received a copy of the GNU General Public License along
018: * with this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.obfuscate;
022:
023: import proguard.classfile.*;
024: import proguard.classfile.attribute.*;
025: import proguard.classfile.attribute.visitor.AttributeVisitor;
026: import proguard.classfile.constant.*;
027: import proguard.classfile.constant.visitor.ConstantVisitor;
028: import proguard.classfile.util.SimplifiedVisitor;
029: import proguard.classfile.visitor.ClassVisitor;
030:
031: /**
032: * This ClassVisitor marks all NameAndType constant pool entries that are
033: * being used in the program classes it visits.
034: *
035: * @see NameAndTypeShrinker
036: *
037: * @author Eric Lafortune
038: */
039: public class NameAndTypeUsageMarker extends SimplifiedVisitor implements
040: ClassVisitor, ConstantVisitor, AttributeVisitor {
041: // A visitor info flag to indicate the NameAndType constant pool entry is being used.
042: private static final Object USED = new Object();
043:
044: // Implementations for ClassVisitor.
045:
046: public void visitProgramClass(ProgramClass programClass) {
047: // Mark the NameAndType entries referenced by all other constant pool
048: // entries.
049: programClass.constantPoolEntriesAccept(this );
050:
051: // Mark the NameAndType entries referenced by all EnclosingMethod
052: // attributes.
053: programClass.attributesAccept(this );
054: }
055:
056: // Implementations for ConstantVisitor.
057:
058: public void visitAnyConstant(Clazz clazz, Constant constant) {
059: }
060:
061: public void visitFieldrefConstant(Clazz clazz,
062: FieldrefConstant fieldrefConstant) {
063: visitRefConstant(clazz, fieldrefConstant);
064: }
065:
066: public void visitInterfaceMethodrefConstant(Clazz clazz,
067: InterfaceMethodrefConstant interfaceMethodrefConstant) {
068: visitRefConstant(clazz, interfaceMethodrefConstant);
069: }
070:
071: public void visitMethodrefConstant(Clazz clazz,
072: MethodrefConstant methodrefConstant) {
073: visitRefConstant(clazz, methodrefConstant);
074: }
075:
076: private void visitRefConstant(Clazz clazz, RefConstant refConstant) {
077: markNameAndTypeConstant(clazz, refConstant.u2nameAndTypeIndex);
078: }
079:
080: // Implementations for AttributeVisitor.
081:
082: public void visitAnyAttribute(Clazz clazz, Attribute attribute) {
083: }
084:
085: public void visitEnclosingMethodAttribute(Clazz clazz,
086: EnclosingMethodAttribute enclosingMethodAttribute) {
087: if (enclosingMethodAttribute.u2nameAndTypeIndex != 0) {
088: markNameAndTypeConstant(clazz,
089: enclosingMethodAttribute.u2nameAndTypeIndex);
090: }
091: }
092:
093: // Small utility methods.
094:
095: /**
096: * Marks the given UTF-8 constant pool entry of the given class.
097: */
098: private void markNameAndTypeConstant(Clazz clazz, int index) {
099: markAsUsed((NameAndTypeConstant) ((ProgramClass) clazz)
100: .getConstant(index));
101: }
102:
103: /**
104: * Marks the given VisitorAccepter as being used.
105: * In this context, the VisitorAccepter will be a NameAndTypeConstant object.
106: */
107: private static void markAsUsed(VisitorAccepter visitorAccepter) {
108: visitorAccepter.setVisitorInfo(USED);
109: }
110:
111: /**
112: * Returns whether the given VisitorAccepter has been marked as being used.
113: * In this context, the VisitorAccepter will be a NameAndTypeConstant object.
114: */
115: static boolean isUsed(VisitorAccepter visitorAccepter) {
116: return visitorAccepter.getVisitorInfo() == USED;
117: }
118: }
|