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.classfile.visitor;
022:
023: import proguard.classfile.*;
024: import proguard.classfile.attribute.*;
025: import proguard.classfile.attribute.annotation.*;
026: import proguard.classfile.attribute.annotation.visitor.*;
027: import proguard.classfile.attribute.visitor.*;
028: import proguard.classfile.constant.*;
029: import proguard.classfile.constant.visitor.ConstantVisitor;
030: import proguard.classfile.util.SimplifiedVisitor;
031:
032: /**
033: * This ClassVisitor, MemberVisitor, ConstantVisitor, AttributeVisitor, etc.
034: * lets a given ClassVisitor visit all the referenced classes of the elements
035: * that it visits.
036: *
037: * @author Eric Lafortune
038: */
039: public class ReferencedClassVisitor extends SimplifiedVisitor implements
040: ClassVisitor, MemberVisitor, ConstantVisitor, AttributeVisitor,
041: LocalVariableInfoVisitor, LocalVariableTypeInfoVisitor,
042: AnnotationVisitor, ElementValueVisitor {
043: private final ClassVisitor classVisitor;
044:
045: public ReferencedClassVisitor(ClassVisitor classVisitor) {
046: this .classVisitor = classVisitor;
047: }
048:
049: // Implementations for ClassVisitor.
050:
051: public void visitProgramClass(ProgramClass programClass) {
052: // Visit the constant pool entries.
053: programClass.constantPoolEntriesAccept(this );
054:
055: // Visit the fields and methods.
056: programClass.fieldsAccept(this );
057: programClass.methodsAccept(this );
058:
059: // Visit the attributes.
060: programClass.attributesAccept(this );
061: }
062:
063: public void visitLibraryClass(LibraryClass libraryClass) {
064: // Visit the fields and methods.
065: libraryClass.fieldsAccept(this );
066: libraryClass.methodsAccept(this );
067: }
068:
069: // Implementations for MemberVisitor.
070:
071: public void visitProgramMember(ProgramClass programClass,
072: ProgramMember programMember) {
073: // Let the visitor visit the classes referenced in the descriptor string.
074: programMember.referencedClassesAccept(classVisitor);
075:
076: // Visit the attributes.
077: programMember.attributesAccept(programClass, this );
078: }
079:
080: public void visitLibraryMember(LibraryClass programClass,
081: LibraryMember libraryMember) {
082: // Let the visitor visit the classes referenced in the descriptor string.
083: libraryMember.referencedClassesAccept(classVisitor);
084: }
085:
086: // Implementations for ConstantVisitor.
087:
088: public void visitAnyConstant(Clazz clazz, Constant constant) {
089: }
090:
091: public void visitStringConstant(Clazz clazz,
092: StringConstant stringConstant) {
093: // Let the visitor visit the class referenced in the string constant.
094: stringConstant.referencedClassAccept(classVisitor);
095: }
096:
097: public void visitAnyRefConstant(Clazz clazz, RefConstant refConstant) {
098: // Let the visitor visit the class referenced in the reference constant.
099: refConstant.referencedClassAccept(classVisitor);
100: }
101:
102: public void visitClassConstant(Clazz clazz,
103: ClassConstant classConstant) {
104: // Let the visitor visit the class referenced in the class constant.
105: classConstant.referencedClassAccept(classVisitor);
106: }
107:
108: // Implementations for AttributeVisitor.
109:
110: public void visitAnyAttribute(Clazz clazz, Attribute attribute) {
111: }
112:
113: public void visitEnclosingMethodAttribute(Clazz clazz,
114: EnclosingMethodAttribute enclosingMethodAttribute) {
115: // Let the visitor visit the class of the enclosing method.
116: enclosingMethodAttribute.referencedClassAccept(classVisitor);
117: }
118:
119: public void visitCodeAttribute(Clazz clazz, Method method,
120: CodeAttribute codeAttribute) {
121: // Visit the attributes of the code attribute.
122: codeAttribute.attributesAccept(clazz, method, this );
123: }
124:
125: public void visitLocalVariableTableAttribute(Clazz clazz,
126: Method method, CodeAttribute codeAttribute,
127: LocalVariableTableAttribute localVariableTableAttribute) {
128: // Visit the local variables.
129: localVariableTableAttribute.localVariablesAccept(clazz, method,
130: codeAttribute, this );
131: }
132:
133: public void visitLocalVariableTypeTableAttribute(
134: Clazz clazz,
135: Method method,
136: CodeAttribute codeAttribute,
137: LocalVariableTypeTableAttribute localVariableTypeTableAttribute) {
138: // Visit the local variable types.
139: localVariableTypeTableAttribute.localVariablesAccept(clazz,
140: method, codeAttribute, this );
141: }
142:
143: public void visitSignatureAttribute(Clazz clazz,
144: SignatureAttribute signatureAttribute) {
145: // Let the visitor visit the classes referenced in the signature string.
146: signatureAttribute.referencedClassesAccept(classVisitor);
147: }
148:
149: public void visitAnyAnnotationsAttribute(Clazz clazz,
150: AnnotationsAttribute annotationsAttribute) {
151: // Visit the annotations.
152: annotationsAttribute.annotationsAccept(clazz, this );
153: }
154:
155: public void visitAnyParameterAnnotationsAttribute(Clazz clazz,
156: Method method,
157: ParameterAnnotationsAttribute parameterAnnotationsAttribute) {
158: // Visit the parameter annotations.
159: parameterAnnotationsAttribute.annotationsAccept(clazz, method,
160: this );
161: }
162:
163: public void visitAnnotationDefaultAttribute(Clazz clazz,
164: Method method,
165: AnnotationDefaultAttribute annotationDefaultAttribute) {
166: // Visit the default element value.
167: annotationDefaultAttribute.defaultValueAccept(clazz, this );
168: }
169:
170: // Implementations for LocalVariableInfoVisitor.
171:
172: public void visitLocalVariableInfo(Clazz clazz, Method method,
173: CodeAttribute codeAttribute,
174: LocalVariableInfo localVariableInfo) {
175: // Let the visitor visit the class referenced in the local variable.
176: localVariableInfo.referencedClassAccept(classVisitor);
177: }
178:
179: // Implementations for LocalVariableTypeInfoVisitor.
180:
181: public void visitLocalVariableTypeInfo(Clazz clazz, Method method,
182: CodeAttribute codeAttribute,
183: LocalVariableTypeInfo localVariableTypeInfo) {
184: // Let the visitor visit the classes referenced in the local variable type.
185: localVariableTypeInfo.referencedClassesAccept(classVisitor);
186: }
187:
188: // Implementations for AnnotationVisitor.
189:
190: public void visitAnnotation(Clazz clazz, Annotation annotation) {
191: // Let the visitor visit the classes referenced in the annotation.
192: annotation.referencedClassesAccept(classVisitor);
193:
194: // Visit the element values.
195: annotation.elementValuesAccept(clazz, this );
196: }
197:
198: // Implementations for ElementValueVisitor.
199:
200: public void visitAnyElementValue(Clazz clazz,
201: Annotation annotation, ElementValue elementValue) {
202: }
203:
204: public void visitEnumConstantElementValue(Clazz clazz,
205: Annotation annotation,
206: EnumConstantElementValue enumConstantElementValue) {
207: // Let the visitor visit the classes referenced in the constant element value.
208: enumConstantElementValue.referencedClassesAccept(classVisitor);
209: }
210:
211: public void visitClassElementValue(Clazz clazz,
212: Annotation annotation, ClassElementValue classElementValue) {
213: // Let the visitor visit the classes referenced in the class element value.
214: classElementValue.referencedClassesAccept(classVisitor);
215: }
216:
217: public void visitArrayElementValue(Clazz clazz,
218: Annotation annotation, ArrayElementValue arrayElementValue) {
219: // Visit the element values.
220: arrayElementValue.elementValuesAccept(clazz, annotation, this);
221: }
222: }
|