summaryrefslogtreecommitdiff
path: root/src/proguard/shrink/AnnotationUsageMarker.java
blob: 2cc0266ca0030acf3070a72c3849625532ad347a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * ProGuard -- shrinking, optimization, obfuscation, and preverification
 *             of Java bytecode.
 *
 * Copyright (c) 2002-2014 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.shrink;

import proguard.classfile.*;
import proguard.classfile.attribute.Attribute;
import proguard.classfile.attribute.annotation.*;
import proguard.classfile.attribute.annotation.visitor.*;
import proguard.classfile.attribute.visitor.AttributeVisitor;
import proguard.classfile.constant.*;
import proguard.classfile.constant.visitor.ConstantVisitor;
import proguard.classfile.util.SimplifiedVisitor;
import proguard.classfile.visitor.*;

/**
 * This AttributeVisitor recursively marks all necessary annotation information
 * in the attributes that it visits.
 *
 * @see UsageMarker
 *
 * @author Eric Lafortune
 */
public class AnnotationUsageMarker
extends      SimplifiedVisitor
implements   AttributeVisitor,
             AnnotationVisitor,
             ElementValueVisitor,
             ConstantVisitor,
             ClassVisitor,
             MemberVisitor
{
    private final UsageMarker usageMarker;

    // Fields acting as a return parameters for several methods.
    private boolean attributeUsed;
    private boolean annotationUsed;
    private boolean allClassesUsed;
    private boolean methodUsed;


    /**
     * Creates a new AnnotationUsageMarker.
     * @param usageMarker the usage marker that is used to mark the classes
     *                    and class members.
     */
    public AnnotationUsageMarker(UsageMarker usageMarker)
    {
        this.usageMarker = usageMarker;
    }


    // Implementations for AttributeVisitor.

    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}


    public void visitAnyAnnotationsAttribute(Clazz clazz, AnnotationsAttribute annotationsAttribute)
    {
        // Mark the necessary annotation information.
        attributeUsed = false;
        annotationsAttribute.annotationsAccept(clazz, this);

        if (attributeUsed)
        {
            // We got a positive used flag, so some annotation is being used.
            // Mark this attribute as being used as well.
            usageMarker.markAsUsed(annotationsAttribute);

            markConstant(clazz, annotationsAttribute.u2attributeNameIndex);
        }
    }


    public void visitAnyParameterAnnotationsAttribute(Clazz clazz, Method method, ParameterAnnotationsAttribute parameterAnnotationsAttribute)
    {
        // Mark the necessary annotation information.
        attributeUsed = false;
        parameterAnnotationsAttribute.annotationsAccept(clazz, method, this);

        if (attributeUsed)
        {
            // We got a positive used flag, so some annotation is being used.
            // Mark this attribute as being used as well.
            usageMarker.markAsUsed(parameterAnnotationsAttribute);

            markConstant(clazz, parameterAnnotationsAttribute.u2attributeNameIndex);
        }
    }


    public void visitAnnotationDefaultAttribute(Clazz clazz, Method method, AnnotationDefaultAttribute annotationDefaultAttribute)
    {
        // Mark the necessary annotation information in any annotation elements.
        annotationDefaultAttribute.defaultValueAccept(clazz, this);

        // Always mark annotation defaults.
        usageMarker.markAsUsed(annotationDefaultAttribute);

        markConstant(clazz, annotationDefaultAttribute.u2attributeNameIndex);
    }


    // Implementations for AnnotationVisitor.

    public void visitAnnotation(Clazz clazz, Annotation annotation)
    {
        if (isReferencedClassUsed(annotation))
        {
            // Mark the annotation as being used.
            usageMarker.markAsUsed(annotation);

            markConstant(clazz, annotation.u2typeIndex);

            // Mark the necessary element values.
            annotation.elementValuesAccept(clazz, this);

            // The return values.
            annotationUsed = true;
            attributeUsed  = true;
        }
    }


    // Implementations for ElementValueVisitor.

    public void visitConstantElementValue(Clazz clazz, Annotation annotation, ConstantElementValue constantElementValue)
    {
        if (isReferencedMethodUsed(constantElementValue))
        {
            // Mark the element value as being used.
            usageMarker.markAsUsed(constantElementValue);

            markConstant(clazz, constantElementValue.u2elementNameIndex);
            markConstant(clazz, constantElementValue.u2constantValueIndex);
        }
    }


    public void visitEnumConstantElementValue(Clazz clazz, Annotation annotation, EnumConstantElementValue enumConstantElementValue)
    {
        if (isReferencedMethodUsed(enumConstantElementValue))
        {
            // Check the referenced classes.
            allClassesUsed = true;
            enumConstantElementValue.referencedClassesAccept(this);

            if (allClassesUsed)
            {
                // Mark the element value as being used.
                usageMarker.markAsUsed(enumConstantElementValue);

                markConstant(clazz, enumConstantElementValue.u2elementNameIndex);
                markConstant(clazz, enumConstantElementValue.u2typeNameIndex);
                markConstant(clazz, enumConstantElementValue.u2constantNameIndex);
            }
        }
    }


    public void visitClassElementValue(Clazz clazz, Annotation annotation, ClassElementValue classElementValue)
    {
        if (isReferencedMethodUsed(classElementValue))
        {
            // Mark the element value as being used.
            usageMarker.markAsUsed(classElementValue);

            markConstant(clazz, classElementValue.u2elementNameIndex);
            markConstant(clazz, classElementValue.u2classInfoIndex);

            // Mark the referenced classes, since they can be retrieved from
            // the annotation and then used.
            // TODO: This could mark more annotation methods, affecting other annotations.
            classElementValue.referencedClassesAccept(usageMarker);
        }
    }


    public void visitAnnotationElementValue(Clazz clazz, Annotation annotation, AnnotationElementValue annotationElementValue)
    {
        if (isReferencedMethodUsed(annotationElementValue))
        {
            boolean oldAnnotationUsed = annotationUsed;

            // Check and mark the contained annotation.
            annotationUsed = false;
            annotationElementValue.annotationAccept(clazz, this);

            if (annotationUsed)
            {
                // Mark the element value as being used.
                usageMarker.markAsUsed(annotationElementValue);

                markConstant(clazz, annotationElementValue.u2elementNameIndex);
            }

            annotationUsed = oldAnnotationUsed;
        }
    }


    public void visitArrayElementValue(Clazz clazz, Annotation annotation, ArrayElementValue arrayElementValue)
    {
        if (isReferencedMethodUsed(arrayElementValue))
        {
            // Check and mark the contained element values.
            arrayElementValue.elementValuesAccept(clazz, annotation, this);

            // Mark the element value as being used.
            usageMarker.markAsUsed(arrayElementValue);

            markConstant(clazz, arrayElementValue.u2elementNameIndex);
        }
    }


    // Implementations for ConstantVisitor.

    public void visitAnyConstant(Clazz clazz, Constant constant)
    {
        usageMarker.markAsUsed(constant);
    }


    public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
    {
        // Is the class constant marked as being used?
        if (!usageMarker.isUsed(classConstant))
        {
            // Check the referenced class.
            allClassesUsed = true;
            classConstant.referencedClassAccept(this);

            // Is the referenced class marked as being used?
            if (allClassesUsed)
            {
                // Mark the class constant and its Utf8 constant.
                usageMarker.markAsUsed(classConstant);

                markConstant(clazz, classConstant.u2nameIndex);
            }
        }
    }


    // Implementations for ClassVisitor.

    public void visitProgramClass(ProgramClass programClass)
    {
        allClassesUsed &= usageMarker.isUsed(programClass);
    }


    public void visitLibraryClass(LibraryClass libraryClass)
    {
    }


    // Implementations for MemberVisitor.

    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
    {
        methodUsed = usageMarker.isUsed(programMethod);
    }


    public void visitLibraryMethod(LibraryClass LibraryClass, LibraryMethod libraryMethod)
    {
    }


    // Small utility methods.

    /**
     * Returns whether the annotation class has been marked as being used.
     */
    private boolean isReferencedClassUsed(Annotation annotation)
    {
        // Check if the referenced class is being used.
        allClassesUsed = true;
        annotation.referencedClassAccept(this);

        return allClassesUsed;
    }


    /**
     * Returns whether the annotation method has been marked as being used.
     */
    private boolean isReferencedMethodUsed(ElementValue elementValue)
    {
        // Check if the referenced method is being used.
        methodUsed = true;
        elementValue.referencedMethodAccept(this);

        return methodUsed;
    }


    /**
     * Marks the specified constant pool entry.
     */
    private void markConstant(Clazz clazz, int index)
    {
        if (index > 0)
        {
            clazz.constantPoolEntryAccept(index, this);
        }
    }
}