You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-dev@jakarta.apache.org by tc...@apache.org on 2006/03/15 12:33:43 UTC
svn commit: r386056 [3/28] - in /jakarta/bcel/trunk: examples/
examples/Mini/ src/java/org/apache/bcel/
src/java/org/apache/bcel/classfile/ src/java/org/apache/bcel/generic/
src/java/org/apache/bcel/util/ src/java/org/apache/bcel/verifier/
src/java/org...
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Attribute.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Attribute.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Attribute.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Attribute.java Wed Mar 15 03:31:56 2006
@@ -45,238 +45,234 @@
* @see Synthetic
* @see Deprecated
* @see Signature
-*/
+ */
public abstract class Attribute implements Cloneable, Node, Serializable {
- protected int name_index; // Points to attribute name in constant pool
- protected int length; // Content length of attribute field
- protected byte tag; // Tag to distiguish subclasses
- protected ConstantPool constant_pool;
-
- protected Attribute(byte tag, int name_index, int length,
- ConstantPool constant_pool) {
- this.tag = tag;
- this.name_index = name_index;
- this.length = length;
- this.constant_pool = constant_pool;
- }
-
- /**
- * Called by objects that are traversing the nodes of the tree implicitely
- * defined by the contents of a Java class. I.e., the hierarchy of methods,
- * fields, attributes, etc. spawns a tree of objects.
- *
- * @param v Visitor object
- */
- public abstract void accept(Visitor v);
-
- /**
- * Dump attribute to file stream in binary format.
- *
- * @param file Output file stream
- * @throws IOException
- */
- public void dump(DataOutputStream file) throws IOException
- {
- file.writeShort(name_index);
- file.writeInt(length);
- }
-
- private static Map readers = new HashMap();
-
- /** Add an Attribute reader capable of parsing (user-defined) attributes
- * named "name". You should not add readers for the standard attributes
- * such as "LineNumberTable", because those are handled internally.
- *
- * @param name the name of the attribute as stored in the class file
- * @param r the reader object
- */
- public static void addAttributeReader(String name, AttributeReader r) {
- readers.put(name, r);
- }
-
- /** Remove attribute reader
- *
- * @param name the name of the attribute as stored in the class file
- */
- public static void removeAttributeReader(String name) {
- readers.remove(name);
- }
-
- /* Class method reads one attribute from the input data stream.
- * This method must not be accessible from the outside. It is
- * called by the Field and Method constructor methods.
- *
- * @see Field
- * @see Method
- * @param file Input stream
- * @param constant_pool Array of constants
- * @return Attribute
- * @throws IOException
- * @throws ClassFormatException
- */
- public static final Attribute readAttribute(DataInputStream file,
- ConstantPool constant_pool)
- throws IOException, ClassFormatException
- {
- ConstantUtf8 c;
- String name;
- int name_index;
- int length;
- byte tag = Constants.ATTR_UNKNOWN; // Unknown attribute
-
- // Get class name from constant pool via `name_index' indirection
- name_index = file.readUnsignedShort();
- c = (ConstantUtf8)constant_pool.getConstant(name_index,
- Constants.CONSTANT_Utf8);
- name = c.getBytes();
-
- // Length of data in bytes
- length = file.readInt();
-
- // Compare strings to find known attribute
- for(byte i=0; i < Constants.KNOWN_ATTRIBUTES; i++) {
- if(name.equals(Constants.ATTRIBUTE_NAMES[i])) {
- tag = i; // found!
- break;
- }
- }
-
- // Call proper constructor, depending on `tag'
- switch(tag) {
- case Constants.ATTR_UNKNOWN:
- AttributeReader r = (AttributeReader)readers.get(name);
-
- if(r != null)
- return r.createAttribute(name_index, length, file, constant_pool);
- return new Unknown(name_index, length, file, constant_pool);
-
- case Constants.ATTR_CONSTANT_VALUE:
- return new ConstantValue(name_index, length, file, constant_pool);
-
- case Constants.ATTR_SOURCE_FILE:
- return new SourceFile(name_index, length, file, constant_pool);
-
- case Constants.ATTR_CODE:
- return new Code(name_index, length, file, constant_pool);
-
- case Constants.ATTR_EXCEPTIONS:
- return new ExceptionTable(name_index, length, file, constant_pool);
-
- case Constants.ATTR_LINE_NUMBER_TABLE:
- return new LineNumberTable(name_index, length, file, constant_pool);
-
- case Constants.ATTR_LOCAL_VARIABLE_TABLE:
- return new LocalVariableTable(name_index, length, file, constant_pool);
-
- case Constants.ATTR_INNER_CLASSES:
- return new InnerClasses(name_index, length, file, constant_pool);
-
- case Constants.ATTR_SYNTHETIC:
- return new Synthetic(name_index, length, file, constant_pool);
-
- case Constants.ATTR_DEPRECATED:
- return new Deprecated(name_index, length, file, constant_pool);
-
- case Constants.ATTR_PMG:
- return new PMGClass(name_index, length, file, constant_pool);
-
- case Constants.ATTR_SIGNATURE:
- return new Signature(name_index, length, file, constant_pool);
-
- case Constants.ATTR_STACK_MAP:
- return new StackMap(name_index, length, file, constant_pool);
-
-// case Constants.ATTR_RUNTIMEVISIBLE_ANNOTATIONS:
-// return new RuntimeVisibleAnnotations(name_index, length, file, constant_pool);
-
-// case Constants.ATTR_RUNTIMEINVISIBLE_ANNOTATIONS:
-// return new RuntimeInvisibleAnnotations(name_index, length, file, constant_pool);
-
-// case Constants.ATTR_RUNTIMEVISIBLE_PARAMETER_ANNOTATIONS:
-// return new RuntimeVisibleParameterAnnotations(name_index, length, file, constant_pool);
-
-// case Constants.ATTR_RUNTIMEINVISIBLE_PARAMETER_ANNOTATIONS:
-// return new RuntimeInvisibleParameterAnnotations(name_index, length, file, constant_pool);
-
-// case Constants.ATTR_ANNOTATION_DEFAULT:
-// return new AnnotationDefault(name_index, length, file, constant_pool);
-
- default: // Never reached
- throw new IllegalStateException("Ooops! default case reached.");
- }
- }
-
- /**
- * @return Length of attribute field in bytes.
- */
- public final int getLength() { return length; }
-
- /**
- * @param length length in bytes.
- */
- public final void setLength(int length) {
- this.length = length;
- }
-
- /**
- * @param name_index of attribute.
- */
- public final void setNameIndex(int name_index) {
- this.name_index = name_index;
- }
-
- /**
- * @return Name index in constant pool of attribute name.
- */
- public final int getNameIndex() { return name_index; }
-
- /**
- * @return Tag of attribute, i.e., its type. Value may not be altered, thus
- * there is no setTag() method.
- */
- public final byte getTag() { return tag; }
-
- /**
- * @return Constant pool used by this object.
- * @see ConstantPool
- */
- public final ConstantPool getConstantPool() { return constant_pool; }
-
- /**
- * @param constant_pool Constant pool to be used for this object.
- * @see ConstantPool
- */
- public final void setConstantPool(ConstantPool constant_pool) {
- this.constant_pool = constant_pool;
- }
-
- /**
- * Use copy() if you want to have a deep copy(), i.e., with all references
- * copied correctly.
- *
- * @return shallow copy of this attribute
- */
- public Object clone() {
- Object o = null;
-
- try {
- o = super.clone();
- } catch(CloneNotSupportedException e) {
- e.printStackTrace(); // Never occurs
- }
-
- return o;
- }
-
- /**
- * @return deep copy of this attribute
- */
- public abstract Attribute copy(ConstantPool _constant_pool);
-
- /**
- * @return attribute name.
- */
- public String toString() {
- return Constants.ATTRIBUTE_NAMES[tag];
- }
+
+ protected int name_index; // Points to attribute name in constant pool
+ protected int length; // Content length of attribute field
+ protected byte tag; // Tag to distiguish subclasses
+ protected ConstantPool constant_pool;
+
+
+ protected Attribute(byte tag, int name_index, int length, ConstantPool constant_pool) {
+ this.tag = tag;
+ this.name_index = name_index;
+ this.length = length;
+ this.constant_pool = constant_pool;
+ }
+
+
+ /**
+ * Called by objects that are traversing the nodes of the tree implicitely
+ * defined by the contents of a Java class. I.e., the hierarchy of methods,
+ * fields, attributes, etc. spawns a tree of objects.
+ *
+ * @param v Visitor object
+ */
+ public abstract void accept( Visitor v );
+
+
+ /**
+ * Dump attribute to file stream in binary format.
+ *
+ * @param file Output file stream
+ * @throws IOException
+ */
+ public void dump( DataOutputStream file ) throws IOException {
+ file.writeShort(name_index);
+ file.writeInt(length);
+ }
+
+ private static Map readers = new HashMap();
+
+
+ /** Add an Attribute reader capable of parsing (user-defined) attributes
+ * named "name". You should not add readers for the standard attributes
+ * such as "LineNumberTable", because those are handled internally.
+ *
+ * @param name the name of the attribute as stored in the class file
+ * @param r the reader object
+ */
+ public static void addAttributeReader( String name, AttributeReader r ) {
+ readers.put(name, r);
+ }
+
+
+ /** Remove attribute reader
+ *
+ * @param name the name of the attribute as stored in the class file
+ */
+ public static void removeAttributeReader( String name ) {
+ readers.remove(name);
+ }
+
+
+ /* Class method reads one attribute from the input data stream.
+ * This method must not be accessible from the outside. It is
+ * called by the Field and Method constructor methods.
+ *
+ * @see Field
+ * @see Method
+ * @param file Input stream
+ * @param constant_pool Array of constants
+ * @return Attribute
+ * @throws IOException
+ * @throws ClassFormatException
+ */
+ public static final Attribute readAttribute( DataInputStream file, ConstantPool constant_pool )
+ throws IOException, ClassFormatException {
+ ConstantUtf8 c;
+ String name;
+ int name_index;
+ int length;
+ byte tag = Constants.ATTR_UNKNOWN; // Unknown attribute
+ // Get class name from constant pool via `name_index' indirection
+ name_index = file.readUnsignedShort();
+ c = (ConstantUtf8) constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8);
+ name = c.getBytes();
+ // Length of data in bytes
+ length = file.readInt();
+ // Compare strings to find known attribute
+ for (byte i = 0; i < Constants.KNOWN_ATTRIBUTES; i++) {
+ if (name.equals(Constants.ATTRIBUTE_NAMES[i])) {
+ tag = i; // found!
+ break;
+ }
+ }
+ // Call proper constructor, depending on `tag'
+ switch (tag) {
+ case Constants.ATTR_UNKNOWN:
+ AttributeReader r = (AttributeReader) readers.get(name);
+ if (r != null) {
+ return r.createAttribute(name_index, length, file, constant_pool);
+ }
+ return new Unknown(name_index, length, file, constant_pool);
+ case Constants.ATTR_CONSTANT_VALUE:
+ return new ConstantValue(name_index, length, file, constant_pool);
+ case Constants.ATTR_SOURCE_FILE:
+ return new SourceFile(name_index, length, file, constant_pool);
+ case Constants.ATTR_CODE:
+ return new Code(name_index, length, file, constant_pool);
+ case Constants.ATTR_EXCEPTIONS:
+ return new ExceptionTable(name_index, length, file, constant_pool);
+ case Constants.ATTR_LINE_NUMBER_TABLE:
+ return new LineNumberTable(name_index, length, file, constant_pool);
+ case Constants.ATTR_LOCAL_VARIABLE_TABLE:
+ return new LocalVariableTable(name_index, length, file, constant_pool);
+ case Constants.ATTR_INNER_CLASSES:
+ return new InnerClasses(name_index, length, file, constant_pool);
+ case Constants.ATTR_SYNTHETIC:
+ return new Synthetic(name_index, length, file, constant_pool);
+ case Constants.ATTR_DEPRECATED:
+ return new Deprecated(name_index, length, file, constant_pool);
+ case Constants.ATTR_PMG:
+ return new PMGClass(name_index, length, file, constant_pool);
+ case Constants.ATTR_SIGNATURE:
+ return new Signature(name_index, length, file, constant_pool);
+ case Constants.ATTR_STACK_MAP:
+ return new StackMap(name_index, length, file, constant_pool);
+ // case Constants.ATTR_RUNTIMEVISIBLE_ANNOTATIONS:
+ // return new RuntimeVisibleAnnotations(name_index, length, file, constant_pool);
+ // case Constants.ATTR_RUNTIMEINVISIBLE_ANNOTATIONS:
+ // return new RuntimeInvisibleAnnotations(name_index, length, file, constant_pool);
+ // case Constants.ATTR_RUNTIMEVISIBLE_PARAMETER_ANNOTATIONS:
+ // return new RuntimeVisibleParameterAnnotations(name_index, length, file, constant_pool);
+ // case Constants.ATTR_RUNTIMEINVISIBLE_PARAMETER_ANNOTATIONS:
+ // return new RuntimeInvisibleParameterAnnotations(name_index, length, file, constant_pool);
+ // case Constants.ATTR_ANNOTATION_DEFAULT:
+ // return new AnnotationDefault(name_index, length, file, constant_pool);
+ default: // Never reached
+ throw new IllegalStateException("Ooops! default case reached.");
+ }
+ }
+
+
+ /**
+ * @return Length of attribute field in bytes.
+ */
+ public final int getLength() {
+ return length;
+ }
+
+
+ /**
+ * @param length length in bytes.
+ */
+ public final void setLength( int length ) {
+ this.length = length;
+ }
+
+
+ /**
+ * @param name_index of attribute.
+ */
+ public final void setNameIndex( int name_index ) {
+ this.name_index = name_index;
+ }
+
+
+ /**
+ * @return Name index in constant pool of attribute name.
+ */
+ public final int getNameIndex() {
+ return name_index;
+ }
+
+
+ /**
+ * @return Tag of attribute, i.e., its type. Value may not be altered, thus
+ * there is no setTag() method.
+ */
+ public final byte getTag() {
+ return tag;
+ }
+
+
+ /**
+ * @return Constant pool used by this object.
+ * @see ConstantPool
+ */
+ public final ConstantPool getConstantPool() {
+ return constant_pool;
+ }
+
+
+ /**
+ * @param constant_pool Constant pool to be used for this object.
+ * @see ConstantPool
+ */
+ public final void setConstantPool( ConstantPool constant_pool ) {
+ this.constant_pool = constant_pool;
+ }
+
+
+ /**
+ * Use copy() if you want to have a deep copy(), i.e., with all references
+ * copied correctly.
+ *
+ * @return shallow copy of this attribute
+ */
+ public Object clone() {
+ Object o = null;
+ try {
+ o = super.clone();
+ } catch (CloneNotSupportedException e) {
+ e.printStackTrace(); // Never occurs
+ }
+ return o;
+ }
+
+
+ /**
+ * @return deep copy of this attribute
+ */
+ public abstract Attribute copy( ConstantPool _constant_pool );
+
+
+ /**
+ * @return attribute name.
+ */
+ public String toString() {
+ return Constants.ATTRIBUTE_NAMES[tag];
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/AttributeReader.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/AttributeReader.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/AttributeReader.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/AttributeReader.java Wed Mar 15 03:31:56 2006
@@ -16,7 +16,6 @@
*/
package org.apache.bcel.classfile;
-
/**
* Unknown (non-standard) attributes may be read via user-defined factory
* objects that can be registered with the Attribute.addAttributeReader
@@ -27,7 +26,8 @@
* @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
*/
public interface AttributeReader {
- /**
+
+ /**
When this attribute reader is added via the static method
Attribute.addAttributeReader, an attribute name is associated with it.
As the class file parser parses attributes, it will call various
@@ -52,9 +52,7 @@
returned which will cause the parsing of the class file to fail.
@see Attribute#addAttributeReader( String, AttributeReader )
- */
- public Attribute createAttribute(int name_index,
- int length,
- java.io.DataInputStream file,
- ConstantPool constant_pool);
+ */
+ public Attribute createAttribute( int name_index, int length, java.io.DataInputStream file,
+ ConstantPool constant_pool );
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassFormatException.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassFormatException.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassFormatException.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassFormatException.java Wed Mar 15 03:31:56 2006
@@ -16,7 +16,6 @@
*/
package org.apache.bcel.classfile;
-
/**
* Thrown when the BCEL attempts to read a class file and determines
* that the file is malformed or otherwise cannot be interpreted as a
@@ -26,7 +25,13 @@
* @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
*/
public class ClassFormatException extends RuntimeException {
- public ClassFormatException() { super(); }
- public ClassFormatException(String s) { super(s); }
-}
+ public ClassFormatException() {
+ super();
+ }
+
+
+ public ClassFormatException(String s) {
+ super(s);
+ }
+}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassParser.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassParser.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassParser.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassParser.java Wed Mar 15 03:31:56 2006
@@ -41,267 +41,255 @@
* @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
*/
public final class ClassParser {
- private DataInputStream file;
- private boolean fileOwned;
- private String file_name;
- private String zip_file;
- private int class_name_index, superclass_name_index;
- private int major, minor; // Compiler version
- private int access_flags; // Access rights of parsed class
- private int[] interfaces; // Names of implemented interfaces
- private ConstantPool constant_pool; // collection of constants
- private Field[] fields; // class fields, i.e., its variables
- private Method[] methods; // methods defined in the class
- private Attribute[] attributes; // attributes defined in the class
- private boolean is_zip; // Loaded from zip file
-
- private static final int BUFSIZE = 8192;
-
- /**
- * Parse class from the given stream.
- *
- * @param file Input stream
- * @param file_name File name
- */
- public ClassParser(InputStream file, String file_name) {
- this.file_name = file_name;
- fileOwned = false;
- String clazz = file.getClass().getName(); // Not a very clean solution ...
- is_zip = clazz.startsWith("java.util.zip.") || clazz.startsWith("java.util.jar.");
-
- if(file instanceof DataInputStream) // Is already a data stream
- this.file = (DataInputStream)file;
- else
- this.file = new DataInputStream(new BufferedInputStream(file, BUFSIZE));
- }
-
- /** Parse class from given .class file.
- *
- * @param file_name file name
- */
- public ClassParser(String file_name) throws IOException
- {
- is_zip = false;
- this.file_name = file_name;
- fileOwned = true;
- }
-
- /** Parse class from given .class file in a ZIP-archive
- *
- * @param zip_file zip file name
- * @param file_name file name
- */
- public ClassParser(String zip_file, String file_name)
- {
- is_zip = true;
- fileOwned = true;
- this.zip_file = zip_file;
- this.file_name = file_name;
- }
-
- /**
- * Parse the given Java class file and return an object that represents
- * the contained data, i.e., constants, methods, fields and commands.
- * A <em>ClassFormatException</em> is raised, if the file is not a valid
- * .class file. (This does not include verification of the byte code as it
- * is performed by the java interpreter).
- *
- * @return Class object representing the parsed class file
- * @throws IOException
- * @throws ClassFormatException
- */
- public JavaClass parse() throws IOException, ClassFormatException
- {
- ZipFile zip = null;
-
- try {
- if (fileOwned) {
- if (is_zip) {
- zip = new ZipFile(zip_file);
- ZipEntry entry = zip.getEntry(file_name);
- file = new DataInputStream(new BufferedInputStream(zip.getInputStream(entry),
- BUFSIZE));
- } else {
- file = new DataInputStream(new BufferedInputStream
- (new FileInputStream(file_name), BUFSIZE));
- }
- }
- /****************** Read headers ********************************/
- // Check magic tag of class file
- readID();
-
- // Get compiler version
- readVersion();
-
- /****************** Read constant pool and related **************/
- // Read constant pool entries
- readConstantPool();
-
- // Get class information
- readClassInfo();
-
- // Get interface information, i.e., implemented interfaces
- readInterfaces();
-
- /****************** Read class fields and methods ***************/
- // Read class fields, i.e., the variables of the class
- readFields();
-
- // Read class methods, i.e., the functions in the class
- readMethods();
-
- // Read class attributes
- readAttributes();
-
- // Check for unknown variables
- //Unknown[] u = Unknown.getUnknownAttributes();
- //for(int i=0; i < u.length; i++)
- // System.err.println("WARNING: " + u[i]);
-
- // Everything should have been read now
- // if(file.available() > 0) {
- // int bytes = file.available();
- // byte[] buf = new byte[bytes];
- // file.read(buf);
-
- // if(!(is_zip && (buf.length == 1))) {
- // System.err.println("WARNING: Trailing garbage at end of " + file_name);
- // System.err.println(bytes + " extra bytes: " + Utility.toHexString(buf));
- // }
- // }
- } finally {
- // Read everything of interest, so close the file
- if (fileOwned) {
- file.close();
- if (zip != null)
- zip.close();
- }
- }
-
- // Return the information we have gathered in a new object
- return new JavaClass(class_name_index, superclass_name_index,
- file_name, major, minor, access_flags,
- constant_pool, interfaces, fields,
- methods, attributes, is_zip? JavaClass.ZIP : JavaClass.FILE);
- }
-
- /**
- * Read information about the attributes of the class.
- * @throws IOException
- * @throws ClassFormatException
- */
- private final void readAttributes() throws IOException, ClassFormatException
- {
- int attributes_count;
-
- attributes_count = file.readUnsignedShort();
- attributes = new Attribute[attributes_count];
-
- for(int i=0; i < attributes_count; i++)
- attributes[i] = Attribute.readAttribute(file, constant_pool);
- }
-
- /**
- * Read information about the class and its super class.
- * @throws IOException
- * @throws ClassFormatException
- */
- private final void readClassInfo() throws IOException, ClassFormatException
- {
- access_flags = file.readUnsignedShort();
-
- /* Interfaces are implicitely abstract, the flag should be set
- * according to the JVM specification.
- */
- if((access_flags & Constants.ACC_INTERFACE) != 0)
- access_flags |= Constants.ACC_ABSTRACT;
-
- if(((access_flags & Constants.ACC_ABSTRACT) != 0) &&
- ((access_flags & Constants.ACC_FINAL) != 0 ))
- throw new ClassFormatException("Class can't be both final and abstract");
-
- class_name_index = file.readUnsignedShort();
- superclass_name_index = file.readUnsignedShort();
- }
- /**
- * Read constant pool entries.
- * @throws IOException
- * @throws ClassFormatException
- */
- private final void readConstantPool() throws IOException, ClassFormatException
- {
- constant_pool = new ConstantPool(file);
- }
-
- /**
- * Read information about the fields of the class, i.e., its variables.
- * @throws IOException
- * @throws ClassFormatException
- */
- private final void readFields() throws IOException, ClassFormatException
- {
- int fields_count;
-
- fields_count = file.readUnsignedShort();
- fields = new Field[fields_count];
-
- for(int i=0; i < fields_count; i++)
- fields[i] = new Field(file, constant_pool);
- }
-
- /******************** Private utility methods **********************/
-
- /**
- * Check whether the header of the file is ok.
- * Of course, this has to be the first action on successive file reads.
- * @throws IOException
- * @throws ClassFormatException
- */
- private final void readID() throws IOException, ClassFormatException
- {
- int magic = 0xCAFEBABE;
-
- if(file.readInt() != magic)
- throw new ClassFormatException(file_name + " is not a Java .class file");
- }
- /**
- * Read information about the interfaces implemented by this class.
- * @throws IOException
- * @throws ClassFormatException
- */
- private final void readInterfaces() throws IOException, ClassFormatException
- {
- int interfaces_count;
-
- interfaces_count = file.readUnsignedShort();
- interfaces = new int[interfaces_count];
-
- for(int i=0; i < interfaces_count; i++)
- interfaces[i] = file.readUnsignedShort();
- }
- /**
- * Read information about the methods of the class.
- * @throws IOException
- * @throws ClassFormatException
- */
- private final void readMethods() throws IOException, ClassFormatException
- {
- int methods_count;
-
- methods_count = file.readUnsignedShort();
- methods = new Method[methods_count];
-
- for(int i=0; i < methods_count; i++)
- methods[i] = new Method(file, constant_pool);
- }
- /**
- * Read major and minor version of compiler which created the file.
- * @throws IOException
- * @throws ClassFormatException
- */
- private final void readVersion() throws IOException, ClassFormatException
- {
- minor = file.readUnsignedShort();
- major = file.readUnsignedShort();
- }
+
+ private DataInputStream file;
+ private boolean fileOwned;
+ private String file_name;
+ private String zip_file;
+ private int class_name_index, superclass_name_index;
+ private int major, minor; // Compiler version
+ private int access_flags; // Access rights of parsed class
+ private int[] interfaces; // Names of implemented interfaces
+ private ConstantPool constant_pool; // collection of constants
+ private Field[] fields; // class fields, i.e., its variables
+ private Method[] methods; // methods defined in the class
+ private Attribute[] attributes; // attributes defined in the class
+ private boolean is_zip; // Loaded from zip file
+ private static final int BUFSIZE = 8192;
+
+
+ /**
+ * Parse class from the given stream.
+ *
+ * @param file Input stream
+ * @param file_name File name
+ */
+ public ClassParser(InputStream file, String file_name) {
+ this.file_name = file_name;
+ fileOwned = false;
+ String clazz = file.getClass().getName(); // Not a very clean solution ...
+ is_zip = clazz.startsWith("java.util.zip.") || clazz.startsWith("java.util.jar.");
+ if (file instanceof DataInputStream) {
+ this.file = (DataInputStream) file;
+ } else {
+ this.file = new DataInputStream(new BufferedInputStream(file, BUFSIZE));
+ }
+ }
+
+
+ /** Parse class from given .class file.
+ *
+ * @param file_name file name
+ */
+ public ClassParser(String file_name) throws IOException {
+ is_zip = false;
+ this.file_name = file_name;
+ fileOwned = true;
+ }
+
+
+ /** Parse class from given .class file in a ZIP-archive
+ *
+ * @param zip_file zip file name
+ * @param file_name file name
+ */
+ public ClassParser(String zip_file, String file_name) {
+ is_zip = true;
+ fileOwned = true;
+ this.zip_file = zip_file;
+ this.file_name = file_name;
+ }
+
+
+ /**
+ * Parse the given Java class file and return an object that represents
+ * the contained data, i.e., constants, methods, fields and commands.
+ * A <em>ClassFormatException</em> is raised, if the file is not a valid
+ * .class file. (This does not include verification of the byte code as it
+ * is performed by the java interpreter).
+ *
+ * @return Class object representing the parsed class file
+ * @throws IOException
+ * @throws ClassFormatException
+ */
+ public JavaClass parse() throws IOException, ClassFormatException {
+ ZipFile zip = null;
+ try {
+ if (fileOwned) {
+ if (is_zip) {
+ zip = new ZipFile(zip_file);
+ ZipEntry entry = zip.getEntry(file_name);
+ file = new DataInputStream(new BufferedInputStream(zip.getInputStream(entry),
+ BUFSIZE));
+ } else {
+ file = new DataInputStream(new BufferedInputStream(new FileInputStream(
+ file_name), BUFSIZE));
+ }
+ }
+ /****************** Read headers ********************************/
+ // Check magic tag of class file
+ readID();
+ // Get compiler version
+ readVersion();
+ /****************** Read constant pool and related **************/
+ // Read constant pool entries
+ readConstantPool();
+ // Get class information
+ readClassInfo();
+ // Get interface information, i.e., implemented interfaces
+ readInterfaces();
+ /****************** Read class fields and methods ***************/
+ // Read class fields, i.e., the variables of the class
+ readFields();
+ // Read class methods, i.e., the functions in the class
+ readMethods();
+ // Read class attributes
+ readAttributes();
+ // Check for unknown variables
+ //Unknown[] u = Unknown.getUnknownAttributes();
+ //for(int i=0; i < u.length; i++)
+ // System.err.println("WARNING: " + u[i]);
+ // Everything should have been read now
+ // if(file.available() > 0) {
+ // int bytes = file.available();
+ // byte[] buf = new byte[bytes];
+ // file.read(buf);
+ // if(!(is_zip && (buf.length == 1))) {
+ // System.err.println("WARNING: Trailing garbage at end of " + file_name);
+ // System.err.println(bytes + " extra bytes: " + Utility.toHexString(buf));
+ // }
+ // }
+ } finally {
+ // Read everything of interest, so close the file
+ if (fileOwned) {
+ file.close();
+ if (zip != null) {
+ zip.close();
+ }
+ }
+ }
+ // Return the information we have gathered in a new object
+ return new JavaClass(class_name_index, superclass_name_index, file_name, major, minor,
+ access_flags, constant_pool, interfaces, fields, methods, attributes, is_zip
+ ? JavaClass.ZIP
+ : JavaClass.FILE);
+ }
+
+
+ /**
+ * Read information about the attributes of the class.
+ * @throws IOException
+ * @throws ClassFormatException
+ */
+ private final void readAttributes() throws IOException, ClassFormatException {
+ int attributes_count;
+ attributes_count = file.readUnsignedShort();
+ attributes = new Attribute[attributes_count];
+ for (int i = 0; i < attributes_count; i++) {
+ attributes[i] = Attribute.readAttribute(file, constant_pool);
+ }
+ }
+
+
+ /**
+ * Read information about the class and its super class.
+ * @throws IOException
+ * @throws ClassFormatException
+ */
+ private final void readClassInfo() throws IOException, ClassFormatException {
+ access_flags = file.readUnsignedShort();
+ /* Interfaces are implicitely abstract, the flag should be set
+ * according to the JVM specification.
+ */
+ if ((access_flags & Constants.ACC_INTERFACE) != 0) {
+ access_flags |= Constants.ACC_ABSTRACT;
+ }
+ if (((access_flags & Constants.ACC_ABSTRACT) != 0)
+ && ((access_flags & Constants.ACC_FINAL) != 0)) {
+ throw new ClassFormatException("Class can't be both final and abstract");
+ }
+ class_name_index = file.readUnsignedShort();
+ superclass_name_index = file.readUnsignedShort();
+ }
+
+
+ /**
+ * Read constant pool entries.
+ * @throws IOException
+ * @throws ClassFormatException
+ */
+ private final void readConstantPool() throws IOException, ClassFormatException {
+ constant_pool = new ConstantPool(file);
+ }
+
+
+ /**
+ * Read information about the fields of the class, i.e., its variables.
+ * @throws IOException
+ * @throws ClassFormatException
+ */
+ private final void readFields() throws IOException, ClassFormatException {
+ int fields_count;
+ fields_count = file.readUnsignedShort();
+ fields = new Field[fields_count];
+ for (int i = 0; i < fields_count; i++) {
+ fields[i] = new Field(file, constant_pool);
+ }
+ }
+
+
+ /******************** Private utility methods **********************/
+ /**
+ * Check whether the header of the file is ok.
+ * Of course, this has to be the first action on successive file reads.
+ * @throws IOException
+ * @throws ClassFormatException
+ */
+ private final void readID() throws IOException, ClassFormatException {
+ int magic = 0xCAFEBABE;
+ if (file.readInt() != magic) {
+ throw new ClassFormatException(file_name + " is not a Java .class file");
+ }
+ }
+
+
+ /**
+ * Read information about the interfaces implemented by this class.
+ * @throws IOException
+ * @throws ClassFormatException
+ */
+ private final void readInterfaces() throws IOException, ClassFormatException {
+ int interfaces_count;
+ interfaces_count = file.readUnsignedShort();
+ interfaces = new int[interfaces_count];
+ for (int i = 0; i < interfaces_count; i++) {
+ interfaces[i] = file.readUnsignedShort();
+ }
+ }
+
+
+ /**
+ * Read information about the methods of the class.
+ * @throws IOException
+ * @throws ClassFormatException
+ */
+ private final void readMethods() throws IOException, ClassFormatException {
+ int methods_count;
+ methods_count = file.readUnsignedShort();
+ methods = new Method[methods_count];
+ for (int i = 0; i < methods_count; i++) {
+ methods[i] = new Method(file, constant_pool);
+ }
+ }
+
+
+ /**
+ * Read major and minor version of compiler which created the file.
+ * @throws IOException
+ * @throws ClassFormatException
+ */
+ private final void readVersion() throws IOException, ClassFormatException {
+ minor = file.readUnsignedShort();
+ major = file.readUnsignedShort();
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Code.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Code.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Code.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Code.java Wed Mar 15 03:31:56 2006
@@ -41,305 +41,312 @@
* @see LocalVariableTable
*/
public final class Code extends Attribute {
- private int max_stack; // Maximum size of stack used by this method
- private int max_locals; // Number of local variables
- private int code_length; // Length of code in bytes
- private byte[] code; // Actual byte code
-
- private int exception_table_length;
- private CodeException[] exception_table; // Table of handled exceptions
- private int attributes_count; // Attributes of code: LineNumber
- private Attribute[] attributes; // or LocalVariable
-
- /**
- * Initialize from another object. Note that both objects use the same
- * references (shallow copy). Use copy() for a physical copy.
- */
- public Code(Code c) {
- this(c.getNameIndex(), c.getLength(), c.getMaxStack(), c.getMaxLocals(),
- c.getCode(), c.getExceptionTable(), c.getAttributes(),
- c.getConstantPool());
- }
-
- /**
- * @param name_index Index pointing to the name <em>Code</em>
- * @param length Content length in bytes
- * @param file Input stream
- * @param constant_pool Array of constants
- */
- Code(int name_index, int length, DataInputStream file,
- ConstantPool constant_pool) throws IOException
- {
- // Initialize with some default values which will be overwritten later
- this(name_index, length,
- file.readUnsignedShort(), file.readUnsignedShort(),
- (byte[])null, (CodeException[])null, (Attribute[])null,
- constant_pool);
-
- code_length = file.readInt();
- code = new byte[code_length]; // Read byte code
- file.readFully(code);
-
- /* Read exception table that contains all regions where an exception
- * handler is active, i.e., a try { ... } catch() block.
- */
- exception_table_length = file.readUnsignedShort();
- exception_table = new CodeException[exception_table_length];
-
- for(int i=0; i < exception_table_length; i++)
- exception_table[i] = new CodeException(file);
-
- /* Read all attributes, currently `LineNumberTable' and
- * `LocalVariableTable'
- */
- attributes_count = file.readUnsignedShort();
- attributes = new Attribute[attributes_count];
- for(int i=0; i < attributes_count; i++)
- attributes[i] = Attribute.readAttribute(file, constant_pool);
-
- /* Adjust length, because of setAttributes in this(), s.b. length
- * is incorrect, because it didn't take the internal attributes
- * into account yet! Very subtle bug, fixed in 3.1.1.
- */
- this.length = length;
- }
-
- /**
- * @param name_index Index pointing to the name <em>Code</em>
- * @param length Content length in bytes
- * @param max_stack Maximum size of stack
- * @param max_locals Number of local variables
- * @param code Actual byte code
- * @param exception_table Table of handled exceptions
- * @param attributes Attributes of code: LineNumber or LocalVariable
- * @param constant_pool Array of constants
- */
- public Code(int name_index, int length,
- int max_stack, int max_locals,
- byte[] code,
- CodeException[] exception_table,
- Attribute[] attributes,
- ConstantPool constant_pool)
- {
- super(Constants.ATTR_CODE, name_index, length, constant_pool);
-
- this.max_stack = max_stack;
- this.max_locals = max_locals;
-
- setCode(code);
- setExceptionTable(exception_table);
- setAttributes(attributes); // Overwrites length!
- }
-
- /**
- * Called by objects that are traversing the nodes of the tree implicitely
- * defined by the contents of a Java class. I.e., the hierarchy of methods,
- * fields, attributes, etc. spawns a tree of objects.
- *
- * @param v Visitor object
- */
- public void accept(Visitor v) {
- v.visitCode(this);
- }
-
- /**
- * Dump code attribute to file stream in binary format.
- *
- * @param file Output file stream
- * @throws IOException
- */
- public final void dump(DataOutputStream file) throws IOException
- {
- super.dump(file);
-
- file.writeShort(max_stack);
- file.writeShort(max_locals);
- file.writeInt(code_length);
- file.write(code, 0, code_length);
-
- file.writeShort(exception_table_length);
- for(int i=0; i < exception_table_length; i++)
- exception_table[i].dump(file);
-
- file.writeShort(attributes_count);
- for(int i=0; i < attributes_count; i++)
- attributes[i].dump(file);
- }
-
- /**
- * @return Collection of code attributes.
- * @see Attribute
- */
- public final Attribute[] getAttributes() { return attributes; }
-
- /**
- * @return LineNumberTable of Code, if it has one
- */
- public LineNumberTable getLineNumberTable() {
- for(int i=0; i < attributes_count; i++)
- if(attributes[i] instanceof LineNumberTable)
- return (LineNumberTable)attributes[i];
-
- return null;
- }
-
- /**
- * @return LocalVariableTable of Code, if it has one
- */
- public LocalVariableTable getLocalVariableTable() {
- for(int i=0; i < attributes_count; i++)
- if(attributes[i] instanceof LocalVariableTable)
- return (LocalVariableTable)attributes[i];
-
- return null;
- }
-
- /**
- * @return Actual byte code of the method.
- */
- public final byte[] getCode() { return code; }
-
- /**
- * @return Table of handled exceptions.
- * @see CodeException
- */
- public final CodeException[] getExceptionTable() { return exception_table; }
-
- /**
- * @return Number of local variables.
- */
- public final int getMaxLocals() { return max_locals; }
-
- /**
- * @return Maximum size of stack used by this method.
- */
-
- public final int getMaxStack() { return max_stack; }
-
- /**
- * @return the internal length of this code attribute (minus the first 6 bytes)
- * and excluding all its attributes
- */
- private final int getInternalLength() {
- return 2 /*max_stack*/ + 2 /*max_locals*/ + 4 /*code length*/
- + code_length /*byte-code*/
- + 2 /*exception-table length*/
- + 8 * exception_table_length /* exception table */
- + 2 /* attributes count */;
- }
-
- /**
- * @return the full size of this code attribute, minus its first 6 bytes,
- * including the size of all its contained attributes
- */
- private final int calculateLength() {
- int len = 0;
-
- for(int i=0; i < attributes_count; i++)
- len += attributes[i].length + 6 /*attribute header size*/;
-
- return len + getInternalLength();
- }
-
- /**
- * @param attributes the attributes to set for this Code
- */
- public final void setAttributes(Attribute[] attributes) {
- this.attributes = attributes;
- attributes_count = (attributes == null)? 0 : attributes.length;
- length = calculateLength(); // Adjust length
- }
-
- /**
- * @param code byte code
- */
- public final void setCode(byte[] code) {
- this.code = code;
- code_length = (code == null)? 0 : code.length;
- }
-
- /**
- * @param exception_table exception table
- */
- public final void setExceptionTable(CodeException[] exception_table) {
- this.exception_table = exception_table;
- exception_table_length = (exception_table == null)? 0 :
- exception_table.length;
- }
-
- /**
- * @param max_locals maximum number of local variables
- */
- public final void setMaxLocals(int max_locals) {
- this.max_locals = max_locals;
- }
-
- /**
- * @param max_stack maximum stack size
- */
- public final void setMaxStack(int max_stack) {
- this.max_stack = max_stack;
- }
-
- /**
- * @return String representation of code chunk.
- */
- public final String toString(boolean verbose) {
- StringBuffer buf;
-
- buf = new StringBuffer(100);
- buf.append("Code(max_stack = ").append(max_stack)
- .append(", max_locals = ").append(max_locals)
- .append(", code_length = ").append(code_length).append(")\n")
- .append(Utility.codeToString(code, constant_pool, 0, -1, verbose));
-
- if(exception_table_length > 0) {
- buf.append("\nException handler(s) = \n").append("From\tTo\tHandler\tType\n");
-
- for(int i=0; i < exception_table_length; i++)
- buf.append(exception_table[i].toString(constant_pool, verbose)).append("\n");
- }
-
- if(attributes_count > 0) {
- buf.append("\nAttribute(s) = \n");
-
- for(int i=0; i < attributes_count; i++)
- buf.append(attributes[i].toString()).append("\n");
- }
-
- return buf.toString();
- }
-
- /**
- * @return String representation of code chunk.
- */
- public final String toString() {
- return toString(true);
- }
-
- /**
- * @return deep copy of this attribute
- *
- * @param _constant_pool the constant pool to duplicate
- */
- public Attribute copy(ConstantPool _constant_pool) {
- Code c = (Code) clone();
-
- if (code != null) {
- c.code = new byte[code.length];
- System.arraycopy(code, 0, c.code, 0, code.length);
- }
-
- c.constant_pool = _constant_pool;
-
- c.exception_table = new CodeException[exception_table_length];
- for(int i=0; i < exception_table_length; i++)
- c.exception_table[i] = exception_table[i].copy();
-
- c.attributes = new Attribute[attributes_count];
- for(int i=0; i < attributes_count; i++)
- c.attributes[i] = attributes[i].copy(_constant_pool);
- return c;
- }
+ private int max_stack; // Maximum size of stack used by this method
+ private int max_locals; // Number of local variables
+ private int code_length; // Length of code in bytes
+ private byte[] code; // Actual byte code
+ private int exception_table_length;
+ private CodeException[] exception_table; // Table of handled exceptions
+ private int attributes_count; // Attributes of code: LineNumber
+ private Attribute[] attributes; // or LocalVariable
+
+
+ /**
+ * Initialize from another object. Note that both objects use the same
+ * references (shallow copy). Use copy() for a physical copy.
+ */
+ public Code(Code c) {
+ this(c.getNameIndex(), c.getLength(), c.getMaxStack(), c.getMaxLocals(), c.getCode(), c
+ .getExceptionTable(), c.getAttributes(), c.getConstantPool());
+ }
+
+
+ /**
+ * @param name_index Index pointing to the name <em>Code</em>
+ * @param length Content length in bytes
+ * @param file Input stream
+ * @param constant_pool Array of constants
+ */
+ Code(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
+ throws IOException {
+ // Initialize with some default values which will be overwritten later
+ this(name_index, length, file.readUnsignedShort(), file.readUnsignedShort(), (byte[]) null,
+ (CodeException[]) null, (Attribute[]) null, constant_pool);
+ code_length = file.readInt();
+ code = new byte[code_length]; // Read byte code
+ file.readFully(code);
+ /* Read exception table that contains all regions where an exception
+ * handler is active, i.e., a try { ... } catch() block.
+ */
+ exception_table_length = file.readUnsignedShort();
+ exception_table = new CodeException[exception_table_length];
+ for (int i = 0; i < exception_table_length; i++) {
+ exception_table[i] = new CodeException(file);
+ }
+ /* Read all attributes, currently `LineNumberTable' and
+ * `LocalVariableTable'
+ */
+ attributes_count = file.readUnsignedShort();
+ attributes = new Attribute[attributes_count];
+ for (int i = 0; i < attributes_count; i++) {
+ attributes[i] = Attribute.readAttribute(file, constant_pool);
+ }
+ /* Adjust length, because of setAttributes in this(), s.b. length
+ * is incorrect, because it didn't take the internal attributes
+ * into account yet! Very subtle bug, fixed in 3.1.1.
+ */
+ this.length = length;
+ }
+
+
+ /**
+ * @param name_index Index pointing to the name <em>Code</em>
+ * @param length Content length in bytes
+ * @param max_stack Maximum size of stack
+ * @param max_locals Number of local variables
+ * @param code Actual byte code
+ * @param exception_table Table of handled exceptions
+ * @param attributes Attributes of code: LineNumber or LocalVariable
+ * @param constant_pool Array of constants
+ */
+ public Code(int name_index, int length, int max_stack, int max_locals, byte[] code,
+ CodeException[] exception_table, Attribute[] attributes, ConstantPool constant_pool) {
+ super(Constants.ATTR_CODE, name_index, length, constant_pool);
+ this.max_stack = max_stack;
+ this.max_locals = max_locals;
+ setCode(code);
+ setExceptionTable(exception_table);
+ setAttributes(attributes); // Overwrites length!
+ }
+
+
+ /**
+ * Called by objects that are traversing the nodes of the tree implicitely
+ * defined by the contents of a Java class. I.e., the hierarchy of methods,
+ * fields, attributes, etc. spawns a tree of objects.
+ *
+ * @param v Visitor object
+ */
+ public void accept( Visitor v ) {
+ v.visitCode(this);
+ }
+
+
+ /**
+ * Dump code attribute to file stream in binary format.
+ *
+ * @param file Output file stream
+ * @throws IOException
+ */
+ public final void dump( DataOutputStream file ) throws IOException {
+ super.dump(file);
+ file.writeShort(max_stack);
+ file.writeShort(max_locals);
+ file.writeInt(code_length);
+ file.write(code, 0, code_length);
+ file.writeShort(exception_table_length);
+ for (int i = 0; i < exception_table_length; i++) {
+ exception_table[i].dump(file);
+ }
+ file.writeShort(attributes_count);
+ for (int i = 0; i < attributes_count; i++) {
+ attributes[i].dump(file);
+ }
+ }
+
+
+ /**
+ * @return Collection of code attributes.
+ * @see Attribute
+ */
+ public final Attribute[] getAttributes() {
+ return attributes;
+ }
+
+
+ /**
+ * @return LineNumberTable of Code, if it has one
+ */
+ public LineNumberTable getLineNumberTable() {
+ for (int i = 0; i < attributes_count; i++) {
+ if (attributes[i] instanceof LineNumberTable) {
+ return (LineNumberTable) attributes[i];
+ }
+ }
+ return null;
+ }
+
+
+ /**
+ * @return LocalVariableTable of Code, if it has one
+ */
+ public LocalVariableTable getLocalVariableTable() {
+ for (int i = 0; i < attributes_count; i++) {
+ if (attributes[i] instanceof LocalVariableTable) {
+ return (LocalVariableTable) attributes[i];
+ }
+ }
+ return null;
+ }
+
+
+ /**
+ * @return Actual byte code of the method.
+ */
+ public final byte[] getCode() {
+ return code;
+ }
+
+
+ /**
+ * @return Table of handled exceptions.
+ * @see CodeException
+ */
+ public final CodeException[] getExceptionTable() {
+ return exception_table;
+ }
+
+
+ /**
+ * @return Number of local variables.
+ */
+ public final int getMaxLocals() {
+ return max_locals;
+ }
+
+
+ /**
+ * @return Maximum size of stack used by this method.
+ */
+ public final int getMaxStack() {
+ return max_stack;
+ }
+
+
+ /**
+ * @return the internal length of this code attribute (minus the first 6 bytes)
+ * and excluding all its attributes
+ */
+ private final int getInternalLength() {
+ return 2 /*max_stack*/+ 2 /*max_locals*/+ 4 /*code length*/
+ + code_length /*byte-code*/
+ + 2 /*exception-table length*/
+ + 8 * exception_table_length /* exception table */
+ + 2 /* attributes count */;
+ }
+
+
+ /**
+ * @return the full size of this code attribute, minus its first 6 bytes,
+ * including the size of all its contained attributes
+ */
+ private final int calculateLength() {
+ int len = 0;
+ for (int i = 0; i < attributes_count; i++) {
+ len += attributes[i].length + 6 /*attribute header size*/;
+ }
+ return len + getInternalLength();
+ }
+
+
+ /**
+ * @param attributes the attributes to set for this Code
+ */
+ public final void setAttributes( Attribute[] attributes ) {
+ this.attributes = attributes;
+ attributes_count = (attributes == null) ? 0 : attributes.length;
+ length = calculateLength(); // Adjust length
+ }
+
+
+ /**
+ * @param code byte code
+ */
+ public final void setCode( byte[] code ) {
+ this.code = code;
+ code_length = (code == null) ? 0 : code.length;
+ }
+
+
+ /**
+ * @param exception_table exception table
+ */
+ public final void setExceptionTable( CodeException[] exception_table ) {
+ this.exception_table = exception_table;
+ exception_table_length = (exception_table == null) ? 0 : exception_table.length;
+ }
+
+
+ /**
+ * @param max_locals maximum number of local variables
+ */
+ public final void setMaxLocals( int max_locals ) {
+ this.max_locals = max_locals;
+ }
+
+
+ /**
+ * @param max_stack maximum stack size
+ */
+ public final void setMaxStack( int max_stack ) {
+ this.max_stack = max_stack;
+ }
+
+
+ /**
+ * @return String representation of code chunk.
+ */
+ public final String toString( boolean verbose ) {
+ StringBuffer buf;
+ buf = new StringBuffer(100);
+ buf.append("Code(max_stack = ").append(max_stack).append(", max_locals = ").append(
+ max_locals).append(", code_length = ").append(code_length).append(")\n").append(
+ Utility.codeToString(code, constant_pool, 0, -1, verbose));
+ if (exception_table_length > 0) {
+ buf.append("\nException handler(s) = \n").append("From\tTo\tHandler\tType\n");
+ for (int i = 0; i < exception_table_length; i++) {
+ buf.append(exception_table[i].toString(constant_pool, verbose)).append("\n");
+ }
+ }
+ if (attributes_count > 0) {
+ buf.append("\nAttribute(s) = \n");
+ for (int i = 0; i < attributes_count; i++) {
+ buf.append(attributes[i].toString()).append("\n");
+ }
+ }
+ return buf.toString();
+ }
+
+
+ /**
+ * @return String representation of code chunk.
+ */
+ public final String toString() {
+ return toString(true);
+ }
+
+
+ /**
+ * @return deep copy of this attribute
+ *
+ * @param _constant_pool the constant pool to duplicate
+ */
+ public Attribute copy( ConstantPool _constant_pool ) {
+ Code c = (Code) clone();
+ if (code != null) {
+ c.code = new byte[code.length];
+ System.arraycopy(code, 0, c.code, 0, code.length);
+ }
+ c.constant_pool = _constant_pool;
+ c.exception_table = new CodeException[exception_table_length];
+ for (int i = 0; i < exception_table_length; i++) {
+ c.exception_table[i] = exception_table[i].copy();
+ }
+ c.attributes = new Attribute[attributes_count];
+ for (int i = 0; i < attributes_count; i++) {
+ c.attributes[i] = attributes[i].copy(_constant_pool);
+ }
+ return c;
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/CodeException.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/CodeException.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/CodeException.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/CodeException.java Wed Mar 15 03:31:56 2006
@@ -16,7 +16,6 @@
*/
package org.apache.bcel.classfile;
-
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@@ -32,164 +31,184 @@
* @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
* @see Code
*/
-public final class CodeException
- implements Cloneable, Constants, Node, Serializable
-{
- private int start_pc; // Range in the code the exception handler is
- private int end_pc; // active. start_pc is inclusive, end_pc exclusive
- private int handler_pc; /* Starting address of exception handler, i.e.,
- * an offset from start of code.
- */
- private int catch_type; /* If this is zero the handler catches any
- * exception, otherwise it points to the
- * exception class which is to be caught.
- */
- /**
- * Initialize from another object.
- */
- public CodeException(CodeException c) {
- this(c.getStartPC(), c.getEndPC(), c.getHandlerPC(), c.getCatchType());
- }
-
- /**
- * Construct object from file stream.
- * @param file Input stream
- * @throws IOException
- */
- CodeException(DataInputStream file) throws IOException
- {
- this(file.readUnsignedShort(), file.readUnsignedShort(),
- file.readUnsignedShort(), file.readUnsignedShort());
- }
-
- /**
- * @param start_pc Range in the code the exception handler is active,
- * start_pc is inclusive while
- * @param end_pc is exclusive
- * @param handler_pc Starting address of exception handler, i.e.,
- * an offset from start of code.
- * @param catch_type If zero the handler catches any
- * exception, otherwise it points to the exception class which is
- * to be caught.
- */
- public CodeException(int start_pc, int end_pc, int handler_pc,
- int catch_type)
- {
- this.start_pc = start_pc;
- this.end_pc = end_pc;
- this.handler_pc = handler_pc;
- this.catch_type = catch_type;
- }
-
- /**
- * Called by objects that are traversing the nodes of the tree implicitely
- * defined by the contents of a Java class. I.e., the hierarchy of methods,
- * fields, attributes, etc. spawns a tree of objects.
- *
- * @param v Visitor object
- */
- public void accept(Visitor v) {
- v.visitCodeException(this);
- }
- /**
- * Dump code exception to file stream in binary format.
- *
- * @param file Output file stream
- * @throws IOException
- */
- public final void dump(DataOutputStream file) throws IOException
- {
- file.writeShort(start_pc);
- file.writeShort(end_pc);
- file.writeShort(handler_pc);
- file.writeShort(catch_type);
- }
-
- /**
- * @return 0, if the handler catches any exception, otherwise it points to
- * the exception class which is to be caught.
- */
- public final int getCatchType() { return catch_type; }
-
- /**
- * @return Exclusive end index of the region where the handler is active.
- */
- public final int getEndPC() { return end_pc; }
-
- /**
- * @return Starting address of exception handler, relative to the code.
- */
- public final int getHandlerPC() { return handler_pc; }
-
- /**
- * @return Inclusive start index of the region where the handler is active.
- */
- public final int getStartPC() { return start_pc; }
-
- /**
- * @param catch_type the type of exception that is caught
- */
- public final void setCatchType(int catch_type) {
- this.catch_type = catch_type;
- }
-
- /**
- * @param end_pc end of handled block
- */
- public final void setEndPC(int end_pc) {
- this.end_pc = end_pc;
- }
-
- /**
- * @param handler_pc where the actual code is
- */
- public final void setHandlerPC(int handler_pc) {
- this.handler_pc = handler_pc;
- }
-
- /**
- * @param start_pc start of handled block
- */
- public final void setStartPC(int start_pc) {
- this.start_pc = start_pc;
- }
-
- /**
- * @return String representation.
- */
- public final String toString() {
- return "CodeException(start_pc = " + start_pc +
- ", end_pc = " + end_pc +
- ", handler_pc = " + handler_pc + ", catch_type = " + catch_type + ")";
- }
-
- /**
- * @return String representation.
- */
- public final String toString(ConstantPool cp, boolean verbose) {
- String str;
-
- if(catch_type == 0)
- str = "<Any exception>(0)";
- else
- str = Utility.compactClassName(cp.getConstantString(catch_type, CONSTANT_Class), false) +
- (verbose? "(" + catch_type + ")" : "");
-
- return start_pc + "\t" + end_pc + "\t" + handler_pc + "\t" + str;
- }
-
- public final String toString(ConstantPool cp) {
- return toString(cp, true);
- }
-
- /**
- * @return deep copy of this object
- */
- public CodeException copy() {
- try {
- return (CodeException)clone();
- } catch(CloneNotSupportedException e) {}
+public final class CodeException implements Cloneable, Constants, Node, Serializable {
- return null;
- }
+ private int start_pc; // Range in the code the exception handler is
+ private int end_pc; // active. start_pc is inclusive, end_pc exclusive
+ private int handler_pc; /* Starting address of exception handler, i.e.,
+ * an offset from start of code.
+ */
+ private int catch_type; /* If this is zero the handler catches any
+ * exception, otherwise it points to the
+ * exception class which is to be caught.
+ */
+
+
+ /**
+ * Initialize from another object.
+ */
+ public CodeException(CodeException c) {
+ this(c.getStartPC(), c.getEndPC(), c.getHandlerPC(), c.getCatchType());
+ }
+
+
+ /**
+ * Construct object from file stream.
+ * @param file Input stream
+ * @throws IOException
+ */
+ CodeException(DataInputStream file) throws IOException {
+ this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file
+ .readUnsignedShort());
+ }
+
+
+ /**
+ * @param start_pc Range in the code the exception handler is active,
+ * start_pc is inclusive while
+ * @param end_pc is exclusive
+ * @param handler_pc Starting address of exception handler, i.e.,
+ * an offset from start of code.
+ * @param catch_type If zero the handler catches any
+ * exception, otherwise it points to the exception class which is
+ * to be caught.
+ */
+ public CodeException(int start_pc, int end_pc, int handler_pc, int catch_type) {
+ this.start_pc = start_pc;
+ this.end_pc = end_pc;
+ this.handler_pc = handler_pc;
+ this.catch_type = catch_type;
+ }
+
+
+ /**
+ * Called by objects that are traversing the nodes of the tree implicitely
+ * defined by the contents of a Java class. I.e., the hierarchy of methods,
+ * fields, attributes, etc. spawns a tree of objects.
+ *
+ * @param v Visitor object
+ */
+ public void accept( Visitor v ) {
+ v.visitCodeException(this);
+ }
+
+
+ /**
+ * Dump code exception to file stream in binary format.
+ *
+ * @param file Output file stream
+ * @throws IOException
+ */
+ public final void dump( DataOutputStream file ) throws IOException {
+ file.writeShort(start_pc);
+ file.writeShort(end_pc);
+ file.writeShort(handler_pc);
+ file.writeShort(catch_type);
+ }
+
+
+ /**
+ * @return 0, if the handler catches any exception, otherwise it points to
+ * the exception class which is to be caught.
+ */
+ public final int getCatchType() {
+ return catch_type;
+ }
+
+
+ /**
+ * @return Exclusive end index of the region where the handler is active.
+ */
+ public final int getEndPC() {
+ return end_pc;
+ }
+
+
+ /**
+ * @return Starting address of exception handler, relative to the code.
+ */
+ public final int getHandlerPC() {
+ return handler_pc;
+ }
+
+
+ /**
+ * @return Inclusive start index of the region where the handler is active.
+ */
+ public final int getStartPC() {
+ return start_pc;
+ }
+
+
+ /**
+ * @param catch_type the type of exception that is caught
+ */
+ public final void setCatchType( int catch_type ) {
+ this.catch_type = catch_type;
+ }
+
+
+ /**
+ * @param end_pc end of handled block
+ */
+ public final void setEndPC( int end_pc ) {
+ this.end_pc = end_pc;
+ }
+
+
+ /**
+ * @param handler_pc where the actual code is
+ */
+ public final void setHandlerPC( int handler_pc ) {
+ this.handler_pc = handler_pc;
+ }
+
+
+ /**
+ * @param start_pc start of handled block
+ */
+ public final void setStartPC( int start_pc ) {
+ this.start_pc = start_pc;
+ }
+
+
+ /**
+ * @return String representation.
+ */
+ public final String toString() {
+ return "CodeException(start_pc = " + start_pc + ", end_pc = " + end_pc + ", handler_pc = "
+ + handler_pc + ", catch_type = " + catch_type + ")";
+ }
+
+
+ /**
+ * @return String representation.
+ */
+ public final String toString( ConstantPool cp, boolean verbose ) {
+ String str;
+ if (catch_type == 0) {
+ str = "<Any exception>(0)";
+ } else {
+ str = Utility.compactClassName(cp.getConstantString(catch_type, CONSTANT_Class), false)
+ + (verbose ? "(" + catch_type + ")" : "");
+ }
+ return start_pc + "\t" + end_pc + "\t" + handler_pc + "\t" + str;
+ }
+
+
+ public final String toString( ConstantPool cp ) {
+ return toString(cp, true);
+ }
+
+
+ /**
+ * @return deep copy of this object
+ */
+ public CodeException copy() {
+ try {
+ return (CodeException) clone();
+ } catch (CloneNotSupportedException e) {
+ }
+ return null;
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Constant.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Constant.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Constant.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/Constant.java Wed Mar 15 03:31:56 2006
@@ -32,133 +32,157 @@
* @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
*/
public abstract class Constant implements Cloneable, Node, Serializable {
- private static BCELComparator _cmp = new BCELComparator() {
- public boolean equals(Object o1, Object o2) {
- Constant THIS = (Constant)o1;
- Constant THAT = (Constant)o2;
-
- return THIS.toString().equals(THAT.toString());
- }
-
- public int hashCode(Object o) {
- Constant THIS = (Constant)o;
- return THIS.toString().hashCode();
- }
- };
-
- /* In fact this tag is redundant since we can distinguish different
- * `Constant' objects by their type, i.e., via `instanceof'. In some
- * places we will use the tag for switch()es anyway.
- *
- * First, we want match the specification as closely as possible. Second we
- * need the tag as an index to select the corresponding class name from the
- * `CONSTANT_NAMES' array.
- */
- protected byte tag;
-
- Constant(byte tag) { this.tag = tag; }
-
- /**
- * Called by objects that are traversing the nodes of the tree implicitely
- * defined by the contents of a Java class. I.e., the hierarchy of methods,
- * fields, attributes, etc. spawns a tree of objects.
- *
- * @param v Visitor object
- */
- public abstract void accept(Visitor v);
-
- public abstract void dump(DataOutputStream file) throws IOException;
-
- /**
- * @return Tag of constant, i.e., its type. No setTag() method to avoid
- * confusion.
- */
- public final byte getTag() { return tag; }
-
- /**
- * @return String representation.
- */
- public String toString() {
- return Constants.CONSTANT_NAMES[tag] + "[" + tag + "]";
- }
-
- /**
- * @return deep copy of this constant
- */
- public Constant copy() {
- try {
- return (Constant)super.clone();
- } catch(CloneNotSupportedException e) {}
-
- return null;
- }
-
- public Object clone() throws CloneNotSupportedException {
- return super.clone();
- }
-
- /**
- * Read one constant from the given file, the type depends on a tag byte.
- *
- * @param file Input stream
- * @return Constant object
- */
- static final Constant readConstant(DataInputStream file)
- throws IOException, ClassFormatException
- {
- byte b = file.readByte(); // Read tag byte
-
- switch(b) {
- case Constants.CONSTANT_Class: return new ConstantClass(file);
- case Constants.CONSTANT_Fieldref: return new ConstantFieldref(file);
- case Constants.CONSTANT_Methodref: return new ConstantMethodref(file);
- case Constants.CONSTANT_InterfaceMethodref: return new
- ConstantInterfaceMethodref(file);
- case Constants.CONSTANT_String: return new ConstantString(file);
- case Constants.CONSTANT_Integer: return new ConstantInteger(file);
- case Constants.CONSTANT_Float: return new ConstantFloat(file);
- case Constants.CONSTANT_Long: return new ConstantLong(file);
- case Constants.CONSTANT_Double: return new ConstantDouble(file);
- case Constants.CONSTANT_NameAndType: return new ConstantNameAndType(file);
- case Constants.CONSTANT_Utf8: return new ConstantUtf8(file);
- default:
- throw new ClassFormatException("Invalid byte tag in constant pool: " + b);
- }
- }
-
-
- /**
- * @return Comparison strategy object
- */
- public static BCELComparator getComparator() {
- return _cmp;
- }
-
- /**
- * @param comparator Comparison strategy object
- */
- public static void setComparator(BCELComparator comparator) {
- _cmp = comparator;
- }
-
- /**
- * Return value as defined by given BCELComparator strategy.
- * By default two Constant objects are said to be equal when
- * the result of toString() is equal.
- *
- * @see java.lang.Object#equals(java.lang.Object)
- */
- public boolean equals(Object obj) {
- return _cmp.equals(this, obj);
- }
-
- /**
- * Return value as defined by given BCELComparator strategy.
- * By default return the hashcode of the result of toString().
- *
- * @see java.lang.Object#hashCode()
- */
- public int hashCode() {
- return _cmp.hashCode(this);
- }
+
+ private static BCELComparator _cmp = new BCELComparator() {
+
+ public boolean equals( Object o1, Object o2 ) {
+ Constant THIS = (Constant) o1;
+ Constant THAT = (Constant) o2;
+ return THIS.toString().equals(THAT.toString());
+ }
+
+
+ public int hashCode( Object o ) {
+ Constant THIS = (Constant) o;
+ return THIS.toString().hashCode();
+ }
+ };
+ /* In fact this tag is redundant since we can distinguish different
+ * `Constant' objects by their type, i.e., via `instanceof'. In some
+ * places we will use the tag for switch()es anyway.
+ *
+ * First, we want match the specification as closely as possible. Second we
+ * need the tag as an index to select the corresponding class name from the
+ * `CONSTANT_NAMES' array.
+ */
+ protected byte tag;
+
+
+ Constant(byte tag) {
+ this.tag = tag;
+ }
+
+
+ /**
+ * Called by objects that are traversing the nodes of the tree implicitely
+ * defined by the contents of a Java class. I.e., the hierarchy of methods,
+ * fields, attributes, etc. spawns a tree of objects.
+ *
+ * @param v Visitor object
+ */
+ public abstract void accept( Visitor v );
+
+
+ public abstract void dump( DataOutputStream file ) throws IOException;
+
+
+ /**
+ * @return Tag of constant, i.e., its type. No setTag() method to avoid
+ * confusion.
+ */
+ public final byte getTag() {
+ return tag;
+ }
+
+
+ /**
+ * @return String representation.
+ */
+ public String toString() {
+ return Constants.CONSTANT_NAMES[tag] + "[" + tag + "]";
+ }
+
+
+ /**
+ * @return deep copy of this constant
+ */
+ public Constant copy() {
+ try {
+ return (Constant) super.clone();
+ } catch (CloneNotSupportedException e) {
+ }
+ return null;
+ }
+
+
+ public Object clone() throws CloneNotSupportedException {
+ return super.clone();
+ }
+
+
+ /**
+ * Read one constant from the given file, the type depends on a tag byte.
+ *
+ * @param file Input stream
+ * @return Constant object
+ */
+ static final Constant readConstant( DataInputStream file ) throws IOException,
+ ClassFormatException {
+ byte b = file.readByte(); // Read tag byte
+ switch (b) {
+ case Constants.CONSTANT_Class:
+ return new ConstantClass(file);
+ case Constants.CONSTANT_Fieldref:
+ return new ConstantFieldref(file);
+ case Constants.CONSTANT_Methodref:
+ return new ConstantMethodref(file);
+ case Constants.CONSTANT_InterfaceMethodref:
+ return new ConstantInterfaceMethodref(file);
+ case Constants.CONSTANT_String:
+ return new ConstantString(file);
+ case Constants.CONSTANT_Integer:
+ return new ConstantInteger(file);
+ case Constants.CONSTANT_Float:
+ return new ConstantFloat(file);
+ case Constants.CONSTANT_Long:
+ return new ConstantLong(file);
+ case Constants.CONSTANT_Double:
+ return new ConstantDouble(file);
+ case Constants.CONSTANT_NameAndType:
+ return new ConstantNameAndType(file);
+ case Constants.CONSTANT_Utf8:
+ return new ConstantUtf8(file);
+ default:
+ throw new ClassFormatException("Invalid byte tag in constant pool: " + b);
+ }
+ }
+
+
+ /**
+ * @return Comparison strategy object
+ */
+ public static BCELComparator getComparator() {
+ return _cmp;
+ }
+
+
+ /**
+ * @param comparator Comparison strategy object
+ */
+ public static void setComparator( BCELComparator comparator ) {
+ _cmp = comparator;
+ }
+
+
+ /**
+ * Return value as defined by given BCELComparator strategy.
+ * By default two Constant objects are said to be equal when
+ * the result of toString() is equal.
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals( Object obj ) {
+ return _cmp.equals(this, obj);
+ }
+
+
+ /**
+ * Return value as defined by given BCELComparator strategy.
+ * By default return the hashcode of the result of toString().
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode() {
+ return _cmp.hashCode(this);
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ConstantCP.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ConstantCP.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ConstantCP.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ConstantCP.java Wed Mar 15 03:31:56 2006
@@ -31,89 +31,101 @@
* @see ConstantInterfaceMethodref
*/
public abstract class ConstantCP extends Constant {
- /** References to the constants containing the class and the field signature
- */
- protected int class_index, name_and_type_index;
-
- /**
- * Initialize from another object.
- */
- public ConstantCP(ConstantCP c) {
- this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
- }
-
- /**
- * Initialize instance from file data.
- *
- * @param tag Constant type tag
- * @param file Input stream
- * @throws IOException
- */
- ConstantCP(byte tag, DataInputStream file) throws IOException
- {
- this(tag, file.readUnsignedShort(), file.readUnsignedShort());
- }
-
- /**
- * @param class_index Reference to the class containing the field
- * @param name_and_type_index and the field signature
- */
- protected ConstantCP(byte tag, int class_index,
- int name_and_type_index) {
- super(tag);
- this.class_index = class_index;
- this.name_and_type_index = name_and_type_index;
- }
-
- /**
- * Dump constant field reference to file stream in binary format.
- *
- * @param file Output file stream
- * @throws IOException
- */
- public final void dump(DataOutputStream file) throws IOException
- {
- file.writeByte(tag);
- file.writeShort(class_index);
- file.writeShort(name_and_type_index);
- }
-
- /**
- * @return Reference (index) to class this field or method belongs to.
- */
- public final int getClassIndex() { return class_index; }
-
- /**
- * @return Reference (index) to signature of the field.
- */
- public final int getNameAndTypeIndex() { return name_and_type_index; }
-
- /**
- * @param class_index points to Constant_class
- */
- public final void setClassIndex(int class_index) {
- this.class_index = class_index;
- }
-
- /**
- * @return Class this field belongs to.
- */
- public String getClass(ConstantPool cp) {
- return cp.constantToString(class_index, Constants.CONSTANT_Class);
- }
-
- /**
- * @param name_and_type_index points to Constant_NameAndType
- */
- public final void setNameAndTypeIndex(int name_and_type_index) {
- this.name_and_type_index = name_and_type_index;
- }
-
- /**
- * @return String representation.
- */
- public final String toString() {
- return super.toString() + "(class_index = " + class_index +
- ", name_and_type_index = " + name_and_type_index + ")";
- }
+
+ /** References to the constants containing the class and the field signature
+ */
+ protected int class_index, name_and_type_index;
+
+
+ /**
+ * Initialize from another object.
+ */
+ public ConstantCP(ConstantCP c) {
+ this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
+ }
+
+
+ /**
+ * Initialize instance from file data.
+ *
+ * @param tag Constant type tag
+ * @param file Input stream
+ * @throws IOException
+ */
+ ConstantCP(byte tag, DataInputStream file) throws IOException {
+ this(tag, file.readUnsignedShort(), file.readUnsignedShort());
+ }
+
+
+ /**
+ * @param class_index Reference to the class containing the field
+ * @param name_and_type_index and the field signature
+ */
+ protected ConstantCP(byte tag, int class_index, int name_and_type_index) {
+ super(tag);
+ this.class_index = class_index;
+ this.name_and_type_index = name_and_type_index;
+ }
+
+
+ /**
+ * Dump constant field reference to file stream in binary format.
+ *
+ * @param file Output file stream
+ * @throws IOException
+ */
+ public final void dump( DataOutputStream file ) throws IOException {
+ file.writeByte(tag);
+ file.writeShort(class_index);
+ file.writeShort(name_and_type_index);
+ }
+
+
+ /**
+ * @return Reference (index) to class this field or method belongs to.
+ */
+ public final int getClassIndex() {
+ return class_index;
+ }
+
+
+ /**
+ * @return Reference (index) to signature of the field.
+ */
+ public final int getNameAndTypeIndex() {
+ return name_and_type_index;
+ }
+
+
+ /**
+ * @param class_index points to Constant_class
+ */
+ public final void setClassIndex( int class_index ) {
+ this.class_index = class_index;
+ }
+
+
+ /**
+ * @return Class this field belongs to.
+ */
+ public String getClass( ConstantPool cp ) {
+ return cp.constantToString(class_index, Constants.CONSTANT_Class);
+ }
+
+
+ /**
+ * @param name_and_type_index points to Constant_NameAndType
+ */
+ public final void setNameAndTypeIndex( int name_and_type_index ) {
+ this.name_and_type_index = name_and_type_index;
+ }
+
+
+ /**
+ * @return String representation.
+ */
+ public final String toString() {
+ return super.toString() + "(class_index = " + class_index + ", name_and_type_index = "
+ + name_and_type_index + ")";
+ }
}
Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ConstantClass.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ConstantClass.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ConstantClass.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ConstantClass.java Wed Mar 15 03:31:56 2006
@@ -31,88 +31,98 @@
* @see Constant
*/
public final class ConstantClass extends Constant implements ConstantObject {
- private int name_index; // Identical to ConstantString except for the name
- /**
- * Initialize from another object.
- */
- public ConstantClass(ConstantClass c) {
- this(c.getNameIndex());
- }
-
- /**
- * Initialize instance from file data.
- *
- * @param file Input stream
- * @throws IOException
- */
- ConstantClass(DataInputStream file) throws IOException
- {
- this(file.readUnsignedShort());
- }
-
- /**
- * @param name_index Name index in constant pool. Should refer to a
- * ConstantUtf8.
- */
- public ConstantClass(int name_index) {
- super(Constants.CONSTANT_Class);
- this.name_index = name_index;
- }
-
- /**
- * Called by objects that are traversing the nodes of the tree implicitely
- * defined by the contents of a Java class. I.e., the hierarchy of methods,
- * fields, attributes, etc. spawns a tree of objects.
- *
- * @param v Visitor object
- */
- public void accept(Visitor v) {
- v.visitConstantClass(this);
- }
-
- /**
- * Dump constant class to file stream in binary format.
- *
- * @param file Output file stream
- * @throws IOException
- */
- public final void dump(DataOutputStream file) throws IOException
- {
- file.writeByte(tag);
- file.writeShort(name_index);
- }
-
- /**
- * @return Name index in constant pool of class name.
- */
- public final int getNameIndex() { return name_index; }
-
- /**
- * @param name_index the name index in the constant pool of this Constant Class
- */
- public final void setNameIndex(int name_index) {
- this.name_index = name_index;
- }
-
-
- /** @return String object
- */
- public Object getConstantValue(ConstantPool cp) {
- Constant c = cp.getConstant(name_index, Constants.CONSTANT_Utf8);
- return ((ConstantUtf8)c).getBytes();
- }
-
- /** @return dereferenced string
- */
- public String getBytes(ConstantPool cp) {
- return (String)getConstantValue(cp);
- }
-
- /**
- * @return String representation.
- */
- public final String toString() {
- return super.toString() + "(name_index = " + name_index + ")";
- }
+ private int name_index; // Identical to ConstantString except for the name
+
+
+ /**
+ * Initialize from another object.
+ */
+ public ConstantClass(ConstantClass c) {
+ this(c.getNameIndex());
+ }
+
+
+ /**
+ * Initialize instance from file data.
+ *
+ * @param file Input stream
+ * @throws IOException
+ */
+ ConstantClass(DataInputStream file) throws IOException {
+ this(file.readUnsignedShort());
+ }
+
+
+ /**
+ * @param name_index Name index in constant pool. Should refer to a
+ * ConstantUtf8.
+ */
+ public ConstantClass(int name_index) {
+ super(Constants.CONSTANT_Class);
+ this.name_index = name_index;
+ }
+
+
+ /**
+ * Called by objects that are traversing the nodes of the tree implicitely
+ * defined by the contents of a Java class. I.e., the hierarchy of methods,
+ * fields, attributes, etc. spawns a tree of objects.
+ *
+ * @param v Visitor object
+ */
+ public void accept( Visitor v ) {
+ v.visitConstantClass(this);
+ }
+
+
+ /**
+ * Dump constant class to file stream in binary format.
+ *
+ * @param file Output file stream
+ * @throws IOException
+ */
+ public final void dump( DataOutputStream file ) throws IOException {
+ file.writeByte(tag);
+ file.writeShort(name_index);
+ }
+
+
+ /**
+ * @return Name index in constant pool of class name.
+ */
+ public final int getNameIndex() {
+ return name_index;
+ }
+
+
+ /**
+ * @param name_index the name index in the constant pool of this Constant Class
+ */
+ public final void setNameIndex( int name_index ) {
+ this.name_index = name_index;
+ }
+
+
+ /** @return String object
+ */
+ public Object getConstantValue( ConstantPool cp ) {
+ Constant c = cp.getConstant(name_index, Constants.CONSTANT_Utf8);
+ return ((ConstantUtf8) c).getBytes();
+ }
+
+
+ /** @return dereferenced string
+ */
+ public String getBytes( ConstantPool cp ) {
+ return (String) getConstantValue(cp);
+ }
+
+
+ /**
+ * @return String representation.
+ */
+ public final String toString() {
+ return super.toString() + "(name_index = " + name_index + ")";
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org