KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > classfile > ProgramMember


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This library 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 library 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 Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.classfile;
22
23
24 import proguard.classfile.visitor.*;
25 import proguard.classfile.attribute.*;
26 import proguard.classfile.attribute.visitor.AttributeVisitor;
27
28 import java.io.*;
29
30 /**
31  * Representation of a field or method from a program class.
32  *
33  * @author Eric Lafortune
34  */

35 public abstract class ProgramMember implements Member
36 {
37     public int u2accessFlags;
38     public int u2nameIndex;
39     public int u2descriptorIndex;
40     public int u2attributesCount;
41     public Attribute[] attributes;
42
43     /**
44      * An extra field in which visitors can store information.
45      */

46     public Object JavaDoc visitorInfo;
47
48
49     protected ProgramMember() {}
50
51
52     /**
53      * Returns the line number range of the given class member as "m:n",
54      * if it can find it, or <code>null</code> otherwise.
55      */

56     public String JavaDoc getLineNumberRange(Clazz clazz)
57     {
58         CodeAttribute codeAttribute =
59             (CodeAttribute)getAttribute(clazz, ClassConstants.ATTR_Code);
60         if (codeAttribute == null)
61         {
62             return null;
63         }
64
65         LineNumberTableAttribute lineNumberTableAttribute =
66             (LineNumberTableAttribute)codeAttribute.getAttribute(clazz,
67                                                                  ClassConstants.ATTR_LineNumberTable);
68         if (lineNumberTableAttribute == null)
69         {
70             return null;
71         }
72
73         return "" +
74                lineNumberTableAttribute.getLineNumber(0) +
75                ":" +
76                lineNumberTableAttribute.getLineNumber(Integer.MAX_VALUE);
77     }
78
79
80     /**
81      * Returns the (first) attribute with the given name.
82      */

83     private Attribute getAttribute(Clazz clazz, String JavaDoc name)
84     {
85         for (int index = 0; index < u2attributesCount; index++)
86         {
87             Attribute attribute = attributes[index];
88             if (attribute.getAttributeName(clazz).equals(name))
89             {
90                 return attribute;
91             }
92         }
93
94         return null;
95     }
96
97
98     /**
99      * Accepts the given member info visitor.
100      */

101     public abstract void accept(ProgramClass programClass,
102                                 MemberVisitor memberVisitor);
103
104
105
106     /**
107      * Lets the given attribute info visitor visit all the attributes of
108      * this member info.
109      */

110     public abstract void attributesAccept(ProgramClass programClass,
111                                           AttributeVisitor attributeVisitor);
112
113
114     // Implementations for Member.
115

116     public int getAccessFlags()
117     {
118         return u2accessFlags;
119     }
120
121     public String JavaDoc getName(Clazz clazz)
122     {
123         return clazz.getString(u2nameIndex);
124     }
125
126     public String JavaDoc getDescriptor(Clazz clazz)
127     {
128         return clazz.getString(u2descriptorIndex);
129     }
130
131     public void accept(Clazz clazz, MemberVisitor memberVisitor)
132     {
133         accept((ProgramClass)clazz, memberVisitor);
134     }
135
136
137     // Implementations for VisitorAccepter.
138

139     public Object JavaDoc getVisitorInfo()
140     {
141         return visitorInfo;
142     }
143
144     public void setVisitorInfo(Object JavaDoc visitorInfo)
145     {
146         this.visitorInfo = visitorInfo;
147     }
148 }
149
Popular Tags