KickJava   Java API By Example, From Geeks To Geeks.

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


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
37 import org.apache.log4j.*;
38
39 public class InnerClass implements Visitable {
40     public static final int ACC_PUBLIC = 0x0001;
41     public static final int ACC_PRIVATE = 0x0002;
42     public static final int ACC_PROTECTED = 0x0004;
43     public static final int ACC_STATIC = 0x0008;
44     public static final int ACC_FINAL = 0x0010;
45     public static final int ACC_INTERFACE = 0x0200;
46     public static final int ACC_ABSTRACT = 0x0400;
47
48     private InnerClasses_attribute innerClasses;
49     private int innerClassInfoIndex;
50     private int outerClassInfoIndex;
51     private int innerNameIndex;
52     private int accessFlag;
53
54     public InnerClass(InnerClasses_attribute innerClasses, DataInputStream in) throws IOException {
55         this.innerClasses = innerClasses;
56
57         innerClassInfoIndex = in.readUnsignedShort();
58         Logger.getLogger(getClass()).debug("Inner class info index: " + innerClassInfoIndex + " (" + getInnerClassInfo() + ")");
59
60         outerClassInfoIndex = in.readUnsignedShort();
61         Logger.getLogger(getClass()).debug("Outer class info index: " + outerClassInfoIndex + " (" + getOuterClassInfo() + ")");
62
63         innerNameIndex = in.readUnsignedShort();
64         Logger.getLogger(getClass()).debug("Inner name index: " + innerNameIndex + " (" + getInnerName() + ")");
65
66         accessFlag = in.readUnsignedShort();
67         Logger.getLogger(getClass()).debug("Inner class access flag: " + accessFlag);
68     }
69
70     public InnerClasses_attribute getInnerClasses() {
71         return innerClasses;
72     }
73
74     public int getInnerClassInfoIndex() {
75         return innerClassInfoIndex;
76     }
77
78     public Class_info getRawInnerClassInfo() {
79         return (Class_info) innerClasses.getClassfile().getConstantPool().get(getInnerClassInfoIndex());
80     }
81
82     public String JavaDoc getInnerClassInfo() {
83         String JavaDoc result = "";
84
85         if (getInnerClassInfoIndex() != 0) {
86             result = getRawInnerClassInfo().toString();
87         }
88
89         return result;
90     }
91
92     public int getOuterClassInfoIndex() {
93         return outerClassInfoIndex;
94     }
95
96     public Class_info getRawOuterClassInfo() {
97         return (Class_info) innerClasses.getClassfile().getConstantPool().get(getOuterClassInfoIndex());
98     }
99
100     public String JavaDoc getOuterClassInfo() {
101         String JavaDoc result = "";
102
103         if (getOuterClassInfoIndex() != 0) {
104             result = getRawOuterClassInfo().toString();
105         }
106
107         return result;
108     }
109
110     public int getInnerNameIndex() {
111         return innerNameIndex;
112     }
113
114     public UTF8_info getRawInnerName() {
115         return (UTF8_info) innerClasses.getClassfile().getConstantPool().get(getInnerNameIndex());
116     }
117
118     public String JavaDoc getInnerName() {
119         String JavaDoc result = "";
120
121         if (getInnerNameIndex() != 0) {
122             result = getRawInnerName().toString();
123         }
124
125         return result;
126     }
127
128     public int getAccessFlag() {
129         return accessFlag;
130     }
131
132     public boolean isPublic() {
133         return (getAccessFlag() & ACC_PUBLIC) != 0;
134     }
135
136     public boolean isProtected() {
137         return (getAccessFlag() & ACC_PROTECTED) != 0;
138     }
139
140     public boolean isPrivate() {
141         return (getAccessFlag() & ACC_PRIVATE) != 0;
142     }
143
144     public boolean isPackage() {
145         return (getAccessFlag() & (ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE)) == 0;
146     }
147
148     public boolean isStatic() {
149         return (getAccessFlag() & ACC_STATIC) != 0;
150     }
151
152     public boolean isFinal() {
153         return (getAccessFlag() & ACC_FINAL) != 0;
154     }
155
156     public boolean isInterface() {
157         return (getAccessFlag() & ACC_INTERFACE) != 0;
158     }
159
160     public boolean isAbstract() {
161         return (getAccessFlag() & ACC_ABSTRACT) != 0;
162     }
163
164     public String JavaDoc toString() {
165         return getInnerClassInfo();
166     }
167
168     public void accept(Visitor visitor) {
169         visitor.visitInnerClass(this);
170     }
171 }
172
Popular Tags