[jboss-cvs] javassist SVN: r573 - in trunk: src/main/javassist and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun Sep 12 08:59:05 EDT 2010


Author: chiba
Date: 2010-09-12 08:59:04 -0400 (Sun, 12 Sep 2010)
New Revision: 573

Modified:
   trunk/Readme.html
   trunk/src/main/javassist/CtClass.java
   trunk/src/main/javassist/bytecode/AnnotationsAttribute.java
   trunk/src/main/javassist/bytecode/AttributeInfo.java
   trunk/src/main/javassist/bytecode/ClassFilePrinter.java
   trunk/src/main/javassist/bytecode/ParameterAnnotationsAttribute.java
Log:
fixed JASSIST-130

Modified: trunk/Readme.html
===================================================================
--- trunk/Readme.html	2010-09-09 17:27:32 UTC (rev 572)
+++ trunk/Readme.html	2010-09-12 12:59:04 UTC (rev 573)
@@ -284,7 +284,7 @@
 <p>-version 3.14
 
 <ul>
-	<li>JIRA JASSIST-131.
+	<li>JIRA JASSIST-130, 131, 132.
 </ul>
 
 <p>-version 3.13 on July 19, 2010

Modified: trunk/src/main/javassist/CtClass.java
===================================================================
--- trunk/src/main/javassist/CtClass.java	2010-09-09 17:27:32 UTC (rev 572)
+++ trunk/src/main/javassist/CtClass.java	2010-09-12 12:59:04 UTC (rev 573)
@@ -409,6 +409,8 @@
      * That collection includes the name of this class.
      *
      * <p>This method may return <code>null</code>.
+     *
+     * @return a <code>Collection&lt;String&gt;</code> object.
      */
     public synchronized Collection getRefClasses() {
         ClassFile cf = getClassFile2();

Modified: trunk/src/main/javassist/bytecode/AnnotationsAttribute.java
===================================================================
--- trunk/src/main/javassist/bytecode/AnnotationsAttribute.java	2010-09-09 17:27:32 UTC (rev 572)
+++ trunk/src/main/javassist/bytecode/AnnotationsAttribute.java	2010-09-12 12:59:04 UTC (rev 573)
@@ -16,6 +16,7 @@
 package javassist.bytecode;
 
 import java.util.Map;
+import java.util.HashMap;
 import java.io.IOException;
 import java.io.DataInputStream;
 import java.io.ByteArrayOutputStream;
@@ -165,7 +166,7 @@
             return new AnnotationsAttribute(newCp, getName(), copier.close());
         }
         catch (Exception e) {
-            throw new RuntimeException(e.toString());
+            throw new RuntimeException(e);
         }
     }
 
@@ -225,7 +226,7 @@
             return new Parser(info, constPool).parseAnnotations();
         }
         catch (Exception e) {
-            throw new RuntimeException(e.toString());
+            throw new RuntimeException(e);
         }
     }
 
