You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-dev@jakarta.apache.org by tc...@apache.org on 2006/08/02 11:39:33 UTC
svn commit: r427943 [1/2] - in /jakarta/bcel/trunk/src:
main/java/org/apache/bcel/classfile/ main/java/org/apache/bcel/generic/
test/java/org/apache/bcel/ test/java/org/apache/bcel/data/
Author: tcurdt
Date: Wed Aug 2 02:39:32 2006
New Revision: 427943
URL: http://svn.apache.org/viewvc?rev=427943&view=rev
Log:
GSoC: latest changes from Dmitriy Khayredinov
Added:
jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationElementValueGen.java (with props)
jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ArrayElementValueGen.java (with props)
jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassElementValueGen.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/AnonymousClassTestCase.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/ElementValueGenTestCase.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/FieldAnnotationsTestCase.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/GeneratingAnnotatedClassesTestCase.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/AnnotatedFields.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/AnnotatedWithCombinedAnnotation.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/AnnotatedWithEnumClass.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/AnnotationEnumElement.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/AnonymousClassTest.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/AttributeTestClassEM01.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/AttributeTestClassEM02.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/CombinedAnnotation.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/ComplexAnnotatedClass.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/ComplexAnnotation.java (with props)
jakarta/bcel/trunk/src/test/java/org/apache/bcel/data/SimpleAnnotatedClass.java (with props)
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
jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/ElementValuePair.java
jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java
jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/RuntimeInvisibleAnnotations.java
jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/RuntimeVisibleAnnotations.java
jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java
jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassGen.java
jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/FieldGen.java
jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java
jakarta/bcel/trunk/src/test/java/org/apache/bcel/AbstractTestCase.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=427943&r1=427942&r2=427943&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 Wed Aug 2 02:39:32 2006
@@ -110,9 +110,14 @@
}
- public void dump(DataOutputStream dos)
+ public void dump(DataOutputStream dos) throws IOException
{
- // TODO Auto-generated method stub
+ 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);
+ }
}
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=427943&r1=427942&r2=427943&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 Wed Aug 2 02:39:32 2006
@@ -17,6 +17,7 @@
package org.apache.bcel.classfile;
import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
/**
@@ -116,4 +117,11 @@
{
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);
+ }
}
Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/ElementValuePair.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/ElementValuePair.java?rev=427943&r1=427942&r2=427943&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/ElementValuePair.java (original)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/ElementValuePair.java Wed Aug 2 02:39:32 2006
@@ -16,6 +16,8 @@
*/
package org.apache.bcel.classfile;
+import java.io.DataOutputStream;
+import java.io.IOException;
import org.apache.bcel.Constants;
/**
@@ -64,5 +66,10 @@
result.append(getNameString()).append("=").append(
getValue().toShortString());
return result.toString();
+ }
+
+ protected void dump(DataOutputStream dos) throws IOException {
+ dos.writeShort(elementNameIndex); // u2 name of the element
+ elementValue.dump(dos);
}
}
Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java?rev=427943&r1=427942&r2=427943&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java (original)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java Wed Aug 2 02:39:32 2006
@@ -235,13 +235,15 @@
if (attribute instanceof Annotations)
{
Annotations annotations = (Annotations) attribute;
- for (int j = 0; i < annotations.getAnnotationEntries().length; i++)
+ for (int j = 0; j < annotations.getAnnotationEntries().length; j++)
+ {
accumulatedAnnotations.add(annotations
.getAnnotationEntries()[j]);
+ }
}
}
annotationEntries = (AnnotationEntry[]) accumulatedAnnotations
- .toArray(new AnnotationEntry[] {});
+ .toArray(new AnnotationEntry[accumulatedAnnotations.size()]);
annotationsOutOfDate = false;
}
}
Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/RuntimeInvisibleAnnotations.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/RuntimeInvisibleAnnotations.java?rev=427943&r1=427942&r2=427943&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/RuntimeInvisibleAnnotations.java (original)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/RuntimeInvisibleAnnotations.java Wed Aug 2 02:39:32 2006
@@ -17,36 +17,50 @@
package org.apache.bcel.classfile;
import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.bcel.Constants;
/**
- * represents an annotation that is represented in the class file
- * but is not provided to the JVM.
+ * represents an annotation that is represented in the class file but is not
+ * provided to the JVM.
*
* @version $Id: RuntimeInvisibleAnnotations
- * @author <A HREF="mailto:dbrosius@qis.net">D. Brosius</A>
+ * @author <A HREF="mailto:dbrosius@qis.net">D. Brosius</A>
* @since 5.2
*/
-public class RuntimeInvisibleAnnotations extends Annotations {
+public class RuntimeInvisibleAnnotations extends Annotations
+{
+ /**
+ * @param name_index
+ * Index pointing to the name <em>Code</em>
+ * @param length
+ * Content length in bytes
+ * @param file
+ * Input stream
+ * @param constant_pool
+ * Array of constants
+ */
+ RuntimeInvisibleAnnotations(int name_index, int length,
+ DataInputStream file, ConstantPool constant_pool)
+ throws IOException
+ {
+ super(Constants.ATTR_RUNTIMEIN_VISIBLE_ANNOTATIONS, name_index, length,
+ file, constant_pool, false);
+ }
- /**
- * @param name_index Index pointing to the name <em>Code</em>
- * @param length Content length in bytes
- * @param file Input stream
- * @param constant_pool Array of constants
- */
- RuntimeInvisibleAnnotations(int name_index, int length, DataInputStream file,
- ConstantPool constant_pool) throws IOException {
- super(Constants.ATTR_RUNTIMEIN_VISIBLE_ANNOTATIONS, name_index, length, file, constant_pool, false);
- }
+ /**
+ * @return deep copy of this attribute
+ */
+ public Attribute copy(ConstantPool constant_pool)
+ {
+ Annotations c = (Annotations) clone();
+ return c;
+ }
-
- /**
- * @return deep copy of this attribute
- */
- public Attribute copy( ConstantPool constant_pool ) {
- Annotations c = (Annotations) clone();
- return c;
- }
+ public final void dump(DataOutputStream dos) throws IOException
+ {
+ super.dump(dos);
+ writeAnnotations(dos);
+ }
}
Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/RuntimeVisibleAnnotations.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/RuntimeVisibleAnnotations.java?rev=427943&r1=427942&r2=427943&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/RuntimeVisibleAnnotations.java (original)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/RuntimeVisibleAnnotations.java Wed Aug 2 02:39:32 2006
@@ -17,36 +17,50 @@
package org.apache.bcel.classfile;
import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.bcel.Constants;
/**
- * represents an annotation that is represented in the class file
- * and is provided to the JVM.
+ * represents an annotation that is represented in the class file and is
+ * provided to the JVM.
*
* @version $Id: RuntimeVisibleAnnotations
- * @author <A HREF="mailto:dbrosius@qis.net">D. Brosius</A>
+ * @author <A HREF="mailto:dbrosius@qis.net">D. Brosius</A>
* @since 5.2
*/
-public class RuntimeVisibleAnnotations extends Annotations {
+public class RuntimeVisibleAnnotations extends Annotations
+{
+ /**
+ * @param name_index
+ * Index pointing to the name <em>Code</em>
+ * @param length
+ * Content length in bytes
+ * @param file
+ * Input stream
+ * @param constant_pool
+ * Array of constants
+ */
+ public RuntimeVisibleAnnotations(int name_index, int length,
+ DataInputStream file, ConstantPool constant_pool)
+ throws IOException
+ {
+ super(Constants.ATTR_RUNTIME_VISIBLE_ANNOTATIONS, name_index, length,
+ file, constant_pool, true);
+ }
- /**
- * @param name_index Index pointing to the name <em>Code</em>
- * @param length Content length in bytes
- * @param file Input stream
- * @param constant_pool Array of constants
- */
- public RuntimeVisibleAnnotations(int name_index, int length, DataInputStream file,
- ConstantPool constant_pool) throws IOException {
- super(Constants.ATTR_RUNTIME_VISIBLE_ANNOTATIONS, name_index, length, file, constant_pool, true);
- }
-
-
- /**
- * @return deep copy of this attribute
- */
- public Attribute copy( ConstantPool constant_pool ) {
- Annotations c = (Annotations) clone();
- return c;
- }
+ /**
+ * @return deep copy of this attribute
+ */
+ public Attribute copy(ConstantPool constant_pool)
+ {
+ Annotations c = (Annotations) clone();
+ return c;
+ }
+
+ public final void dump(DataOutputStream dos) throws IOException
+ {
+ super.dump(dos);
+ writeAnnotations(dos);
+ }
}
Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java?rev=427943&r1=427942&r2=427943&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java (original)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/classfile/Utility.java Wed Aug 2 02:39:32 2006
@@ -30,6 +30,7 @@
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.zip.GZIPInputStream;
@@ -1412,4 +1413,112 @@
}
return null;
}
+
+
+ /**
+ * Annotations against a class are stored in one of four attribute kinds:
+ * - RuntimeVisibleParameterAnnotations
+ * - RuntimeInvisibleParameterAnnotations
+ */
+ public static Attribute[] getParameterAnnotationAttributes(
+ ConstantPoolGen cp,
+ List[] /*Array of lists, array size depends on #params */vec)
+ {
+ int visCount[] = new int[vec.length];
+ int totalVisCount = 0;
+ int invisCount[] = new int[vec.length];
+ int totalInvisCount = 0;
+ try
+ {
+ for (int i = 0; i < vec.length; i++)
+ {
+ List l = vec[i];
+ if (l != null)
+ {
+ for (Iterator iter = l.iterator(); iter.hasNext();)
+ {
+ AnnotationEntryGen element = (AnnotationEntryGen) iter.next();
+ if (element.isRuntimeVisible())
+ {
+ visCount[i]++;
+ totalVisCount++;
+ }
+ else
+ {
+ invisCount[i]++;
+ totalInvisCount++;
+ }
+ }
+ }
+ }
+ // Lets do the visible ones
+ ByteArrayOutputStream rvaBytes = new ByteArrayOutputStream();
+ DataOutputStream rvaDos = new DataOutputStream(rvaBytes);
+ rvaDos.writeByte(vec.length); // First goes number of parameters
+ for (int i = 0; i < vec.length; i++)
+ {
+ rvaDos.writeShort(visCount[i]);
+ if (visCount[i] > 0)
+ {
+ List l = vec[i];
+ for (Iterator iter = l.iterator(); iter.hasNext();)
+ {
+ AnnotationEntryGen element = (AnnotationEntryGen) iter.next();
+ if (element.isRuntimeVisible())
+ element.dump(rvaDos);
+ }
+ }
+ }
+ rvaDos.close();
+ // Lets do the invisible ones
+ ByteArrayOutputStream riaBytes = new ByteArrayOutputStream();
+ DataOutputStream riaDos = new DataOutputStream(riaBytes);
+ riaDos.writeByte(vec.length); // First goes number of parameters
+ for (int i = 0; i < vec.length; i++)
+ {
+ riaDos.writeShort(invisCount[i]);
+ if (invisCount[i] > 0)
+ {
+ List l = vec[i];
+ for (Iterator iter = l.iterator(); iter.hasNext();)
+ {
+ AnnotationEntryGen element = (AnnotationEntryGen) iter.next();
+ if (!element.isRuntimeVisible())
+ element.dump(riaDos);
+ }
+ }
+ }
+ riaDos.close();
+ byte[] rvaData = rvaBytes.toByteArray();
+ byte[] riaData = riaBytes.toByteArray();
+ int rvaIndex = -1;
+ int riaIndex = -1;
+ if (totalVisCount > 0)
+ rvaIndex = cp.addUtf8("RuntimeVisibleParameterAnnotations");
+ if (totalInvisCount > 0)
+ riaIndex = cp.addUtf8("RuntimeInvisibleParameterAnnotations");
+ List newAttributes = new ArrayList();
+ if (totalVisCount > 0)
+ {
+ newAttributes
+ .add(new RuntimeVisibleParameterAnnotations(rvaIndex,
+ rvaData.length, new DataInputStream(new ByteArrayInputStream(rvaData)), cp.getConstantPool()));
+ }
+ if (totalInvisCount > 0)
+ {
+ newAttributes
+ .add(new RuntimeInvisibleParameterAnnotations(riaIndex,
+ riaData.length, new DataInputStream(new ByteArrayInputStream(riaData)), cp.getConstantPool()));
+ }
+ return (Attribute[]) newAttributes.toArray(new Attribute[] {});
+ }
+ catch (IOException e)
+ {
+ System.err
+ .println("IOException whilst processing parameter annotations");
+ e.printStackTrace();
+ }
+ return null;
+ }
+
}
Added: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationElementValueGen.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationElementValueGen.java?rev=427943&view=auto
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationElementValueGen.java (added)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationElementValueGen.java Wed Aug 2 02:39:32 2006
@@ -0,0 +1,60 @@
+package org.apache.bcel.generic;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import org.apache.bcel.classfile.AnnotationElementValue;
+import org.apache.bcel.classfile.ElementValue;
+
+public class AnnotationElementValueGen extends ElementValueGen
+{
+ // For annotation element values, this is the annotation
+ private AnnotationEntryGen a;
+
+ public AnnotationElementValueGen(AnnotationEntryGen a, ConstantPoolGen cpool)
+ {
+ super(ANNOTATION, cpool);
+ this.a = a;
+ }
+
+ public AnnotationElementValueGen(int type, AnnotationEntryGen 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, boolean copyPoolEntries)
+ {
+ super(ANNOTATION, cpool);
+ a = new AnnotationEntryGen(value.getAnnotationEntry(), cpool, copyPoolEntries);
+ }
+
+ public void dump(DataOutputStream dos) throws IOException
+ {
+ dos.writeByte(type); // u1 type of value (ANNOTATION == '@')
+ a.dump(dos);
+ }
+
+ public String stringifyValue()
+ {
+ throw new RuntimeException("Not implemented yet");
+ }
+
+ /**
+ * Return immutable variant of this AnnotationElementValueGen
+ */
+ public ElementValue getElementValue()
+ {
+ return new AnnotationElementValue(this.type, a.getAnnotation(), cpGen
+ .getConstantPool());
+ }
+
+ public AnnotationEntryGen getAnnotation()
+ {
+ return a;
+ }
+}
Propchange: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationElementValueGen.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/AnnotationElementValueGen.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ArrayElementValueGen.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ArrayElementValueGen.java?rev=427943&view=auto
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ArrayElementValueGen.java (added)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ArrayElementValueGen.java Wed Aug 2 02:39:32 2006
@@ -0,0 +1,109 @@
+package org.apache.bcel.generic;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.bcel.classfile.ArrayElementValue;
+import org.apache.bcel.classfile.ElementValue;
+
+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, ElementValue[] 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]);
+ }
+ }
+
+ /**
+ * Return immutable variant of this ArrayElementValueGen
+ */
+ public ElementValue getElementValue()
+ {
+ ElementValue[] immutableData = new ElementValue[evalues.size()];
+ int i = 0;
+ for (Iterator iter = evalues.iterator(); iter.hasNext();)
+ {
+ ElementValueGen element = (ElementValueGen) iter.next();
+ immutableData[i++] = element.getElementValue();
+ }
+ return new ArrayElementValue(type, immutableData, cpGen
+ .getConstantPool());
+ }
+
+ /**
+ * @param value
+ * @param cpool
+ */
+ public ArrayElementValueGen(ArrayElementValue value, ConstantPoolGen cpool,
+ boolean copyPoolEntries)
+ {
+ 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, copyPoolEntries));
+ }
+ }
+
+ 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);
+ }
+}
Propchange: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ArrayElementValueGen.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ArrayElementValueGen.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassElementValueGen.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassElementValueGen.java?rev=427943&view=auto
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassElementValueGen.java (added)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassElementValueGen.java Wed Aug 2 02:39:32 2006
@@ -0,0 +1,80 @@
+package org.apache.bcel.generic;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import org.apache.bcel.classfile.ElementValue;
+import org.apache.bcel.classfile.ConstantUtf8;
+import org.apache.bcel.classfile.ClassElementValue;
+import org.apache.bcel.generic.ConstantPoolGen;
+import org.apache.bcel.generic.ObjectType;
+import org.apache.bcel.generic.ElementValueGen;
+
+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);
+ idx = cpool.addUtf8(t.getSignature());
+ }
+
+ /**
+ * Return immutable variant of this ClassElementValueGen
+ */
+ public ElementValue getElementValue()
+ {
+ return new ClassElementValue(type, idx, cpGen.getConstantPool());
+ }
+
+ public ClassElementValueGen(ClassElementValue value, ConstantPoolGen cpool,
+ boolean copyPoolEntries)
+ {
+ super(CLASS, cpool);
+ if (copyPoolEntries)
+ {
+ // idx = cpool.addClass(value.getClassString());
+ idx = cpool.addUtf8(value.getClassString());
+ }
+ else
+ {
+ idx = value.getIndex();
+ }
+ }
+
+ public int getIndex()
+ {
+ return idx;
+ }
+
+ public String getClassString()
+ {
+ ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx);
+ return cu8.getBytes();
+ // 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);
+ }
+}
Propchange: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassElementValueGen.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassElementValueGen.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassGen.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassGen.java?rev=427943&r1=427942&r2=427943&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassGen.java (original)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/ClassGen.java Wed Aug 2 02:39:32 2006
@@ -22,6 +22,7 @@
import org.apache.bcel.Constants;
import org.apache.bcel.classfile.AccessFlags;
import org.apache.bcel.classfile.AnnotationEntry;
+import org.apache.bcel.classfile.Annotations;
import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.ConstantPool;
import org.apache.bcel.classfile.Field;
@@ -169,9 +170,9 @@
{
RuntimeVisibleAnnotations rva = (RuntimeVisibleAnnotations) attr;
AnnotationEntry[] annos = rva.getAnnotationEntries();
- for (int j = 0; i < annos.length; i++)
+ for (int j = 0; j < annos.length; j++)
{
- AnnotationEntry a = annos[i];
+ AnnotationEntry a = annos[j];
annotationGenObjs.add(new AnnotationEntryGen(a,
getConstantPool(), false));
}
@@ -181,9 +182,9 @@
{
RuntimeInvisibleAnnotations ria = (RuntimeInvisibleAnnotations) attr;
AnnotationEntry[] annos = ria.getAnnotationEntries();
- for (int j = 0; i < annos.length; i++)
+ for (int j = 0; j < annos.length; j++)
{
- AnnotationEntry a = annos[i];
+ AnnotationEntry a = annos[j];
annotationGenObjs.add(new AnnotationEntryGen(a,
getConstantPool(), false));
}
Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/FieldGen.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/FieldGen.java?rev=427943&r1=427942&r2=427943&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/FieldGen.java (original)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/FieldGen.java Wed Aug 2 02:39:32 2006
@@ -20,6 +20,8 @@
import java.util.Iterator;
import java.util.List;
import org.apache.bcel.Constants;
+import org.apache.bcel.classfile.AnnotationEntry;
+import org.apache.bcel.classfile.Annotations;
import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.Constant;
import org.apache.bcel.classfile.ConstantObject;
@@ -88,6 +90,13 @@
for (int i = 0; i < attrs.length; i++) {
if (attrs[i] instanceof ConstantValue) {
setValue(((ConstantValue) attrs[i]).getConstantValueIndex());
+ } else if (attrs[i] instanceof Annotations) {
+ Annotations runtimeAnnotations = (Annotations)attrs[i];
+ AnnotationEntry[] annotationEntries = runtimeAnnotations.getAnnotationEntries();
+ for (int j = 0; j < annotationEntries.length; j++) {
+ AnnotationEntry element = annotationEntries[j];
+ addAnnotationEntry(new AnnotationEntryGen(element,cp,false));
+ }
} else {
addAttribute(attrs[i]);
}
@@ -211,9 +220,19 @@
addAttribute(new ConstantValue(cp.addUtf8("ConstantValue"), 2, index, cp
.getConstantPool()));
}
+ addAnnotationsAsAttribute(cp);
return new Field(access_flags, name_index, signature_index, getAttributes(), cp
.getConstantPool());
}
+
+ private void addAnnotationsAsAttribute(ConstantPoolGen cp) {
+ Attribute[] attrs = Utility.getAnnotationAttributes(cp,annotation_vec);
+ if (attrs!=null) {
+ for (int i = 0; i < attrs.length; i++) {
+ addAttribute(attrs[i]);
+ }
+ }
+ }
private int addConstant() {
Modified: jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java?rev=427943&r1=427942&r2=427943&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java (original)
+++ jakarta/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java Wed Aug 2 02:39:32 2006
@@ -22,6 +22,8 @@
import java.util.List;
import java.util.Stack;
import org.apache.bcel.Constants;
+import org.apache.bcel.classfile.AnnotationEntry;
+import org.apache.bcel.classfile.Annotations;
import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.CodeException;
@@ -31,6 +33,9 @@
import org.apache.bcel.classfile.LocalVariable;
import org.apache.bcel.classfile.LocalVariableTable;
import org.apache.bcel.classfile.Method;
+import org.apache.bcel.classfile.ParameterAnnotationEntry;
+import org.apache.bcel.classfile.ParameterAnnotations;
+import org.apache.bcel.classfile.RuntimeVisibleParameterAnnotations;
import org.apache.bcel.classfile.Utility;
import org.apache.bcel.util.BCELComparator;
@@ -64,6 +69,11 @@
private List exception_vec = new ArrayList();
private List throws_vec = new ArrayList();
private List code_attrs_vec = new ArrayList();
+
+ private List[] param_annotations; // Array of lists containing AnnotationGen objects
+ private boolean hasParameterAnnotations = false;
+ private boolean haveUnpackedParameterAnnotations = false;
+
private static BCELComparator _cmp = new BCELComparator() {
public boolean equals( Object o1, Object o2 ) {
@@ -236,6 +246,13 @@
for (int j = 0; j < names.length; j++) {
addException(names[j]);
}
+ } else if (a instanceof Annotations) {
+ Annotations runtimeAnnotations = (Annotations) a;
+ AnnotationEntry[] aes = runtimeAnnotations.getAnnotationEntries();
+ for (int k = 0; k < aes.length; k++) {
+ AnnotationEntry element = aes[k];
+ addAnnotationEntry(new AnnotationEntryGen(element, cp, false));
+ }
} else {
addAttribute(a);
}
@@ -596,6 +613,25 @@
code_attrs_vec.toArray(attributes);
return attributes;
}
+
+ public void addAnnotationsAsAttribute(ConstantPoolGen cp) {
+ Attribute[] attrs = Utility.getAnnotationAttributes(cp,annotation_vec);
+ if (attrs!=null) {
+ for (int i = 0; i < attrs.length; i++) {
+ addAttribute(attrs[i]);
+ }
+ }
+ }
+
+ public void addParameterAnnotationsAsAttribute(ConstantPoolGen cp) {
+ if (!hasParameterAnnotations) return;
+ Attribute[] attrs = Utility.getParameterAnnotationAttributes(cp,param_annotations);
+ if (attrs!=null) {
+ for (int i = 0; i < attrs.length; i++) {
+ addAttribute(attrs[i]);
+ }
+ }
+ }
/**
@@ -649,6 +685,8 @@
max_stack, max_locals, byte_code, c_exc, code_attrs, cp.getConstantPool());
addAttribute(code);
}
+ addAnnotationsAsAttribute(cp);
+ addParameterAnnotationsAsAttribute(cp);
ExceptionTable et = null;
if (throws_vec.size() > 0) {
addAttribute(et = getExceptionTable(cp));
@@ -1044,11 +1082,111 @@
}
return mg;
}
+
+ //J5TODO: Should param_annotations be an array of arrays? Rather than an array of lists, this
+ // is more likely to suggest to the caller it is readonly (which a List does not).
+ /**
+ * Return a list of AnnotationGen objects representing parameter annotations
+ */
+ public List getAnnotationsOnParameter(int i) {
+ ensureExistingParameterAnnotationsUnpacked();
+ if (!hasParameterAnnotations || i>arg_types.length) return null;
+ return param_annotations[i];
+ }
+
+ /**
+ * Goes through the attributes on the method and identifies any that are
+ * RuntimeParameterAnnotations, extracting their contents and storing them
+ * as parameter annotations. There are two kinds of parameter annotation -
+ * visible and invisible. Once they have been unpacked, these attributes are
+ * deleted. (The annotations will be rebuilt as attributes when someone
+ * builds a Method object out of this MethodGen object).
+ */
+ private void ensureExistingParameterAnnotationsUnpacked()
+ {
+ if (haveUnpackedParameterAnnotations)
+ return;
+ // Find attributes that contain parameter annotation data
+ Attribute[] attrs = getAttributes();
+ ParameterAnnotations paramAnnVisAttr = null;
+ ParameterAnnotations paramAnnInvisAttr = null;
+ List accumulatedAnnotations = new ArrayList();
+ for (int i = 0; i < attrs.length; i++)
+ {
+ Attribute attribute = attrs[i];
+ if (attribute instanceof ParameterAnnotations)
+ {
+ // Initialize param_annotations
+ if (!hasParameterAnnotations)
+ {
+ param_annotations = new List[arg_types.length];
+ for (int j = 0; j < arg_types.length; j++)
+ param_annotations[j] = new ArrayList();
+ }
+ hasParameterAnnotations = true;
+ ParameterAnnotations rpa = (ParameterAnnotations) attribute;
+ if (rpa instanceof RuntimeVisibleParameterAnnotations)
+ paramAnnVisAttr = rpa;
+ else
+ paramAnnInvisAttr = rpa;
+ for (int j = 0; j < arg_types.length; j++)
+ {
+ // This returns Annotation[] ...
+ ParameterAnnotationEntry immutableArray = rpa
+ .getParameterAnnotationEntries()[j];
+ // ... which needs transforming into an AnnotationGen[] ...
+ List mutable = makeMutableVersion(immutableArray.getAnnotationEntries());
+ // ... then add these to any we already know about
+ param_annotations[j].addAll(mutable);
+ }
+ }
+ }
+ if (paramAnnVisAttr != null)
+ removeAttribute(paramAnnVisAttr);
+ if (paramAnnInvisAttr != null)
+ removeAttribute(paramAnnInvisAttr);
+ haveUnpackedParameterAnnotations = true;
+ }
+
+ private List /* AnnotationGen */makeMutableVersion(AnnotationEntry[] mutableArray)
+ {
+ List result = new ArrayList();
+ for (int i = 0; i < mutableArray.length; i++)
+ {
+ result.add(new AnnotationEntryGen(mutableArray[i], getConstantPool(),
+ false));
+ }
+ return result;
+ }
+
+ public void addParameterAnnotation(int parameterIndex,
+ AnnotationEntryGen annotation)
+ {
+ ensureExistingParameterAnnotationsUnpacked();
+ if (!hasParameterAnnotations)
+ {
+ param_annotations = new List[arg_types.length];
+ hasParameterAnnotations = true;
+ }
+ List existingAnnotations = param_annotations[parameterIndex];
+ if (existingAnnotations != null)
+ {
+ existingAnnotations.add(annotation);
+ }
+ else
+ {
+ List l = new ArrayList();
+ l.add(annotation);
+ param_annotations[parameterIndex] = l;
+ }
+ }
+
+
/**
- * @return Comparison strategy object
- */
+ * @return Comparison strategy object
+ */
public static BCELComparator getComparator() {
return _cmp;
}
Modified: jakarta/bcel/trunk/src/test/java/org/apache/bcel/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/test/java/org/apache/bcel/AbstractTestCase.java?rev=427943&r1=427942&r2=427943&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/test/java/org/apache/bcel/AbstractTestCase.java (original)
+++ jakarta/bcel/trunk/src/test/java/org/apache/bcel/AbstractTestCase.java Wed Aug 2 02:39:32 2006
@@ -1,6 +1,7 @@
package org.apache.bcel;
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
@@ -66,8 +67,7 @@
public SyntheticRepository createRepos(String cpentry)
{
ClassPath cp = new ClassPath("target" + File.separator + "testdata"
- + File.separator + cpentry + File.pathSeparator
- + System.getProperty("java.class.path"));
+ + File.separator + cpentry + File.separator);
return SyntheticRepository.getInstance(cp);
}
@@ -129,14 +129,17 @@
result.append("]");
return result.toString();
}
-
- protected String dumpAnnotationEntries(AnnotationEntryGen[] as) {
+
+ protected String dumpAnnotationEntries(AnnotationEntryGen[] as)
+ {
StringBuffer result = new StringBuffer();
result.append("[");
- for (int i = 0; i < as.length; i++) {
+ for (int i = 0; i < as.length; i++)
+ {
AnnotationEntryGen annotation = as[i];
result.append(annotation.toShortString());
- if (i+1<as.length) result.append(",");
+ if (i + 1 < as.length)
+ result.append(",");
}
result.append("]");
return result.toString();
@@ -147,8 +150,7 @@
{
SimpleElementValueGen evg = new SimpleElementValueGen(
ElementValueGen.STRING, cp, aFruit);
- ElementValuePairGen nvGen = new ElementValuePairGen("fruit",
- evg, cp);
+ ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp);
ObjectType t = new ObjectType("SimpleStringAnnotation");
List elements = new ArrayList();
elements.add(nvGen);
Added: jakarta/bcel/trunk/src/test/java/org/apache/bcel/AnonymousClassTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/test/java/org/apache/bcel/AnonymousClassTestCase.java?rev=427943&view=auto
==============================================================================
--- jakarta/bcel/trunk/src/test/java/org/apache/bcel/AnonymousClassTestCase.java (added)
+++ jakarta/bcel/trunk/src/test/java/org/apache/bcel/AnonymousClassTestCase.java Wed Aug 2 02:39:32 2006
@@ -0,0 +1,40 @@
+package org.apache.bcel;
+
+import org.apache.bcel.classfile.JavaClass;
+
+public class AnonymousClassTestCase extends AbstractTestCase
+{
+ public void testRegularClassIsNotAnonymous() throws ClassNotFoundException
+ {
+ JavaClass clazz = getTestClass("org.apache.bcel.data.AnonymousClassTest");
+ assertFalse("regular outer classes are not anonymous", clazz
+ .isAnonymous());
+ assertFalse("regular outer classes are not nested", clazz.isNested());
+ }
+
+ public void testNamedInnerClassIsNotAnonymous()
+ throws ClassNotFoundException
+ {
+ JavaClass clazz = getTestClass("org.apache.bcel.data.AnonymousClassTest$X");
+ assertFalse("regular inner classes are not anonymous", clazz
+ .isAnonymous());
+ assertTrue("regular inner classes are nested", clazz.isNested());
+ }
+
+ public void testStaticInnerClassIsNotAnonymous()
+ throws ClassNotFoundException
+ {
+ JavaClass clazz = getTestClass("org.apache.bcel.data.AnonymousClassTest$Y");
+ assertFalse("regular static inner classes are not anonymous", clazz
+ .isAnonymous());
+ assertTrue("regular static inner classes are nested", clazz.isNested());
+ }
+
+ public void testAnonymousInnerClassIsAnonymous()
+ throws ClassNotFoundException
+ {
+ JavaClass clazz = getTestClass("org.apache.bcel.data.AnonymousClassTest$1");
+ assertTrue("anonymous inner classes are anonymous", clazz.isAnonymous());
+ assertTrue("anonymous inner classes are anonymous", clazz.isNested());
+ }
+}
Propchange: jakarta/bcel/trunk/src/test/java/org/apache/bcel/AnonymousClassTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/bcel/trunk/src/test/java/org/apache/bcel/AnonymousClassTestCase.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: jakarta/bcel/trunk/src/test/java/org/apache/bcel/ElementValueGenTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/test/java/org/apache/bcel/ElementValueGenTestCase.java?rev=427943&view=auto
==============================================================================
--- jakarta/bcel/trunk/src/test/java/org/apache/bcel/ElementValueGenTestCase.java (added)
+++ jakarta/bcel/trunk/src/test/java/org/apache/bcel/ElementValueGenTestCase.java Wed Aug 2 02:39:32 2006
@@ -0,0 +1,223 @@
+package org.apache.bcel;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import org.apache.bcel.Constants;
+import org.apache.bcel.generic.ClassGen;
+import org.apache.bcel.generic.ConstantPoolGen;
+import org.apache.bcel.generic.ElementValueGen;
+import org.apache.bcel.generic.SimpleElementValueGen;
+import org.apache.bcel.generic.ObjectType;
+import org.apache.bcel.generic.ClassElementValueGen;
+import org.apache.bcel.generic.EnumElementValueGen;
+
+public class ElementValueGenTestCase extends AbstractTestCase
+{
+ private ClassGen createClassGen(String classname)
+ {
+ 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");
+ ConstantPoolGen cp = cg.getConstantPool();
+ SimpleElementValueGen evg = new SimpleElementValueGen(
+ ElementValueGen.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);
+ }
+
+ public void testCreateFloatElementValue()
+ {
+ ClassGen cg = createClassGen("HelloWorld");
+ ConstantPoolGen cp = cg.getConstantPool();
+ SimpleElementValueGen evg = new SimpleElementValueGen(
+ ElementValueGen.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);
+ }
+
+ public void testCreateDoubleElementValue()
+ {
+ ClassGen cg = createClassGen("HelloWorld");
+ ConstantPoolGen cp = cg.getConstantPool();
+ SimpleElementValueGen evg = new SimpleElementValueGen(
+ ElementValueGen.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);
+ }
+
+ public void testCreateLongElementValue()
+ {
+ ClassGen cg = createClassGen("HelloWorld");
+ ConstantPoolGen cp = cg.getConstantPool();
+ SimpleElementValueGen evg = new SimpleElementValueGen(
+ ElementValueGen.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);
+ }
+
+ public void testCreateCharElementValue()
+ {
+ ClassGen cg = createClassGen("HelloWorld");
+ ConstantPoolGen cp = cg.getConstantPool();
+ SimpleElementValueGen evg = new SimpleElementValueGen(
+ ElementValueGen.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);
+ }
+
+ public void testCreateByteElementValue()
+ {
+ ClassGen cg = createClassGen("HelloWorld");
+ ConstantPoolGen cp = cg.getConstantPool();
+ SimpleElementValueGen evg = new SimpleElementValueGen(
+ ElementValueGen.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);
+ }
+
+ public void testCreateBooleanElementValue()
+ {
+ ClassGen cg = createClassGen("HelloWorld");
+ ConstantPoolGen cp = cg.getConstantPool();
+ SimpleElementValueGen evg = new SimpleElementValueGen(
+ ElementValueGen.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);
+ }
+
+ public void testCreateShortElementValue()
+ {
+ ClassGen cg = createClassGen("HelloWorld");
+ ConstantPoolGen cp = cg.getConstantPool();
+ SimpleElementValueGen evg = new SimpleElementValueGen(
+ ElementValueGen.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);
+ }
+
+ // //
+ // Create string element values
+ public void testCreateStringElementValue()
+ {
+ // Create HelloWorld
+ ClassGen cg = createClassGen("HelloWorld");
+ ConstantPoolGen cp = cg.getConstantPool();
+ SimpleElementValueGen evg = new SimpleElementValueGen(
+ ElementValueGen.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);
+ }
+
+ // //
+ // Create enum element value
+ public void testCreateEnumElementValue()
+ {
+ ClassGen cg = createClassGen("HelloWorld");
+ ConstantPoolGen cp = cg.getConstantPool();
+ ObjectType enumType = new ObjectType("SimpleEnum"); // Supports rainbow
+ // :)
+ EnumElementValueGen evg = new EnumElementValueGen(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);
+ }
+
+ // //
+ // Create class element value
+ public void testCreateClassElementValue()
+ {
+ ClassGen cg = createClassGen("HelloWorld");
+ ConstantPoolGen cp = cg.getConstantPool();
+ ObjectType classType = new ObjectType("java.lang.Integer");
+ ClassElementValueGen evg = new ClassElementValueGen(classType, cp);
+ assertTrue("Unexpected value for contained class: '"
+ + evg.getClassString() + "'", evg.getClassString().indexOf(
+ "Integer") != -1);
+ checkSerialize(evg, cp);
+ }
+
+ private void checkSerialize(ElementValueGen evgBefore, ConstantPoolGen 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);
+ ElementValueGen evgAfter = ElementValueGen.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);
+ }
+ }
+}
Propchange: jakarta/bcel/trunk/src/test/java/org/apache/bcel/ElementValueGenTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/bcel/trunk/src/test/java/org/apache/bcel/ElementValueGenTestCase.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java?rev=427943&view=auto
==============================================================================
--- jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java (added)
+++ jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java Wed Aug 2 02:39:32 2006
@@ -0,0 +1,89 @@
+package org.apache.bcel;
+
+import java.io.File;
+import java.io.IOException;
+import org.apache.bcel.classfile.Attribute;
+import org.apache.bcel.classfile.ConstantPool;
+import org.apache.bcel.classfile.EnclosingMethod;
+import org.apache.bcel.classfile.JavaClass;
+import org.apache.bcel.util.SyntheticRepository;
+
+public class EnclosingMethodAttributeTestCase extends AbstractTestCase
+{
+ /**
+ * Verify for an inner class declared inside the 'main' method that the
+ * enclosing method attribute is set correctly.
+ */
+ public void testCheckMethodLevelNamedInnerClass()
+ throws ClassNotFoundException
+ {
+ JavaClass clazz = getTestClass("org.apache.bcel.data.AttributeTestClassEM01$1S");
+ ConstantPool pool = clazz.getConstantPool();
+ Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
+ assertTrue("Expected 1 EnclosingMethod attribute but found "
+ + encMethodAttrs.length, encMethodAttrs.length == 1);
+ EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
+ String enclosingClassName = em.getEnclosingClass().getBytes(pool);
+ String enclosingMethodName = em.getEnclosingMethod().getName(pool);
+ assertTrue(
+ "Expected class name to be 'org/apache/bcel/data/AttributeTestClassEM01' but was "
+ + enclosingClassName, enclosingClassName
+ .equals("org/apache/bcel/data/AttributeTestClassEM01"));
+ assertTrue("Expected method name to be 'main' but was "
+ + enclosingMethodName, enclosingMethodName.equals("main"));
+ }
+
+ /**
+ * Verify for an inner class declared at the type level that the
+ * EnclosingMethod attribute is set correctly (i.e. to a null value)
+ */
+ public void testCheckClassLevelNamedInnerClass()
+ throws ClassNotFoundException
+ {
+ JavaClass clazz = getTestClass("org.apache.bcel.data.AttributeTestClassEM02$1");
+ ConstantPool pool = clazz.getConstantPool();
+ Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
+ assertTrue("Expected 1 EnclosingMethod attribute but found "
+ + encMethodAttrs.length, encMethodAttrs.length == 1);
+ EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
+ String enclosingClassName = em.getEnclosingClass().getBytes(pool);
+ assertTrue(
+ "The class is not within a method, so method_index should be null, but it is "
+ + em.getEnclosingMethodIndex(), em
+ .getEnclosingMethodIndex() == 0);
+ assertTrue(
+ "Expected class name to be 'org/apache/bcel/data/AttributeTestClassEM02' but was "
+ + enclosingClassName, enclosingClassName
+ .equals("org/apache/bcel/data/AttributeTestClassEM02"));
+ }
+
+ /**
+ * Check that we can save and load the attribute correctly.
+ */
+ public void testAttributeSerializtion() throws ClassNotFoundException,
+ IOException
+ {
+ JavaClass clazz = getTestClass("org.apache.bcel.data.AttributeTestClassEM02$1");
+ ConstantPool pool = clazz.getConstantPool();
+ Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
+ assertTrue("Expected 1 EnclosingMethod attribute but found "
+ + encMethodAttrs.length, encMethodAttrs.length == 1);
+ // Write it out
+ File tfile = createTestdataFile("AttributeTestClassEM02$1.class");
+ clazz.dump(tfile);
+ // Read in the new version and check it is OK
+ SyntheticRepository repos2 = createRepos(".");
+ JavaClass clazz2 = repos2.loadClass("AttributeTestClassEM02$1");
+ EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
+ String enclosingClassName = em.getEnclosingClass().getBytes(pool);
+ assertTrue(
+ "The class is not within a method, so method_index should be null, but it is "
+ + em.getEnclosingMethodIndex(), em
+ .getEnclosingMethodIndex() == 0);
+ assertTrue(
+ "Expected class name to be 'org/apache/bcel/data/AttributeTestClassEM02' but was "
+ + enclosingClassName, enclosingClassName
+ .equals("org/apache/bcel/data/AttributeTestClassEM02"));
+ tfile.deleteOnExit();
+ }
+}
Propchange: jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java?rev=427943&view=auto
==============================================================================
--- jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java (added)
+++ jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java Wed Aug 2 02:39:32 2006
@@ -0,0 +1,23 @@
+package org.apache.bcel;
+
+import org.apache.bcel.classfile.JavaClass;
+
+public class EnumAccessFlagTestCase extends AbstractTestCase
+{
+ /**
+ * An enumerated type, once compiled, should result in a class file that is
+ * marked such that we can determine from the access flags (through BCEL)
+ * that it was originally an enum type declaration.
+ */
+ public void testEnumClassSaysItIs() throws ClassNotFoundException
+ {
+ JavaClass clazz = getTestClass("org.apache.bcel.data.SimpleEnum");
+ assertTrue(
+ "Expected SimpleEnum class to say it was an enum - but it didn't !",
+ clazz.isEnum());
+ clazz = getTestClass("org.apache.bcel.data.SimpleClass");
+ assertTrue(
+ "Expected SimpleClass class to say it was not an enum - but it didn't !",
+ !clazz.isEnum());
+ }
+}
Propchange: jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/bcel/trunk/src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: jakarta/bcel/trunk/src/test/java/org/apache/bcel/FieldAnnotationsTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/bcel/trunk/src/test/java/org/apache/bcel/FieldAnnotationsTestCase.java?rev=427943&view=auto
==============================================================================
--- jakarta/bcel/trunk/src/test/java/org/apache/bcel/FieldAnnotationsTestCase.java (added)
+++ jakarta/bcel/trunk/src/test/java/org/apache/bcel/FieldAnnotationsTestCase.java Wed Aug 2 02:39:32 2006
@@ -0,0 +1,145 @@
+package org.apache.bcel;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Iterator;
+import org.apache.bcel.classfile.Field;
+import org.apache.bcel.classfile.JavaClass;
+import org.apache.bcel.classfile.AnnotationEntry;
+import org.apache.bcel.classfile.ElementValuePair;
+import org.apache.bcel.generic.ClassGen;
+import org.apache.bcel.generic.FieldGen;
+import org.apache.bcel.generic.AnnotationEntryGen;
+import org.apache.bcel.util.SyntheticRepository;
+
+public class FieldAnnotationsTestCase extends AbstractTestCase
+{
+ /**
+ * Check field AnnotationEntrys are retrievable.
+ */
+ public void testFieldAnnotationEntrys() throws ClassNotFoundException
+ {
+ JavaClass clazz = getTestClass("org.apache.bcel.data.AnnotatedFields");
+ // TODO L...;?
+ checkAnnotatedField(clazz, "i", "Lorg/apache/bcel/data/SimpleAnnotation;", "id", "1");
+ checkAnnotatedField(clazz, "s", "Lorg/apache/bcel/data/SimpleAnnotation;", "id", "2");
+ }
+
+ /**
+ * Check field AnnotationEntrys (de)serialize ok.
+ */
+ public void testFieldAnnotationEntrysReadWrite() throws ClassNotFoundException,
+ IOException
+ {
+ JavaClass clazz = getTestClass("org.apache.bcel.data.AnnotatedFields");
+ checkAnnotatedField(clazz, "i", "Lorg/apache/bcel/data/SimpleAnnotation;", "id", "1");
+ checkAnnotatedField(clazz, "s", "Lorg/apache/bcel/data/SimpleAnnotation;", "id", "2");
+ // Write it out
+ File tfile = createTestdataFile("AnnotatedFields.class");
+ clazz.dump(tfile);
+ SyntheticRepository repos2 = createRepos(".");
+ JavaClass clazz2 = repos2.loadClass("AnnotatedFields");
+ checkAnnotatedField(clazz, "i", "Lorg/apache/bcel/data/SimpleAnnotation;", "id", "1");
+ checkAnnotatedField(clazz, "s", "Lorg/apache/bcel/data/SimpleAnnotation;", "id", "2");
+ assertTrue(tfile.delete());
+ }
+
+ /**
+ * Check we can load in a class, modify its field AnnotationEntrys, save it,
+ * reload it and everything is correct.
+ */
+ public void testFieldAnnotationModification()
+ throws ClassNotFoundException, IOException
+ {
+ boolean dbg = false;
+ JavaClass clazz = getTestClass("org.apache.bcel.data.AnnotatedFields");
+ ClassGen clg = new ClassGen(clazz);
+ Field f = clg.getFields()[0];
+ if (dbg)
+ System.err.println("Field in freshly constructed class is: " + f);
+ if (dbg)
+ System.err.println("AnnotationEntrys on field are: "
+ + dumpAnnotationEntries(f.getAnnotationEntries()));
+ AnnotationEntryGen fruitBasedAnnotationEntry = createFruitAnnotationEntry(clg
+ .getConstantPool(), "Tomato", false);
+ FieldGen fg = new FieldGen(f, clg.getConstantPool());
+ if (dbg)
+ System.err.println("Adding AnnotationEntry to the field");
+ fg.addAnnotationEntry(fruitBasedAnnotationEntry);
+ if (dbg)
+ System.err.println("FieldGen (mutable field) is " + fg);
+ if (dbg)
+ System.err.println("with AnnotationEntrys: "
+ + dumpAnnotationEntries(fg.getAnnotationEntries()));
+ if (dbg)
+ System.err
+ .println("Replacing original field with new field that has extra AnnotationEntry");
+ clg.removeField(f);
+ clg.addField(fg.getField());
+ f = clg.getFields()[1]; // there are two fields in the class, removing
+ // and readding has changed the order
+ // so this time index [1] is the 'int i' field
+ if (dbg)
+ System.err.println("Field now looks like this: " + f);
+ if (dbg)
+ System.err.println("With AnnotationEntrys: "
+ + dumpAnnotationEntries(f.getAnnotationEntries()));
+ assertTrue("Should be 2 AnnotationEntrys on this field, but there are "
+ + f.getAnnotationEntries().length, f.getAnnotationEntries().length == 2);
+ }
+
+ // helper methods
+ public void checkAnnotatedField(JavaClass clazz, String fieldname,
+ String AnnotationEntryName, String AnnotationEntryElementName,
+ String AnnotationEntryElementValue)
+ {
+ Field[] fields = clazz.getFields();
+ for (int i = 0; i < fields.length; i++)
+ {
+ Field f = fields[i];
+ AnnotationEntry[] fieldAnnotationEntrys = f.getAnnotationEntries();
+ if (f.getName().equals(fieldname))
+ {
+ checkAnnotationEntry(fieldAnnotationEntrys[0], AnnotationEntryName,
+ AnnotationEntryElementName, AnnotationEntryElementValue);
+ }
+ }
+ }
+
+ private void checkAnnotationEntry(AnnotationEntry a, String name, String elementname,
+ String elementvalue)
+ {
+ assertTrue("Expected AnnotationEntry to have name " + name
+ + " but it had name " + a.getAnnotationType(), a.getAnnotationType()
+ .equals(name));
+ assertTrue("Expected AnnotationEntry to have one element but it had "
+ + a.getElementValuePairs().length, a.getElementValuePairs().length == 1);
+ ElementValuePair envp = (ElementValuePair) a.getElementValuePairs()[0];
+ assertTrue("Expected element name " + elementname + " but was "
+ + envp.getNameString(), elementname
+ .equals(envp.getNameString()));
+ assertTrue("Expected element value " + elementvalue + " but was "
+ + envp.getValue().stringifyValue(), elementvalue.equals(envp
+ .getValue().stringifyValue()));
+ }
+
+ // helper methods
+ public void checkValue(AnnotationEntry a, String name, String tostring)
+ {
+ for (int i = 0; i < a.getElementValuePairs().length; i++)
+ {
+ ElementValuePair element = a.getElementValuePairs()[i];
+ if (element.getNameString().equals(name))
+ {
+ if (!element.getValue().stringifyValue().equals(tostring))
+ {
+ fail("Expected element " + name + " to have value "
+ + tostring + " but it had value "
+ + element.getValue().stringifyValue());
+ }
+ return;
+ }
+ }
+ fail("Didnt find named element " + name);
+ }
+}
Propchange: jakarta/bcel/trunk/src/test/java/org/apache/bcel/FieldAnnotationsTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/bcel/trunk/src/test/java/org/apache/bcel/FieldAnnotationsTestCase.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org