From: chiba Date: Sun, 12 Sep 2010 12:59:04 +0000 (+0000) Subject: fixed JASSIST-130 X-Git-Tag: rel_3_17_1_ga~84 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=325084e1ad1d5de64953b5eb9bc8b064484b7f97;p=javassist.git fixed JASSIST-130 git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@573 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- diff --git a/Readme.html b/Readme.html index b1bd8a6b..87b9e634 100644 --- a/Readme.html +++ b/Readme.html @@ -284,7 +284,7 @@ see javassist.Dump.

-version 3.14

-version 3.13 on July 19, 2010 diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index adb81455..c3d41760 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -409,6 +409,8 @@ public abstract class CtClass { * That collection includes the name of this class. * *

This method may return null. + * + * @return a Collection<String> object. */ public synchronized Collection getRefClasses() { ClassFile cf = getClassFile2(); diff --git a/src/main/javassist/bytecode/AnnotationsAttribute.java b/src/main/javassist/bytecode/AnnotationsAttribute.java index 4612d38e..7e53ce02 100644 --- a/src/main/javassist/bytecode/AnnotationsAttribute.java +++ b/src/main/javassist/bytecode/AnnotationsAttribute.java @@ -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 @@ public class AnnotationsAttribute extends AttributeInfo { return new AnnotationsAttribute(newCp, getName(), copier.close()); } catch (Exception e) { - throw new RuntimeException(e.toString()); + throw new RuntimeException(e); } } @@ -225,7 +226,7 @@ public class AnnotationsAttribute extends AttributeInfo { return new Parser(info, constPool).parseAnnotations(); } catch (Exception e) { - throw new RuntimeException(e.toString()); + throw new RuntimeException(e); } } @@ -265,12 +266,31 @@ public class AnnotationsAttribute extends AttributeInfo { setAnnotations(new Annotation[] { annotation }); } + /** + * @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 @@ public class AnnotationsAttribute extends AttributeInfo { 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 @@ public class AnnotationsAttribute extends AttributeInfo { 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 @@ public class AnnotationsAttribute extends AttributeInfo { } } + static class Renamer extends Walker { + ConstPool cpool; + Map classnames; + + /** + * Constructs a renamer. It renames some class names + * into the new names specified by map. + * + * @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 @@ public class AnnotationsAttribute extends AttributeInfo { } 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 @@ public class AnnotationsAttribute extends AttributeInfo { 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 @@ public class AnnotationsAttribute extends AttributeInfo { 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; 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 @@ public class AnnotationsAttribute extends AttributeInfo { 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 { diff --git a/src/main/javassist/bytecode/AttributeInfo.java b/src/main/javassist/bytecode/AttributeInfo.java index c7bcf149..29c950ac 100644 --- a/src/main/javassist/bytecode/AttributeInfo.java +++ b/src/main/javassist/bytecode/AttributeInfo.java @@ -251,8 +251,8 @@ public class AttributeInfo { /* 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) {} diff --git a/src/main/javassist/bytecode/ClassFilePrinter.java b/src/main/javassist/bytecode/ClassFilePrinter.java index 59834114..08078dc0 100644 --- a/src/main/javassist/bytecode/ClassFilePrinter.java +++ b/src/main/javassist/bytecode/ClassFilePrinter.java @@ -111,6 +111,9 @@ public class ClassFilePrinter { 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(""); StackMapTable.Printer.print((StackMapTable)ai, out); diff --git a/src/main/javassist/bytecode/ParameterAnnotationsAttribute.java b/src/main/javassist/bytecode/ParameterAnnotationsAttribute.java index 2fd5a714..66386429 100644 --- a/src/main/javassist/bytecode/ParameterAnnotationsAttribute.java +++ b/src/main/javassist/bytecode/ParameterAnnotationsAttribute.java @@ -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 java.io.ByteArrayOutputStream; import javassist.bytecode.AnnotationsAttribute.Copier; import javassist.bytecode.AnnotationsAttribute.Parser; +import javassist.bytecode.AnnotationsAttribute.Renamer; import javassist.bytecode.annotation.*; /** @@ -164,4 +166,47 @@ public class ParameterAnnotationsAttribute extends AttributeInfo { 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(); + + } }