diff options
author | aclement <aclement> | 2011-11-17 17:45:00 +0000 |
---|---|---|
committer | aclement <aclement> | 2011-11-17 17:45:00 +0000 |
commit | e8a2793d601e0ee47f1f7a491ec657aad5add5de (patch) | |
tree | d9aac75c390e13336e832f2221fe290198fb33a3 /bcel-builder | |
parent | 942da06faacec35b0aab76eb2729e55b174e3c97 (diff) | |
download | aspectj-e8a2793d601e0ee47f1f7a491ec657aad5add5de.tar.gz aspectj-e8a2793d601e0ee47f1f7a491ec657aad5add5de.zip |
missing impl for annotation value handling, time to fill it in
Diffstat (limited to 'bcel-builder')
2 files changed, 131 insertions, 111 deletions
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 index 1b2d4023d..2e6024580 100644 --- a/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValue.java +++ b/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValue.java @@ -14,8 +14,11 @@ package org.aspectj.apache.bcel.classfile.annotation; import java.io.DataOutputStream; import java.io.IOException; +import java.util.List; +import org.aspectj.apache.bcel.Constants; import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.ConstantUtf8; public class AnnotationElementValue extends ElementValue { @@ -45,7 +48,22 @@ public class AnnotationElementValue extends ElementValue { @Override public String stringifyValue() { - throw new RuntimeException("Not implemented yet"); + StringBuffer sb = new StringBuffer(); + ConstantUtf8 cu8 = (ConstantUtf8) cpool.getConstant(a.getTypeIndex(), Constants.CONSTANT_Utf8); + sb.append(cu8.getValue()); + // haven't really tested this values section: + List<NameValuePair> pairs = a.getValues(); + if (pairs != null && pairs.size() > 0) { + sb.append("("); + for (int p = 0; p < pairs.size(); p++) { + if (p > 0) { + sb.append(","); + } + sb.append(pairs.get(p).getNameString()).append("=").append(pairs.get(p).getValue().stringifyValue()); + } + sb.append(")"); + } + return sb.toString(); } public AnnotationGen getAnnotation() { diff --git a/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/ElementValueGenTest.java b/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/ElementValueGenTest.java index 0a9f415aa..7d91bd7d3 100644 --- a/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/ElementValueGenTest.java +++ b/bcel-builder/testsrc/org/aspectj/apache/bcel/classfile/tests/ElementValueGenTest.java @@ -14,14 +14,18 @@ import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Collections; import org.aspectj.apache.bcel.Constants; -import org.aspectj.apache.bcel.generic.ClassGen; import org.aspectj.apache.bcel.classfile.ConstantPool; +import org.aspectj.apache.bcel.classfile.annotation.AnnotationElementValue; +import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen; import org.aspectj.apache.bcel.classfile.annotation.ClassElementValue; import org.aspectj.apache.bcel.classfile.annotation.ElementValue; import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue; +import org.aspectj.apache.bcel.classfile.annotation.NameValuePair; import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue; +import org.aspectj.apache.bcel.generic.ClassGen; import org.aspectj.apache.bcel.generic.ObjectType; public class ElementValueGenTest extends BcelTestCase { @@ -31,108 +35,101 @@ public class ElementValueGenTest extends BcelTestCase { } private ClassGen createClassGen(String classname) { - return new ClassGen(classname, "java.lang.Object", - "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null); + return new ClassGen(classname, "java.lang.Object", "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null); } - //// + // // // Create primitive element values public void testCreateIntegerElementValue() { ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - - SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_INT,cp,555); + + SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_INT, cp, 555); // Creation of an element like that should leave a new entry in the cpool - assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+cp.lookupInteger(555), - evg.getIndex()==cp.lookupInteger(555)); - checkSerialize(evg,cp); + assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + cp.lookupInteger(555), + evg.getIndex() == cp.lookupInteger(555)); + checkSerialize(evg, cp); } public void testCreateFloatElementValue() { ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - - SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_FLOAT,cp,111.222f); + + SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_FLOAT, cp, 111.222f); // Creation of an element like that should leave a new entry in the cpool - assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+cp.lookupFloat(111.222f), - evg.getIndex()==cp.lookupFloat(111.222f)); - checkSerialize(evg,cp); + assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + cp.lookupFloat(111.222f), + evg.getIndex() == cp.lookupFloat(111.222f)); + checkSerialize(evg, cp); } - + public void testCreateDoubleElementValue() { ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - - SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_DOUBLE,cp,333.44); + + SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_DOUBLE, cp, 333.44); // Creation of an element like that should leave a new entry in the cpool int idx = cp.lookupDouble(333.44); - assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx, - evg.getIndex()==idx); - checkSerialize(evg,cp); + assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); } - + public void testCreateLongElementValue() { ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - - SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_LONG,cp,3334455L); + + SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_LONG, cp, 3334455L); // Creation of an element like that should leave a new entry in the cpool int idx = cp.lookupLong(3334455L); - assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx, - evg.getIndex()==idx); - checkSerialize(evg,cp); + assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); } public void testCreateCharElementValue() { ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - - SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_CHAR,cp,(char)'t'); + + SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_CHAR, cp, (char) 't'); // Creation of an element like that should leave a new entry in the cpool - int idx = cp.lookupInteger((char)'t'); - assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx, - evg.getIndex()==idx); - checkSerialize(evg,cp); + int idx = cp.lookupInteger((char) 't'); + assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); } - + public void testCreateByteElementValue() { ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - - SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_CHAR,cp,(byte)'z'); + + SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_CHAR, cp, (byte) 'z'); // Creation of an element like that should leave a new entry in the cpool - int idx = cp.lookupInteger((byte)'z'); - assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx, - evg.getIndex()==idx); - checkSerialize(evg,cp); + int idx = cp.lookupInteger((byte) 'z'); + assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); } public void testCreateBooleanElementValue() { ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - - SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_BOOLEAN,cp,true); + + SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_BOOLEAN, cp, true); // Creation of an element like that should leave a new entry in the cpool int idx = cp.lookupInteger(1); // 1 == true - assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx, - evg.getIndex()==idx); - checkSerialize(evg,cp); + assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); } public void testCreateShortElementValue() { ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - - SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_SHORT,cp,(short)42); + + SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_SHORT, cp, (short) 42); // Creation of an element like that should leave a new entry in the cpool - int idx = cp.lookupInteger(42); - assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx, - evg.getIndex()==idx); - checkSerialize(evg,cp); + int idx = cp.lookupInteger(42); + assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); } - - //// + + // // // Create string element values public void testCreateStringElementValue() { @@ -140,85 +137,90 @@ public class ElementValueGenTest extends BcelTestCase { // Create HelloWorld ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - - SimpleElementValue evg = new SimpleElementValue(ElementValue.STRING,cp,"hello"); + + SimpleElementValue evg = new SimpleElementValue(ElementValue.STRING, cp, "hello"); // Creation of an element like that should leave a new entry in the cpool - assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+cp.lookupUtf8("hello"), - evg.getIndex()==cp.lookupUtf8("hello")); - checkSerialize(evg,cp); + assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + cp.lookupUtf8("hello"), + evg.getIndex() == cp.lookupUtf8("hello")); + checkSerialize(evg, cp); } - - //// + + // // // Create enum element value - + public void testCreateEnumElementValue() { ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - ObjectType enumType = new ObjectType("SimpleEnum"); // Supports rainbow :) - - EnumElementValue evg = new EnumElementValue(enumType,"Red",cp); + + EnumElementValue evg = new EnumElementValue(enumType, "Red", cp); // Creation of an element like that should leave a new entry in the cpool - assertTrue("The new ElementValue value index should match the contents of the constantpool but "+ - evg.getValueIndex()+"!="+cp.lookupUtf8("Red"), - evg.getValueIndex()==cp.lookupUtf8("Red")); - //BCELBUG: Should the class signature or class name be in the constant pool? (see note in ConstantPool) -// assertTrue("The new ElementValue type index should match the contents of the constantpool but "+ -// evg.getTypeIndex()+"!="+cp.lookupClass(enumType.getSignature()), -// evg.getTypeIndex()==cp.lookupClass(enumType.getSignature())); - - checkSerialize(evg,cp); - } - - //// + assertTrue("The new ElementValue value index should match the contents of the constantpool but " + evg.getValueIndex() + + "!=" + cp.lookupUtf8("Red"), evg.getValueIndex() == cp.lookupUtf8("Red")); + // BCELBUG: Should the class signature or class name be in the constant pool? (see note in ConstantPool) + // assertTrue("The new ElementValue type index should match the contents of the constantpool but "+ + // evg.getTypeIndex()+"!="+cp.lookupClass(enumType.getSignature()), + // evg.getTypeIndex()==cp.lookupClass(enumType.getSignature())); + + checkSerialize(evg, cp); + } + + public void testCreateMarkerAnnotationElementValue() { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPool cp = cg.getConstantPool(); + ObjectType annoType = new ObjectType("SimpleMarkerAnnotation"); + AnnotationGen annoGen = new AnnotationGen(annoType, Collections.<NameValuePair> emptyList(), true, cp); + AnnotationElementValue evg = new AnnotationElementValue(annoGen, cp); + checkSerialize(evg, cp); + } + + // // // Create class element value - + public void testCreateClassElementValue() { ClassGen cg = createClassGen("HelloWorld"); ConstantPool cp = cg.getConstantPool(); - + ObjectType classType = new ObjectType("java.lang.Integer"); - - ClassElementValue evg = new ClassElementValue(classType,cp); - - assertTrue("Unexpected value for contained class: '"+evg.getClassString()+"'", - evg.getClassString().indexOf("Integer")!=-1); - checkSerialize(evg,cp); + ClassElementValue evg = new ClassElementValue(classType, cp); + + assertTrue("Unexpected value for contained class: '" + evg.getClassString() + "'", + evg.getClassString().indexOf("Integer") != -1); + + checkSerialize(evg, cp); } - - - //// + + // // // Helper methods - - private void checkSerialize(ElementValue evgBefore,ConstantPool cpg) { + + private void checkSerialize(ElementValue evgBefore, ConstantPool cpg) { try { - String beforeValue = evgBefore.stringifyValue(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DataOutputStream dos = new DataOutputStream(baos); - evgBefore.dump(dos); - dos.flush(); - dos.close(); - - byte[] bs = baos.toByteArray(); - - ByteArrayInputStream bais = new ByteArrayInputStream(bs); - DataInputStream dis = new DataInputStream(bais); - ElementValue evgAfter = ElementValue.readElementValue(dis,cpg); - - dis.close(); - String afterValue = evgAfter.stringifyValue(); - - if (!beforeValue.equals(afterValue)) { - fail("Deserialization failed: before='"+beforeValue+"' after='"+afterValue+"'"); - } - + String beforeValue = evgBefore.stringifyValue(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + evgBefore.dump(dos); + dos.flush(); + dos.close(); + + byte[] bs = baos.toByteArray(); + + ByteArrayInputStream bais = new ByteArrayInputStream(bs); + DataInputStream dis = new DataInputStream(bais); + ElementValue evgAfter = ElementValue.readElementValue(dis, cpg); + + dis.close(); + String afterValue = evgAfter.stringifyValue(); + + if (!beforeValue.equals(afterValue)) { + fail("Deserialization failed: before='" + beforeValue + "' after='" + afterValue + "'"); + } + } catch (IOException ioe) { - fail("Unexpected exception whilst checking serialization: "+ioe); + fail("Unexpected exception whilst checking serialization: " + ioe); } } - protected void tearDown() throws Exception { super.tearDown(); |