@@ -266,11 +267,30 @@
     }
 
     /**
+     * @param oldname       a JVM class name.
+     * @param newname       a JVM class name.
+     */
+    void renameClass(String oldname, String newname) {
+        HashMap map = new HashMap();
+        map.put(oldname, newname);
+        renameClass(map);
+    }
+
+    void renameClass(Map classnames) {
+        Renamer renamer = new Renamer(info, getConstPool(), classnames);
+        try {
+            renamer.annotationArray();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
      * Returns a string representation of this object.
      */
     public String toString() {
         Annotation[] a = getAnnotations();
-        StringBuffer sbuf = new StringBuffer();
+        StringBuilder sbuf = new StringBuilder();
         int i = 0;
         while (i < a.length) {
             sbuf.append(a[i++].toString());
@@ -341,12 +361,12 @@
             if (tag == 'e') {
                 int typeNameIndex = ByteArray.readU16bit(info, pos + 1);
                 int constNameIndex = ByteArray.readU16bit(info, pos + 3);
-                enumMemberValue(typeNameIndex, constNameIndex);
+                enumMemberValue(pos, typeNameIndex, constNameIndex);
                 return pos + 5;
             }
             else if (tag == 'c') {
                 int index = ByteArray.readU16bit(info, pos + 1);
-                classMemberValue(index);
+                classMemberValue(pos, index);
                 return pos + 3;
             }
             else if (tag == '@')
@@ -364,11 +384,11 @@
 
         void constValueMember(int tag, int index) throws Exception {}
 
-        void enumMemberValue(int typeNameIndex, int constNameIndex)
+        void enumMemberValue(int pos, int typeNameIndex, int constNameIndex)
             throws Exception {
         }
 
-        void classMemberValue(int index) throws Exception {}
+        void classMemberValue(int pos, int index) throws Exception {}
 
         int annotationMemberValue(int pos) throws Exception {
             return annotation(pos);
@@ -383,6 +403,52 @@
         }
     }
 
+    static class Renamer extends Walker {
+        ConstPool cpool;
+        Map classnames;
+
+        /**
+         * Constructs a renamer.  It renames some class names
+         * into the new names specified by <code>map</code>.
+         *
+         * @param info      the annotations attribute.
+         * @param cp        the constant pool.
+         * @param map       pairs of replaced and substituted class names.
+         *                  It can be null.
+         */
+        Renamer(byte[] info, ConstPool cp, Map map) {
+            super(info);
+            cpool = cp;
+            classnames = map;
+        }
+
+        int annotation(int pos, int type, int numPairs) throws Exception {
+            renameType(pos - 4, type);
+            return super.annotation(pos, type, numPairs);
+        }
+
+        void enumMemberValue(int pos, int typeNameIndex, int constNameIndex)
+            throws Exception
+        {
+            renameType(pos + 1, typeNameIndex);
+            super.enumMemberValue(pos, typeNameIndex, constNameIndex);
+        }
+
+        void classMemberValue(int pos, int index) throws Exception {
+            renameType(pos + 1, index);
+            super.classMemberValue(pos, index);
+        }
+
+        private void renameType(int pos, int index) {
+            String name = cpool.getUtf8Info(index);
+            String newName = Descriptor.rename(name, classnames);
+            if (!name.equals(newName)) {
+                int index2 = cpool.addUtf8Info(newName);
+                ByteArray.write16bit(index2, info, pos);
+            }
+        }
+    }
+
     static class Copier extends Walker {
         ByteArrayOutputStream output;
         AnnotationsWriter writer;
@@ -425,7 +491,7 @@
         }
 
         int annotation(int pos, int type, int numPairs) throws Exception {
-            writer.annotation(copy(type), numPairs);
+            writer.annotation(copyType(type), numPairs);
             return super.annotation(pos, type, numPairs);
         }
 
@@ -439,16 +505,16 @@
             super.constValueMember(tag, index);
         }
 
-        void enumMemberValue(int typeNameIndex, int constNameIndex)
+        void enumMemberValue(int pos, int typeNameIndex, int constNameIndex)
             throws Exception
         {
-            writer.enumConstValue(copy(typeNameIndex), copy(constNameIndex));
-            super.enumMemberValue(typeNameIndex, constNameIndex);
+            writer.enumConstValue(copyType(typeNameIndex), copy(constNameIndex));
+            super.enumMemberValue(pos, typeNameIndex, constNameIndex);
         }
 
-        void classMemberValue(int index) throws Exception {
-            writer.classInfoIndex(copy(index));
-            super.classMemberValue(index);
+        void classMemberValue(int pos, int index) throws Exception {
+            writer.classInfoIndex(copyType(index));
+            super.classMemberValue(pos, index);
         }
 
         int annotationMemberValue(int pos) throws Exception {
@@ -473,6 +539,22 @@
         int copy(int srcIndex) {
             return srcPool.copy(srcIndex, destPool, classnames);
         }
+
+        /**
+         * Copies a constant pool entry into the destination constant pool
+         * and returns the index of the copied entry.  That entry must be
+         * a Utf8Info representing a class name in the L<class name>; form.
+         *
+         * @param srcIndex  the index of the copied entry into the source
+         *                  constant pool.
+         * @return          the index of the copied item into the destination
+         *                  constant pool.
+         */
+        int copyType(int srcIndex) {
+            String name = srcPool.getUtf8Info(srcIndex);
+            String newName = Descriptor.rename(name, classnames);
+            return destPool.addUtf8Info(newName);
+        }
     }
 
     static class Parser extends Walker {
@@ -580,17 +662,17 @@
             super.constValueMember(tag, index);
         }
 
-        void enumMemberValue(int typeNameIndex, int constNameIndex)
+        void enumMemberValue(int pos, int typeNameIndex, int constNameIndex)
             throws Exception
         {
             currentMember = new EnumMemberValue(typeNameIndex,
                                               constNameIndex, pool);
-            super.enumMemberValue(typeNameIndex, constNameIndex);
+            super.enumMemberValue(pos, typeNameIndex, constNameIndex);
         }
 
-        void classMemberValue(int index) throws Exception {
+        void classMemberValue(int pos, int index) throws Exception {
             currentMember = new ClassMemberValue(index, pool);
-            super.classMemberValue(index);
+            super.classMemberValue(pos, index);
         }
 
         int annotationMemberValue(int pos) throws Exception {

Modified: trunk/src/main/javassist/bytecode/AttributeInfo.java
===================================================================
--- trunk/src/main/javassist/bytecode/AttributeInfo.java	2010-09-09 17:27:32 UTC (rev 572)
+++ trunk/src/main/javassist/bytecode/AttributeInfo.java	2010-09-12 12:59:04 UTC (rev 573)
@@ -251,8 +251,8 @@
 
     /* The following two methods are used to implement
      * ClassFile.renameClass().
-     * Only CodeAttribute and LocalVariableAttribute override
-     * this method.
+     * Only CodeAttribute, LocalVariableAttribute, and
+     * AnnotationsAttribute override these methods.
      */
     void renameClass(String oldname, String newname) {}
     void renameClass(Map classnames) {}

Modified: trunk/src/main/javassist/bytecode/ClassFilePrinter.java
===================================================================
--- trunk/src/main/javassist/bytecode/ClassFilePrinter.java	2010-09-09 17:27:32 UTC (rev 572)
+++ trunk/src/main/javassist/bytecode/ClassFilePrinter.java	2010-09-12 12:59:04 UTC (rev 573)
@@ -111,6 +111,9 @@
             else if (ai instanceof AnnotationsAttribute) {
                 out.println("annnotation: " + ai.toString());
             }
+            else if (ai instanceof ParameterAnnotationsAttribute) {
+                out.println("parameter annnotations: " + ai.toString());
+            }
             else if (ai instanceof StackMapTable) {
                 out.println("<stack map table begin>");
                 StackMapTable.Printer.print((StackMapTable)ai, out);

Modified: trunk/src/main/javassist/bytecode/ParameterAnnotationsAttribute.java
===================================================================
--- trunk/src/main/javassist/bytecode/ParameterAnnotationsAttribute.java	2010-09-09 17:27:32 UTC (rev 572)
+++ trunk/src/main/javassist/bytecode/ParameterAnnotationsAttribute.java	2010-09-12 12:59:04 UTC (rev 573)
@@ -15,6 +15,7 @@
 
 package javassist.bytecode;
 
+import java.util.HashMap;
 import java.util.Map;
 import java.io.IOException;
 import java.io.DataInputStream;
@@ -22,6 +23,7 @@
 
 import javassist.bytecode.AnnotationsAttribute.Copier;
 import javassist.bytecode.AnnotationsAttribute.Parser;
+import javassist.bytecode.AnnotationsAttribute.Renamer;
 import javassist.bytecode.annotation.*;
 
 /**
@@ -164,4 +166,47 @@
 
         set(output.toByteArray());
     }
+
+    /**
+     * @param oldname       a JVM class name.
+     * @param newname       a JVM class name.
+     */
+    void renameClass(String oldname, String newname) {
+        HashMap map = new HashMap();
+        map.put(oldname, newname);
+        renameClass(map);
+    }
+
+    void renameClass(Map classnames) {
+        Renamer renamer = new Renamer(info, getConstPool(), classnames);
+        try {
+            renamer.parameters();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Returns a string representation of this object.
+     */
+    public String toString() {
+        Annotation[][] aa = getAnnotations();
+        StringBuilder sbuf = new StringBuilder();
+        int k = 0;
+        while (k < aa.length) {
+            Annotation[] a = aa[k++]; 
+            int i = 0;
+            while (i < a.length) {
+                sbuf.append(a[i++].toString());
+                if (i != a.length)
+                    sbuf.append(" ");
+            }
+
+            if (k != aa.length)
+                sbuf.append(", ");
+        }
+
+        return sbuf.toString();
+
+    }
 }



More information about the jboss-cvs-commits mailing list