1b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato/*
2b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * ProGuard -- shrinking, optimization, obfuscation, and preverification
3b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *             of Java bytecode.
4b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
5b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
6b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
7b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This program is free software; you can redistribute it and/or modify it
8b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * under the terms of the GNU General Public License as published by the Free
9b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Software Foundation; either version 2 of the License, or (at your option)
10b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * any later version.
11b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
12b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This program is distributed in the hope that it will be useful, but WITHOUT
13b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * more details.
16b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
17b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * You should have received a copy of the GNU General Public License along
18b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * with this program; if not, write to the Free Software Foundation, Inc.,
19b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */
21b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratopackage proguard.classfile.editor;
22b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
23b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.*;
24b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.attribute.annotation.*;
25b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.attribute.annotation.visitor.AnnotationVisitor;
26b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.util.SimplifiedVisitor;
27b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
28b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato/**
29b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This AnnotationVisitor adds all annotations that it visits to the given
30b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * target annotation element value, target annotation attribute, or target
31b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * parameter annotation attribute.
32b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
33b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @author Eric Lafortune
34b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */
35b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratopublic class AnnotationAdder
36b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoextends      SimplifiedVisitor
37b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimplements   AnnotationVisitor
38b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato{
39b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private static final ElementValue[] EMPTY_ELEMENT_VALUES = new ElementValue[0];
40b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
41b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
42b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private final ProgramClass                        targetClass;
43b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private final AnnotationElementValue              targetAnnotationElementValue;
44b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private final AnnotationsAttributeEditor          annotationsAttributeEditor;
45b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private final ParameterAnnotationsAttributeEditor parameterAnnotationsAttributeEditor;
46b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
47b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private final ConstantAdder constantAdder;
48b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
49b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
50b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    /**
51b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * Creates a new AnnotationAdder that will copy annotations into the given
52b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * target annotation element value.
53b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     */
54b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public AnnotationAdder(ProgramClass           targetClass,
55b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                           AnnotationElementValue targetAnnotationElementValue)
56b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
57b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.targetClass                         = targetClass;
58b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.targetAnnotationElementValue        = targetAnnotationElementValue;
59b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.annotationsAttributeEditor          = null;
60b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.parameterAnnotationsAttributeEditor = null;
61b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
62b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        constantAdder = new ConstantAdder(targetClass);
63b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
64b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
65b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
66b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    /**
67b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * Creates a new AnnotationAdder that will copy annotations into the given
68b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * target annotations attribute.
69b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     */
70b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public AnnotationAdder(ProgramClass         targetClass,
71b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                           AnnotationsAttribute targetAnnotationsAttribute)
72b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
73b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.targetClass                         = targetClass;
74b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.targetAnnotationElementValue        = null;
75b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.annotationsAttributeEditor          = new AnnotationsAttributeEditor(targetAnnotationsAttribute);
76b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.parameterAnnotationsAttributeEditor = null;
77b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
78b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        constantAdder = new ConstantAdder(targetClass);
79b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
80b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
81b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
82b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    /**
83b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * Creates a new AnnotationAdder that will copy annotations into the given
84b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * target parameter annotations attribute.
85b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     */
86b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public AnnotationAdder(ProgramClass                  targetClass,
87b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                           ParameterAnnotationsAttribute targetParameterAnnotationsAttribute)
88b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
89b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.targetClass                         = targetClass;
90b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.targetAnnotationElementValue        = null;
91b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.annotationsAttributeEditor          = null;
92b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.parameterAnnotationsAttributeEditor = new ParameterAnnotationsAttributeEditor(targetParameterAnnotationsAttribute);
93b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
94b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        constantAdder = new ConstantAdder(targetClass);
95b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
96b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
97b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
98b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    // Implementations for AnnotationVisitor.
99b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
100b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public void visitAnnotation(Clazz clazz, Annotation annotation)
101b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
102b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        Annotation newAnnotation =
103b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            new Annotation(constantAdder.addConstant(clazz, annotation.u2typeIndex),
104b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                           0,
105b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                           annotation.u2elementValuesCount > 0 ?
106b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                               new ElementValue[annotation.u2elementValuesCount] :
107b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                               EMPTY_ELEMENT_VALUES);
108b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
109b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // TODO: Clone array.
110b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        newAnnotation.referencedClasses = annotation.referencedClasses;
111b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
112b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // Add the element values.
113b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        annotation.elementValuesAccept(clazz,
114b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                                       new ElementValueAdder(targetClass,
115b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                                                             newAnnotation,
116b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                                                             false));
117b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
118b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // What's the target?
119b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        if (targetAnnotationElementValue != null)
120b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        {
121b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            // Simply set the completed annotation.
122b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            targetAnnotationElementValue.annotationValue = newAnnotation;
123b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        }
124b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        else
125b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        {
126b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            // Add the completed annotation.
127b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            annotationsAttributeEditor.addAnnotation(newAnnotation);
128b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        }
129b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
130b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
131b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
132b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public void visitAnnotation(Clazz clazz, Method method, int parameterIndex, Annotation annotation)
133b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
134b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        Annotation newAnnotation =
135b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            new Annotation(constantAdder.addConstant(clazz, annotation.u2typeIndex),
136b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                           0,
137b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                           annotation.u2elementValuesCount > 0 ?
138b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                               new ElementValue[annotation.u2elementValuesCount] :
139b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                               EMPTY_ELEMENT_VALUES);
140b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
141b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // TODO: Clone array.
142b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        newAnnotation.referencedClasses = annotation.referencedClasses;
143b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
144b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // Add the element values.
145b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        annotation.elementValuesAccept(clazz,
146b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                                       new ElementValueAdder(targetClass,
147b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                                                             newAnnotation,
148b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                                                             false));
149b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
150b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // Add the completed annotation.
151b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        parameterAnnotationsAttributeEditor.addAnnotation(parameterIndex, newAnnotation);
152b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
153b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato}