1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 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.attribute.annotation.visitor;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.Attribute;
25import proguard.classfile.attribute.annotation.*;
26import proguard.classfile.attribute.visitor.AttributeVisitor;
27import proguard.classfile.util.SimplifiedVisitor;
28import proguard.util.*;
29
30/**
31 * This AttributeVisitor and AnnotationVisitor lets a given ElementValueVisitor
32 * visit all ElementValue objects of the attributes or annotations that it
33 * visits.
34 *
35 * @author Eric Lafortune
36 */
37public class AllElementValueVisitor
38extends      SimplifiedVisitor
39implements   AttributeVisitor,
40             AnnotationVisitor,
41             ElementValueVisitor
42{
43    private final boolean             deep;
44    private final ElementValueVisitor elementValueVisitor;
45
46
47    /**
48     * Creates a new AllElementValueVisitor.
49     * @param elementValueVisitor the AllElementValueVisitor to which visits
50     *                            will be delegated.
51     */
52    public AllElementValueVisitor(ElementValueVisitor elementValueVisitor)
53    {
54        this(false, elementValueVisitor);
55    }
56
57
58    /**
59     * Creates a new AllElementValueVisitor.
60     * @param deep                specifies whether the element values
61     *                            further down the hierarchy should be
62     *                            visited too.
63     * @param elementValueVisitor the AllElementValueVisitor to which visits
64     *                            will be delegated.
65     */
66    public AllElementValueVisitor(boolean             deep,
67                                  ElementValueVisitor elementValueVisitor)
68    {
69        this.deep                = deep;
70        this.elementValueVisitor = elementValueVisitor;
71    }
72
73
74    // Implementations for AttributeVisitor.
75
76    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
77
78
79    public void visitRuntimeVisibleAnnotationsAttribute(Clazz clazz, RuntimeVisibleAnnotationsAttribute runtimeVisibleAnnotationsAttribute)
80    {
81        // Visit the annotations.
82        runtimeVisibleAnnotationsAttribute.annotationsAccept(clazz, this);
83    }
84
85
86    public void visitRuntimeVisibleAnnotationsAttribute(Clazz clazz, Field field, RuntimeVisibleAnnotationsAttribute runtimeVisibleAnnotationsAttribute)
87    {
88        // Visit the annotations.
89        runtimeVisibleAnnotationsAttribute.annotationsAccept(clazz, field, this);
90    }
91
92
93    public void visitRuntimeVisibleAnnotationsAttribute(Clazz clazz, Method method, RuntimeVisibleAnnotationsAttribute runtimeVisibleAnnotationsAttribute)
94    {
95        // Visit the annotations.
96        runtimeVisibleAnnotationsAttribute.annotationsAccept(clazz, method, this);
97    }
98
99
100    public void visitRuntimeInvisibleAnnotationsAttribute(Clazz clazz, RuntimeInvisibleAnnotationsAttribute runtimeInvisibleAnnotationsAttribute)
101    {
102        // Visit the annotations.
103        runtimeInvisibleAnnotationsAttribute.annotationsAccept(clazz, this);
104    }
105
106
107    public void visitRuntimeInvisibleAnnotationsAttribute(Clazz clazz, Field field, RuntimeInvisibleAnnotationsAttribute runtimeInvisibleAnnotationsAttribute)
108    {
109        // Visit the annotations.
110        runtimeInvisibleAnnotationsAttribute.annotationsAccept(clazz, field, this);
111    }
112
113
114    public void visitRuntimeInvisibleAnnotationsAttribute(Clazz clazz, Method method, RuntimeInvisibleAnnotationsAttribute runtimeInvisibleAnnotationsAttribute)
115    {
116        // Visit the annotations.
117        runtimeInvisibleAnnotationsAttribute.annotationsAccept(clazz, method, this);
118    }
119
120
121    public void visitAnyParameterAnnotationsAttribute(Clazz clazz, Method method, ParameterAnnotationsAttribute parameterAnnotationsAttribute)
122    {
123        // Visit the annotations.
124        parameterAnnotationsAttribute.annotationsAccept(clazz, method, this);
125    }
126
127
128    public void visitAnnotationDefaultAttribute(Clazz clazz, Method method, AnnotationDefaultAttribute annotationDefaultAttribute)
129    {
130        // Visit the default element value.
131        annotationDefaultAttribute.defaultValueAccept(clazz, this);
132    }
133
134
135    // Implementations for AnnotationVisitor.
136
137    public void visitAnnotation(Clazz clazz, Annotation annotation)
138    {
139        annotation.elementValuesAccept(clazz, this);
140    }
141
142
143    public void visitAnnotation(Clazz clazz, Field field, Annotation annotation)
144    {
145        annotation.elementValuesAccept(clazz, this);
146    }
147
148
149    public void visitAnnotation(Clazz clazz, Method method, Annotation annotation)
150    {
151        annotation.elementValuesAccept(clazz, this);
152    }
153
154
155    public void visitAnnotation(Clazz clazz, Method method, int parameterIndex, Annotation annotation)
156    {
157        annotation.elementValuesAccept(clazz, this);
158    }
159
160
161    // Implementations for ElementValueVisitor.
162
163    public void visitConstantElementValue(Clazz clazz, Annotation annotation, ConstantElementValue constantElementValue)
164    {
165        elementValueVisitor.visitConstantElementValue(clazz, annotation, constantElementValue);
166    }
167
168
169    public void visitEnumConstantElementValue(Clazz clazz, Annotation annotation, EnumConstantElementValue enumConstantElementValue)
170    {
171        elementValueVisitor.visitEnumConstantElementValue(clazz, annotation, enumConstantElementValue);
172    }
173
174
175    public void visitClassElementValue(Clazz clazz, Annotation annotation, ClassElementValue classElementValue)
176    {
177        elementValueVisitor.visitClassElementValue(clazz, annotation, classElementValue);
178    }
179
180
181    public void visitAnnotationElementValue(Clazz clazz, Annotation annotation, AnnotationElementValue annotationElementValue)
182    {
183        elementValueVisitor.visitAnnotationElementValue(clazz, annotation, annotationElementValue);
184
185        if (deep)
186        {
187            annotationElementValue.annotationAccept(clazz, this);
188        }
189    }
190
191
192    public void visitArrayElementValue(Clazz clazz, Annotation annotation, ArrayElementValue arrayElementValue)
193    {
194        elementValueVisitor.visitArrayElementValue(clazz, annotation, arrayElementValue);
195
196        if (deep)
197        {
198            arrayElementValue.elementValuesAccept(clazz, annotation, elementValueVisitor);
199        }
200    }
201}
202