diff options
author | aclement <aclement> | 2004-11-19 16:35:15 +0000 |
---|---|---|
committer | aclement <aclement> | 2004-11-19 16:35:15 +0000 |
commit | 8644e07c0dcee8a4e24b0bcdb320c9bfa47d9612 (patch) | |
tree | 73e8702570983a46ba4129d57fa16d6df9950327 /bcel-builder/src/org/aspectj/apache | |
parent | bfd1d4d0b5a553afdbbf93eb1972ff9d802906b3 (diff) | |
download | aspectj-8644e07c0dcee8a4e24b0bcdb320c9bfa47d9612.tar.gz aspectj-8644e07c0dcee8a4e24b0bcdb320c9bfa47d9612.zip |
BCEL Java5 Support
Diffstat (limited to 'bcel-builder/src/org/aspectj/apache')
22 files changed, 1805 insertions, 0 deletions
diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/Annotation.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/Annotation.java new file mode 100644 index 000000000..f5c280018 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/Annotation.java @@ -0,0 +1,125 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM Corporation + * + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement initial implementation + * ******************************************************************/ +package org.aspectj.apache.bcel.classfile.annotation; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.aspectj.apache.bcel.Constants; +import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.ConstantUtf8; +import org.aspectj.apache.bcel.classfile.Utility; + +/** + * An annotation is an immutable object (AnnotationGen is the mutable variant) - it basically contains a list + * of name-value pairs. + */ +public class Annotation { + private int typeIndex; + private List /* ElementNameValuePair */ evs; + private ConstantPool cpool; + private boolean isRuntimeVisible; + + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("ANNOTATION ["+getTypeSignature()+"] ["+ + (isRuntimeVisible?"runtimeVisible":"runtimeInvisible")+"] ["); + for (Iterator iter = evs.iterator(); iter.hasNext();) { + ElementNameValuePair element = (ElementNameValuePair) iter.next(); + sb.append(element.toString()); + if (iter.hasNext()) sb.append(","); + } + sb.append("]"); + return sb.toString(); + } + + private Annotation(ConstantPool cpool) { + this.cpool = cpool; + } + + protected static Annotation read(DataInputStream dis,ConstantPool cpool,boolean isRuntimeVisible) throws IOException { + Annotation a = new Annotation(cpool); + a.typeIndex = dis.readUnsignedShort(); + int elemValuePairCount = dis.readUnsignedShort(); + for (int i=0;i<elemValuePairCount;i++) { + int nidx = dis.readUnsignedShort(); + a.addElementNameValuePair( + new ElementNameValuePair(nidx,ElementValue.readElementValue(dis,cpool),cpool)); + } + a.isRuntimeVisible(isRuntimeVisible); + return a; + } + + protected void dump(DataOutputStream dos) throws IOException { + dos.writeShort(typeIndex); // u2 index of type name in cpool + dos.writeShort(evs.size()); // u2 element_value pair count + for (int i = 0 ; i<evs.size();i++) { + ElementNameValuePair envp = (ElementNameValuePair) evs.get(i); + envp.dump(dos); + } + } + + public void addElementNameValuePair(ElementNameValuePair evp) { + if (evs == null) evs = new ArrayList(); + evs.add(evp); + } + + + public int getTypeIndex() { + return typeIndex; + } + + public final String getTypeSignature() { + ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(typeIndex,Constants.CONSTANT_Utf8); + return c.getBytes(); + } + + public final String getTypeName() { + ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(typeIndex,Constants.CONSTANT_Utf8); + return Utility.signatureToString(c.getBytes()); + } + + /** + * Returns list of ElementNameValuePair objects + */ + public List getValues() { + return evs; + } + + protected void isRuntimeVisible(boolean b) { + isRuntimeVisible = b; + } + + public boolean isRuntimeVisible() { + return isRuntimeVisible; + } + + public String toShortString() { + StringBuffer result = new StringBuffer(); + result.append("@"); + result.append(getTypeName()); + if (getValues().size()>0) { + result.append("("); + for (Iterator iter = getValues().iterator(); iter.hasNext();) { + ElementNameValuePair element = (ElementNameValuePair) iter.next(); + result.append(element.toShortString()); + } + result.append(")"); + } + return result.toString(); + } +} 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..eba2cb1f3 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValue.java @@ -0,0 +1,51 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-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; + +/** + * An element value that is an annotation. + */ +public class AnnotationElementValue extends ElementValue { + + // For annotation element values, this is the annotation + private Annotation a; + + public AnnotationElementValue(int type, Annotation 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 void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 type of value (ANNOTATION == '@') + a.dump(dos); + } + + public String stringifyValue() { + StringBuffer sb = new StringBuffer(); + sb.append(a.toString()); + return sb.toString(); + } + + public String toString() { + return stringifyValue(); + } + + public Annotation getAnnotation() { return a;} + +} 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..de1b0cc70 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ArrayElementValue.java @@ -0,0 +1,66 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-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 { + + // For array types, this is the array + private ElementValue[] evalues; + + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("{"); + for (int i = 0; i < evalues.length; i++) { + sb.append(evalues[i].toString()); + if ((i+1)<evalues.length) sb.append(","); + } + sb.append("}"); + return sb.toString(); + } + + 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 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); + } + } + + public String stringifyValue() { + StringBuffer sb = new StringBuffer(); + sb.append("["); + for(int i=0; i<evalues.length; i++) { + sb.append(evalues[i].stringifyValue()); + if ((i+1)<evalues.length) sb.append(","); + } + sb.append("]"); + return sb.toString(); + } + + public ElementValue[] getElementValuesArray() { return evalues;} + public int getElementValuesArraySize() { return evalues.length;} + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ClassElementValue.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ClassElementValue.java new file mode 100644 index 000000000..9143b1bbb --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ClassElementValue.java @@ -0,0 +1,53 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-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.Constants; +import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.ConstantUtf8; + + +public class ClassElementValue extends ElementValue { + + // For primitive types and string type, this points to the value entry in the cpool + // For 'class' this points to the class entry in the cpool + private int idx; + + protected ClassElementValue(int type,int idx,ConstantPool cpool) { + super(type,cpool); + this.idx = idx; + } + + public int getIndex() { + return idx; + } + + public String getClassString() { + ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(idx,Constants.CONSTANT_Utf8); + return c.getBytes(); + } + + public String stringifyValue() { + ConstantUtf8 cu8 = (ConstantUtf8)cpool.getConstant(idx,Constants.CONSTANT_Utf8); + return cu8.getBytes(); + } + + public void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 kind of value + dos.writeShort(idx); + } + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ElementNameValuePair.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ElementNameValuePair.java new file mode 100644 index 000000000..50ac79f38 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ElementNameValuePair.java @@ -0,0 +1,62 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-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.Constants; +import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.ConstantUtf8; + + +public class ElementNameValuePair { + private int nameIdx; + private ElementValue value; + private ConstantPool cpool; + + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append(getNameString()+"="+value.toString()); + return sb.toString(); + } + public ElementNameValuePair(int idx,ElementValue value,ConstantPool cpool) { + this.nameIdx = idx; + this.value = value; + this.cpool = cpool; + } + + protected void dump(DataOutputStream dos) throws IOException { + dos.writeShort(nameIdx); // u2 name of the element + value.dump(dos); + } + + public int getNameIndex() { + return nameIdx; + } + + public final String getNameString() { + ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(nameIdx,Constants.CONSTANT_Utf8); + return c.getBytes(); + } + + public final ElementValue getValue() { + return value; + } + + public String toShortString() { + StringBuffer result = new StringBuffer(); + result.append(getNameString()).append("=").append(getValue().toShortString()); + return result.toString(); + } +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ElementValue.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ElementValue.java new file mode 100644 index 000000000..c40494975 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/ElementValue.java @@ -0,0 +1,112 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement - initial implementation {date} + * ******************************************************************/ + +package org.aspectj.apache.bcel.classfile.annotation; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.aspectj.apache.bcel.classfile.ConstantPool; + +public abstract class ElementValue { + + protected int type; + protected ConstantPool cpool; + + public String toString() { + return stringifyValue(); + } + + protected ElementValue(int type,ConstantPool cpool) { + this.type = type; + this.cpool = cpool; + } + + public int getElementValueType() { + return type; + } + + public abstract String stringifyValue(); + public abstract void dump(DataOutputStream dos) throws IOException; + + public static final int STRING = 's'; + public static final int ENUM_CONSTANT = 'e'; + public static final int CLASS = 'c'; + public static final int ANNOTATION = '@'; + public static final int ARRAY = '['; + + public static final int PRIMITIVE_INT = 'I'; + public static final int PRIMITIVE_BYTE = 'B'; + public static final int PRIMITIVE_CHAR = 'C'; + public static final int PRIMITIVE_DOUBLE = 'D'; + public static final int PRIMITIVE_FLOAT = 'F'; + public static final int PRIMITIVE_LONG = 'J'; + public static final int PRIMITIVE_SHORT = 'S'; + public static final int PRIMITIVE_BOOLEAN= 'Z'; + + public static ElementValue readElementValue(DataInputStream dis,ConstantPool cpool) throws IOException { + int type= dis.readUnsignedByte(); + switch (type) { + case 'B': // byte + return new SimpleElementValue(PRIMITIVE_BYTE,dis.readUnsignedShort(),cpool); + case 'C': // char + return new SimpleElementValue(PRIMITIVE_CHAR,dis.readUnsignedShort(),cpool); + case 'D': // double + return new SimpleElementValue(PRIMITIVE_DOUBLE,dis.readUnsignedShort(),cpool); + case 'F': // float + return new SimpleElementValue(PRIMITIVE_FLOAT,dis.readUnsignedShort(),cpool); + case 'I': // int + return new SimpleElementValue(PRIMITIVE_INT,dis.readUnsignedShort(),cpool); + case 'J': // long + return new SimpleElementValue(PRIMITIVE_LONG,dis.readUnsignedShort(),cpool); + case 'S': // short + return new SimpleElementValue(PRIMITIVE_SHORT,dis.readUnsignedShort(),cpool); + case 'Z': // boolean + return new SimpleElementValue(PRIMITIVE_BOOLEAN,dis.readUnsignedShort(),cpool); + case 's': // String + return new SimpleElementValue(STRING,dis.readUnsignedShort(),cpool); + + case 'e': // Enum constant + return new EnumElementValue(ENUM_CONSTANT,dis.readUnsignedShort(),dis.readUnsignedShort(),cpool); + + case 'c': // Class + return new ClassElementValue(CLASS,dis.readUnsignedShort(),cpool); + + //J5TODO: Should it be 'true' in the next statement? What difference does it make? Should it be + // the same as the 'super annotation' in which we are contained? + case '@': // Annotation + return new AnnotationElementValue(ANNOTATION,Annotation.read(dis,cpool,true),cpool); + + case '[': // Array + int numArrayVals = dis.readUnsignedShort(); + List arrayVals = new ArrayList(); + ElementValue[] evalues = new ElementValue[numArrayVals]; + for (int j=0;j<numArrayVals;j++) { + evalues[j] = ElementValue.readElementValue(dis,cpool); + } + return new ArrayElementValue(ARRAY,evalues,cpool); + + default: + throw new RuntimeException("Unexpected element value kind in annotation: "+type); + } + } + + + public String toShortString() { + StringBuffer result = new StringBuffer(); + result.append(stringifyValue()); + return result.toString(); + } +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/EnumElementValue.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/EnumElementValue.java new file mode 100644 index 000000000..c7fabd866 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/EnumElementValue.java @@ -0,0 +1,63 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-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.Constants; +import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.ConstantUtf8; +import org.aspectj.apache.bcel.classfile.Utility; + + +public class EnumElementValue extends ElementValue { + + // For enum types, these two indices point to the type and value + private int typeIdx; + private int valueIdx; + + public EnumElementValue(int type,int typeIdx,int valueIdx,ConstantPool cpool) { + super(type,cpool); + if (type != ENUM_CONSTANT) + throw new RuntimeException("Only element values of type enum can be built with this ctor"); + this.typeIdx = typeIdx; + this.valueIdx= valueIdx; + } + + public void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 type of value (ENUM_CONSTANT == 'e') + dos.writeShort(typeIdx); // u2 + dos.writeShort(valueIdx); // u2 + } + + public String stringifyValue() { + ConstantUtf8 cu8 = (ConstantUtf8)cpool.getConstant(valueIdx,Constants.CONSTANT_Utf8); + return cu8.getBytes(); + } + + public String getEnumTypeString() { + ConstantUtf8 cu8 = (ConstantUtf8)cpool.getConstant(typeIdx,Constants.CONSTANT_Utf8); + return Utility.signatureToString(cu8.getBytes()); + } + + public String getEnumValueString() { + ConstantUtf8 cu8 = (ConstantUtf8)cpool.getConstant(valueIdx,Constants.CONSTANT_Utf8); + return cu8.getBytes(); + } + + public int getValueIndex() { return valueIdx;} + public int getTypeIndex() { return typeIdx; } + + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeAnnotations.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeAnnotations.java new file mode 100644 index 000000000..c0d06aa82 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeAnnotations.java @@ -0,0 +1,83 @@ +package org.aspectj.apache.bcel.classfile.annotation; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.aspectj.apache.bcel.classfile.Attribute; +import org.aspectj.apache.bcel.classfile.ConstantPool; + +public abstract class RuntimeAnnotations extends Attribute { + + private List /*Annotation*/ 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); + } + + private void inflate() { + try { + DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data)); + int numberOfAnnotations = dis.readUnsignedShort(); + for (int i = 0 ;i<numberOfAnnotations;i++) { + annotations.add(Annotation.read(dis,getConstantPool(),visible)); + } + dis.close(); + inflated = true; + } catch (IOException ioe) { + throw new RuntimeException("Unabled to inflate annotation data, badly formed? "); + } + } + + 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();) { + Annotation ann = (Annotation) i.next(); + ann.dump(dos); + } + } + } + + /** FOR TESTING ONLY: Tells you if the annotations have been inflated to an object graph */ + public boolean isInflated() { + return inflated; + } + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeInvisibleAnnotations.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeInvisibleAnnotations.java new file mode 100644 index 000000000..626b48c65 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeInvisibleAnnotations.java @@ -0,0 +1,40 @@ +package org.aspectj.apache.bcel.classfile.annotation; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.Constants; +import org.aspectj.apache.bcel.classfile.Attribute; +import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.Visitor; + +public class RuntimeInvisibleAnnotations extends RuntimeAnnotations { + + public RuntimeInvisibleAnnotations(int nameIdx, int len, ConstantPool cpool) { + super(Constants.ATTR_RUNTIME_INVISIBLE_ANNOTATIONS, false, nameIdx, len, cpool); + } + + public RuntimeInvisibleAnnotations(int nameIdx, int len, + DataInputStream dis,ConstantPool cpool) throws IOException { + this(nameIdx, len, cpool); + readAnnotations(dis,cpool); + } + + public RuntimeInvisibleAnnotations(int nameIndex, int len, byte[] rvaData,ConstantPool cpool) { + super(Constants.ATTR_RUNTIME_INVISIBLE_ANNOTATIONS,true,nameIndex,len,rvaData,cpool); + } + + public void accept(Visitor v) { + v.visitRuntimeInvisibleAnnotations(this); + } + + 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!"); + } +}
\ No newline at end of file diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeInvisibleParameterAnnotations.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeInvisibleParameterAnnotations.java new file mode 100644 index 000000000..9665dd6a2 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeInvisibleParameterAnnotations.java @@ -0,0 +1,34 @@ +package org.aspectj.apache.bcel.classfile.annotation; + +import java.io.DataInputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.Constants; +import org.aspectj.apache.bcel.classfile.Attribute; +import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.Visitor; + +public class RuntimeInvisibleParameterAnnotations extends RuntimeParameterAnnotations { + + public RuntimeInvisibleParameterAnnotations(int nameIdx, int len, ConstantPool cpool) { + super(Constants.ATTR_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS, false, nameIdx, len, cpool); + } + + public RuntimeInvisibleParameterAnnotations(int nameIdx, int len, + DataInputStream dis,ConstantPool cpool) throws IOException { + this(nameIdx, len, cpool); + readParameterAnnotations(dis,cpool); + } + + public RuntimeInvisibleParameterAnnotations(int nameIndex, int len, byte[] rvaData,ConstantPool cpool) { + super(Constants.ATTR_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS,true,nameIndex,len,rvaData,cpool); + } + + public void accept(Visitor v) { + v.visitRuntimeInvisibleParameterAnnotations(this); + } + + public Attribute copy(ConstantPool constant_pool) { + throw new RuntimeException("Not implemented yet!"); + } +}
\ No newline at end of file diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeParameterAnnotations.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeParameterAnnotations.java new file mode 100644 index 000000000..5a90fe07c --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeParameterAnnotations.java @@ -0,0 +1,110 @@ +package org.aspectj.apache.bcel.classfile.annotation; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.aspectj.apache.bcel.classfile.Attribute; +import org.aspectj.apache.bcel.classfile.ConstantPool; + +public abstract class RuntimeParameterAnnotations extends Attribute { + + private List /*Annotation[]*/ 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 Annotation[] getAnnotationsOnParameter(int parameterIndex) { + if (!inflated) inflate(); + return (Annotation[])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<numParameters; i++) { + int numAnnotations = dis.readUnsignedShort(); + Annotation[] annotations = new Annotation[numAnnotations]; + for (int j=0; j<numAnnotations; j++) { + annotations[j] = Annotation.read(dis,getConstantPool(),visible); + } + parameterAnnotations.add(annotations); + } + inflated = true; + } catch (IOException ioe) { + throw new RuntimeException("Unabled to inflate annotation data, badly formed?"); + } + } + + + protected void writeAnnotations(DataOutputStream dos) throws IOException { + if (!inflated) { + dos.write(annotation_data,0,length); + } else { + dos.writeByte(parameterAnnotations.size()); + for (int i=0; i<parameterAnnotations.size(); i++) { + Annotation[] annotations = (Annotation[])parameterAnnotations.get(i); + dos.writeShort(annotations.length); + for (int j=0; j<annotations.length;j++) { + annotations[j].dump(dos); + } + } + } + } + + /** FOR TESTING ONLY: Tells you if the annotations have been inflated to an object graph */ + public boolean isInflated() { + return inflated; + } + + public String toString() { + return "Runtime"+(visible?"Visible":"Invisible")+"ParameterAnnotations ["+(inflated?"inflated":"not yet inflated")+"]"; + } + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeVisibleAnnotations.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeVisibleAnnotations.java new file mode 100644 index 000000000..e43c55b92 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeVisibleAnnotations.java @@ -0,0 +1,40 @@ +package org.aspectj.apache.bcel.classfile.annotation; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.Constants; +import org.aspectj.apache.bcel.classfile.Attribute; +import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.Visitor; + +public class RuntimeVisibleAnnotations extends RuntimeAnnotations { + + public RuntimeVisibleAnnotations(int nameIdx, int len, ConstantPool cpool) { + super(Constants.ATTR_RUNTIME_VISIBLE_ANNOTATIONS, true, nameIdx, len, cpool); + } + + public RuntimeVisibleAnnotations(int nameIdx, int len, + DataInputStream dis,ConstantPool cpool) throws IOException { + this(nameIdx, len, cpool); + readAnnotations(dis,cpool); + } + + public RuntimeVisibleAnnotations(int nameIndex, int len, byte[] rvaData,ConstantPool cpool) { + super(Constants.ATTR_RUNTIME_VISIBLE_ANNOTATIONS,true,nameIndex,len,rvaData,cpool); + } + + public void accept(Visitor v) { + v.visitRuntimeVisibleAnnotations(this); + } + + 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!"); + } +}
\ No newline at end of file diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeVisibleParameterAnnotations.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeVisibleParameterAnnotations.java new file mode 100644 index 000000000..b57068bc1 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/RuntimeVisibleParameterAnnotations.java @@ -0,0 +1,34 @@ +package org.aspectj.apache.bcel.classfile.annotation; + +import java.io.DataInputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.Constants; +import org.aspectj.apache.bcel.classfile.Attribute; +import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.Visitor; + +public class RuntimeVisibleParameterAnnotations extends RuntimeParameterAnnotations { + + public RuntimeVisibleParameterAnnotations(int nameIdx, int len, ConstantPool cpool) { + super(Constants.ATTR_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS, true, nameIdx, len, cpool); + } + + public RuntimeVisibleParameterAnnotations(int nameIndex, int len, byte[] rvaData,ConstantPool cpool) { + super(Constants.ATTR_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS,true,nameIndex,len,rvaData,cpool); + } + + public RuntimeVisibleParameterAnnotations(int nameIdx, int len, + DataInputStream dis,ConstantPool cpool) throws IOException { + this(nameIdx, len, cpool); + readParameterAnnotations(dis,cpool); + } + + public void accept(Visitor v) { + v.visitRuntimeVisibleParameterAnnotations(this); + } + + public Attribute copy(ConstantPool constant_pool) { + throw new RuntimeException("Not implemented yet!"); + } +}
\ No newline at end of file diff --git a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/SimpleElementValue.java b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/SimpleElementValue.java new file mode 100644 index 000000000..339365e54 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/SimpleElementValue.java @@ -0,0 +1,119 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-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.Constants; +import org.aspectj.apache.bcel.classfile.ConstantDouble; +import org.aspectj.apache.bcel.classfile.ConstantFloat; +import org.aspectj.apache.bcel.classfile.ConstantInteger; +import org.aspectj.apache.bcel.classfile.ConstantLong; +import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.ConstantUtf8; + +/** + * An element value representing a primitive or string value. + */ +public class SimpleElementValue extends ElementValue { + + // For primitive types and string type, this points to the value entry in the cpool + // For 'class' this points to the class entry in the cpool + private int idx; + + protected SimpleElementValue(int type,int idx,ConstantPool cpool) { + super(type,cpool); + this.idx = idx; + } + + public int getIndex() { + return idx; + } + + + public String getValueString() { + if (type != STRING) + throw new RuntimeException("Dont call getValueString() on a non STRING ElementValue"); + ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(idx,Constants.CONSTANT_Utf8); + return c.getBytes(); + } + + public int getValueInt() { + if (type != PRIMITIVE_INT) + throw new RuntimeException("Dont call getValueString() on a non STRING ElementValue"); + ConstantInteger c = (ConstantInteger)cpool.getConstant(idx,Constants.CONSTANT_Integer); + return c.getBytes(); + } + + public String toString() { + return stringifyValue(); + } + + // Whatever kind of value it is, return it as a string + public String stringifyValue() { + switch (type) { + case PRIMITIVE_INT: + ConstantInteger c = (ConstantInteger)cpool.getConstant(idx,Constants.CONSTANT_Integer); + return Integer.toString(c.getBytes()); + case PRIMITIVE_LONG: + ConstantLong j = (ConstantLong)cpool.getConstant(idx,Constants.CONSTANT_Long); + return Long.toString(j.getBytes()); + case PRIMITIVE_DOUBLE: + ConstantDouble d = (ConstantDouble)cpool.getConstant(idx,Constants.CONSTANT_Double); + return Double.toString(d.getBytes()); + case PRIMITIVE_FLOAT: + ConstantFloat f = (ConstantFloat)cpool.getConstant(idx,Constants.CONSTANT_Float); + return Float.toString(f.getBytes()); + case PRIMITIVE_SHORT: + ConstantInteger s = (ConstantInteger)cpool.getConstant(idx,Constants.CONSTANT_Integer); + return Integer.toString(s.getBytes()); + case PRIMITIVE_BYTE: + ConstantInteger b = (ConstantInteger)cpool.getConstant(idx,Constants.CONSTANT_Integer); + return Integer.toString(b.getBytes()); + case PRIMITIVE_CHAR: + ConstantInteger ch = (ConstantInteger)cpool.getConstant(idx,Constants.CONSTANT_Integer); + return Integer.toString(ch.getBytes()); + case PRIMITIVE_BOOLEAN: + ConstantInteger bo = (ConstantInteger)cpool.getConstant(idx,Constants.CONSTANT_Integer); + if (bo.getBytes() == 0) return "false"; + if (bo.getBytes() != 0) return "true"; + case STRING: + ConstantUtf8 cu8 = (ConstantUtf8)cpool.getConstant(idx,Constants.CONSTANT_Utf8); + return cu8.getBytes(); + + default: + throw new RuntimeException("SimpleElementValue class does not know how to stringify type "+type); + } + } + + public void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 kind of value + switch (type) { + case PRIMITIVE_INT: + case PRIMITIVE_BYTE: + case PRIMITIVE_CHAR: + case PRIMITIVE_FLOAT: + case PRIMITIVE_LONG: + case PRIMITIVE_BOOLEAN: + case PRIMITIVE_SHORT: + case PRIMITIVE_DOUBLE: + case STRING: + dos.writeShort(idx); + break; + default: + throw new RuntimeException("SimpleElementValue doesnt know how to write out type "+type); + } + } + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/AnnotationElementValueGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/AnnotationElementValueGen.java new file mode 100644 index 000000000..0ddee226f --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/AnnotationElementValueGen.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 Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement - initial implementation {date} + * ******************************************************************/ + +package org.aspectj.apache.bcel.generic.annotation; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.classfile.annotation.AnnotationElementValue; +import org.aspectj.apache.bcel.generic.ConstantPoolGen; + + +public class AnnotationElementValueGen extends ElementValueGen { + + // For annotation element values, this is the annotation + private AnnotationGen a; + + public AnnotationElementValueGen(AnnotationGen a,ConstantPoolGen cpool) { + super(ANNOTATION,cpool); + this.a = a; + } + + public AnnotationElementValueGen(int type, AnnotationGen annotation, ConstantPoolGen 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(AnnotationElementValue value, ConstantPoolGen cpool) { + super(ANNOTATION,cpool); + a = new AnnotationGen(value.getAnnotation(),cpool); + } + + 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"); + } + + public AnnotationGen getAnnotation() { return a;} + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/AnnotationGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/AnnotationGen.java new file mode 100644 index 000000000..94236bc36 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/AnnotationGen.java @@ -0,0 +1,148 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM Corporation + * + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement initial implementation + * ******************************************************************/ +package org.aspectj.apache.bcel.generic.annotation; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.aspectj.apache.bcel.classfile.ConstantUtf8; +import org.aspectj.apache.bcel.classfile.annotation.Annotation; +import org.aspectj.apache.bcel.classfile.annotation.ElementNameValuePair; +import org.aspectj.apache.bcel.generic.ConstantPoolGen; +import org.aspectj.apache.bcel.generic.ObjectType; + +public class AnnotationGen { + private int typeIndex; + private List /* ElementNameValuePairGen */ evs; + private ConstantPoolGen cpool; + private boolean isRuntimeVisible = false; + + /** + * Here we are taking a fixed annotation of type Annotation and building a + * modifiable AnnotationGen object. The ConstantPool entries should already + * be correct, we could assert that ... + * We need to copy the type and the element name value pairs and the visibility. + */ + public AnnotationGen(Annotation a,ConstantPoolGen cpool) { + this.cpool = cpool; + // Could assert a.getTypeSignature() == (ConstantUtf8)cpool.getConstant(a.getTypeIndex())).getBytes() + typeIndex = a.getTypeIndex(); + + isRuntimeVisible = a.isRuntimeVisible(); + + evs = copyValues(a.getValues(),cpool); + } + + private List copyValues(List in,ConstantPoolGen cpool) { + List out = new ArrayList(); + for (Iterator iter = in.iterator(); iter.hasNext();) { + ElementNameValuePair nvp = (ElementNameValuePair) iter.next(); + out.add(new ElementNameValuePairGen(nvp,cpool)); + } + return out; + } + + private AnnotationGen(ConstantPoolGen cpool) { + this.cpool = cpool; + } + + public AnnotationGen(ObjectType type,List /*ElementNameValuePairGen*/ elements,boolean vis,ConstantPoolGen cpool) { + this.cpool = cpool; + this.typeIndex = cpool.addUtf8(type.getSignature()); + evs = elements; + isRuntimeVisible = vis; + } + + public static AnnotationGen read(DataInputStream dis,ConstantPoolGen cpool,boolean b) throws IOException { + AnnotationGen a = new AnnotationGen(cpool); + a.typeIndex = dis.readUnsignedShort(); + 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.isRuntimeVisible(b); + return a; + } + + public void dump(DataOutputStream dos) throws IOException { + dos.writeShort(typeIndex); // u2 index of type name in cpool + dos.writeShort(evs.size()); // u2 element_value pair count + for (int i = 0 ; i<evs.size();i++) { + ElementNameValuePairGen envp = (ElementNameValuePairGen) evs.get(i); + envp.dump(dos); + } + } + + public void addElementNameValuePair(ElementNameValuePairGen evp) { + if (evs == null) evs = new ArrayList(); + evs.add(evp); + } + + + public int getTypeIndex() { + return typeIndex; + } + + public final String getTypeSignature() { +// ConstantClass c = (ConstantClass)cpool.getConstant(typeIndex); + ConstantUtf8 utf8 = (ConstantUtf8)cpool.getConstant(typeIndex/*c.getNameIndex()*/); + return utf8.getBytes(); + } + + public final String getTypeName() { + return getTypeSignature();// BCELBUG: Should I use this instead? Utility.signatureToString(getTypeSignature()); + } + + /** + * Returns list of ElementNameValuePair objects + */ + public List getValues() { + return evs; + } + + public String toString() { + StringBuffer s = new StringBuffer(); + s.append("AnnotationGen:["+getTypeName()+" #"+evs.size()+" {"); + for (int i = 0; i<evs.size();i++) { + s.append(evs.get(i)); + if (i+1<evs.size()) s.append(","); + } + s.append("}]"); + return s.toString(); + } + + public String toShortString() { + StringBuffer s = new StringBuffer(); + s.append("@"+getTypeName()+"("); + for (int i = 0; i<evs.size();i++) { + s.append(evs.get(i)); + if (i+1<evs.size()) s.append(","); + } + s.append(")"); + return s.toString(); + } + + private void isRuntimeVisible(boolean b) { + isRuntimeVisible = b; + } + + public boolean isRuntimeVisible() { + return isRuntimeVisible; + } +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ArrayElementValueGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ArrayElementValueGen.java new file mode 100644 index 000000000..f5b176437 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ArrayElementValueGen.java @@ -0,0 +1,87 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement - initial implementation {date} + * ******************************************************************/ + +package org.aspectj.apache.bcel.generic.annotation; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.aspectj.apache.bcel.classfile.annotation.ArrayElementValue; +import org.aspectj.apache.bcel.classfile.annotation.ElementValue; +import org.aspectj.apache.bcel.generic.ConstantPoolGen; + + +public class ArrayElementValueGen extends ElementValueGen { + + // J5TODO: Should we make this an array or a list? A list would be easier to modify ... + private List /*ElementValueGen*/ evalues; + + public ArrayElementValueGen(ConstantPoolGen cp) { + super(ARRAY,cp); + evalues = new ArrayList(); + } + + public ArrayElementValueGen(int type, ElementValueGen[] datums, ConstantPoolGen cpool) { + super(type,cpool); + if (type != ARRAY) + throw new RuntimeException("Only element values of type array can be built with this ctor"); + this.evalues = new ArrayList(); + for (int i = 0; i < datums.length; i++) { + evalues.add(datums[i]); + } + } + + /** + * @param value + * @param cpool + */ + public ArrayElementValueGen(ArrayElementValue value, ConstantPoolGen cpool) { + super(ARRAY,cpool); + evalues = new ArrayList(); + ElementValue[] in = value.getElementValuesArray(); + for (int i = 0; i < in.length; i++) { + evalues.add(ElementValueGen.copy(in[i],cpool)); + } + } + + public void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 type of value (ARRAY == '[') + dos.writeShort(evalues.size()); + for (Iterator iter = evalues.iterator(); iter.hasNext();) { + ElementValueGen element = (ElementValueGen) iter.next(); + element.dump(dos); + } + } + + public String stringifyValue() { + StringBuffer sb = new StringBuffer(); + sb.append("["); + for (Iterator iter = evalues.iterator(); iter.hasNext();) { + ElementValueGen element = (ElementValueGen) iter.next(); + sb.append(element.stringifyValue()); + if (iter.hasNext()) sb.append(","); + } + sb.append("]"); + return sb.toString(); + } + + public List getElementValues() { return evalues;} + public int getElementValuesSize() { return evalues.size();} + + public void addElement(ElementValueGen gen) { + evalues.add(gen); + } + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ClassElementValueGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ClassElementValueGen.java new file mode 100644 index 000000000..4639e6b8b --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ClassElementValueGen.java @@ -0,0 +1,59 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement - initial implementation {date} + * ******************************************************************/ + +package org.aspectj.apache.bcel.generic.annotation; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.classfile.ConstantClass; +import org.aspectj.apache.bcel.classfile.ConstantUtf8; +import org.aspectj.apache.bcel.generic.ConstantPoolGen; +import org.aspectj.apache.bcel.generic.ObjectType; + + +public class ClassElementValueGen extends ElementValueGen { + + // For primitive types and string type, this points to the value entry in the cpool + // For 'class' this points to the class entry in the cpool + private int idx; + + protected ClassElementValueGen(int typeIdx,ConstantPoolGen cpool) { + super(ElementValueGen.CLASS,cpool); + this.idx = typeIdx; + } + + public ClassElementValueGen(ObjectType t,ConstantPoolGen cpool) { + super(ElementValueGen.CLASS,cpool); + this.idx = cpool.addClass(t); + } + + public int getIndex() { + return idx; + } + + public String getClassString() { + ConstantClass c = (ConstantClass)getConstantPool().getConstant(idx); + ConstantUtf8 utf8 = (ConstantUtf8)getConstantPool().getConstant(c.getNameIndex()); + return utf8.getBytes(); + } + + public String stringifyValue() { + return getClassString(); + } + + public void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 kind of value + dos.writeShort(idx); + } + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ElementNameValuePairGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ElementNameValuePairGen.java new file mode 100644 index 000000000..23b84e6b7 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ElementNameValuePairGen.java @@ -0,0 +1,70 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement - initial implementation + * ******************************************************************/ + +package org.aspectj.apache.bcel.generic.annotation; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.classfile.ConstantUtf8; +import org.aspectj.apache.bcel.classfile.annotation.ElementNameValuePair; +import org.aspectj.apache.bcel.generic.ConstantPoolGen; + + +public class ElementNameValuePairGen { + private int nameIdx; + private ElementValueGen value; + private ConstantPoolGen cpool; + + public ElementNameValuePairGen(ElementNameValuePair nvp, ConstantPoolGen cpool) { + this.cpool = cpool; + // J5ASSERT: + // Could assert nvp.getNameString() points to the same thing as cpool.getConstant(nvp.getNameIndex()) + nameIdx = nvp.getNameIndex(); + value = ElementValueGen.copy(nvp.getValue(),cpool); + } + + + protected ElementNameValuePairGen(int idx,ElementValueGen value,ConstantPoolGen cpool) { + this.nameIdx = idx; + this.value = value; + this.cpool = cpool; + } + + public ElementNameValuePairGen(String name,ElementValueGen value,ConstantPoolGen cpool) { + this.nameIdx = cpool.addUtf8(name); + this.value = value; + this.cpool = cpool; + } + + protected void dump(DataOutputStream dos) throws IOException { + dos.writeShort(nameIdx); // u2 name of the element + value.dump(dos); + } + + public int getNameIndex() { + return nameIdx; + } + + public final String getNameString() { +// ConstantString cu8 = (ConstantString)cpool.getConstant(nameIdx); + return ((ConstantUtf8)cpool.getConstant(nameIdx)).getBytes(); + } + + public final ElementValueGen getValue() { + return value; + } + + public String toString() { + return "ElementNameValuePair:["+getNameString()+"="+value.stringifyValue()+"]"; + } +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ElementValueGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ElementValueGen.java new file mode 100644 index 000000000..b97afc8e3 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ElementValueGen.java @@ -0,0 +1,137 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement - initial implementation {date} + * ******************************************************************/ + +package org.aspectj.apache.bcel.generic.annotation; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.classfile.annotation.AnnotationElementValue; +import org.aspectj.apache.bcel.classfile.annotation.ArrayElementValue; +import org.aspectj.apache.bcel.classfile.annotation.ElementValue; +import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue; +import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue; +import org.aspectj.apache.bcel.generic.ConstantPoolGen; + +public abstract class ElementValueGen { + + protected int type; + protected ConstantPoolGen cpGen; + + + protected ElementValueGen(int type,ConstantPoolGen cpGen) { + this.type = type; + this.cpGen = cpGen; + } + + public int getElementValueType() { + return type; + } + + public abstract String stringifyValue(); + public abstract void dump(DataOutputStream dos) throws IOException; + + public static final int STRING = 's'; + public static final int ENUM_CONSTANT = 'e'; + public static final int CLASS = 'c'; + public static final int ANNOTATION = '@'; + public static final int ARRAY = '['; + + public static final int PRIMITIVE_INT = 'I'; + public static final int PRIMITIVE_BYTE = 'B'; + public static final int PRIMITIVE_CHAR = 'C'; + public static final int PRIMITIVE_DOUBLE = 'D'; + public static final int PRIMITIVE_FLOAT = 'F'; + public static final int PRIMITIVE_LONG = 'J'; + public static final int PRIMITIVE_SHORT = 'S'; + public static final int PRIMITIVE_BOOLEAN= 'Z'; + + public static ElementValueGen readElementValue(DataInputStream dis,ConstantPoolGen cpGen) throws IOException { + int type= dis.readUnsignedByte(); + switch (type) { + case 'B': // byte + return new SimpleElementValueGen(PRIMITIVE_BYTE,dis.readUnsignedShort(),cpGen); + case 'C': // char + return new SimpleElementValueGen(PRIMITIVE_CHAR,dis.readUnsignedShort(),cpGen); + case 'D': // double + return new SimpleElementValueGen(PRIMITIVE_DOUBLE,dis.readUnsignedShort(),cpGen); + case 'F': // float + return new SimpleElementValueGen(PRIMITIVE_FLOAT,dis.readUnsignedShort(),cpGen); + case 'I': // int + return new SimpleElementValueGen(PRIMITIVE_INT,dis.readUnsignedShort(),cpGen); + case 'J': // long + return new SimpleElementValueGen(PRIMITIVE_LONG,dis.readUnsignedShort(),cpGen); + case 'S': // short + return new SimpleElementValueGen(PRIMITIVE_SHORT,dis.readUnsignedShort(),cpGen); + case 'Z': // boolean + return new SimpleElementValueGen(PRIMITIVE_BOOLEAN,dis.readUnsignedShort(),cpGen); + case 's': // String + return new SimpleElementValueGen(STRING,dis.readUnsignedShort(),cpGen); + + case 'e': // Enum constant + return new EnumElementValueGen(dis.readUnsignedShort(),dis.readUnsignedShort(),cpGen); + + case 'c': // Class + return new ClassElementValueGen(dis.readUnsignedShort(),cpGen); +// +// case '@': // Annotation +// return new AnnotationElementValueGen(ANNOTATION,Annotation.read(dis,cpGen),cpGen); +// +// case '[': // Array +// int numArrayVals = dis.readUnsignedShort(); +// List arrayVals = new ArrayList(); +// ElementValue[] evalues = new ElementValue[numArrayVals]; +// for (int j=0;j<numArrayVals;j++) { +// evalues[j] = ElementValue.readElementValue(dis,cpGen); +// } +// return new ArrayElementValue(ARRAY,evalues,cpGen); + + default: + throw new RuntimeException("Unexpected element value kind in annotation: "+type); + } + } + + protected ConstantPoolGen getConstantPool() { + return cpGen; + } + + /** + * Creates an (modifiable) ElementValueGen copy of an (immutable) ElementValue - constant pool is assumed correct. + */ + public static ElementValueGen copy(ElementValue value,ConstantPoolGen cpool) { + switch (value.getElementValueType()) { + case 'B': // byte + case 'C': // char + case 'D': // double + case 'F': // float + case 'I': // int + case 'J': // long + case 'S': // short + case 'Z': // boolean + case 's': // String + return new SimpleElementValueGen((SimpleElementValue)value,cpool); + + case 'e': // Enum constant + return new EnumElementValueGen((EnumElementValue)value,cpool); + + case '@': // Annotation + return new AnnotationElementValueGen((AnnotationElementValue)value,cpool); + + case '[': // Array + return new ArrayElementValueGen((ArrayElementValue)value,cpool); + + default: + throw new RuntimeException("Not implemented yet! ("+value.getElementValueType()+")"); + } + } +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/EnumElementValueGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/EnumElementValueGen.java new file mode 100644 index 000000000..f30283601 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/EnumElementValueGen.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 Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement - initial implementation {date} + * ******************************************************************/ + +package org.aspectj.apache.bcel.generic.annotation; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.classfile.ConstantClass; +import org.aspectj.apache.bcel.classfile.ConstantString; +import org.aspectj.apache.bcel.classfile.ConstantUtf8; +import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue; +import org.aspectj.apache.bcel.generic.ConstantPoolGen; +import org.aspectj.apache.bcel.generic.ObjectType; + + +public class EnumElementValueGen extends ElementValueGen { + + // For enum types, these two indices point to the type and value + private int typeIdx; + private int valueIdx; + + /** + * This ctor assumes the constant pool already contains the right type and value - + * as indicated by typeIdx and valueIdx. This ctor is used for deserialization + */ + protected EnumElementValueGen(int typeIdx,int valueIdx,ConstantPoolGen cpool) { + super(ElementValueGen.ENUM_CONSTANT,cpool); + if (type != ENUM_CONSTANT) + throw new RuntimeException("Only element values of type enum can be built with this ctor"); + this.typeIdx = typeIdx; + this.valueIdx= valueIdx; + } + + public EnumElementValueGen(ObjectType t,String value,ConstantPoolGen cpool) { + super(ElementValueGen.ENUM_CONSTANT,cpool); + typeIdx = cpool.addClass(t); + valueIdx= cpool.addString(value); + } + + public EnumElementValueGen(EnumElementValue value, ConstantPoolGen cpool) { + super(ENUM_CONSTANT,cpool); + typeIdx = value.getTypeIndex(); + valueIdx = value.getValueIndex(); + } + + public void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 type of value (ENUM_CONSTANT == 'e') + dos.writeShort(typeIdx); // u2 + dos.writeShort(valueIdx); // u2 + } + + public String stringifyValue() { + ConstantString cu8 = (ConstantString)getConstantPool().getConstant(valueIdx); + return ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes(); + } + + + // BCELBUG: Should we need to call utility.signatureToString() on the output here? + public String getEnumTypeString() { + ConstantClass cu8 = (ConstantClass)getConstantPool().getConstant(typeIdx); + return ((ConstantUtf8)getConstantPool().getConstant(cu8.getNameIndex())).getBytes(); + // return Utility.signatureToString(cu8.getBytes()); + } + + public String getEnumValueString() { + ConstantString cu8 = (ConstantString)getConstantPool().getConstant(valueIdx); + return ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes(); + } + + public int getValueIndex() { return valueIdx;} + public int getTypeIndex() { return typeIdx; } + + +} diff --git a/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/SimpleElementValueGen.java b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/SimpleElementValueGen.java new file mode 100644 index 000000000..49e5c57d2 --- /dev/null +++ b/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/SimpleElementValueGen.java @@ -0,0 +1,173 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Andy Clement - initial implementation {date} + * ******************************************************************/ + +package org.aspectj.apache.bcel.generic.annotation; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.aspectj.apache.bcel.classfile.ConstantDouble; +import org.aspectj.apache.bcel.classfile.ConstantFloat; +import org.aspectj.apache.bcel.classfile.ConstantInteger; +import org.aspectj.apache.bcel.classfile.ConstantLong; +import org.aspectj.apache.bcel.classfile.ConstantUtf8; +import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue; +import org.aspectj.apache.bcel.generic.ConstantPoolGen; + + +public class SimpleElementValueGen extends ElementValueGen { + + // For primitive types and string type, this points to the value entry in the cpGen + // For 'class' this points to the class entry in the cpGen + private int idx; + + + // ctors for each supported type... type could be inferred but for now lets + // force it to be passed + + /** + * Protected ctor used for deserialization, doesn't *put* an entry in the constant pool, + * assumes the one at the supplied index is correct. + */ + protected SimpleElementValueGen(int type,int idx,ConstantPoolGen cpGen) { + super(type,cpGen); + this.idx = idx; + } + + public SimpleElementValueGen(int type,ConstantPoolGen cpGen,int value) { + super(type,cpGen); + idx = cpGen.addInteger(value); + } + + public SimpleElementValueGen(int type,ConstantPoolGen cpGen,long value) { + super(type,cpGen); + idx = cpGen.addLong(value); + } + + public SimpleElementValueGen(int type,ConstantPoolGen cpGen,double value) { + super(type,cpGen); + idx = cpGen.addDouble(value); + } + + public SimpleElementValueGen(int type,ConstantPoolGen cpGen,float value) { + super(type,cpGen); + idx = cpGen.addFloat(value); + } + + public SimpleElementValueGen(int type,ConstantPoolGen cpGen,short value) { + super(type,cpGen); + idx = cpGen.addInteger(value); + } + + public SimpleElementValueGen(int type,ConstantPoolGen cpGen,byte value) { + super(type,cpGen); + idx = cpGen.addInteger(value); + } + + public SimpleElementValueGen(int type,ConstantPoolGen cpGen,char value) { + super(type,cpGen); + idx = cpGen.addInteger(value); + } + + public SimpleElementValueGen(int type,ConstantPoolGen cpGen,boolean value) { + super(type,cpGen); + if (value) idx = cpGen.addInteger(1); + else idx = cpGen.addInteger(0); + } + + public SimpleElementValueGen(int type,ConstantPoolGen cpGen,String value) { + super(type,cpGen); + idx = cpGen.addUtf8(value); + } + + public SimpleElementValueGen(SimpleElementValue value,ConstantPoolGen cpool) { + super(value.getElementValueType(),cpool); + // J5ASSERT: Could assert value.stringifyValue() is the same as + // cpool.getConstant(SimpleElementValuevalue.getIndex()) + idx = value.getIndex(); + } + + public int getIndex() { + return idx; + } + + public String getValueString() { + if (type != STRING) + throw new RuntimeException("Dont call getValueString() on a non STRING ElementValue"); + ConstantUtf8 c = (ConstantUtf8)cpGen.getConstant(idx); + return c.getBytes(); + } + + public int getValueInt() { + if (type != PRIMITIVE_INT) + throw new RuntimeException("Dont call getValueString() on a non STRING ElementValue"); + ConstantInteger c = (ConstantInteger)cpGen.getConstant(idx); + return c.getBytes(); + } + + // Whatever kind of value it is, return it as a string + public String stringifyValue() { + switch (type) { + case PRIMITIVE_INT: + ConstantInteger c = (ConstantInteger)cpGen.getConstant(idx); + return Integer.toString(c.getBytes()); + case PRIMITIVE_LONG: + ConstantLong j = (ConstantLong)cpGen.getConstant(idx); + return Long.toString(j.getBytes()); + case PRIMITIVE_DOUBLE: + ConstantDouble d = (ConstantDouble)cpGen.getConstant(idx); + return Double.toString(d.getBytes()); + case PRIMITIVE_FLOAT: + ConstantFloat f = (ConstantFloat)cpGen.getConstant(idx); + return Float.toString(f.getBytes()); + case PRIMITIVE_SHORT: + ConstantInteger s = (ConstantInteger)cpGen.getConstant(idx); + return Integer.toString(s.getBytes()); + case PRIMITIVE_BYTE: + ConstantInteger b = (ConstantInteger)cpGen.getConstant(idx); + return Integer.toString(b.getBytes()); + case PRIMITIVE_CHAR: + ConstantInteger ch = (ConstantInteger)cpGen.getConstant(idx); + return Integer.toString(ch.getBytes()); + case PRIMITIVE_BOOLEAN: + ConstantInteger bo = (ConstantInteger)cpGen.getConstant(idx); + if (bo.getBytes() == 0) return "false"; + if (bo.getBytes() != 0) return "true"; + case STRING: + ConstantUtf8 cu8 = (ConstantUtf8)cpGen.getConstant(idx); + return cu8.getBytes(); + + default: + throw new RuntimeException("SimpleElementValueGen class does not know how to stringify type "+type); + } + } + + public void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 kind of value + switch (type) { + case PRIMITIVE_INT: + case PRIMITIVE_BYTE: + case PRIMITIVE_CHAR: + case PRIMITIVE_FLOAT: + case PRIMITIVE_LONG: + case PRIMITIVE_BOOLEAN: + case PRIMITIVE_SHORT: + case PRIMITIVE_DOUBLE: + case STRING: + dos.writeShort(idx); + break; + default: + throw new RuntimeException("SimpleElementValueGen doesnt know how to write out type "+type); + } + } + +} |