KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > Feature_info


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.classreader;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.log4j.*;
39
40 public abstract class Feature_info implements Deprecatable, Visitable {
41     public static final int ACC_PUBLIC = 0x0001;
42     public static final int ACC_PRIVATE = 0x0002;
43     public static final int ACC_PROTECTED = 0x0004;
44     public static final int ACC_STATIC = 0x0008;
45     public static final int ACC_FINAL = 0x0010;
46
47     private Classfile classfile;
48     private int accessFlag;
49     private int nameIndex;
50     private int descriptorIndex;
51     private Collection attributes = new LinkedList();
52
53     public Feature_info(Classfile classfile, DataInputStream in) throws IOException {
54         this.classfile = classfile;
55
56         accessFlag = in.readUnsignedShort();
57         Logger.getLogger(getClass()).debug(getFeatureType() + " access flag: " + accessFlag);
58
59         nameIndex = in.readUnsignedShort();
60         Logger.getLogger(getClass()).debug(getFeatureType() + " name: " + nameIndex + " (" + getName() + ")");
61
62         descriptorIndex = in.readUnsignedShort();
63         Logger.getLogger(getClass()).debug(getFeatureType() + " Descriptor: " + descriptorIndex + " (" + getDescriptor() + ")");
64
65         int attributeCount = in.readUnsignedShort();
66         Logger.getLogger(getClass()).debug("Reading " + attributeCount + " " + getFeatureType() + " attribute(s)");
67         for (int i=0; i<attributeCount; i++) {
68             Logger.getLogger(getClass()).debug(getFeatureType() + " attribute " + i + ":");
69             attributes.add(AttributeFactory.create(getClassfile(), this, in));
70         }
71     }
72
73     public Classfile getClassfile() {
74         return classfile;
75     }
76
77     public int getAccessFlag() {
78         return accessFlag;
79     }
80
81     public boolean isPublic() {
82         return (getAccessFlag() & ACC_PUBLIC) != 0;
83     }
84
85     public boolean isProtected() {
86         return (getAccessFlag() & ACC_PROTECTED) != 0;
87     }
88
89     public boolean isPrivate() {
90         return (getAccessFlag() & ACC_PRIVATE) != 0;
91     }
92
93     public boolean isPackage() {
94         return (getAccessFlag() & (ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE)) == 0;
95     }
96
97     public boolean isStatic() {
98         return (getAccessFlag() & ACC_STATIC) != 0;
99     }
100
101     public boolean isFinal() {
102         return (getAccessFlag() & ACC_FINAL) != 0;
103     }
104
105     public int getNameIndex() {
106         return nameIndex;
107     }
108
109     public UTF8_info getRawName() {
110         return (UTF8_info) getClassfile().getConstantPool().get(nameIndex);
111     }
112
113     public String JavaDoc getName() {
114         return getRawName().toString();
115     }
116
117     public String JavaDoc getFullName() {
118         return getClassfile().getClassName() + "." + getName();
119     }
120
121     public int getDescriptorIndex() {
122         return descriptorIndex;
123     }
124
125     public UTF8_info getRawDescriptor() {
126         return (UTF8_info) getClassfile().getConstantPool().get(descriptorIndex);
127     }
128
129     public String JavaDoc getDescriptor() {
130         return getRawDescriptor().toString();
131     }
132
133     public Collection getAttributes() {
134         return attributes;
135     }
136
137     public boolean isSynthetic() {
138         boolean result = false;
139
140         Iterator i = getAttributes().iterator();
141         while (!result && i.hasNext()) {
142             result = i.next() instanceof Synthetic_attribute;
143         }
144     
145         return result;
146     }
147
148     public boolean isDeprecated() {
149         boolean result = false;
150
151         Iterator i = getAttributes().iterator();
152         while (!result && i.hasNext()) {
153             result = i.next() instanceof Deprecated_attribute;
154         }
155     
156         return result;
157     }
158
159     public String JavaDoc toString() {
160         return getFullName();
161     }
162
163     public abstract String JavaDoc getFeatureType();
164     public abstract String JavaDoc getDeclaration();
165     public abstract String JavaDoc getSignature();
166
167     public String JavaDoc getFullSignature() {
168         return getClassfile().getClassName() + "." + getSignature();
169     }
170 }
171
Popular Tags