Author: tcurdt Date: Sun Jan 10 19:26:09 2010 New Revision: 897687 URL: http://svn.apache.org/viewvc?rev=897687&view=rev Log: https://issues.apache.org/bugzilla/show_bug.cgi?id=41660
Expose annotations to subclasses Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/Annotations.java Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java?rev=897687&r1=897686&r2=897687&view=diff ============================================================================== --- jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java (original) +++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/AnnotationEntry.java Sun Jan 10 19:26:09 2010 @@ -29,124 +29,129 @@ * represents one annotation in the annotation table * * @version $Id: AnnotationEntry - * @author <A HREF="mailto:dbros...@mebigfatguy.com">D. Brosius</A> + * @author <A HREF="mailto:dbros...@mebigfatguy.com">D. Brosius</A> * @since 5.3 */ public class AnnotationEntry implements Node, Constants, Serializable { - private int type_index; - private int num_element_value_pairs; + private static final long serialVersionUID = 1L; + + private final int type_index; + private final ConstantPool constant_pool; + private final boolean isRuntimeVisible; + private List element_value_pairs; - private ConstantPool constant_pool; - private boolean isRuntimeVisible; + /** + * Factory method to create an AnnotionEntry from a DataInputStream + * + * @param file + * @param constant_pool + * @param isRuntimeVisible + * @return + * @throws IOException + */ + public static AnnotationEntry read(DataInputStream file, ConstantPool constant_pool, boolean isRuntimeVisible) throws IOException { + + final AnnotationEntry annotationEntry = new AnnotationEntry(file.readUnsignedShort(), constant_pool, isRuntimeVisible); + final int num_element_value_pairs = (file.readUnsignedShort()); + annotationEntry.element_value_pairs = new ArrayList(); + for (int i = 0; i < num_element_value_pairs; i++) { + annotationEntry.element_value_pairs.add(new ElementValuePair(file.readUnsignedShort(), ElementValue.readElementValue(file, constant_pool), + constant_pool)); + } + return annotationEntry; + } /** * Construct object from file stream. + * * @param file Input stream */ public AnnotationEntry(int type_index, ConstantPool constant_pool, boolean isRuntimeVisible) { this.type_index = type_index; - this.constant_pool = constant_pool; this.isRuntimeVisible = isRuntimeVisible; } - - public static AnnotationEntry read(DataInputStream file, ConstantPool constant_pool, boolean isRuntimeVisible) throws IOException - { - AnnotationEntry annotationEntry = new AnnotationEntry(file.readUnsignedShort(), constant_pool, isRuntimeVisible); - annotationEntry.num_element_value_pairs = (file.readUnsignedShort()); - annotationEntry.element_value_pairs = new ArrayList(); - for (int i = 0; i < annotationEntry.num_element_value_pairs; i++) { - annotationEntry.element_value_pairs.add(new ElementValuePair(file.readUnsignedShort(), ElementValue.readElementValue(file, constant_pool), constant_pool)); - } - return annotationEntry; + + public int getTypeIndex() { + return type_index; + } + + public ConstantPool getConstantPool() { + return constant_pool; } + public boolean isRuntimeVisible() { + return isRuntimeVisible; + } /** - * 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. - * + * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. + * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. + * * @param v Visitor object */ - public void accept( Visitor v ) { - // v.visitAnnotationEntry(this); + public void accept(Visitor v) { + // v.visitAnnotationEntry(this); } - /** * @return the annotation type name */ public String getAnnotationType() { - ConstantUtf8 c; - c = (ConstantUtf8) constant_pool.getConstant(type_index, CONSTANT_Utf8); + final ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(type_index, CONSTANT_Utf8); return c.getBytes(); } - + /** * @return the annotation type index */ - public int getAnnotationTypeIndex() - { - return type_index; + public int getAnnotationTypeIndex() { + return type_index; } - /** * @return the number of element value pairs in this annotation entry */ public final int getNumElementValuePairs() { - return num_element_value_pairs; + return element_value_pairs.size(); } - /** * @return the element value pairs in this annotation entry */ public ElementValuePair[] getElementValuePairs() { - // TOFO return List + // TODO return List return (ElementValuePair[]) element_value_pairs.toArray(new ElementValuePair[element_value_pairs.size()]); } + public void dump(DataOutputStream dos) throws IOException { + dos.writeShort(type_index); // u2 index of type name in cpool + dos.writeShort(element_value_pairs.size()); // u2 element_value pair + // count + for (int i = 0; i < element_value_pairs.size(); i++) { + final ElementValuePair envp = (ElementValuePair) element_value_pairs.get(i); + envp.dump(dos); + } + } + + public void addElementNameValuePair(ElementValuePair elementNameValuePair) { + element_value_pairs.add(elementNameValuePair); + } - public void dump(DataOutputStream dos) throws IOException - { - dos.writeShort(type_index); // u2 index of type name in cpool - dos.writeShort(element_value_pairs.size()); // u2 element_value pair count - for (int i = 0 ; i<element_value_pairs.size();i++) { - ElementValuePair envp = (ElementValuePair) element_value_pairs.get(i); - envp.dump(dos); - } - } - - - public boolean isRuntimeVisible() - { - return isRuntimeVisible; - } - - public void addElementNameValuePair(ElementValuePair elementNameValuePair) - { - element_value_pairs.add(elementNameValuePair); - } - - public String toShortString() - { - StringBuffer result = new StringBuffer(); - result.append("@"); - result.append(getAnnotationType()); - if (getElementValuePairs().length > 0) - { - result.append("("); - for (int i = 0; i < getElementValuePairs().length; i++) - { - ElementValuePair element = getElementValuePairs()[i]; - result.append(element.toShortString()); - } - result.append(")"); - } - return result.toString(); - } + public String toShortString() { + final StringBuffer result = new StringBuffer(); + result.append("@"); + result.append(getAnnotationType()); + if (getElementValuePairs().length > 0) { + result.append("("); + for (int i = 0; i < getElementValuePairs().length; i++) { + final ElementValuePair element = getElementValuePairs()[i]; + result.append(element.toShortString()); + } + result.append(")"); + } + return result.toString(); + } } Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/Annotations.java URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/Annotations.java?rev=897687&r1=897686&r2=897687&view=diff ============================================================================== --- jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/Annotations.java (original) +++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/Annotations.java Sun Jan 10 19:26:09 2010 @@ -24,15 +24,15 @@ * base class for annotations * * @version $Id: Annotations - * @author <A HREF="mailto:dbros...@qis.net">D. Brosius</A> + * @author <A HREF="mailto:dbros...@qis.net">D. Brosius</A> * @since 5.3 */ public abstract class Annotations extends Attribute { - private int annotation_table_length; - private AnnotationEntry[] annotation_table; // Table of annotations - private boolean isRuntimeVisible; + private static final long serialVersionUID = 1L; + private AnnotationEntry[] annotation_table; + private final boolean isRuntimeVisible; /** * @param annotation_type the subclass type of the annotation @@ -41,17 +41,15 @@ * @param file Input stream * @param constant_pool Array of constants */ - public Annotations(byte annotation_type, int name_index, int length, DataInputStream file, - ConstantPool constant_pool, boolean isRuntimeVisible) throws IOException { + public Annotations(byte annotation_type, int name_index, int length, DataInputStream file, ConstantPool constant_pool, boolean isRuntimeVisible) throws IOException { this(annotation_type, name_index, length, (AnnotationEntry[]) null, constant_pool, isRuntimeVisible); - annotation_table_length = (file.readUnsignedShort()); + final int annotation_table_length = (file.readUnsignedShort()); annotation_table = new AnnotationEntry[annotation_table_length]; for (int i = 0; i < annotation_table_length; i++) { annotation_table[i] = AnnotationEntry.read(file, constant_pool, isRuntimeVisible); } } - /** * @param annotation_type the subclass type of the annotation * @param name_index Index pointing to the name <em>Code</em> @@ -59,45 +57,29 @@ * @param annotation_table the actual annotations * @param constant_pool Array of constants */ - public Annotations(byte annotation_type, int name_index, int length, - AnnotationEntry[] annotation_table, ConstantPool constant_pool , boolean isRuntimeVisible) { + public Annotations(byte annotation_type, int name_index, int length, AnnotationEntry[] annotation_table, ConstantPool constant_pool, boolean isRuntimeVisible) { super(annotation_type, name_index, length, constant_pool); setAnnotationTable(annotation_table); this.isRuntimeVisible = isRuntimeVisible; } - /** - * 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. - * + * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. + * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. + * * @param v Visitor object */ - public void accept( Visitor v ) { + public void accept(Visitor v) { v.visitAnnotation(this); } - /** * @param annotation_table the entries to set in this annotation */ - public final void setAnnotationTable( AnnotationEntry[] annotation_table ) { + public final void setAnnotationTable(AnnotationEntry[] annotation_table) { this.annotation_table = annotation_table; - annotation_table_length = (annotation_table == null) ? 0 : annotation_table.length; } - - // TODO: update method names - /** - * @return the annotation entry table - */ - /* - public final AnnotationEntry[] getAnnotationTable() { - return annotation_table; - }*/ - - /** * returns the array of annotation entries in this annotation */ @@ -105,23 +87,27 @@ return annotation_table; } - /** * @return the number of annotation entries in this annotation */ public final int getNumAnnotations() { - return annotation_table_length; + if (annotation_table == null) { + return 0; + } + return annotation_table.length; + } + + public boolean isRuntimeVisible() { + return isRuntimeVisible; + } + + protected void writeAnnotations(DataOutputStream dos) throws IOException { + if (annotation_table == null) { + return; + } + dos.writeShort(annotation_table.length); + for (int i = 0; i < annotation_table.length; i++) { + annotation_table[i].dump(dos); + } } - - public boolean isRuntimeVisible() - { - return isRuntimeVisible; - } - - protected void writeAnnotations(DataOutputStream dos) throws IOException - { - dos.writeShort(annotation_table_length); - for (int i = 0; i < annotation_table_length; i++) - annotation_table[i].dump(dos); - } } --------------------------------------------------------------------- To unsubscribe, e-mail: bcel-dev-unsubscr...@jakarta.apache.org For additional commands, e-mail: bcel-dev-h...@jakarta.apache.org