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: import java.util.*;
037:
038: import org.apache.log4j.*;
039:
040: public abstract class Feature_info implements Deprecatable, Visitable {
041: public static final int ACC_PUBLIC = 0x0001;
042: public static final int ACC_PRIVATE = 0x0002;
043: public static final int ACC_PROTECTED = 0x0004;
044: public static final int ACC_STATIC = 0x0008;
045: public static final int ACC_FINAL = 0x0010;
046:
047: private Classfile classfile;
048: private int accessFlag;
049: private int nameIndex;
050: private int descriptorIndex;
051: private Collection<Attribute_info> attributes = new LinkedList<Attribute_info>();
052:
053: public Feature_info(Classfile classfile, DataInputStream in)
054: throws IOException {
055: this .classfile = classfile;
056:
057: accessFlag = in.readUnsignedShort();
058: Logger.getLogger(getClass()).debug(
059: getFeatureType() + " access flag: " + accessFlag);
060:
061: nameIndex = in.readUnsignedShort();
062: Logger.getLogger(getClass()).debug(
063: getFeatureType() + " name: " + nameIndex + " ("
064: + getName() + ")");
065:
066: descriptorIndex = in.readUnsignedShort();
067: Logger.getLogger(getClass()).debug(
068: getFeatureType() + " Descriptor: " + descriptorIndex
069: + " (" + getDescriptor() + ")");
070:
071: int attributeCount = in.readUnsignedShort();
072: Logger.getLogger(getClass()).debug(
073: "Reading " + attributeCount + " " + getFeatureType()
074: + " attribute(s)");
075: for (int i = 0; i < attributeCount; i++) {
076: Logger.getLogger(getClass()).debug(
077: getFeatureType() + " attribute " + i + ":");
078: attributes.add(AttributeFactory.create(getClassfile(),
079: this , in));
080: }
081: }
082:
083: public Classfile getClassfile() {
084: return classfile;
085: }
086:
087: public int getAccessFlag() {
088: return accessFlag;
089: }
090:
091: public boolean isPublic() {
092: return (getAccessFlag() & ACC_PUBLIC) != 0;
093: }
094:
095: public boolean isProtected() {
096: return (getAccessFlag() & ACC_PROTECTED) != 0;
097: }
098:
099: public boolean isPrivate() {
100: return (getAccessFlag() & ACC_PRIVATE) != 0;
101: }
102:
103: public boolean isPackage() {
104: return (getAccessFlag() & (ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE)) == 0;
105: }
106:
107: public boolean isStatic() {
108: return (getAccessFlag() & ACC_STATIC) != 0;
109: }
110:
111: public boolean isFinal() {
112: return (getAccessFlag() & ACC_FINAL) != 0;
113: }
114:
115: public int getNameIndex() {
116: return nameIndex;
117: }
118:
119: public UTF8_info getRawName() {
120: return (UTF8_info) getClassfile().getConstantPool().get(
121: nameIndex);
122: }
123:
124: public String getName() {
125: return getRawName().toString();
126: }
127:
128: public String getFullName() {
129: return getClassfile().getClassName() + "." + getName();
130: }
131:
132: public int getDescriptorIndex() {
133: return descriptorIndex;
134: }
135:
136: public UTF8_info getRawDescriptor() {
137: return (UTF8_info) getClassfile().getConstantPool().get(
138: descriptorIndex);
139: }
140:
141: public String getDescriptor() {
142: return getRawDescriptor().toString();
143: }
144:
145: public Collection<Attribute_info> getAttributes() {
146: return attributes;
147: }
148:
149: public boolean isSynthetic() {
150: boolean result = false;
151:
152: Iterator i = getAttributes().iterator();
153: while (!result && i.hasNext()) {
154: result = i.next() instanceof Synthetic_attribute;
155: }
156:
157: return result;
158: }
159:
160: public boolean isDeprecated() {
161: boolean result = false;
162:
163: Iterator i = getAttributes().iterator();
164: while (!result && i.hasNext()) {
165: result = i.next() instanceof Deprecated_attribute;
166: }
167:
168: return result;
169: }
170:
171: public String toString() {
172: return getFullName();
173: }
174:
175: public abstract String getFeatureType();
176:
177: public abstract String getDeclaration();
178:
179: public abstract String getSignature();
180:
181: public String getFullSignature() {
182: return getClassfile().getClassName() + "." + getSignature();
183: }
184: }
|