]> source.dussan.org Git - aspectj.git/commitdiff
Declare annotation: infrastructure upgrade. Mainly to ensure we copy entries between...
authoraclement <aclement>
Thu, 10 Mar 2005 12:15:04 +0000 (12:15 +0000)
committeraclement <aclement>
Thu, 10 Mar 2005 12:15:04 +0000 (12:15 +0000)
12 files changed:
bcel-builder/src/org/aspectj/apache/bcel/classfile/FieldOrMethod.java
bcel-builder/src/org/aspectj/apache/bcel/generic/ClassGen.java
bcel-builder/src/org/aspectj/apache/bcel/generic/FieldGen.java
bcel-builder/src/org/aspectj/apache/bcel/generic/MethodGen.java
bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/AnnotationElementValueGen.java
bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/AnnotationGen.java
bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ArrayElementValueGen.java
bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ClassElementValueGen.java
bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ElementNameValuePairGen.java
bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ElementValueGen.java
bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/EnumElementValueGen.java
bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/SimpleElementValueGen.java

index 647c2211ad8b9771ed3335681a367d42044074e0..2780e7c44780627cd04375ad16389c423d7721d9 100644 (file)
@@ -64,7 +64,7 @@ import java.util.List;
 /** 
  * Abstract super class for fields and methods.
  *
- * @version $Id: FieldOrMethod.java,v 1.2 2004/11/19 16:45:18 aclement Exp $
+ * @version $Id: FieldOrMethod.java,v 1.3 2005/03/10 12:15:04 aclement Exp $
  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  */
 public abstract class FieldOrMethod extends AccessFlags implements Cloneable, Node {
@@ -231,12 +231,13 @@ public abstract class FieldOrMethod extends AccessFlags implements Cloneable, No
     return c;
   }
   
+
   /**
    * Ensure we have unpacked any attributes that contain annotations.
    * We don't remove these annotation attributes from the attributes list, they
    * remain there.
    */
-  public Annotation[] getAnnotations() {
+  private void ensureAnnotationsUpToDate() {
        if (annotationsOutOfDate) { 
                // Find attributes that contain annotation data
                Attribute[] attrs = getAttributes();
@@ -251,6 +252,19 @@ public abstract class FieldOrMethod extends AccessFlags implements Cloneable, No
                annotations = (Annotation[])accumulatedAnnotations.toArray(new Annotation[]{});
                annotationsOutOfDate = false;
        }
+  }
+  
+  public Annotation[] getAnnotations() {
+    ensureAnnotationsUpToDate();
        return annotations;
   }
+  
+  public void addAnnotation(Annotation a) {
+    ensureAnnotationsUpToDate();
+       int len = annotations.length;
+       Annotation[] newAnnotations = new Annotation[len+1];
+       System.arraycopy(annotations, 0, newAnnotations, 0, len);
+       newAnnotations[len] = a;
+       annotations = newAnnotations;
+  }
 }
index a4fd19d13552cba098c28f4566e6a76ff7dbe1e3..bec436bf6267be01f6446302137a53055d83734f 100644 (file)
@@ -77,7 +77,7 @@ import org.aspectj.apache.bcel.generic.annotation.AnnotationGen;
  * existing java class (file).
  *
  * @see JavaClass
- * @version $Id: ClassGen.java,v 1.4 2004/12/09 15:19:39 aclement Exp $
+ * @version $Id: ClassGen.java,v 1.5 2005/03/10 12:15:04 aclement Exp $
  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  */
 public class ClassGen extends AccessFlags implements Cloneable {
@@ -199,14 +199,14 @@ public class ClassGen extends AccessFlags implements Cloneable {
                        List annos = rva.getAnnotations();
                        for (Iterator iter = annos.iterator(); iter.hasNext();) {
                                Annotation a = (Annotation) iter.next();
-                               annotationGenObjs.add(new AnnotationGen(a,getConstantPool()));
+                               annotationGenObjs.add(new AnnotationGen(a,getConstantPool(),false));
                        }
                } else if (attr instanceof RuntimeInvisibleAnnotations) {
                        RuntimeInvisibleAnnotations ria = (RuntimeInvisibleAnnotations)attr;
                        List annos = ria.getAnnotations();
                        for (Iterator iter = annos.iterator(); iter.hasNext();) {
                                Annotation a = (Annotation) iter.next();
-                               annotationGenObjs.add(new AnnotationGen(a,getConstantPool()));
+                               annotationGenObjs.add(new AnnotationGen(a,getConstantPool(),false));
                        }
                }
        }
