ElementValue.java revision 8a6199f0c36a778f22394364347a301b0b28e94b
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;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.annotation.visitor.ElementValueVisitor;
25import proguard.classfile.visitor.MemberVisitor;
26
27/**
28 * This abstract class represents an element value that is attached to an
29 * annotation or an annotation default. Specific types of element values are
30 * subclassed from it.
31 *
32 * @author Eric Lafortune
33 */
34public abstract class ElementValue implements VisitorAccepter
35{
36    /**
37     * An extra field for the optional element name. It is used in element value
38     * pairs of annotations. Otherwise, it is 0.
39     */
40    public int u2elementNameIndex;
41
42    /**
43     * An extra field pointing to the referenced <code>Clazz</code>
44     * object, if applicable. This field is typically filled out by the
45     * <code>{@link proguard.classfile.util.ClassReferenceInitializer}</code>.
46     */
47    public Clazz referencedClass;
48
49    /**
50     * An extra field pointing to the referenced <code>Method</code>
51     * object, if applicable. This field is typically filled out by the
52     * <code>{@link proguard.classfile.util.ClassReferenceInitializer}</code>.
53     */
54    public Method referencedMethod;
55
56    /**
57     * An extra field in which visitors can store information.
58     */
59    public Object visitorInfo;
60
61
62    /**
63     * Creates an uninitialized ElementValue.
64     */
65    protected ElementValue()
66    {
67    }
68
69
70    /**
71     * Creates an initialized ElementValue.
72     */
73    protected ElementValue(int u2elementNameIndex)
74    {
75        this.u2elementNameIndex = u2elementNameIndex;
76    }
77
78
79    /**
80     * Returns the element name.
81     */
82    public String getMethodName(Clazz clazz)
83    {
84        return clazz.getString(u2elementNameIndex);
85    }
86
87
88    // Abstract methods to be implemented by extensions.
89
90    /**
91     * Returns the tag of this element value.
92     */
93    public abstract char getTag();
94
95
96    /**
97     * Accepts the given visitor.
98     */
99    public abstract void accept(Clazz clazz, Annotation annotation, ElementValueVisitor elementValueVisitor);
100
101
102
103    /**
104     * Applies the given visitor to the referenced method.
105     */
106    public void referencedMethodAccept(MemberVisitor memberVisitor)
107    {
108        if (referencedMethod != null)
109        {
110            referencedMethod.accept(referencedClass, memberVisitor);
111        }
112    }
113
114
115    // Implementations for VisitorAccepter.
116
117    public Object getVisitorInfo()
118    {
119        return visitorInfo;
120    }
121
122    public void setVisitorInfo(Object visitorInfo)
123    {
124        this.visitorInfo = visitorInfo;
125    }
126}
127