1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 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;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.visitor.AttributeVisitor;
25
26/**
27 * This abstract class represents an attribute that is attached to a class,
28 * a class member, or a code attribute. Specific types of attributes are
29 * subclassed from it.
30 *
31 * @author Eric Lafortune
32 * @noinspection AbstractClassWithoutAbstractMethods
33 */
34public abstract class Attribute implements VisitorAccepter
35{
36    public int u2attributeNameIndex;
37    //public int  u4attributeLength;
38    //public byte info[];
39
40    /**
41     * An extra field in which visitors can store information.
42     */
43    public Object visitorInfo;
44
45
46    /**
47     * Create an uninitialized Attribute.
48     */
49    protected Attribute()
50    {
51    }
52
53
54    /**
55     * Create an initialized Attribute.
56     */
57    protected Attribute(int u2attributeNameIndex)
58    {
59        this.u2attributeNameIndex = u2attributeNameIndex;
60    }
61
62
63    /**
64     * Returns the String name of the attribute.
65     */
66    public String getAttributeName(Clazz clazz)
67    {
68        return clazz.getString(u2attributeNameIndex);
69    }
70
71
72    // Methods to be implemented by extensions, if applicable.
73
74    /**
75     * Accepts the given visitor.
76     */
77    public void accept(Clazz clazz, AttributeVisitor attributeVisitor)
78    {
79        throw new UnsupportedOperationException("Method must be overridden in ["+this.getClass().getName()+"] if ever called");
80    }
81
82    /**
83     * Accepts the given visitor in the context of the given field.
84     */
85    public void accept(Clazz clazz, Field field, AttributeVisitor attributeVisitor)
86    {
87        // Delegate the default invocation if the field is null anyway.
88        if (field == null)
89        {
90            accept(clazz, attributeVisitor);
91        }
92        else
93        {
94            throw new UnsupportedOperationException("Method must be overridden in ["+this.getClass().getName()+"] if ever called");
95        }
96    }
97
98    /**
99     * Accepts the given visitor in the context of the given method.
100     */
101    public void accept(Clazz clazz, Method method, AttributeVisitor attributeVisitor)
102    {
103        // Delegate the default invocation if the method is null anyway.
104        if (method == null)
105        {
106            accept(clazz, (Field)null, attributeVisitor);
107        }
108        else
109        {
110            throw new UnsupportedOperationException("Method must be overridden in ["+this.getClass().getName()+"] if ever called");
111        }
112    }
113
114    /**
115     * Accepts the given visitor in the context of the given code attribute.
116     */
117    public void accept(Clazz clazz, Method method, CodeAttribute codeAttribute, AttributeVisitor attributeVisitor)
118    {
119        // Delegate the default invocation if the code attribute is null anyway.
120        if (codeAttribute == null)
121        {
122            accept(clazz, method, attributeVisitor);
123        }
124        else
125        {
126            throw new UnsupportedOperationException("Method must be overridden in ["+this.getClass().getName()+"] if ever called");
127        }
128    }
129
130
131    // Implementations for VisitorAccepter.
132
133    public Object getVisitorInfo()
134    {
135        return visitorInfo;
136    }
137
138    public void setVisitorInfo(Object visitorInfo)
139    {
140        this.visitorInfo = visitorInfo;
141    }
142}
143