1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.classfile.visitor;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.*;
25import proguard.classfile.attribute.annotation.*;
26import proguard.classfile.attribute.annotation.visitor.*;
27import proguard.classfile.attribute.preverification.*;
28import proguard.classfile.attribute.preverification.visitor.*;
29import proguard.classfile.attribute.visitor.*;
30import proguard.classfile.constant.Constant;
31import proguard.classfile.constant.visitor.ConstantVisitor;
32import proguard.classfile.util.SimplifiedVisitor;
33
34/**
35 * This <code>ClassVisitor</code> removes all visitor information of the
36 * classes it visits.
37 *
38 * @author Eric Lafortune
39 */
40public class ClassCleaner
41extends      SimplifiedVisitor
42implements   ClassVisitor,
43             ConstantVisitor,
44             MemberVisitor,
45             AttributeVisitor,
46             BootstrapMethodInfoVisitor,
47             ExceptionInfoVisitor,
48             InnerClassesInfoVisitor,
49             StackMapFrameVisitor,
50             VerificationTypeVisitor,
51             ParameterInfoVisitor,
52             LocalVariableInfoVisitor,
53             LocalVariableTypeInfoVisitor,
54             AnnotationVisitor,
55             TypeAnnotationVisitor,
56             ElementValueVisitor
57{
58    // Implementations for ClassVisitor.
59
60    public void visitProgramClass(ProgramClass programClass)
61    {
62        clean(programClass);
63
64        programClass.constantPoolEntriesAccept(this);
65
66        programClass.fieldsAccept(this);
67        programClass.methodsAccept(this);
68
69        programClass.attributesAccept(this);
70    }
71
72
73    public void visitLibraryClass(LibraryClass libraryClass)
74    {
75        clean(libraryClass);
76
77        libraryClass.fieldsAccept(this);
78        libraryClass.methodsAccept(this);
79    }
80
81
82    // Implementations for ConstantVisitor.
83
84    public void visitAnyConstant(Clazz clazz, Constant constant)
85    {
86        clean(constant);
87    }
88
89
90    // Implementations for MemberVisitor.
91
92    public void visitProgramMember(ProgramClass programClass, ProgramMember programMember)
93    {
94        clean(programMember);
95
96        programMember.attributesAccept(programClass, this);
97    }
98
99
100    public void visitLibraryMember(LibraryClass libraryClass, LibraryMember libraryMember)
101    {
102        clean(libraryMember);
103    }
104
105
106    // Implementations for AttributeVisitor.
107
108    public void visitAnyAttribute(Clazz clazz, Attribute attribute)
109    {
110        clean(attribute);
111    }
112
113
114    public void visitBootstrapMethodsAttribute(Clazz clazz, BootstrapMethodsAttribute bootstrapMethodsAttribute)
115    {
116        clean(bootstrapMethodsAttribute);
117
118        bootstrapMethodsAttribute.bootstrapMethodEntriesAccept(clazz, this);
119    }
120
121
122    public void visitInnerClassesAttribute(Clazz clazz, InnerClassesAttribute innerClassesAttribute)
123    {
124        clean(innerClassesAttribute);
125
126        innerClassesAttribute.innerClassEntriesAccept(clazz, this);
127    }
128
129
130    public void visitMethodParametersAttribute(Clazz clazz, Method method, MethodParametersAttribute methodParametersAttribute)
131    {
132        clean(methodParametersAttribute);
133
134        methodParametersAttribute.parametersAccept(clazz, method, this);
135    }
136
137
138    public void visitExceptionsAttribute(Clazz clazz, Method method, ExceptionsAttribute exceptionsAttribute)
139    {
140        clean(exceptionsAttribute);
141
142        exceptionsAttribute.exceptionEntriesAccept((ProgramClass)clazz, this);
143    }
144
145
146    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
147    {
148        clean(codeAttribute);
149
150        codeAttribute.exceptionsAccept(clazz, method, this);
151        codeAttribute.attributesAccept(clazz, method, this);
152    }
153
154
155    public void visitStackMapAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapAttribute stackMapAttribute)
156    {
157        clean(stackMapAttribute);
158
159        stackMapAttribute.stackMapFramesAccept(clazz, method, codeAttribute, this);
160    }
161
162
163    public void visitStackMapTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapTableAttribute stackMapTableAttribute)
164    {
165        clean(stackMapTableAttribute);
166
167        stackMapTableAttribute.stackMapFramesAccept(clazz, method, codeAttribute, this);
168    }
169
170
171    public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute)
172    {
173        clean(localVariableTableAttribute);
174
175        localVariableTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this);
176    }
177
178
179    public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute)
180    {
181        clean(localVariableTypeTableAttribute);
182
183        localVariableTypeTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this);
184    }
185
186
187    public void visitAnyAnnotationsAttribute(Clazz clazz, AnnotationsAttribute annotationsAttribute)
188    {
189        clean(annotationsAttribute);
190
191        annotationsAttribute.annotationsAccept(clazz, this);
192    }
193
194
195    public void visitAnyParameterAnnotationsAttribute(Clazz clazz, Method method, ParameterAnnotationsAttribute parameterAnnotationsAttribute)
196    {
197        clean(parameterAnnotationsAttribute);
198
199        parameterAnnotationsAttribute.annotationsAccept(clazz, method, this);
200    }
201
202
203    public void visitAnyTypeAnnotationsAttribute(Clazz clazz, TypeAnnotationsAttribute typeAnnotationsAttribute)
204    {
205        clean(typeAnnotationsAttribute);
206
207        typeAnnotationsAttribute.typeAnnotationsAccept(clazz, this);
208    }
209
210
211    public void visitAnnotationDefaultAttribute(Clazz clazz, Method method, AnnotationDefaultAttribute annotationDefaultAttribute)
212    {
213        clean(annotationDefaultAttribute);
214
215        annotationDefaultAttribute.defaultValueAccept(clazz, this);
216    }
217
218
219    // Implementations for BootstrapMethodInfoVisitor.
220
221    public void visitBootstrapMethodInfo(Clazz clazz, BootstrapMethodInfo bootstrapMethodInfo)
222    {
223        clean(bootstrapMethodInfo);
224    }
225
226
227    // Implementations for InnerClassesInfoVisitor.
228
229    public void visitInnerClassesInfo(Clazz clazz, InnerClassesInfo innerClassesInfo)
230    {
231        clean(innerClassesInfo);
232    }
233
234
235    // Implementations for ExceptionInfoVisitor.
236
237    public void visitExceptionInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, ExceptionInfo exceptionInfo)
238    {
239        clean(exceptionInfo);
240    }
241
242
243    // Implementations for StackMapFrameVisitor.
244
245    public void visitSameZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SameZeroFrame sameZeroFrame)
246    {
247        clean(sameZeroFrame);
248    }
249
250
251    public void visitSameOneFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SameOneFrame sameOneFrame)
252    {
253        clean(sameOneFrame);
254
255        sameOneFrame.stackItemAccept(clazz, method, codeAttribute, offset, this);
256    }
257
258
259    public void visitLessZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, LessZeroFrame lessZeroFrame)
260    {
261        clean(lessZeroFrame);
262    }
263
264
265    public void visitMoreZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, MoreZeroFrame moreZeroFrame)
266    {
267        clean(moreZeroFrame);
268
269        moreZeroFrame.additionalVariablesAccept(clazz, method, codeAttribute, offset, this);
270    }
271
272
273    public void visitFullFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, FullFrame fullFrame)
274    {
275        clean(fullFrame);
276
277        fullFrame.variablesAccept(clazz, method, codeAttribute, offset, this);
278        fullFrame.stackAccept(clazz, method, codeAttribute, offset, this);
279    }
280
281
282    // Implementations for VerificationTypeVisitor.
283
284    public void visitAnyVerificationType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VerificationType verificationType)
285    {
286        clean(verificationType);
287    }
288
289
290    // Implementations for ParameterInfoVisitor.
291
292    public void visitParameterInfo(Clazz clazz, Method method, int parameterIndex, ParameterInfo parameterInfo)
293    {
294        clean(parameterInfo);
295    }
296
297
298    // Implementations for LocalVariableInfoVisitor.
299
300    public void visitLocalVariableInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableInfo localVariableInfo)
301    {
302        clean(localVariableInfo);
303    }
304
305
306    // Implementations for LocalVariableTypeInfoVisitor.
307
308    public void visitLocalVariableTypeInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeInfo localVariableTypeInfo)
309    {
310        clean(localVariableTypeInfo);
311    }
312
313
314    // Implementations for AnnotationVisitor.
315
316    public void visitAnnotation(Clazz clazz, Annotation annotation)
317    {
318        clean(annotation);
319
320        annotation.elementValuesAccept(clazz, this);
321    }
322
323
324    // Implementations for TypeAnnotationVisitor.
325
326    public void visitTypeAnnotation(Clazz clazz, TypeAnnotation typeAnnotation)
327    {
328        clean(typeAnnotation);
329
330        //typeAnnotation.targetInfoAccept(clazz, this);
331        //typeAnnotation.typePathInfosAccept(clazz, this);
332        typeAnnotation.elementValuesAccept(clazz, this);
333    }
334
335
336    // Implementations for ElementValueVisitor.
337
338    public void visitAnyElementValue(Clazz clazz, Annotation annotation, ElementValue elementValue)
339    {
340        clean(elementValue);
341    }
342
343
344    public void visitAnnotationElementValue(Clazz clazz, Annotation annotation, AnnotationElementValue annotationElementValue)
345    {
346        clean(annotationElementValue);
347
348        annotationElementValue.annotationAccept(clazz, this);
349    }
350
351
352    public void visitArrayElementValue(Clazz clazz, Annotation annotation, ArrayElementValue arrayElementValue)
353    {
354        clean(arrayElementValue);
355    }
356
357
358    // Small utility methods.
359
360    private void clean(VisitorAccepter visitorAccepter)
361    {
362        visitorAccepter.setVisitorInfo(null);
363    }
364}
365