From: aclement Date: Tue, 15 Sep 2009 19:40:09 +0000 (+0000) Subject: refactoring X-Git-Tag: V1_6_6~37 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7c89067090cc77b93ed9936919e9439b0aa336e2;p=aspectj.git refactoring --- diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/AnnotationDefault.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/AnnotationDefault.java index 53cf1c33d..bd5a7251f 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/AnnotationDefault.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/AnnotationDefault.java @@ -17,7 +17,7 @@ import java.io.DataOutputStream; import java.io.IOException; import org.aspectj.apache.bcel.Constants; -import org.aspectj.apache.bcel.classfile.annotation.ElementValueGen; +import org.aspectj.apache.bcel.classfile.annotation.ElementValue; /** * This attribute is attached to a method and indicates the default @@ -25,13 +25,13 @@ import org.aspectj.apache.bcel.classfile.annotation.ElementValueGen; */ public class AnnotationDefault extends Attribute { - private ElementValueGen value; + private ElementValue value; public AnnotationDefault(int nameIndex, int len, DataInputStream dis, ConstantPool cpool) throws IOException { - this(nameIndex, len, ElementValueGen.readElementValue(dis,cpool), cpool); + this(nameIndex, len, ElementValue.readElementValue(dis,cpool), cpool); } - private AnnotationDefault(int nameIndex, int len, ElementValueGen value, ConstantPool cpool) { + private AnnotationDefault(int nameIndex, int len, ElementValue value, ConstantPool cpool) { super(Constants.ATTR_ANNOTATION_DEFAULT, nameIndex, len, cpool); this.value = value; } @@ -42,7 +42,7 @@ public class AnnotationDefault extends Attribute { // return (EnclosingMethod)clone(); } - public final ElementValueGen getElementValue() { return value; } + public final ElementValue getElementValue() { return value; } public final void dump(DataOutputStream dos) throws IOException { super.dump(dos); diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Attribute.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Attribute.java index 3f730f217..aefa0ed44 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Attribute.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Attribute.java @@ -60,17 +60,17 @@ import java.io.IOException; import java.io.Serializable; import org.aspectj.apache.bcel.Constants; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisibleAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisibleParameterAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisParamAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisParamAnnos; /** * Abstract super class for Attribute objects. Currently the ConstantValue, SourceFile, Code, * Exceptiontable, LineNumberTable, LocalVariableTable, InnerClasses and Synthetic * attributes are supported. The Unknown attribute stands for non-standard-attributes. * - * @version $Id: Attribute.java,v 1.6 2009/09/10 15:35:05 aclement Exp $ + * @version $Id: Attribute.java,v 1.7 2009/09/15 19:40:12 aclement Exp $ * @author M. Dahm * @see ConstantValue * @see SourceFile @@ -143,13 +143,13 @@ public abstract class Attribute implements Cloneable, Node, Serializable { case Constants.ATTR_STACK_MAP: return new StackMap(idx, len, file, cpool); case Constants.ATTR_RUNTIME_VISIBLE_ANNOTATIONS: - return new RuntimeVisibleAnnotations(idx, len, file, cpool); + return new RuntimeVisAnnos(idx, len, file, cpool); case Constants.ATTR_RUNTIME_INVISIBLE_ANNOTATIONS: - return new RuntimeInvisibleAnnotations(idx, len, file, cpool); + return new RuntimeInvisAnnos(idx, len, file, cpool); case Constants.ATTR_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS: - return new RuntimeVisibleParameterAnnotations(idx, len, file, cpool); + return new RuntimeVisParamAnnos(idx, len, file, cpool); case Constants.ATTR_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS: - return new RuntimeInvisibleParameterAnnotations(idx, len, file, cpool); + return new RuntimeInvisParamAnnos(idx, len, file, cpool); case Constants.ATTR_ANNOTATION_DEFAULT: return new AnnotationDefault(idx, len, file, cpool); case Constants.ATTR_LOCAL_VARIABLE_TYPE_TABLE: @@ -181,28 +181,29 @@ public abstract class Attribute implements Cloneable, Node, Serializable { return cpool; } - /** - * Use copy() if you want to have a deep copy(), ie. with all references copied correctly. - * - * @return shallow copy of this attribute - */ - @Override - 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); + // /** + // * Use copy() if you want to have a deep copy(), ie. with all references copied correctly. + // * + // * @return shallow copy of this attribute + // */ + // @Override + // 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); @Override public String toString() { diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/AttributeUtils.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/AttributeUtils.java index c2f7ed1c0..45d1597a7 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/AttributeUtils.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/AttributeUtils.java @@ -36,17 +36,6 @@ public class AttributeUtils { } } - public static Attribute[] copy(Attribute[] attributes, ConstantPool constant_pool) { - if (attributes == null || attributes.length == 0) { - return Attribute.NoAttributes; - } - Attribute[] attrs = new Attribute[attributes.length]; - for (int i = 0; i < attributes.length; i++) { - attrs[i] = attributes[i].copy(constant_pool); - } - return attrs; - } - public static Signature getSignatureAttribute(Attribute[] attributes) { for (int i = 0; i < attributes.length; i++) { if (attributes[i].tag == Constants.ATTR_SIGNATURE) { diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/ClassVisitor.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/ClassVisitor.java index 9513e02f9..0957bfba8 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/ClassVisitor.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/ClassVisitor.java @@ -1,9 +1,9 @@ package org.aspectj.apache.bcel.classfile; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisibleAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisibleParameterAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisParamAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisParamAnnos; /* ==================================================================== * The Apache Software License, Version 1.1 @@ -65,7 +65,7 @@ import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnot * * Implemented by wish of Boris Bokowski. * - * @version $Id: ClassVisitor.java,v 1.3 2009/09/10 03:59:33 aclement Exp $ + * @version $Id: ClassVisitor.java,v 1.4 2009/09/15 19:40:13 aclement Exp $ * @author M. Dahm */ public interface ClassVisitor { @@ -135,13 +135,13 @@ public interface ClassVisitor { public void visitEnclosingMethod(EnclosingMethod obj); - public void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations obj); + public void visitRuntimeVisibleAnnotations(RuntimeVisAnnos obj); - public void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations obj); + public void visitRuntimeInvisibleAnnotations(RuntimeInvisAnnos obj); - public void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations obj); + public void visitRuntimeVisibleParameterAnnotations(RuntimeVisParamAnnos obj); - public void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations obj); + public void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisParamAnnos obj); public void visitAnnotationDefault(AnnotationDefault obj); diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Code.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Code.java index 237b80029..fb96fbe5f 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Code.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Code.java @@ -54,325 +54,324 @@ package org.aspectj.apache.bcel.classfile; * . */ -import org.aspectj.apache.bcel.Constants; -import java.io.*; - -/** - * This class represents a chunk of Java byte code contained in a - * method. It is instantiated by the - * Attribute.readAttribute() method. A Code - * attribute contains informations about operand stack, local - * variables, byte code and the exceptions handled within this - * method. - * - * This attribute has attributes itself, namely LineNumberTable which - * is used for debugging purposes and LocalVariableTable which - * contains information about the local variables. - * - * @version $Id: Code.java,v 1.7 2009/09/10 15:35:05 aclement Exp $ - * @author M. Dahm - * @see Attribute - * @see CodeException - * @see LineNumberTable - * @see LocalVariableTable +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.Constants; + +/** + * This class represents a chunk of Java byte code contained in a method. It is instantiated by the + * Attribute.readAttribute() method. A Code attribute contains informations about operand stack, local variables, + * byte code and the exceptions handled within this method. + * + * This attribute has attributes itself, namely LineNumberTable which is used for debugging purposes and + * LocalVariableTable which contains information about the local variables. + * + * @version $Id: Code.java,v 1.8 2009/09/15 19:40:12 aclement Exp $ + * @author M. Dahm + * @see Attribute + * @see CodeException + * @see LineNumberTable + * @see LocalVariableTable */ public final class Code extends Attribute { - private int maxStack; // Maximum size of stack used by this method - private int maxLocals; // Number of local variables - private byte[] code; // Actual byte code - private CodeException[] exceptionTable; - private Attribute[] attributes; - private static final CodeException[] NO_EXCEPTIONS = new CodeException[]{}; - - /** - * 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()); - } - - - 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); - - int len = file.readInt(); - code = new byte[len]; // 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. - */ - len = file.readUnsignedShort(); - if (len==0) { - exceptionTable = NO_EXCEPTIONS; - } else { - exceptionTable = new CodeException[len]; - for(int i=0; i < len; i++) - exceptionTable[i] = new CodeException(file); - } - - // Read all attributes, eg: LineNumberTable, LocalVariableTable - attributes = AttributeUtils.readAttributes(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 Code - * @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.maxStack = max_stack; - this.maxLocals = 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(ClassVisitor 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(maxStack); - file.writeShort(maxLocals); - file.writeInt(code.length); - file.write(code, 0, code.length); - - file.writeShort(exceptionTable.length); - for(int i=0; i < exceptionTable.length; i++) - exceptionTable[i].dump(file); - - file.writeShort(attributes.length); - for(int i=0; i < attributes.length; 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.length; i++) - if (attributes[i].tag==Constants.ATTR_LINE_NUMBER_TABLE) return (LineNumberTable)attributes[i]; - return null; - } - - /** - * @return LocalVariableTable of Code, if it has one - */ - public LocalVariableTable getLocalVariableTable() { - for(int i=0; i < attributes.length; i++) - if (attributes[i].tag==Constants.ATTR_LOCAL_VARIABLE_TABLE) 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 exceptionTable; } - - /** - * @return Number of local variables. - */ - public final int getMaxLocals() { return maxLocals; } - - /** - * @return Maximum size of stack used by this method. - */ - - public final int getMaxStack() { return maxStack; } - - /** - * @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==null?0:code.length) /*byte-code*/ - + 2 /*exception-table length*/ - + 8 * (exceptionTable==null?0:exceptionTable.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; - if (attributes!=null) { - for(int i=0; i < attributes.length; i++) - len += attributes[i].length + 6 /*attribute header size*/; - } - return len + getInternalLength(); - } - - /** - * @param attributes. - */ - public final void setAttributes(Attribute[] attributes) { - this.attributes = attributes; - length = calculateLength(); // Adjust length - } - - /** - * @param code byte code - */ - public final void setCode(byte[] code) { - this.code = code; - } - - /** - * @param exception_table exception table - */ - public final void setExceptionTable(CodeException[] exception_table) { - this.exceptionTable = exception_table; - } - - /** - * @param max_locals maximum number of local variables - */ - public final void setMaxLocals(int max_locals) { - this.maxLocals = max_locals; - } - - /** - * @param max_stack maximum stack size - */ - public final void setMaxStack(int max_stack) { - this.maxStack = max_stack; - } - - /** - * @return String representation of code chunk. - */ - public final String toString(boolean verbose) { - StringBuffer buf; - - buf = new StringBuffer("Code(max_stack = " + maxStack + - ", max_locals = " + maxLocals + - ", code_length = " + code.length + ")\n" + - Utility.codeToString(code, cpool, 0, -1, verbose)); - - if(exceptionTable.length > 0) { - buf.append("\nException handler(s) = \n" + "From\tTo\tHandler\tType\n"); - - for(int i=0; i < exceptionTable.length; i++) - buf.append(exceptionTable[i].toString(cpool, verbose) + "\n"); - } - - if(attributes.length > 0) { - buf.append("\nAttribute(s) = \n"); - - for(int i=0; i < attributes.length; i++) - buf.append(attributes[i].toString() + "\n"); - } - - return buf.toString(); - } - - /** - * @return String representation of code chunk. - */ - public final String toString() { - return toString(true); - } - - /** - * @return deep copy of this attribute - */ - public Attribute copy(ConstantPool constant_pool) { - Code c = (Code)clone(); - c.code = (byte[])code.clone(); - c.cpool = constant_pool; - - c.exceptionTable = new CodeException[exceptionTable.length]; - for(int i=0; i < exceptionTable.length; i++) - c.exceptionTable[i] = exceptionTable[i].copy(); - - c.attributes = new Attribute[attributes.length]; - for(int i=0; i < attributes.length; i++) - c.attributes[i] = attributes[i].copy(constant_pool); - - return c; - } - - /** - * Returns the same as toString(true) except that the attribute information - * isn't included (line numbers). - * Can be used to check whether two pieces of code are equivalent. - */ - public String getCodeString() { - StringBuffer codeString = new StringBuffer(); - codeString.append("Code(max_stack = ").append(maxStack); - codeString.append(", max_locals = ").append(maxLocals); - codeString.append(", code_length = ").append(code.length).append(")\n"); - codeString.append(Utility.codeToString(code, cpool, 0, -1,true)); - if (exceptionTable.length>0) { - codeString.append("\n").append("Exception entries = ").append(exceptionTable.length).append("\n"); - for (int i = 0; i < exceptionTable.length; i++) { - CodeException exc = exceptionTable[i]; - int type = exc.getCatchType(); - String name = "finally"; - if (type!=0) name = this.cpool.getConstantString(type,Constants.CONSTANT_Class); - codeString.append(name).append("["); - codeString.append(exc.getStartPC()).append(">").append(exc.getEndPC()).append("]\n"); - } - } - return codeString.toString(); - } + private int maxStack; // Maximum size of stack used by this method + private int maxLocals; // Number of local variables + private byte[] code; // Actual byte code + private CodeException[] exceptionTable; + private Attribute[] attributes; + private static final CodeException[] NO_EXCEPTIONS = new CodeException[] {}; + + /** + * 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()); + } + + 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); + + int len = file.readInt(); + code = new byte[len]; // 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. + */ + len = file.readUnsignedShort(); + if (len == 0) { + exceptionTable = NO_EXCEPTIONS; + } else { + exceptionTable = new CodeException[len]; + for (int i = 0; i < len; i++) + exceptionTable[i] = new CodeException(file); + } + + // Read all attributes, eg: LineNumberTable, LocalVariableTable + attributes = AttributeUtils.readAttributes(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 Code + * @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.maxStack = max_stack; + this.maxLocals = 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 + */ + @Override + public void accept(ClassVisitor v) { + v.visitCode(this); + } + + /** + * Dump code attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + @Override + public final void dump(DataOutputStream file) throws IOException { + super.dump(file); + + file.writeShort(maxStack); + file.writeShort(maxLocals); + file.writeInt(code.length); + file.write(code, 0, code.length); + + file.writeShort(exceptionTable.length); + for (int i = 0; i < exceptionTable.length; i++) + exceptionTable[i].dump(file); + + file.writeShort(attributes.length); + for (int i = 0; i < attributes.length; 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.length; i++) + if (attributes[i].tag == Constants.ATTR_LINE_NUMBER_TABLE) + return (LineNumberTable) attributes[i]; + return null; + } + + /** + * @return LocalVariableTable of Code, if it has one + */ + public LocalVariableTable getLocalVariableTable() { + for (int i = 0; i < attributes.length; i++) + if (attributes[i].tag == Constants.ATTR_LOCAL_VARIABLE_TABLE) + 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 exceptionTable; + } + + /** + * @return Number of local variables. + */ + public final int getMaxLocals() { + return maxLocals; + } + + /** + * @return Maximum size of stack used by this method. + */ + + public final int getMaxStack() { + return maxStack; + } + + /** + * @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 == null ? 0 : code.length) /* byte-code */ + + 2 /* exception-table length */ + + 8 * (exceptionTable == null ? 0 : exceptionTable.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; + if (attributes != null) { + for (int i = 0; i < attributes.length; i++) + len += attributes[i].length + 6 /* attribute header size */; + } + return len + getInternalLength(); + } + + /** + * @param attributes. + */ + public final void setAttributes(Attribute[] attributes) { + this.attributes = attributes; + length = calculateLength(); // Adjust length + } + + /** + * @param code byte code + */ + public final void setCode(byte[] code) { + this.code = code; + } + + /** + * @param exception_table exception table + */ + public final void setExceptionTable(CodeException[] exception_table) { + this.exceptionTable = exception_table; + } + + /** + * @param max_locals maximum number of local variables + */ + public final void setMaxLocals(int max_locals) { + this.maxLocals = max_locals; + } + + /** + * @param max_stack maximum stack size + */ + public final void setMaxStack(int max_stack) { + this.maxStack = max_stack; + } + + /** + * @return String representation of code chunk. + */ + public final String toString(boolean verbose) { + StringBuffer buf; + + buf = new StringBuffer("Code(max_stack = " + maxStack + ", max_locals = " + maxLocals + ", code_length = " + code.length + + ")\n" + Utility.codeToString(code, cpool, 0, -1, verbose)); + + if (exceptionTable.length > 0) { + buf.append("\nException handler(s) = \n" + "From\tTo\tHandler\tType\n"); + + for (int i = 0; i < exceptionTable.length; i++) + buf.append(exceptionTable[i].toString(cpool, verbose) + "\n"); + } + + if (attributes.length > 0) { + buf.append("\nAttribute(s) = \n"); + + for (int i = 0; i < attributes.length; i++) + buf.append(attributes[i].toString() + "\n"); + } + + return buf.toString(); + } + + /** + * @return String representation of code chunk. + */ + @Override + public final String toString() { + return toString(true); + } + + // /** + // * @return deep copy of this attribute + // */ + // public Attribute copy(ConstantPool constant_pool) { + // Code c = (Code)clone(); + // c.code = (byte[])code.clone(); + // c.cpool = constant_pool; + // + // c.exceptionTable = new CodeException[exceptionTable.length]; + // for(int i=0; i < exceptionTable.length; i++) + // c.exceptionTable[i] = exceptionTable[i].copy(); + // + // c.attributes = new Attribute[attributes.length]; + // for(int i=0; i < attributes.length; i++) + // c.attributes[i] = attributes[i].copy(constant_pool); + // + // return c; + // } + + /** + * Returns the same as toString(true) except that the attribute information isn't included (line numbers). Can be used to check + * whether two pieces of code are equivalent. + */ + public String getCodeString() { + StringBuffer codeString = new StringBuffer(); + codeString.append("Code(max_stack = ").append(maxStack); + codeString.append(", max_locals = ").append(maxLocals); + codeString.append(", code_length = ").append(code.length).append(")\n"); + codeString.append(Utility.codeToString(code, cpool, 0, -1, true)); + if (exceptionTable.length > 0) { + codeString.append("\n").append("Exception entries = ").append(exceptionTable.length).append("\n"); + for (int i = 0; i < exceptionTable.length; i++) { + CodeException exc = exceptionTable[i]; + int type = exc.getCatchType(); + String name = "finally"; + if (type != 0) + name = this.cpool.getConstantString(type, Constants.CONSTANT_Class); + codeString.append(name).append("["); + codeString.append(exc.getStartPC()).append(">").append(exc.getEndPC()).append("]\n"); + } + } + return codeString.toString(); + } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/ConstantValue.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/ConstantValue.java index c07ddcc62..67cc0851e 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/ConstantValue.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/ConstantValue.java @@ -64,7 +64,7 @@ import org.aspectj.apache.bcel.Constants; * This class is derived from Attribute and represents a constant value, i.e., a default value for initializing a class * field. This class is instantiated by the Attribute.readAttribute() method. * - * @version $Id: ConstantValue.java,v 1.4 2009/09/10 15:35:04 aclement Exp $ + * @version $Id: ConstantValue.java,v 1.5 2009/09/15 19:40:12 aclement Exp $ * @author M. Dahm * @see Attribute */ @@ -176,14 +176,14 @@ public final class ConstantValue extends Attribute { return buf; } - - /** - * @return deep copy of this attribute - */ - @Override - public Attribute copy(ConstantPool constant_pool) { - ConstantValue c = (ConstantValue) clone(); - c.cpool = constant_pool; - return c; - } + // + // /** + // * @return deep copy of this attribute + // */ + // @Override + // public Attribute copy(ConstantPool constant_pool) { + // ConstantValue c = (ConstantValue) clone(); + // c.cpool = constant_pool; + // return c; + // } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Deprecated.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Deprecated.java index f3cc4e32c..21bbbcb46 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Deprecated.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Deprecated.java @@ -54,116 +54,118 @@ package org.aspectj.apache.bcel.classfile; * . */ -import org.aspectj.apache.bcel.Constants; -import java.io.*; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.Constants; /** - * This class is derived from Attribute and denotes that this is a - * deprecated method. - * It is instantiated from the Attribute.readAttribute() method. - * - * @version $Id: Deprecated.java,v 1.4 2009/09/10 15:35:04 aclement Exp $ - * @author M. Dahm - * @see Attribute + * This class is derived from Attribute and denotes that this is a deprecated method. It is instantiated from the + * Attribute.readAttribute() method. + * + * @version $Id: Deprecated.java,v 1.5 2009/09/15 19:40:12 aclement Exp $ + * @author M. Dahm + * @see Attribute */ public final class Deprecated extends Attribute { - private byte[] bytes; - - /** - * Initialize from another object. Note that both objects use the same - * references (shallow copy). Use clone() for a physical copy. - */ - public Deprecated(Deprecated c) { - this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); - } - - /** - * @param name_index Index in constant pool to CONSTANT_Utf8 - * @param length Content length in bytes - * @param bytes Attribute contents - * @param constant_pool Array of constants - */ - public Deprecated(int name_index, int length, byte[] bytes, - ConstantPool constant_pool) - { - super(Constants.ATTR_DEPRECATED, name_index, length, constant_pool); - this.bytes = bytes; - } - - /** - * Construct object from file stream. - * @param name_index Index in constant pool to CONSTANT_Utf8 - * @param length Content length in bytes - * @param file Input stream - * @param constant_pool Array of constants - * @throws IOException - */ - Deprecated(int name_index, int length, DataInputStream file, - ConstantPool constant_pool) throws IOException - { - this(name_index, length, (byte [])null, constant_pool); - - if(length > 0) { - bytes = new byte[length]; - file.readFully(bytes); - System.err.println("Deprecated attribute with length > 0"); - } - } - - /** - * 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(ClassVisitor v) { - v.visitDeprecated(this); - } - - /** - * Dump source file 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); - - if(length > 0) - file.write(bytes, 0, length); - } - - /** - * @return data bytes. - */ - public final byte[] getBytes() { return bytes; } - - /** - * @param bytes. - */ - public final void setBytes(byte[] bytes) { - this.bytes = bytes; - } - - /** - * @return attribute name - */ - public final String toString() { - return Constants.ATTRIBUTE_NAMES[Constants.ATTR_DEPRECATED]; - } - - /** - * @return deep copy of this attribute - */ - public Attribute copy(ConstantPool constant_pool) { - Deprecated c = (Deprecated)clone(); - - if(bytes != null) - c.bytes = (byte[])bytes.clone(); - - c.cpool = constant_pool; - return c; - } + private byte[] bytes; + + /** + * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a physical + * copy. + */ + public Deprecated(Deprecated c) { + this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); + } + + /** + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param bytes Attribute contents + * @param constant_pool Array of constants + */ + public Deprecated(int name_index, int length, byte[] bytes, ConstantPool constant_pool) { + super(Constants.ATTR_DEPRECATED, name_index, length, constant_pool); + this.bytes = bytes; + } + + /** + * Construct object from file stream. + * + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + Deprecated(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException { + this(name_index, length, (byte[]) null, constant_pool); + + if (length > 0) { + bytes = new byte[length]; + file.readFully(bytes); + System.err.println("Deprecated attribute with length > 0"); + } + } + + /** + * 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 + */ + @Override + public void accept(ClassVisitor v) { + v.visitDeprecated(this); + } + + /** + * Dump source file attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + @Override + public final void dump(DataOutputStream file) throws IOException { + super.dump(file); + + if (length > 0) + file.write(bytes, 0, length); + } + + /** + * @return data bytes. + */ + public final byte[] getBytes() { + return bytes; + } + + /** + * @param bytes. + */ + public final void setBytes(byte[] bytes) { + this.bytes = bytes; + } + + /** + * @return attribute name + */ + @Override + public final String toString() { + return Constants.ATTRIBUTE_NAMES[Constants.ATTR_DEPRECATED]; + } + + // /** + // * @return deep copy of this attribute + // */ + // public Attribute copy(ConstantPool constant_pool) { + // Deprecated c = (Deprecated)clone(); + // + // if(bytes != null) + // c.bytes = (byte[])bytes.clone(); + // + // c.cpool = constant_pool; + // return c; + // } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/ExceptionTable.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/ExceptionTable.java index a7a084cef..ad7d43f2e 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/ExceptionTable.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/ExceptionTable.java @@ -54,149 +54,146 @@ package org.aspectj.apache.bcel.classfile; * . */ -import org.aspectj.apache.bcel.Constants; -import java.io.*; - -/** - * This class represents the table of exceptions that are thrown by a - * method. This attribute may be used once per method. The name of - * this class is ExceptionTable for historical reasons; The - * Java Virtual Machine Specification, Second Edition defines this - * attribute using the name Exceptions (which is inconsistent - * with the other classes). - * - * @version $Id: ExceptionTable.java,v 1.4 2009/09/10 15:35:05 aclement Exp $ - * @author M. Dahm - * @see Code +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.Constants; + +/** + * This class represents the table of exceptions that are thrown by a method. This attribute may be used once per method. The name + * of this class is ExceptionTable for historical reasons; The Java Virtual Machine Specification, Second Edition defines + * this attribute using the name Exceptions (which is inconsistent with the other classes). + * + * @version $Id: ExceptionTable.java,v 1.5 2009/09/15 19:40:12 aclement Exp $ + * @author M. Dahm + * @see Code */ public final class ExceptionTable extends Attribute { - private int number_of_exceptions; // Table of indices into - private int[] exception_index_table; // constant pool - - /** - * Initialize from another object. Note that both objects use the same - * references (shallow copy). Use copy() for a physical copy. - */ - public ExceptionTable(ExceptionTable c) { - this(c.getNameIndex(), c.getLength(), c.getExceptionIndexTable(), - c.getConstantPool()); - } - - /** - * @param name_index Index in constant pool - * @param length Content length in bytes - * @param exception_index_table Table of indices in constant pool - * @param constant_pool Array of constants - */ - public ExceptionTable(int name_index, int length, - int[] exception_index_table, - ConstantPool constant_pool) - { - super(Constants.ATTR_EXCEPTIONS, name_index, length, constant_pool); - setExceptionIndexTable(exception_index_table); - } - - /** - * Construct object from file stream. - * @param name_index Index in constant pool - * @param length Content length in bytes - * @param file Input stream - * @param constant_pool Array of constants - * @throws IOException - */ - ExceptionTable(int name_index, int length, DataInputStream file, - ConstantPool constant_pool) throws IOException - { - this(name_index, length, (int[])null, constant_pool); - - number_of_exceptions = file.readUnsignedShort(); - exception_index_table = new int[number_of_exceptions]; - - for(int i=0; i < number_of_exceptions; i++) - exception_index_table[i] = file.readUnsignedShort(); - } - - /** - * 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(ClassVisitor v) { - v.visitExceptionTable(this); - } - - /** - * Dump exceptions 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(number_of_exceptions); - for(int i=0; i < number_of_exceptions; i++) - file.writeShort(exception_index_table[i]); - } - - /** - * @return Array of indices into constant pool of thrown exceptions. - */ - public final int[] getExceptionIndexTable() {return exception_index_table;} - /** - * @return Length of exception table. - */ - public final int getNumberOfExceptions() { return number_of_exceptions; } - - /** - * @return class names of thrown exceptions - */ - public final String[] getExceptionNames() { - String[] names = new String[number_of_exceptions]; - for(int i=0; i < number_of_exceptions; i++) - names[i] = cpool.getConstantString(exception_index_table[i], - Constants.CONSTANT_Class). - replace('/', '.'); - return names; - } - - /** - * @param exception_index_table. - * Also redefines number_of_exceptions according to table length. - */ - public final void setExceptionIndexTable(int[] exception_index_table) { - this.exception_index_table = exception_index_table; - number_of_exceptions = (exception_index_table == null)? 0 : - exception_index_table.length; - } - /** - * @return String representation, i.e., a list of thrown exceptions. - */ - public final String toString() { - StringBuffer buf = new StringBuffer(""); - String str; - - for(int i=0; i < number_of_exceptions; i++) { - str = cpool.getConstantString(exception_index_table[i], - Constants.CONSTANT_Class); - buf.append(Utility.compactClassName(str, false)); - - if(i < number_of_exceptions - 1) - buf.append(", "); - } - - return buf.toString(); - } - - /** - * @return deep copy of this attribute - */ - public Attribute copy(ConstantPool constant_pool) { - ExceptionTable c = (ExceptionTable)clone(); - c.exception_index_table = (int[])exception_index_table.clone(); - c.cpool = constant_pool; - return c; - } + private int number_of_exceptions; // Table of indices into + private int[] exception_index_table; // constant pool + + /** + * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical + * copy. + */ + public ExceptionTable(ExceptionTable c) { + this(c.getNameIndex(), c.getLength(), c.getExceptionIndexTable(), c.getConstantPool()); + } + + /** + * @param name_index Index in constant pool + * @param length Content length in bytes + * @param exception_index_table Table of indices in constant pool + * @param constant_pool Array of constants + */ + public ExceptionTable(int name_index, int length, int[] exception_index_table, ConstantPool constant_pool) { + super(Constants.ATTR_EXCEPTIONS, name_index, length, constant_pool); + setExceptionIndexTable(exception_index_table); + } + + /** + * Construct object from file stream. + * + * @param name_index Index in constant pool + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + ExceptionTable(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException { + this(name_index, length, (int[]) null, constant_pool); + + number_of_exceptions = file.readUnsignedShort(); + exception_index_table = new int[number_of_exceptions]; + + for (int i = 0; i < number_of_exceptions; i++) + exception_index_table[i] = file.readUnsignedShort(); + } + + /** + * 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 + */ + @Override + public void accept(ClassVisitor v) { + v.visitExceptionTable(this); + } + + /** + * Dump exceptions attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + @Override + public final void dump(DataOutputStream file) throws IOException { + super.dump(file); + file.writeShort(number_of_exceptions); + for (int i = 0; i < number_of_exceptions; i++) + file.writeShort(exception_index_table[i]); + } + + /** + * @return Array of indices into constant pool of thrown exceptions. + */ + public final int[] getExceptionIndexTable() { + return exception_index_table; + } + + /** + * @return Length of exception table. + */ + public final int getNumberOfExceptions() { + return number_of_exceptions; + } + + /** + * @return class names of thrown exceptions + */ + public final String[] getExceptionNames() { + String[] names = new String[number_of_exceptions]; + for (int i = 0; i < number_of_exceptions; i++) + names[i] = cpool.getConstantString(exception_index_table[i], Constants.CONSTANT_Class).replace('/', '.'); + return names; + } + + /** + * @param exception_index_table. Also redefines number_of_exceptions according to table length. + */ + public final void setExceptionIndexTable(int[] exception_index_table) { + this.exception_index_table = exception_index_table; + number_of_exceptions = (exception_index_table == null) ? 0 : exception_index_table.length; + } + + /** + * @return String representation, i.e., a list of thrown exceptions. + */ + @Override + public final String toString() { + StringBuffer buf = new StringBuffer(""); + String str; + + for (int i = 0; i < number_of_exceptions; i++) { + str = cpool.getConstantString(exception_index_table[i], Constants.CONSTANT_Class); + buf.append(Utility.compactClassName(str, false)); + + if (i < number_of_exceptions - 1) + buf.append(", "); + } + + return buf.toString(); + } + + // /** + // * @return deep copy of this attribute + // */ + // public Attribute copy(ConstantPool constant_pool) { + // ExceptionTable c = (ExceptionTable)clone(); + // c.exception_index_table = (int[])exception_index_table.clone(); + // c.cpool = constant_pool; + // return c; + // } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/FieldOrMethod.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/FieldOrMethod.java index 1babd200a..a152b616f 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/FieldOrMethod.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/FieldOrMethod.java @@ -61,12 +61,12 @@ import java.util.List; import org.aspectj.apache.bcel.Constants; import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnotations; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnos; /** * Abstract super class for fields and methods. * - * @version $Id: FieldOrMethod.java,v 1.11 2009/09/15 03:33:52 aclement Exp $ + * @version $Id: FieldOrMethod.java,v 1.12 2009/09/15 19:40:12 aclement Exp $ * @author M. Dahm */ public abstract class FieldOrMethod extends Modifiers implements Node { @@ -170,8 +170,8 @@ public abstract class FieldOrMethod extends Modifiers implements Node { List accumulatedAnnotations = new ArrayList(); for (int i = 0; i < attributes.length; i++) { Attribute attribute = attributes[i]; - if (attribute instanceof RuntimeAnnotations) { - RuntimeAnnotations runtimeAnnotations = (RuntimeAnnotations) attribute; + if (attribute instanceof RuntimeAnnos) { + RuntimeAnnos runtimeAnnotations = (RuntimeAnnos) attribute; accumulatedAnnotations.addAll(runtimeAnnotations.getAnnotations()); } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/InnerClasses.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/InnerClasses.java index 493012bf9..0078156ed 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/InnerClasses.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/InnerClasses.java @@ -54,128 +54,128 @@ package org.aspectj.apache.bcel.classfile; * . */ -import org.aspectj.apache.bcel.Constants; -import java.io.*; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.Constants; /** - * This class is derived from Attribute and denotes that this class - * is an Inner class of another. - * to the source file of this class. - * It is instantiated from the Attribute.readAttribute() method. - * - * @version $Id: InnerClasses.java,v 1.4 2009/09/10 15:35:05 aclement Exp $ - * @author M. Dahm - * @see Attribute + * This class is derived from Attribute and denotes that this class is an Inner class of another. to the source file of + * this class. It is instantiated from the Attribute.readAttribute() method. + * + * @version $Id: InnerClasses.java,v 1.5 2009/09/15 19:40:12 aclement Exp $ + * @author M. Dahm + * @see Attribute */ public final class InnerClasses extends Attribute { - private InnerClass[] inner_classes; - private int number_of_classes; - - /** - * Initialize from another object. Note that both objects use the same - * references (shallow copy). Use clone() for a physical copy. - */ - public InnerClasses(InnerClasses c) { - this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), - c.getConstantPool()); - } - - /** - * @param name_index Index in constant pool to CONSTANT_Utf8 - * @param length Content length in bytes - * @param inner_classes array of inner classes attributes - * @param constant_pool Array of constants - * @param sourcefile_index Index in constant pool to CONSTANT_Utf8 - */ - public InnerClasses(int name_index, int length, - InnerClass[] inner_classes, - ConstantPool constant_pool) - { - super(Constants.ATTR_INNER_CLASSES, name_index, length, constant_pool); - setInnerClasses(inner_classes); - } - - /** - * Construct object from file stream. - * - * @param name_index Index in constant pool to CONSTANT_Utf8 - * @param length Content length in bytes - * @param file Input stream - * @param constant_pool Array of constants - * @throws IOException - */ - InnerClasses(int name_index, int length, DataInputStream file, - ConstantPool constant_pool) throws IOException - { - this(name_index, length, (InnerClass[])null, constant_pool); - - number_of_classes = file.readUnsignedShort(); - inner_classes = new InnerClass[number_of_classes]; - - for(int i=0; i < number_of_classes; i++) - inner_classes[i] = new InnerClass(file); - } - /** - * 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(ClassVisitor v) { - v.visitInnerClasses(this); - } - /** - * Dump source file 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(number_of_classes); - - for(int i=0; i < number_of_classes; i++) - inner_classes[i].dump(file); - } - - /** - * @return array of inner class "records" - */ - public final InnerClass[] getInnerClasses() { return inner_classes; } - - /** - * @param inner_classes. - */ - public final void setInnerClasses(InnerClass[] inner_classes) { - this.inner_classes = inner_classes; - number_of_classes = (inner_classes == null)? 0 : inner_classes.length; - } - - /** - * @return String representation. - */ - public final String toString() { - StringBuffer buf = new StringBuffer(); - - for(int i=0; i < number_of_classes; i++) - buf.append(inner_classes[i].toString(cpool) + "\n"); - - return buf.toString(); - } - - /** - * @return deep copy of this attribute - */ - public Attribute copy(ConstantPool constant_pool) { - InnerClasses c = (InnerClasses)clone(); - - c.inner_classes = new InnerClass[number_of_classes]; - for(int i=0; i < number_of_classes; i++) - c.inner_classes[i] = inner_classes[i].copy(); - - c.cpool = constant_pool; - return c; - } + private InnerClass[] inner_classes; + private int number_of_classes; + + /** + * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a physical + * copy. + */ + public InnerClasses(InnerClasses c) { + this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), c.getConstantPool()); + } + + /** + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param inner_classes array of inner classes attributes + * @param constant_pool Array of constants + * @param sourcefile_index Index in constant pool to CONSTANT_Utf8 + */ + public InnerClasses(int name_index, int length, InnerClass[] inner_classes, ConstantPool constant_pool) { + super(Constants.ATTR_INNER_CLASSES, name_index, length, constant_pool); + setInnerClasses(inner_classes); + } + + /** + * Construct object from file stream. + * + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + InnerClasses(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException { + this(name_index, length, (InnerClass[]) null, constant_pool); + + number_of_classes = file.readUnsignedShort(); + inner_classes = new InnerClass[number_of_classes]; + + for (int i = 0; i < number_of_classes; i++) + inner_classes[i] = new InnerClass(file); + } + + /** + * 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 + */ + @Override + public void accept(ClassVisitor v) { + v.visitInnerClasses(this); + } + + /** + * Dump source file attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + @Override + public final void dump(DataOutputStream file) throws IOException { + super.dump(file); + file.writeShort(number_of_classes); + + for (int i = 0; i < number_of_classes; i++) + inner_classes[i].dump(file); + } + + /** + * @return array of inner class "records" + */ + public final InnerClass[] getInnerClasses() { + return inner_classes; + } + + /** + * @param inner_classes. + */ + public final void setInnerClasses(InnerClass[] inner_classes) { + this.inner_classes = inner_classes; + number_of_classes = (inner_classes == null) ? 0 : inner_classes.length; + } + + /** + * @return String representation. + */ + @Override + public final String toString() { + StringBuffer buf = new StringBuffer(); + + for (int i = 0; i < number_of_classes; i++) + buf.append(inner_classes[i].toString(cpool) + "\n"); + + return buf.toString(); + } + + // /** + // * @return deep copy of this attribute + // */ + // public Attribute copy(ConstantPool constant_pool) { + // InnerClasses c = (InnerClasses)clone(); + // + // c.inner_classes = new InnerClass[number_of_classes]; + // for(int i=0; i < number_of_classes; i++) + // c.inner_classes[i] = inner_classes[i].copy(); + // + // c.cpool = constant_pool; + // return c; + // } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java index 27d2c241a..25c415295 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/JavaClass.java @@ -69,7 +69,7 @@ import java.util.StringTokenizer; import org.aspectj.apache.bcel.Constants; import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnotations; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnos; import org.aspectj.apache.bcel.generic.Type; import org.aspectj.apache.bcel.util.SyntheticRepository; @@ -80,7 +80,7 @@ import org.aspectj.apache.bcel.util.SyntheticRepository; * The intent of this class is to represent a parsed or otherwise existing class file. Those interested in programatically * generating classes should see the ClassGen class. * - * @version $Id: JavaClass.java,v 1.21 2009/09/15 16:39:56 aclement Exp $ + * @version $Id: JavaClass.java,v 1.22 2009/09/15 19:40:14 aclement Exp $ * @see org.aspectj.apache.bcel.generic.ClassGen * @author M. Dahm */ @@ -289,8 +289,8 @@ public class JavaClass extends Modifiers implements Cloneable, Node { List accumulatedAnnotations = new ArrayList(); for (int i = 0; i < attributes.length; i++) { Attribute attribute = attributes[i]; - if (attribute instanceof RuntimeAnnotations) { - RuntimeAnnotations runtimeAnnotations = (RuntimeAnnotations) attribute; + if (attribute instanceof RuntimeAnnos) { + RuntimeAnnos runtimeAnnotations = (RuntimeAnnos) attribute; accumulatedAnnotations.addAll(runtimeAnnotations.getAnnotations()); } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/LineNumberTable.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/LineNumberTable.java index e0ba7ebae..10d72fd3e 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/LineNumberTable.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/LineNumberTable.java @@ -65,7 +65,7 @@ import org.aspectj.apache.bcel.Constants; * This class represents a table of line numbers for debugging purposes. This attribute is used by the Code attribute. It * contains pairs of PCs and line numbers. * - * @version $Id: LineNumberTable.java,v 1.7 2009/09/10 15:35:05 aclement Exp $ + * @version $Id: LineNumberTable.java,v 1.8 2009/09/15 19:40:12 aclement Exp $ * @author M. Dahm * @see Code changes: asc Feb06 Made unpacking lazy */ @@ -258,18 +258,17 @@ public final class LineNumberTable extends Attribute { /** * @return deep copy of this attribute */ - @Override - public Attribute copy(ConstantPool constant_pool) { - unpack(); - LineNumberTable newTable = (LineNumberTable) clone(); - newTable.table = new LineNumber[tableLength]; - for (int i = 0; i < tableLength; i++) { - newTable.table[i] = table[i].copy(); - } - newTable.cpool = constant_pool; - return newTable; - } - + // @Override + // public Attribute copy(ConstantPool constant_pool) { + // unpack(); + // LineNumberTable newTable = (LineNumberTable) clone(); + // newTable.table = new LineNumber[tableLength]; + // for (int i = 0; i < tableLength; i++) { + // newTable.table[i] = table[i].copy(); + // } + // newTable.cpool = constant_pool; + // return newTable; + // } public final int getTableLength() { unpack(); return tableLength; diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/LocalVariableTable.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/LocalVariableTable.java index 138269700..717bb5965 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/LocalVariableTable.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/LocalVariableTable.java @@ -64,7 +64,7 @@ import org.aspectj.apache.bcel.Constants; /** * This class represents collection of local variables in a method. This attribute is contained in the Code attribute. * - * @version $Id: LocalVariableTable.java,v 1.7 2009/09/10 15:35:04 aclement Exp $ + * @version $Id: LocalVariableTable.java,v 1.8 2009/09/15 19:40:12 aclement Exp $ * @author M. Dahm * @see Code * @see LocalVariable Updates: Andy 14Feb06 - Made unpacking of the data lazy, depending on someone actually asking for it. @@ -120,6 +120,7 @@ public class LocalVariableTable extends Attribute { * * @param v Visitor object */ + @Override public void accept(ClassVisitor v) { unpack(); v.visitLocalVariableTable(this); @@ -131,6 +132,7 @@ public class LocalVariableTable extends Attribute { * @param file Output file stream * @throws IOException */ + @Override public final void dump(DataOutputStream file) throws IOException { super.dump(file); if (isInPackedState) { @@ -173,6 +175,7 @@ public class LocalVariableTable extends Attribute { /** * @return String representation. */ + @Override public final String toString() { StringBuffer buf = new StringBuffer(""); unpack(); @@ -189,18 +192,17 @@ public class LocalVariableTable extends Attribute { /** * @return deep copy of this attribute */ - public Attribute copy(ConstantPool constant_pool) { - unpack(); - LocalVariableTable c = (LocalVariableTable) clone(); - - c.localVariableTable = new LocalVariable[localVariableTableLength]; - for (int i = 0; i < localVariableTableLength; i++) - c.localVariableTable[i] = localVariableTable[i].copy(); - - c.cpool = constant_pool; - return c; - } - + // public Attribute copy(ConstantPool constant_pool) { + // unpack(); + // LocalVariableTable c = (LocalVariableTable) clone(); + // + // c.localVariableTable = new LocalVariable[localVariableTableLength]; + // for (int i = 0; i < localVariableTableLength; i++) + // c.localVariableTable[i] = localVariableTable[i].copy(); + // + // c.cpool = constant_pool; + // return c; + // } public final int getTableLength() { unpack(); return localVariableTableLength; diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/LocalVariableTypeTable.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/LocalVariableTypeTable.java index ea1f3badf..ecd7e4383 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/LocalVariableTypeTable.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/LocalVariableTypeTable.java @@ -13,9 +13,11 @@ * ******************************************************************/ package org.aspectj.apache.bcel.classfile; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; -import org.aspectj.apache.bcel.Constants; -import java.io.*; +import org.aspectj.apache.bcel.Constants; // The new table is used when generic types are about... @@ -45,91 +47,90 @@ import java.io.*; // } // J5TODO: Needs some testing ! public class LocalVariableTypeTable extends Attribute { - private int local_variable_type_table_length; // Table of local - private LocalVariable[] local_variable_type_table; // variables - - public LocalVariableTypeTable(LocalVariableTypeTable c) { - this(c.getNameIndex(), c.getLength(), c.getLocalVariableTypeTable(), - c.getConstantPool()); - } - - public LocalVariableTypeTable(int name_index, int length, - LocalVariable[] local_variable_table, - ConstantPool constant_pool) - { - super(Constants.ATTR_LOCAL_VARIABLE_TYPE_TABLE, name_index, length, constant_pool); - setLocalVariableTable(local_variable_table); - } - - LocalVariableTypeTable(int nameIdx, int len, DataInputStream dis,ConstantPool cpool) throws IOException { - this(nameIdx, len, (LocalVariable[])null, cpool); - - local_variable_type_table_length = (dis.readUnsignedShort()); - local_variable_type_table = new LocalVariable[local_variable_type_table_length]; - - for(int i=0; i < local_variable_type_table_length; i++) - local_variable_type_table[i] = new LocalVariable(dis, cpool); - } - - public void accept(ClassVisitor v) { - v.visitLocalVariableTypeTable(this); - } - - public final void dump(DataOutputStream file) throws IOException - { - super.dump(file); - file.writeShort(local_variable_type_table_length); - for(int i=0; i < local_variable_type_table_length; i++) - local_variable_type_table[i].dump(file); - } - - public final LocalVariable[] getLocalVariableTypeTable() { - return local_variable_type_table; - } - - public final LocalVariable getLocalVariable(int index) { - for(int i=0; i < local_variable_type_table_length; i++) - if(local_variable_type_table[i].getIndex() == index) - return local_variable_type_table[i]; - - return null; - } - - public final void setLocalVariableTable(LocalVariable[] local_variable_table) - { - this.local_variable_type_table = local_variable_table; - local_variable_type_table_length = (local_variable_table == null)? 0 : - local_variable_table.length; - } - - /** - * @return String representation. - */ - public final String toString() { - StringBuffer buf = new StringBuffer(""); - - for(int i=0; i < local_variable_type_table_length; i++) { - buf.append(local_variable_type_table[i].toString()); - - if(i < local_variable_type_table_length - 1) buf.append('\n'); - } - - return buf.toString(); - } - - /** - * @return deep copy of this attribute - */ - public Attribute copy(ConstantPool constant_pool) { - LocalVariableTypeTable c = (LocalVariableTypeTable)clone(); - - c.local_variable_type_table = new LocalVariable[local_variable_type_table_length]; - for(int i=0; i < local_variable_type_table_length; i++) - c.local_variable_type_table[i] = local_variable_type_table[i].copy(); - - c.cpool = constant_pool; - return c; - } - - public final int getTableLength() { return local_variable_type_table_length; } + private int local_variable_type_table_length; // Table of local + private LocalVariable[] local_variable_type_table; // variables + + public LocalVariableTypeTable(LocalVariableTypeTable c) { + this(c.getNameIndex(), c.getLength(), c.getLocalVariableTypeTable(), c.getConstantPool()); + } + + public LocalVariableTypeTable(int name_index, int length, LocalVariable[] local_variable_table, ConstantPool constant_pool) { + super(Constants.ATTR_LOCAL_VARIABLE_TYPE_TABLE, name_index, length, constant_pool); + setLocalVariableTable(local_variable_table); + } + + LocalVariableTypeTable(int nameIdx, int len, DataInputStream dis, ConstantPool cpool) throws IOException { + this(nameIdx, len, (LocalVariable[]) null, cpool); + + local_variable_type_table_length = (dis.readUnsignedShort()); + local_variable_type_table = new LocalVariable[local_variable_type_table_length]; + + for (int i = 0; i < local_variable_type_table_length; i++) + local_variable_type_table[i] = new LocalVariable(dis, cpool); + } + + @Override + public void accept(ClassVisitor v) { + v.visitLocalVariableTypeTable(this); + } + + @Override + public final void dump(DataOutputStream file) throws IOException { + super.dump(file); + file.writeShort(local_variable_type_table_length); + for (int i = 0; i < local_variable_type_table_length; i++) + local_variable_type_table[i].dump(file); + } + + public final LocalVariable[] getLocalVariableTypeTable() { + return local_variable_type_table; + } + + public final LocalVariable getLocalVariable(int index) { + for (int i = 0; i < local_variable_type_table_length; i++) + if (local_variable_type_table[i].getIndex() == index) + return local_variable_type_table[i]; + + return null; + } + + public final void setLocalVariableTable(LocalVariable[] local_variable_table) { + this.local_variable_type_table = local_variable_table; + local_variable_type_table_length = (local_variable_table == null) ? 0 : local_variable_table.length; + } + + /** + * @return String representation. + */ + @Override + public final String toString() { + StringBuffer buf = new StringBuffer(""); + + for (int i = 0; i < local_variable_type_table_length; i++) { + buf.append(local_variable_type_table[i].toString()); + + if (i < local_variable_type_table_length - 1) + buf.append('\n'); + } + + return buf.toString(); + } + + // /** + // * @return deep copy of this attribute + // */ + // public Attribute copy(ConstantPool constant_pool) { + // LocalVariableTypeTable c = (LocalVariableTypeTable)clone(); + // + // c.local_variable_type_table = new LocalVariable[local_variable_type_table_length]; + // for(int i=0; i < local_variable_type_table_length; i++) + // c.local_variable_type_table[i] = local_variable_type_table[i].copy(); + // + // c.cpool = constant_pool; + // return c; + // } + + public final int getTableLength() { + return local_variable_type_table_length; + } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Method.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Method.java index cec26bd34..d354b7fe1 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Method.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Method.java @@ -60,15 +60,15 @@ import java.util.List; import org.aspectj.apache.bcel.Constants; import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisibleParameterAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisParamAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisParamAnnos; import org.aspectj.apache.bcel.generic.Type; /** * This class represents the method info structure, i.e., the representation for a method in the class. See JVM specification for * details. A method has access flags, a name, a signature and a number of attributes. * - * @version $Id: Method.java,v 1.10 2009/09/15 03:33:52 aclement Exp $ + * @version $Id: Method.java,v 1.11 2009/09/15 19:40:12 aclement Exp $ * @author M. Dahm */ public final class Method extends FieldOrMethod { @@ -206,18 +206,18 @@ public final class Method extends FieldOrMethod { return; } - RuntimeVisibleParameterAnnotations parameterAnnotationsVis = null; - RuntimeInvisibleParameterAnnotations parameterAnnotationsInvis = null; + RuntimeVisParamAnnos parameterAnnotationsVis = null; + RuntimeInvisParamAnnos parameterAnnotationsInvis = null; // Find attributes that contain annotation data Attribute[] attrs = getAttributes(); for (int i = 0; i < attrs.length; i++) { Attribute attribute = attrs[i]; - if (attribute instanceof RuntimeVisibleParameterAnnotations) { - parameterAnnotationsVis = (RuntimeVisibleParameterAnnotations) attribute; - } else if (attribute instanceof RuntimeInvisibleParameterAnnotations) { - parameterAnnotationsInvis = (RuntimeInvisibleParameterAnnotations) attribute; + if (attribute instanceof RuntimeVisParamAnnos) { + parameterAnnotationsVis = (RuntimeVisParamAnnos) attribute; + } else if (attribute instanceof RuntimeInvisParamAnnos) { + parameterAnnotationsInvis = (RuntimeInvisParamAnnos) attribute; } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Signature.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Signature.java index b1991afef..b7488e9a4 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Signature.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Signature.java @@ -68,7 +68,7 @@ import org.aspectj.apache.bcel.Constants; * This class is derived from Attribute and represents a reference to a GJ * attribute. * - * @version $Id: Signature.java,v 1.10 2009/09/10 15:35:04 aclement Exp $ + * @version $Id: Signature.java,v 1.11 2009/09/15 19:40:12 aclement Exp $ * @author M. Dahm * @see Attribute */ @@ -299,12 +299,12 @@ public final class Signature extends Attribute { return "Signature(" + s + ")"; } - /** - * @return deep copy of this attribute - */ - @Override - public Attribute copy(ConstantPool constant_pool) { - return (Signature) clone(); - } + // /** + // * @return deep copy of this attribute + // */ + // @Override + // public Attribute copy(ConstantPool constant_pool) { + // return (Signature) clone(); + // } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/SourceFile.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/SourceFile.java index 6832f86cd..9fd840fc0 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/SourceFile.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/SourceFile.java @@ -65,7 +65,7 @@ import org.aspectj.apache.bcel.Constants; * attribute should appear per classfile. The intention of this class is that it is instantiated from the * Attribute.readAttribute() method. * - * @version $Id: SourceFile.java,v 1.4 2009/09/10 15:35:04 aclement Exp $ + * @version $Id: SourceFile.java,v 1.5 2009/09/15 19:40:12 aclement Exp $ * @author M. Dahm * @see Attribute */ @@ -160,10 +160,10 @@ public final class SourceFile extends Attribute { } /** - * @return deep copy of this attribute + * @return deep copy of this attribute // */ - @Override - public Attribute copy(ConstantPool constant_pool) { - return (SourceFile) clone(); - } + // @Override + // public Attribute copy(ConstantPool constant_pool) { + // return (SourceFile) clone(); + // } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/StackMap.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/StackMap.java index 0ddf0e88f..80f943bbf 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/StackMap.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/StackMap.java @@ -54,132 +54,137 @@ package org.aspectj.apache.bcel.classfile; * . */ -import org.aspectj.apache.bcel.Constants; -import java.io.*; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.Constants; /** - * This class represents a stack map attribute used for - * preverification of Java classes for the Java 2 Micro Edition - * (J2ME). This attribute is used by the KVM and contained - * within the Code attribute of a method. See CLDC specification - * §5.3.1.2 - * - * @version $Id: StackMap.java,v 1.5 2009/09/10 15:35:04 aclement Exp $ - * @author M. Dahm - * @see Code - * @see StackMapEntry - * @see StackMapType + * This class represents a stack map attribute used for preverification of Java classes for the + * Java 2 Micro Edition (J2ME). This attribute is used by the KVM and contained + * within the Code attribute of a method. See CLDC specification §5.3.1.2 + * + * @version $Id: StackMap.java,v 1.6 2009/09/15 19:40:12 aclement Exp $ + * @author M. Dahm + * @see Code + * @see StackMapEntry + * @see StackMapType */ public final class StackMap extends Attribute { - private int map_length; - private StackMapEntry[] map; // Table of stack map entries - - /* - * @param name_index Index of name - * @param length Content length in bytes - * @param map Table of stack map entries - * @param constant_pool Array of constants - */ - public StackMap(int name_index, int length, StackMapEntry[] map, - ConstantPool constant_pool) - { - super(Constants.ATTR_STACK_MAP, name_index, length, constant_pool); - - setStackMap(map); - } - - /** - * Construct object from file stream. - * @param name_index Index of name - * @param length Content length in bytes - * @param file Input stream - * @throws IOException - * @param constant_pool Array of constants - */ - StackMap(int name_index, int length, DataInputStream file, - ConstantPool constant_pool) throws IOException - { - this(name_index, length, (StackMapEntry[])null, constant_pool); - - map_length = file.readUnsignedShort(); - map = new StackMapEntry[map_length]; - - for(int i=0; i < map_length; i++) - map[i] = new StackMapEntry(file, constant_pool); - } - - /** - * Dump line number table 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(map_length); - for(int i=0; i < map_length; i++) - map[i].dump(file); - } - - /** - * @return Array of stack map entries - */ - public final StackMapEntry[] getStackMap() { return map; } - - /** - * @param map Array of stack map entries - */ - public final void setStackMap(StackMapEntry[] map) { - this.map = map; - - map_length = (map == null)? 0 : map.length; - } - - /** - * @return String representation. - */ - public final String toString() { - StringBuffer buf = new StringBuffer("StackMap("); - - for(int i=0; i < map_length; i++) { - buf.append(map[i].toString()); - - if(i < map_length - 1) - buf.append(", "); - } - - buf.append(')'); - - return buf.toString(); - } - - /** - * @return deep copy of this attribute - */ - public Attribute copy(ConstantPool constant_pool) { - StackMap c = (StackMap)clone(); - - c.map = new StackMapEntry[map_length]; - for(int i=0; i < map_length; i++) - c.map[i] = map[i].copy(); - - c.cpool = constant_pool; - return c; - } - - /** - * 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(ClassVisitor v) { - v.visitStackMap(this); - } - - public final int getMapLength() { return map_length; } + private int map_length; + private StackMapEntry[] map; // Table of stack map entries + + /* + * @param name_index Index of name + * + * @param length Content length in bytes + * + * @param map Table of stack map entries + * + * @param constant_pool Array of constants + */ + public StackMap(int name_index, int length, StackMapEntry[] map, ConstantPool constant_pool) { + super(Constants.ATTR_STACK_MAP, name_index, length, constant_pool); + + setStackMap(map); + } + + /** + * Construct object from file stream. + * + * @param name_index Index of name + * @param length Content length in bytes + * @param file Input stream + * @throws IOException + * @param constant_pool Array of constants + */ + StackMap(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException { + this(name_index, length, (StackMapEntry[]) null, constant_pool); + + map_length = file.readUnsignedShort(); + map = new StackMapEntry[map_length]; + + for (int i = 0; i < map_length; i++) + map[i] = new StackMapEntry(file, constant_pool); + } + + /** + * Dump line number table attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + @Override + public final void dump(DataOutputStream file) throws IOException { + super.dump(file); + file.writeShort(map_length); + for (int i = 0; i < map_length; i++) + map[i].dump(file); + } + + /** + * @return Array of stack map entries + */ + public final StackMapEntry[] getStackMap() { + return map; + } + + /** + * @param map Array of stack map entries + */ + public final void setStackMap(StackMapEntry[] map) { + this.map = map; + + map_length = (map == null) ? 0 : map.length; + } + + /** + * @return String representation. + */ + @Override + public final String toString() { + StringBuffer buf = new StringBuffer("StackMap("); + + for (int i = 0; i < map_length; i++) { + buf.append(map[i].toString()); + + if (i < map_length - 1) + buf.append(", "); + } + + buf.append(')'); + + return buf.toString(); + } + + // + // /** + // * @return deep copy of this attribute + // */ + // public Attribute copy(ConstantPool constant_pool) { + // StackMap c = (StackMap)clone(); + // + // c.map = new StackMapEntry[map_length]; + // for(int i=0; i < map_length; i++) + // c.map[i] = map[i].copy(); + // + // c.cpool = constant_pool; + // return c; + // } + + /** + * 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 + */ + @Override + public void accept(ClassVisitor v) { + v.visitStackMap(this); + } + + public final int getMapLength() { + return map_length; + } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Synthetic.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Synthetic.java index e3eebe36b..57fe97468 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Synthetic.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Synthetic.java @@ -54,131 +54,132 @@ package org.aspectj.apache.bcel.classfile; * . */ -import org.aspectj.apache.bcel.Constants; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; -import java.io.*; +import org.aspectj.apache.bcel.Constants; /** - * This class is derived from Attribute and declares this class as - * `synthetic', i.e., it needs special handling. The JVM specification - * states "A class member that does not appear in the source code must be - * marked using a Synthetic attribute." It may appear in the ClassFile - * attribute table, a field_info table or a method_info table. This class - * is intended to be instantiated from the - * Attribute.readAttribute() method. - * - * @version $Id: Synthetic.java,v 1.4 2009/09/10 15:35:04 aclement Exp $ - * @author M. Dahm - * @see Attribute + * This class is derived from Attribute and declares this class as `synthetic', i.e., it needs special handling. The JVM + * specification states "A class member that does not appear in the source code must be marked using a Synthetic attribute." It may + * appear in the ClassFile attribute table, a field_info table or a method_info table. This class is intended to be instantiated + * from the Attribute.readAttribute() method. + * + * @version $Id: Synthetic.java,v 1.5 2009/09/15 19:40:12 aclement Exp $ + * @author M. Dahm + * @see Attribute */ public final class Synthetic extends Attribute { - private byte[] bytes; - - /** - * Initialize from another object. Note that both objects use the same - * references (shallow copy). Use copy() for a physical copy. - */ - public Synthetic(Synthetic c) { - this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); - } -// -// public Synthetic(ConstantPoolGen cpool) { -// super(Constants.ATTR_SYNTHETIC, cpool.addUtf8("Synthetic"), 0, cpool); -// ConstantPoolGen cpg = myGen.getConstantPool(); -// int index = ; -// Attribute synthetic = new Synthetic(index, 0, new byte[0], cpg.getConstantPool()); -// } - - /** - * @param name_index Index in constant pool to CONSTANT_Utf8, which - * should represent the string "Synthetic". - * @param length Content length in bytes - should be zero. - * @param bytes Attribute contents - * @param constant_pool The constant pool this attribute is associated - * with. - */ - public Synthetic(int name_index, int length, byte[] bytes, - ConstantPool constant_pool) - { - super(Constants.ATTR_SYNTHETIC, name_index, length, constant_pool); - this.bytes = bytes; - } - - /** - * Construct object from file stream. - * @param name_index Index in constant pool to CONSTANT_Utf8 - * @param length Content length in bytes - * @param file Input stream - * @param constant_pool Array of constants - * @throws IOException - */ - Synthetic(int name_index, int length, DataInputStream file, - ConstantPool constant_pool) throws IOException - { - this(name_index, length, (byte [])null, constant_pool); - - if(length > 0) { - bytes = new byte[length]; - file.readFully(bytes); - System.err.println("Synthetic attribute with length > 0"); - } - } - /** - * 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(ClassVisitor v) { - v.visitSynthetic(this); - } - /** - * Dump source file 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); - if(length > 0) - file.write(bytes, 0, length); - } - /** - * @return data bytes. - */ - public final byte[] getBytes() { return bytes; } - - /** - * @param bytes. - */ - public final void setBytes(byte[] bytes) { - this.bytes = bytes; - } - - /** - * @return String representation. - */ - public final String toString() { - StringBuffer buf = new StringBuffer("Synthetic"); - - if(length > 0) - buf.append(" " + Utility.toHexString(bytes)); - - return buf.toString(); - } - - /** - * @return deep copy of this attribute - */ - public Attribute copy(ConstantPool constant_pool) { - Synthetic c = (Synthetic)clone(); - - if(bytes != null) - c.bytes = (byte[])bytes.clone(); - - c.cpool = constant_pool; - return c; - } + private byte[] bytes; + + /** + * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical + * copy. + */ + public Synthetic(Synthetic c) { + this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); + } + + // + // public Synthetic(ConstantPoolGen cpool) { + // super(Constants.ATTR_SYNTHETIC, cpool.addUtf8("Synthetic"), 0, cpool); + // ConstantPoolGen cpg = myGen.getConstantPool(); + // int index = ; + // Attribute synthetic = new Synthetic(index, 0, new byte[0], cpg.getConstantPool()); + // } + + /** + * @param name_index Index in constant pool to CONSTANT_Utf8, which should represent the string "Synthetic". + * @param length Content length in bytes - should be zero. + * @param bytes Attribute contents + * @param constant_pool The constant pool this attribute is associated with. + */ + public Synthetic(int name_index, int length, byte[] bytes, ConstantPool constant_pool) { + super(Constants.ATTR_SYNTHETIC, name_index, length, constant_pool); + this.bytes = bytes; + } + + /** + * Construct object from file stream. + * + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + Synthetic(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException { + this(name_index, length, (byte[]) null, constant_pool); + + if (length > 0) { + bytes = new byte[length]; + file.readFully(bytes); + System.err.println("Synthetic attribute with length > 0"); + } + } + + /** + * 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 + */ + @Override + public void accept(ClassVisitor v) { + v.visitSynthetic(this); + } + + /** + * Dump source file attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + @Override + public final void dump(DataOutputStream file) throws IOException { + super.dump(file); + if (length > 0) + file.write(bytes, 0, length); + } + + /** + * @return data bytes. + */ + public final byte[] getBytes() { + return bytes; + } + + /** + * @param bytes. + */ + public final void setBytes(byte[] bytes) { + this.bytes = bytes; + } + + /** + * @return String representation. + */ + @Override + public final String toString() { + StringBuffer buf = new StringBuffer("Synthetic"); + + if (length > 0) + buf.append(" " + Utility.toHexString(bytes)); + + return buf.toString(); + } + + // /** + // * @return deep copy of this attribute + // */ + // public Attribute copy(ConstantPool constant_pool) { + // Synthetic c = (Synthetic)clone(); + // + // if(bytes != null) + // c.bytes = (byte[])bytes.clone(); + // + // c.cpool = constant_pool; + // return c; + // } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Unknown.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Unknown.java index c4f86441b..416d105b1 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Unknown.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Unknown.java @@ -68,7 +68,7 @@ import org.aspectj.apache.bcel.Constants; * org.aspectj.apache.bcel.classfile.AttributeReader)">Attribute.addAttributeReader. * * - * @version $Id: Unknown.java,v 1.5 2009/09/10 15:35:05 aclement Exp $ + * @version $Id: Unknown.java,v 1.6 2009/09/15 19:40:12 aclement Exp $ * @see org.aspectj.apache.bcel.classfile.Attribute * @author M. Dahm */ @@ -202,14 +202,14 @@ public final class Unknown extends Attribute { /** * @return deep copy of this attribute */ - @Override - public Attribute copy(ConstantPool constant_pool) { - Unknown c = (Unknown) clone(); - - if (bytes != null) - c.bytes = bytes.clone(); - - c.cpool = constant_pool; - return c; - } + // @Override + // public Attribute copy(ConstantPool constant_pool) { + // Unknown c = (Unknown) clone(); + // + // if (bytes != null) + // c.bytes = bytes.clone(); + // + // c.cpool = constant_pool; + // return c; + // } } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Utility.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Utility.java index df6b7f376..efcbf9fb5 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/Utility.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/Utility.java @@ -63,19 +63,19 @@ import java.util.List; import org.aspectj.apache.bcel.Constants; import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisibleAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisibleParameterAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeParameterAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisParamAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeParamAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisParamAnnos; import org.aspectj.apache.bcel.generic.Type; import org.aspectj.apache.bcel.util.ByteSequence; /** * Utility functions that do not really belong to any class in particular. * - * @version $Id: Utility.java,v 1.11 2009/09/14 20:29:10 aclement Exp $ + * @version $Id: Utility.java,v 1.12 2009/09/15 19:40:13 aclement Exp $ * @author M. Dahm * * modified: Andy Clement 2-mar-05 Removed unnecessary static and optimized @@ -396,18 +396,16 @@ public abstract class Utility { brackets = new StringBuffer(); // Accumulate []'s // Count opening brackets and look for optional size argument - for (n = 0; signature.charAt(n) == '['; n++) + for (n = 0; signature.charAt(n) == '['; n++) { brackets.append("[]"); + } consumedChars = n; - // The rest of the string denotes a `' + // The rest of the string denotes a '' ResultHolder restOfIt = signatureToStringInternal(signature.substring(n), chopit); - // type = signatureToString(signature.substring(n), chopit); - consumedChars += restOfIt.getConsumedChars(); return new ResultHolder(restOfIt.getResult() + brackets.toString(), consumedChars); } - case 'V': return ResultHolder.VOID; @@ -545,7 +543,7 @@ public abstract class Utility { * @param cp The constant pool gen where we can create the necessary name refs * @param annotations A list of AnnotationGen objects */ - public static Collection getAnnotationAttributes(ConstantPool cp, List annotations) { + public static Collection getAnnotationAttributes(ConstantPool cp, List annotations) { if (annotations.size() == 0) return null; @@ -593,12 +591,12 @@ public abstract class Utility { if (riaData.length > 2) riaIndex = cp.addUtf8("RuntimeInvisibleAnnotations"); - List newAttributes = new ArrayList(); + List newAttributes = new ArrayList(); if (rvaData.length > 2) { - newAttributes.add(new RuntimeVisibleAnnotations(rvaIndex, rvaData.length, rvaData, cp)); + newAttributes.add(new RuntimeVisAnnos(rvaIndex, rvaData.length, rvaData, cp)); } if (riaData.length > 2) { - newAttributes.add(new RuntimeInvisibleAnnotations(riaIndex, riaData.length, riaData, cp)); + newAttributes.add(new RuntimeInvisAnnos(riaIndex, riaData.length, riaData, cp)); } return newAttributes; @@ -682,14 +680,14 @@ public abstract class Utility { if (totalInvisCount > 0) riaIndex = cp.addUtf8("RuntimeInvisibleParameterAnnotations"); - List newAttributes = new ArrayList(); + List newAttributes = new ArrayList(); if (totalVisCount > 0) { - newAttributes.add(new RuntimeVisibleParameterAnnotations(rvaIndex, rvaData.length, rvaData, cp)); + newAttributes.add(new RuntimeVisParamAnnos(rvaIndex, rvaData.length, rvaData, cp)); } if (totalInvisCount > 0) { - newAttributes.add(new RuntimeInvisibleParameterAnnotations(riaIndex, riaData.length, riaData, cp)); + newAttributes.add(new RuntimeInvisParamAnnos(riaIndex, riaData.length, riaData, cp)); } return newAttributes.toArray(new Attribute[] {}); diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValue.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValue.java new file mode 100644 index 000000000..1b2d4023d --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValue.java @@ -0,0 +1,55 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andy Clement - initial implementation {date} + * ******************************************************************/ + +package org.aspectj.apache.bcel.classfile.annotation; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.classfile.ConstantPool; + +public class AnnotationElementValue extends ElementValue { + + private AnnotationGen a; + + public AnnotationElementValue(AnnotationGen a, ConstantPool cpool) { + super(ANNOTATION, cpool); + this.a = a; + } + + public AnnotationElementValue(int type, AnnotationGen annotation, ConstantPool cpool) { + super(type, cpool); + assert type == ANNOTATION; + this.a = annotation; + } + + public AnnotationElementValue(AnnotationElementValue value, ConstantPool cpool, boolean copyPoolEntries) { + super(ANNOTATION, cpool); + a = new AnnotationGen(value.getAnnotation(), cpool, copyPoolEntries); + } + + @Override + public void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 type of value (ANNOTATION == '@') + a.dump(dos); + } + + @Override + public String stringifyValue() { + throw new RuntimeException("Not implemented yet"); + } + + public AnnotationGen getAnnotation() { + return a; + } + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValueGen.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValueGen.java deleted file mode 100644 index e1b9f47d9..000000000 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValueGen.java +++ /dev/null @@ -1,61 +0,0 @@ -/* ******************************************************************* - * Copyright (c) 2004 IBM - * All rights reserved. - * This program and the accompanying materials are made available - * under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Andy Clement - initial implementation {date} - * ******************************************************************/ - -package org.aspectj.apache.bcel.classfile.annotation; - -import java.io.DataOutputStream; -import java.io.IOException; - -import org.aspectj.apache.bcel.classfile.ConstantPool; - - -public class AnnotationElementValueGen extends ElementValueGen { - - // For annotation element values, this is the annotation - private AnnotationGen a; - - public AnnotationElementValueGen(AnnotationGen a,ConstantPool cpool) { - super(ANNOTATION,cpool); - this.a = a; - } - - public AnnotationElementValueGen(int type, AnnotationGen annotation, ConstantPool cpool) { - super(type,cpool); - if (type != ANNOTATION) - throw new RuntimeException("Only element values of type annotation can be built with this ctor"); - this.a = annotation; - } - - public AnnotationElementValueGen(AnnotationElementValueGen value, ConstantPool cpool,boolean copyPoolEntries) { - super(ANNOTATION,cpool); - a = new AnnotationGen(value.getAnnotation(),cpool,copyPoolEntries); - } - - public void dump(DataOutputStream dos) throws IOException { - dos.writeByte(type); // u1 type of value (ANNOTATION == '@') - a.dump(dos); - } - - public String stringifyValue() { - throw new RuntimeException("Not implemented yet"); - } - - /** - * Return immutable variant of this AnnotationElementValueGen - */ - public ElementValueGen getElementValue() { - return new AnnotationElementValueGen(this.type,a,cpGen); - } - - public AnnotationGen getAnnotation() { return a;} - -} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationGen.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationGen.java index e5d2566b7..af442522d 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationGen.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationGen.java @@ -28,7 +28,7 @@ public class AnnotationGen { public static final AnnotationGen[] NO_ANNOTATIONS = new AnnotationGen[0]; private int typeIndex; - private List pairs = Collections.emptyList(); + private List pairs = Collections.emptyList(); private ConstantPool cpool; private boolean isRuntimeVisible = false; @@ -43,10 +43,10 @@ public class AnnotationGen { pairs = copyValues(a.getValues(), cpool, copyPoolEntries); } - private List copyValues(List in, ConstantPool cpool, boolean copyPoolEntries) { - List out = new ArrayList(); - for (ElementNameValuePairGen nvp : in) { - out.add(new ElementNameValuePairGen(nvp, cpool, copyPoolEntries)); + private List copyValues(List in, ConstantPool cpool, boolean copyPoolEntries) { + List out = new ArrayList(); + for (NameValuePair nvp : in) { + out.add(new NameValuePair(nvp, cpool, copyPoolEntries)); } return out; } @@ -58,7 +58,7 @@ public class AnnotationGen { /** * Retrieve an immutable version of this AnnotationGen */ - public AnnotationGen(ObjectType type, List pairs, boolean runtimeVisible, ConstantPool cpool) { + public AnnotationGen(ObjectType type, List pairs, boolean runtimeVisible, ConstantPool cpool) { this.cpool = cpool; if (type != null) { this.typeIndex = cpool.addUtf8(type.getSignature()); // Only null for funky *temporary* FakeAnnotation objects @@ -73,7 +73,7 @@ public class AnnotationGen { int elemValuePairCount = dis.readUnsignedShort(); for (int i = 0; i < elemValuePairCount; i++) { int nidx = dis.readUnsignedShort(); - a.addElementNameValuePair(new ElementNameValuePairGen(nidx, ElementValueGen.readElementValue(dis, cpool), cpool)); + a.addElementNameValuePair(new NameValuePair(nidx, ElementValue.readElementValue(dis, cpool), cpool)); } a.isRuntimeVisible(b); return a; @@ -83,14 +83,14 @@ public class AnnotationGen { dos.writeShort(typeIndex); // u2 index of type name in cpool dos.writeShort(pairs.size()); // u2 element_value pair count for (int i = 0; i < pairs.size(); i++) { - ElementNameValuePairGen envp = pairs.get(i); + NameValuePair envp = pairs.get(i); envp.dump(dos); } } - public void addElementNameValuePair(ElementNameValuePairGen evp) { + public void addElementNameValuePair(NameValuePair evp) { if (pairs == Collections.EMPTY_LIST) { - pairs = new ArrayList(); + pairs = new ArrayList(); } pairs.add(evp); } @@ -108,7 +108,7 @@ public class AnnotationGen { return Utility.signatureToString(getTypeSignature()); } - public List getValues() { + public List getValues() { return pairs; } @@ -149,7 +149,7 @@ public class AnnotationGen { * @return true if the annotation has a value with the specified name and (toString'd) value */ public boolean hasNameValuePair(String name, String value) { - for (ElementNameValuePairGen pair : pairs) { + for (NameValuePair pair : pairs) { if (pair.getNameString().equals(name)) { if (pair.getValue().stringifyValue().equals(value)) { return true; @@ -163,7 +163,7 @@ public class AnnotationGen { * @return true if the annotation has a value with the specified name */ public boolean hasNamedValue(String name) { - for (ElementNameValuePairGen pair : pairs) { + for (NameValuePair pair : pairs) { if (pair.getNameString().equals(name)) { return true; } diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ArrayElementValue.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ArrayElementValue.java new file mode 100644 index 000000000..3dd19af71 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ArrayElementValue.java @@ -0,0 +1,84 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andy Clement - initial implementation {date} + * ******************************************************************/ + +package org.aspectj.apache.bcel.classfile.annotation; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.classfile.ConstantPool; + +public class ArrayElementValue extends ElementValue { + private static final ElementValue[] NO_VALUES = new ElementValue[0]; + + // J5TODO: Should we make this an array or a list? A list would be easier to modify ... + private ElementValue[] evalues = NO_VALUES; + + public ElementValue[] getElementValuesArray() { + return evalues; + } + + public int getElementValuesArraySize() { + return evalues.length; + } + + public ArrayElementValue(ConstantPool cp) { + super(ARRAY, cp); + } + + public ArrayElementValue(int type, ElementValue[] datums, ConstantPool cpool) { + super(type, cpool); + if (type != ARRAY) + throw new RuntimeException("Only element values of type array can be built with this ctor"); + this.evalues = datums; + } + + public ArrayElementValue(ArrayElementValue value, ConstantPool cpool, boolean copyPoolEntries) { + super(ARRAY, cpool); + evalues = new ElementValue[value.getElementValuesArraySize()]; + ElementValue[] in = value.getElementValuesArray(); + for (int i = 0; i < in.length; i++) { + evalues[i] = ElementValue.copy(in[i], cpool, copyPoolEntries); + } + } + + @Override + public void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 type of value (ARRAY == '[') + dos.writeShort(evalues.length); + for (int i = 0; i < evalues.length; i++) { + evalues[i].dump(dos); + } + } + + @Override + public String stringifyValue() { + StringBuffer sb = new StringBuffer(); + sb.append("["); + for (int i = 0; i < evalues.length; i++) { + ElementValue element = evalues[i]; + sb.append(element.stringifyValue()); + if ((i + 1) < evalues.length) + sb.append(","); + } + sb.append("]"); + return sb.toString(); + } + + public void addElement(ElementValue gen) { + ElementValue[] old = evalues; + evalues = new ElementValue[evalues.length + 1]; + System.arraycopy(old, 0, evalues, 0, old.length); + evalues[old.length] = gen; + } + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ArrayElementValueGen.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ArrayElementValueGen.java deleted file mode 100644 index 10599225a..000000000 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ArrayElementValueGen.java +++ /dev/null @@ -1,93 +0,0 @@ -/* ******************************************************************* - * Copyright (c) 2004 IBM - * All rights reserved. - * This program and the accompanying materials are made available - * under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Andy Clement - initial implementation {date} - * ******************************************************************/ - -package org.aspectj.apache.bcel.classfile.annotation; - -import java.io.DataOutputStream; -import java.io.IOException; - -import org.aspectj.apache.bcel.classfile.ConstantPool; - - -public class ArrayElementValueGen extends ElementValueGen { - private static final ElementValueGen[] NO_VALUES = new ElementValueGen[0]; - - // J5TODO: Should we make this an array or a list? A list would be easier to modify ... - private ElementValueGen[] evalues = NO_VALUES; - - public ElementValueGen[] getElementValuesArray() { return evalues;} - public int getElementValuesArraySize() { return evalues.length;} - - public ArrayElementValueGen(ConstantPool cp) { - super(ARRAY,cp); - } - - public ArrayElementValueGen(int type, ElementValueGen[] datums, ConstantPool cpool) { - super(type,cpool); - if (type != ARRAY) - throw new RuntimeException("Only element values of type array can be built with this ctor"); - this.evalues = datums; - } - - /** - * Return immutable variant of this ArrayElementValueGen - */ - public ElementValueGen getElementValue() { - ElementValueGen[] immutableData = new ElementValueGen[evalues.length]; - for (int i = 0; i annotations; - private boolean visible; - - // Keep just a byte stream of the data until someone actually asks for it - private boolean inflated = false; - private byte[] annotation_data; - - public RuntimeAnnotations(byte attrid, boolean visible, - int nameIdx, int len, - ConstantPool cpool) { - super(attrid,nameIdx,len,cpool); - this.visible = visible; - annotations = new ArrayList(); - } - - public RuntimeAnnotations(byte attrid,boolean visible,int nameIdx,int len,byte[] data,ConstantPool cpool) { - super(attrid,nameIdx,len,cpool); - this.visible = visible; - annotations = new ArrayList(); - annotation_data = data; - } - - public List getAnnotations() { - if (!inflated) inflate(); - return annotations; - } - - public boolean areVisible() { - return visible; - } - - protected void readAnnotations(DataInputStream dis,ConstantPool cpool) throws IOException { - annotation_data = new byte[length]; - dis.read(annotation_data,0,length); - } - - protected void writeAnnotations(DataOutputStream dos) throws IOException { - if (!inflated) { - dos.write(annotation_data,0,length); - } else { - dos.writeShort(annotations.size()); - for (Iterator i = annotations.iterator(); i.hasNext();) { - AnnotationGen ann = i.next(); - ann.dump(dos); - } - } - } - - private void inflate() { - try { - DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data)); - int numberOfAnnotations = dis.readUnsignedShort(); - for (int i = 0 ;i parameterAnnotations; + private boolean visible; + + + // Keep just a byte stream of the data until someone actually asks for it + private boolean inflated = false; + private byte[] annotation_data; + + + public RuntimeParamAnnos(byte attrid, boolean visible, + int nameIdx, int len, ConstantPool cpool) { + super(attrid,nameIdx,len,cpool); + this.visible = visible; + parameterAnnotations = new ArrayList(); + } + + public RuntimeParamAnnos(byte attrid,boolean visible,int nameIdx,int len,byte[] data,ConstantPool cpool) { + super(attrid,nameIdx,len,cpool); + this.visible = visible; + parameterAnnotations = new ArrayList(); + annotation_data = data; + } + + public final void dump(DataOutputStream dos) throws IOException { + super.dump(dos); + writeAnnotations(dos); + } + + public Attribute copy(ConstantPool constant_pool) { + throw new RuntimeException("Not implemented yet!"); + } + + /** Return a list of Annotation[] - each list entry contains the annotations for one parameter */ + public List /*Annotation[]*/ getParameterAnnotations() { + if (!inflated) inflate(); + return parameterAnnotations; + } + + public AnnotationGen[] getAnnotationsOnParameter(int parameterIndex) { + if (!inflated) inflate(); + // This may happen. In a ctor for a non static inner type the compiler + // may have added an extra parameter to the generated ctor (the parameter + // contains the instance of the outer class) - in this case + // it may appear that there are more parameters than there are entries + // in the parameter annotations array + if (parameterIndex>=parameterAnnotations.size()) { + return AnnotationGen.NO_ANNOTATIONS; + } + return parameterAnnotations.get(parameterIndex); + } + + public boolean areVisible() { + return visible; + } + + protected void readParameterAnnotations(DataInputStream dis,ConstantPool cpool) throws IOException { + annotation_data = new byte[length]; + dis.read(annotation_data,0,length); + } + + private void inflate() { + try { + DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data)); + int numParameters = dis.readUnsignedByte(); + for (int i=0; i parameterAnnotations; - private boolean visible; - - - // Keep just a byte stream of the data until someone actually asks for it - private boolean inflated = false; - private byte[] annotation_data; - - - public RuntimeParameterAnnotations(byte attrid, boolean visible, - int nameIdx, int len, ConstantPool cpool) { - super(attrid,nameIdx,len,cpool); - this.visible = visible; - parameterAnnotations = new ArrayList(); - } - - public RuntimeParameterAnnotations(byte attrid,boolean visible,int nameIdx,int len,byte[] data,ConstantPool cpool) { - super(attrid,nameIdx,len,cpool); - this.visible = visible; - parameterAnnotations = new ArrayList(); - annotation_data = data; - } - - public final void dump(DataOutputStream dos) throws IOException { - super.dump(dos); - writeAnnotations(dos); - } - - public Attribute copy(ConstantPool constant_pool) { - throw new RuntimeException("Not implemented yet!"); - } - - /** Return a list of Annotation[] - each list entry contains the annotations for one parameter */ - public List /*Annotation[]*/ getParameterAnnotations() { - if (!inflated) inflate(); - return parameterAnnotations; - } - - public AnnotationGen[] getAnnotationsOnParameter(int parameterIndex) { - if (!inflated) inflate(); - // This may happen. In a ctor for a non static inner type the compiler - // may have added an extra parameter to the generated ctor (the parameter - // contains the instance of the outer class) - in this case - // it may appear that there are more parameters than there are entries - // in the parameter annotations array - if (parameterIndex>=parameterAnnotations.size()) { - return AnnotationGen.NO_ANNOTATIONS; - } - return parameterAnnotations.get(parameterIndex); - } - - public boolean areVisible() { - return visible; - } - - protected void readParameterAnnotations(DataInputStream dis,ConstantPool cpool) throws IOException { - annotation_data = new byte[length]; - dis.read(annotation_data,0,length); - } - - private void inflate() { - try { - DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data)); - int numParameters = dis.readUnsignedByte(); - for (int i=0; iM. Dahm * * Upgraded, Andy Clement 9th Mar 06 - calculates SUID @@ -148,14 +148,14 @@ public class ClassGen extends Modifiers implements Cloneable { // OPTIMIZE Could make unpacking lazy, done on first reference Attribute[] attributes = clazz.getAttributes(); for (Attribute attr : attributes) { - if (attr instanceof RuntimeVisibleAnnotations) { - RuntimeVisibleAnnotations rva = (RuntimeVisibleAnnotations) attr; + if (attr instanceof RuntimeVisAnnos) { + RuntimeVisAnnos rva = (RuntimeVisAnnos) attr; List annos = rva.getAnnotations(); for (AnnotationGen a : annos) { annotationsList.add(new AnnotationGen(a, cpool, false)); } - } else if (attr instanceof RuntimeInvisibleAnnotations) { - RuntimeInvisibleAnnotations ria = (RuntimeInvisibleAnnotations) attr; + } else if (attr instanceof RuntimeInvisAnnos) { + RuntimeInvisAnnos ria = (RuntimeInvisAnnos) attr; List annos = ria.getAnnotations(); for (AnnotationGen anno : annos) { annotationsList.add(new AnnotationGen(anno, cpool, false)); diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldGen.java index 220844633..fdda9109a 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldGen.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldGen.java @@ -66,13 +66,13 @@ import org.aspectj.apache.bcel.classfile.ConstantValue; import org.aspectj.apache.bcel.classfile.Field; import org.aspectj.apache.bcel.classfile.Utility; import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnotations; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnos; /** * Template class for building up a field. The only extraordinary thing one can do is to add a constant value attribute to a field * (which must of course be compatible with the declared type). * - * @version $Id: FieldGen.java,v 1.8 2009/09/14 20:29:10 aclement Exp $ + * @version $Id: FieldGen.java,v 1.9 2009/09/15 19:40:14 aclement Exp $ * @author M. Dahm * @see Field */ @@ -109,8 +109,8 @@ public class FieldGen extends FieldGenOrMethodGen { for (int i = 0; i < attrs.length; i++) { if (attrs[i] instanceof ConstantValue) { setValue(((ConstantValue) attrs[i]).getConstantValueIndex()); - } else if (attrs[i] instanceof RuntimeAnnotations) { - RuntimeAnnotations runtimeAnnotations = (RuntimeAnnotations) attrs[i]; + } else if (attrs[i] instanceof RuntimeAnnos) { + RuntimeAnnos runtimeAnnotations = (RuntimeAnnos) attrs[i]; List l = runtimeAnnotations.getAnnotations(); for (Iterator it = l.iterator(); it.hasNext();) { AnnotationGen element = it.next(); diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldGenOrMethodGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldGenOrMethodGen.java index 74ede06ec..ff8b6f42c 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldGenOrMethodGen.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/FieldGenOrMethodGen.java @@ -63,12 +63,12 @@ import org.aspectj.apache.bcel.classfile.ConstantPool; import org.aspectj.apache.bcel.classfile.Modifiers; import org.aspectj.apache.bcel.classfile.Utility; import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnotations; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnos; /** * Super class for FieldGen and MethodGen objects, since they have some methods in common! * - * @version $Id: FieldGenOrMethodGen.java,v 1.7 2009/09/14 20:29:10 aclement Exp $ + * @version $Id: FieldGenOrMethodGen.java,v 1.8 2009/09/15 19:40:14 aclement Exp $ * @author M. Dahm */ public abstract class FieldGenOrMethodGen extends Modifiers { @@ -145,7 +145,7 @@ public abstract class FieldGenOrMethodGen extends Modifiers { } protected void addAnnotationsAsAttribute(ConstantPool cp) { - Collection attrs = Utility.getAnnotationAttributes(cp, annotationList); + Collection attrs = Utility.getAnnotationAttributes(cp, annotationList); if (attrs != null) { for (Attribute attr : attrs) { addAttribute(attr); diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/MethodGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/MethodGen.java index b85bf0165..15c9000e9 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/generic/MethodGen.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/MethodGen.java @@ -73,8 +73,8 @@ import org.aspectj.apache.bcel.classfile.LocalVariableTable; import org.aspectj.apache.bcel.classfile.Method; import org.aspectj.apache.bcel.classfile.Utility; import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnotations; -import org.aspectj.apache.bcel.classfile.annotation.RuntimeParameterAnnotations; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnos; +import org.aspectj.apache.bcel.classfile.annotation.RuntimeParamAnnos; /** * Template class for building up a method. This is done by defining exception handlers, adding thrown exceptions, local variables @@ -84,7 +84,7 @@ import org.aspectj.apache.bcel.classfile.annotation.RuntimeParameterAnnotations; * While generating code it may be necessary to insert NOP operations. You can use the `removeNOPs' method to get rid off them. The * resulting method object can be obtained via the `getMethod()' method. * - * @version $Id: MethodGen.java,v 1.14 2009/09/14 20:29:10 aclement Exp $ + * @version $Id: MethodGen.java,v 1.15 2009/09/15 19:40:14 aclement Exp $ * @author M. Dahm * @author Patrick C. Beard [setMaxStack()] * @see InstructionList @@ -329,8 +329,8 @@ public class MethodGen extends FieldGenOrMethodGen { String[] names = ((ExceptionTable) a).getExceptionNames(); for (int j = 0; j < names.length; j++) addException(names[j]); - } else if (a instanceof RuntimeAnnotations) { - RuntimeAnnotations runtimeAnnotations = (RuntimeAnnotations) a; + } else if (a instanceof RuntimeAnnos) { + RuntimeAnnos runtimeAnnotations = (RuntimeAnnos) a; List l = runtimeAnnotations.getAnnotations(); annotationList.addAll(l); // for (Iterator it = l.iterator(); it.hasNext();) { @@ -1040,11 +1040,11 @@ public class MethodGen extends FieldGenOrMethodGen { } // Find attributes that contain parameter annotation data List attrs = getAttributes(); - RuntimeParameterAnnotations paramAnnVisAttr = null; - RuntimeParameterAnnotations paramAnnInvisAttr = null; + RuntimeParamAnnos paramAnnVisAttr = null; + RuntimeParamAnnos paramAnnInvisAttr = null; for (Attribute attribute : attrs) { - if (attribute instanceof RuntimeParameterAnnotations) { + if (attribute instanceof RuntimeParamAnnos) { if (!hasParameterAnnotations) { param_annotations = new List[parameterTypes.length]; @@ -1054,7 +1054,7 @@ public class MethodGen extends FieldGenOrMethodGen { } hasParameterAnnotations = true; - RuntimeParameterAnnotations rpa = (RuntimeParameterAnnotations) attribute; + RuntimeParamAnnos rpa = (RuntimeParamAnnos) attribute; if (rpa.areVisible()) paramAnnVisAttr = rpa; else