index af579c4ff7cfbdc7dd7e49220e0d6ac5c56cd6e1..71e453dfd36c6f9568c77e950b2bf4986598994d 100644 (file)
@@ -75,7 +75,7 @@ import org.aspectj.apache.bcel.generic.annotation.AnnotationGen;
  * one can do is to add a constant value attribute to a field (which must of
  * course be compatible with to the declared type).
  *
- * @version $Id: FieldGen.java,v 1.3 2004/11/22 08:31:27 aclement Exp $
+ * @version $Id: FieldGen.java,v 1.4 2005/03/10 12:15:04 aclement Exp $
  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  * @see Field
  */
@@ -118,7 +118,7 @@ public class FieldGen extends FieldGenOrMethodGen {
                List l = runtimeAnnotations.getAnnotations();
                for (Iterator it = l.iterator(); it.hasNext();) {
                        Annotation element = (Annotation) it.next();
-                       addAnnotation(new AnnotationGen(element,cp));
+                       addAnnotation(new AnnotationGen(element,cp,false));
                }
       } else {
            addAttribute(attrs[i]);
index 5b66be328f851a7fe8e4ec266d9cb2568ae0853c..6f021e024301f82cb0cb92c0fab994e97b46e475 100644 (file)
@@ -86,7 +86,7 @@ import org.aspectj.apache.bcel.generic.annotation.AnnotationGen;
  * use the `removeNOPs' method to get rid off them.
  * The resulting method object can be obtained via the `getMethod()' method.
  *
- * @version $Id: MethodGen.java,v 1.3 2005/01/26 14:01:30 aclement Exp $
+ * @version $Id: MethodGen.java,v 1.4 2005/03/10 12:15:04 aclement Exp $
  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  * @author  <A HREF="http://www.vmeng.com/beard">Patrick C. Beard</A> [setMaxStack()]
  * @see     InstructionList
@@ -288,7 +288,7 @@ public class MethodGen extends FieldGenOrMethodGen {
                List l = runtimeAnnotations.getAnnotations();
                for (Iterator it = l.iterator(); it.hasNext();) {
                        Annotation element = (Annotation) it.next();
-                       addAnnotation(new AnnotationGen(element,cp));
+                       addAnnotation(new AnnotationGen(element,cp,false));
                }
       } else {
        addAttribute(a);
@@ -1085,7 +1085,7 @@ public class MethodGen extends FieldGenOrMethodGen {
   private List /*AnnotationGen*/ makeMutableVersion(Annotation[] mutableArray) {
        List result = new ArrayList();
        for (int i = 0; i < mutableArray.length; i++) {
-               result.add(new AnnotationGen(mutableArray[i],getConstantPool()));
+               result.add(new AnnotationGen(mutableArray[i],getConstantPool(),false));
        }
        return result;
   }
index 0ddee226f2053bc9a359824fbe02420d57358797..ad7a08de9b9b88162cb1ccfc7a69850ec172acba 100644 (file)
@@ -36,9 +36,9 @@ public class AnnotationElementValueGen extends ElementValueGen {
                this.a = annotation;    
        }
 
-       public AnnotationElementValueGen(AnnotationElementValue value, ConstantPoolGen cpool) {
+       public AnnotationElementValueGen(AnnotationElementValue value, ConstantPoolGen cpool,boolean copyPoolEntries) {
                super(ANNOTATION,cpool);
-               a = new AnnotationGen(value.getAnnotation(),cpool);
+               a = new AnnotationGen(value.getAnnotation(),cpool,copyPoolEntries);
        }
 
        public void dump(DataOutputStream dos) throws IOException {
index 94236bc36abf31dd6d55fcb722355d591c1422b0..d017668fbf0979d781c75fb10e1702bb507783ab 100644 (file)
@@ -33,25 +33,31 @@ public class AnnotationGen {
        
        /**
         * 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 ...
+        * modifiable AnnotationGen object.  If the pool passed in is for a different
+        * class file, then copyPoolEntries should have been passed as true as that
+        * will force us to do a deep copy of the annotation and move the cpool entries
+        * across.
         * We need to copy the type and the element name value pairs and the visibility.
         */
-       public AnnotationGen(Annotation a,ConstantPoolGen cpool) {
+       public AnnotationGen(Annotation a,ConstantPoolGen cpool,boolean copyPoolEntries) {
                this.cpool = cpool;
-               // Could assert a.getTypeSignature()  == (ConstantUtf8)cpool.getConstant(a.getTypeIndex())).getBytes()
-               typeIndex = a.getTypeIndex();
+               
+               if (copyPoolEntries) {
+                       typeIndex = cpool.addUtf8(a.getTypeSignature());                        
+               } else {
+                       typeIndex = a.getTypeIndex();
+               }
                
                isRuntimeVisible   = a.isRuntimeVisible();
                
-               evs = copyValues(a.getValues(),cpool);
+               evs = copyValues(a.getValues(),cpool,copyPoolEntries);
        }
        
-       private List copyValues(List in,ConstantPoolGen cpool) {
+       private List copyValues(List in,ConstantPoolGen cpool,boolean copyPoolEntries) {
                List out = new ArrayList();
                for (Iterator iter = in.iterator(); iter.hasNext();) {
                        ElementNameValuePair nvp = (ElementNameValuePair) iter.next();
-                       out.add(new ElementNameValuePairGen(nvp,cpool));
+                       out.add(new ElementNameValuePairGen(nvp,cpool,copyPoolEntries));
                }
                return out;
        }
index f5b1764371e445f0c0eac2f3dbfd665bc457c662..3fb54096c7c23c92b356259b42dc1ff6509a2fff 100644 (file)
@@ -47,12 +47,12 @@ public class ArrayElementValueGen extends ElementValueGen {
         * @param value
         * @param cpool
         */
-       public ArrayElementValueGen(ArrayElementValue value, ConstantPoolGen 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));
+                       evalues.add(ElementValueGen.copy(in[i],cpool,copyPoolEntries));
                }
        }
 
index 051d2d8df406efbf5251810496ce7e11c41a4e87..5241fd3534fb5db0b3de4e0c4678c16e77286ca1 100644 (file)
@@ -15,7 +15,6 @@ 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.classfile.annotation.ClassElementValue;
 import org.aspectj.apache.bcel.generic.ConstantPoolGen;
@@ -35,12 +34,19 @@ public class ClassElementValueGen extends ElementValueGen {
     
     public ClassElementValueGen(ObjectType t,ConstantPoolGen cpool) {
        super(ElementValueGen.CLASS,cpool);
-       this.idx = cpool.addClass(t);
+       //this.idx = cpool.addClass(t);
+       idx = cpool.addUtf8(t.getSignature());
     }
     
-       public ClassElementValueGen(ClassElementValue value, ConstantPoolGen cpool) {
+       public ClassElementValueGen(ClassElementValue value, ConstantPoolGen cpool,boolean copyPoolEntries) {
                super(CLASS,cpool);
-               idx = value.getIndex();
+               if (copyPoolEntries) {
+                       //idx = cpool.addClass(value.getClassString());
+                       idx = cpool.addUtf8(value.getClassString());
+               } else {        
+                       idx = value.getIndex();
+                       
+               }
        }
 
        public int getIndex() {
@@ -48,9 +54,11 @@ public class ClassElementValueGen extends ElementValueGen {
     }
      
     public String getClassString() {
-               ConstantClass c = (ConstantClass)getConstantPool().getConstant(idx);
-               ConstantUtf8 utf8 = (ConstantUtf8)getConstantPool().getConstant(c.getNameIndex());
-               return utf8.getBytes();
+       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() {
index 23b84e6b7261281e9cdb1bd02ae576636e639e50..d64ad0d7210ab69913a96f8e44c94f5a75358971 100644 (file)
@@ -25,12 +25,19 @@ public class ElementNameValuePairGen {
        private ElementValueGen value;
        private ConstantPoolGen cpool;
 
-       public ElementNameValuePairGen(ElementNameValuePair nvp, ConstantPoolGen cpool) {
+       public ElementNameValuePairGen(ElementNameValuePair nvp, ConstantPoolGen cpool, boolean copyPoolEntries) {
                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);
+//             if (!nvp.getNameString().equals(((ConstantUtf8)cpool.getConstant(nvp.getNameIndex())).getBytes())) {
+//                     throw new RuntimeException("envp buggered");
+//             }
+               if (copyPoolEntries) { 
+                       nameIdx = cpool.addUtf8(nvp.getNameString());
+               } else {
+                       nameIdx = nvp.getNameIndex();
+               }
+               value = ElementValueGen.copy(nvp.getValue(),cpool,copyPoolEntries);
        }
 
 
index 054e1cbf80738c2d2b381eda54e660ced4d49519..386320c7566432877b5c1ece72293decc8d6442f 100644 (file)
@@ -109,7 +109,7 @@ public abstract class ElementValueGen {
        /** 
         * Creates an (modifiable) ElementValueGen copy of an (immutable) ElementValue - constant pool is assumed correct.
         */
-       public static ElementValueGen copy(ElementValue value,ConstantPoolGen cpool) {
+       public static ElementValueGen copy(ElementValue value,ConstantPoolGen cpool,boolean copyPoolEntries) {
                switch (value.getElementValueType()) {
                         case 'B': // byte
                         case 'C': // char
@@ -120,19 +120,19 @@ public abstract class ElementValueGen {
                         case 'S': // short
                         case 'Z': // boolean
                         case 's': // String
-                         return new SimpleElementValueGen((SimpleElementValue)value,cpool);
+                         return new SimpleElementValueGen((SimpleElementValue)value,cpool,copyPoolEntries);
                                
                         case 'e': // Enum constant
-                               return new EnumElementValueGen((EnumElementValue)value,cpool);
+                               return new EnumElementValueGen((EnumElementValue)value,cpool,copyPoolEntries);
 
                         case '@': // Annotation
-                               return new AnnotationElementValueGen((AnnotationElementValue)value,cpool);
+                               return new AnnotationElementValueGen((AnnotationElementValue)value,cpool,copyPoolEntries);
                                
                     case '[': // Array
-                       return new ArrayElementValueGen((ArrayElementValue)value,cpool);
+                       return new ArrayElementValueGen((ArrayElementValue)value,cpool,copyPoolEntries);
 
                     case 'c': // Class
-                       return new ClassElementValueGen((ClassElementValue)value,cpool);
+                       return new ClassElementValueGen((ClassElementValue)value,cpool,copyPoolEntries);
                        
                        default:
                                throw new RuntimeException("Not implemented yet! ("+value.getElementValueType()+")");
index f302836019fc0dbd498ba195f92c36c666909218..2f66ac8fd5314b1ea89089b0b238fc7f0b99d4cf 100644 (file)
@@ -43,14 +43,19 @@ public class EnumElementValueGen extends ElementValueGen {
     
     public EnumElementValueGen(ObjectType t,String value,ConstantPoolGen cpool) {
        super(ElementValueGen.ENUM_CONSTANT,cpool);
-       typeIdx = cpool.addClass(t);
-       valueIdx= cpool.addString(value);
+       typeIdx = cpool.addUtf8(t.getSignature());// was addClass(t);
+       valueIdx= cpool.addUtf8(value);// was addString(value);
     }
 
-       public EnumElementValueGen(EnumElementValue value, ConstantPoolGen cpool) {
+       public EnumElementValueGen(EnumElementValue value, ConstantPoolGen cpool, boolean copyPoolEntries) {
                super(ENUM_CONSTANT,cpool);
-               typeIdx  = value.getTypeIndex();
-               valueIdx = value.getValueIndex();
+               if (copyPoolEntries) {
+                       typeIdx = cpool.addUtf8(value.getEnumTypeString());// was addClass(value.getEnumTypeString());
+                       valueIdx = cpool.addUtf8(value.getEnumValueString()); // was addString(value.getEnumValueString());
+               } else {
+                       typeIdx  = value.getTypeIndex();
+                       valueIdx = value.getValueIndex();
+               }
        }
 
        public void dump(DataOutputStream dos) throws IOException {
@@ -60,8 +65,10 @@ public class EnumElementValueGen extends ElementValueGen {
     }
     
     public String stringifyValue() {
-       ConstantString cu8 = (ConstantString)getConstantPool().getConstant(valueIdx);
-       return ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
+       ConstantUtf8 cu8 = (ConstantUtf8)getConstantPool().getConstant(valueIdx);
+               return cu8.getBytes();
+//     ConstantString cu8 = (ConstantString)getConstantPool().getConstant(valueIdx);
+//     return ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
     }
     
     
index 49e5c57d26bdea3a94dff574d874ecf1baa97242..d36abe648b5ee470aef389e21070ef6b999a1a84 100644 (file)
@@ -89,11 +89,36 @@ public class SimpleElementValueGen extends ElementValueGen {
        idx = cpGen.addUtf8(value);
     }
     
-       public SimpleElementValueGen(SimpleElementValue value,ConstantPoolGen cpool) {
+    /**
+     * The boolean controls whether we copy info from the 'old' constant pool
+     * to the 'new'.   You need to use this ctor if the annotation is 
+     * being copied from one file to another.
+     */
+       public SimpleElementValueGen(SimpleElementValue value,ConstantPoolGen cpool,boolean copyPoolEntries) {
                super(value.getElementValueType(),cpool);
-               // J5ASSERT: Could assert value.stringifyValue() is the same as
-               // cpool.getConstant(SimpleElementValuevalue.getIndex())
-               idx = value.getIndex();
+               if (!copyPoolEntries) {
+                       // J5ASSERT: Could assert value.stringifyValue() is the same as
+                       // cpool.getConstant(SimpleElementValuevalue.getIndex())
+                       idx = value.getIndex();
+               } else {
+                       switch (value.getElementValueType()) {
+                               case STRING:          idx = cpool.addUtf8(value.getValueString()); break;
+                               case PRIMITIVE_INT:   idx = cpool.addInteger(value.getValueInt()); break;
+                               case PRIMITIVE_BYTE:  idx = cpool.addInteger(value.getValueByte()); break;
+                               case PRIMITIVE_CHAR:  idx = cpool.addInteger(value.getValueChar()); break;
+                               case PRIMITIVE_LONG:  idx = cpool.addLong(value.getValueLong()); break;
+                               case PRIMITIVE_FLOAT: idx = cpool.addFloat(value.getValueFloat());break;
+                               case PRIMITIVE_DOUBLE:idx = cpool.addDouble(value.getValueDouble());break;
+                               case PRIMITIVE_BOOLEAN:
+                                       if (value.getValueBoolean()) { idx = cpool.addInteger(1);
+                                       } else {                                           idx = cpool.addInteger(0);}
+                                       break;
+                               case PRIMITIVE_SHORT: idx = cpool.addInteger(value.getValueShort());break;
+                               default:
+                                 throw new RuntimeException("SimpleElementValueGen class does not know how "+
+                                                                    "to copy this type "+type);
+                       }
+               }
        }
 
        public int getIndex() {