ProgramMember.java revision b9cc48a43ed984587c939d02fba5316bf5c0df6e
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;
22
23
24import proguard.classfile.attribute.*;
25import proguard.classfile.attribute.visitor.AttributeVisitor;
26import proguard.classfile.visitor.MemberVisitor;
27
28/**
29 * Representation of a field or method from a program class.
30 *
31 * @author Eric Lafortune
32 */
33public abstract class ProgramMember implements Member
34{
35    public int         u2accessFlags;
36    public int         u2nameIndex;
37    public int         u2descriptorIndex;
38    public int         u2attributesCount;
39    public Attribute[] attributes;
40
41    /**
42     * An extra field in which visitors can store information.
43     */
44    public Object visitorInfo;
45
46
47    /**
48     * Creates an uninitialized ProgramMember.
49     */
50    protected ProgramMember()
51    {
52    }
53
54
55    /**
56     * Creates an initialized ProgramMember.
57     */
58    protected ProgramMember(int         u2accessFlags,
59                            int         u2nameIndex,
60                            int         u2descriptorIndex,
61                            int         u2attributesCount,
62                            Attribute[] attributes)
63    {
64        this.u2accessFlags     = u2accessFlags;
65        this.u2nameIndex       = u2nameIndex;
66        this.u2descriptorIndex = u2descriptorIndex;
67        this.u2attributesCount = u2attributesCount;
68        this.attributes        = attributes;
69    }
70
71
72    /**
73     * Returns the (first) attribute with the given name.
74     */
75    private Attribute getAttribute(Clazz clazz, String name)
76    {
77        for (int index = 0; index < u2attributesCount; index++)
78        {
79            Attribute attribute = attributes[index];
80            if (attribute.getAttributeName(clazz).equals(name))
81            {
82                return attribute;
83            }
84        }
85
86        return null;
87    }
88
89
90    /**
91     * Accepts the given member info visitor.
92     */
93    public abstract void accept(ProgramClass  programClass,
94                                MemberVisitor memberVisitor);
95
96
97
98    /**
99     * Lets the given attribute info visitor visit all the attributes of
100     * this member info.
101     */
102    public abstract void attributesAccept(ProgramClass     programClass,
103                                          AttributeVisitor attributeVisitor);
104
105
106    // Implementations for Member.
107
108    public int getAccessFlags()
109    {
110        return u2accessFlags;
111    }
112
113    public String getName(Clazz clazz)
114    {
115        return clazz.getString(u2nameIndex);
116    }
117
118    public String getDescriptor(Clazz clazz)
119    {
120        return clazz.getString(u2descriptorIndex);
121    }
122
123    public void accept(Clazz clazz, MemberVisitor memberVisitor)
124    {
125        accept((ProgramClass)clazz, memberVisitor);
126    }
127
128
129    // Implementations for VisitorAccepter.
130
131    public Object getVisitorInfo()
132    {
133        return visitorInfo;
134    }
135
136    public void setVisitorInfo(Object visitorInfo)
137    {
138        this.visitorInfo = visitorInfo;
139    }
140}
141