001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.io.*;
036:
037: import org.apache.log4j.*;
038:
039: public class InnerClass implements Visitable {
040: public static final int ACC_PUBLIC = 0x0001;
041: public static final int ACC_PRIVATE = 0x0002;
042: public static final int ACC_PROTECTED = 0x0004;
043: public static final int ACC_STATIC = 0x0008;
044: public static final int ACC_FINAL = 0x0010;
045: public static final int ACC_INTERFACE = 0x0200;
046: public static final int ACC_ABSTRACT = 0x0400;
047:
048: private InnerClasses_attribute innerClasses;
049: private int innerClassInfoIndex;
050: private int outerClassInfoIndex;
051: private int innerNameIndex;
052: private int accessFlag;
053:
054: public InnerClass(InnerClasses_attribute innerClasses,
055: DataInputStream in) throws IOException {
056: this .innerClasses = innerClasses;
057:
058: innerClassInfoIndex = in.readUnsignedShort();
059: Logger.getLogger(getClass()).debug(
060: "Inner class info index: " + innerClassInfoIndex + " ("
061: + getInnerClassInfo() + ")");
062:
063: outerClassInfoIndex = in.readUnsignedShort();
064: Logger.getLogger(getClass()).debug(
065: "Outer class info index: " + outerClassInfoIndex + " ("
066: + getOuterClassInfo() + ")");
067:
068: innerNameIndex = in.readUnsignedShort();
069: Logger.getLogger(getClass()).debug(
070: "Inner name index: " + innerNameIndex + " ("
071: + getInnerName() + ")");
072:
073: accessFlag = in.readUnsignedShort();
074: Logger.getLogger(getClass()).debug(
075: "Inner class access flag: " + accessFlag);
076: }
077:
078: public InnerClasses_attribute getInnerClasses() {
079: return innerClasses;
080: }
081:
082: public int getInnerClassInfoIndex() {
083: return innerClassInfoIndex;
084: }
085:
086: public Class_info getRawInnerClassInfo() {
087: return (Class_info) innerClasses.getClassfile()
088: .getConstantPool().get(getInnerClassInfoIndex());
089: }
090:
091: public String getInnerClassInfo() {
092: String result = "";
093:
094: if (getInnerClassInfoIndex() != 0) {
095: result = getRawInnerClassInfo().toString();
096: }
097:
098: return result;
099: }
100:
101: public int getOuterClassInfoIndex() {
102: return outerClassInfoIndex;
103: }
104:
105: public Class_info getRawOuterClassInfo() {
106: return (Class_info) innerClasses.getClassfile()
107: .getConstantPool().get(getOuterClassInfoIndex());
108: }
109:
110: public String getOuterClassInfo() {
111: String result = "";
112:
113: if (getOuterClassInfoIndex() != 0) {
114: result = getRawOuterClassInfo().toString();
115: }
116:
117: return result;
118: }
119:
120: public int getInnerNameIndex() {
121: return innerNameIndex;
122: }
123:
124: public UTF8_info getRawInnerName() {
125: return (UTF8_info) innerClasses.getClassfile()
126: .getConstantPool().get(getInnerNameIndex());
127: }
128:
129: public String getInnerName() {
130: String result = "";
131:
132: if (getInnerNameIndex() != 0) {
133: result = getRawInnerName().toString();
134: }
135:
136: return result;
137: }
138:
139: public int getAccessFlag() {
140: return accessFlag;
141: }
142:
143: public boolean isPublic() {
144: return (getAccessFlag() & ACC_PUBLIC) != 0;
145: }
146:
147: public boolean isProtected() {
148: return (getAccessFlag() & ACC_PROTECTED) != 0;
149: }
150:
151: public boolean isPrivate() {
152: return (getAccessFlag() & ACC_PRIVATE) != 0;
153: }
154:
155: public boolean isPackage() {
156: return (getAccessFlag() & (ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE)) == 0;
157: }
158:
159: public boolean isStatic() {
160: return (getAccessFlag() & ACC_STATIC) != 0;
161: }
162:
163: public boolean isFinal() {
164: return (getAccessFlag() & ACC_FINAL) != 0;
165: }
166:
167: public boolean isInterface() {
168: return (getAccessFlag() & ACC_INTERFACE) != 0;
169: }
170:
171: public boolean isAbstract() {
172: return (getAccessFlag() & ACC_ABSTRACT) != 0;
173: }
174:
175: public String toString() {
176: return getInnerClassInfo();
177: }
178:
179: public void accept(Visitor visitor) {
180: visitor.visitInnerClass(this);
181: }
182: }
|