aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/bytecode/SignatureAttribute.java
diff options
context:
space:
mode:
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2009-07-02 11:17:17 +0000
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2009-07-02 11:17:17 +0000
commitd8ba90667b8d2ca3ce409d355373aecaf48ae871 (patch)
tree65e7cb15554db75f0751f1c89ae096aa9e037661 /src/main/javassist/bytecode/SignatureAttribute.java
parenta690c8bf872c9e5fe4e14b6095ddb81de01a34c4 (diff)
downloadjavassist-d8ba90667b8d2ca3ce409d355373aecaf48ae871.tar.gz
javassist-d8ba90667b8d2ca3ce409d355373aecaf48ae871.zip
fixed JIRA JASSIST-77
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@485 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist/bytecode/SignatureAttribute.java')
-rw-r--r--src/main/javassist/bytecode/SignatureAttribute.java113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/main/javassist/bytecode/SignatureAttribute.java b/src/main/javassist/bytecode/SignatureAttribute.java
index 2c8f0e9b..267098f8 100644
--- a/src/main/javassist/bytecode/SignatureAttribute.java
+++ b/src/main/javassist/bytecode/SignatureAttribute.java
@@ -62,6 +62,18 @@ public class SignatureAttribute extends AttributeInfo {
}
/**
+ * Sets <code>signature_index</code> to the index of the given signature,
+ * which is added to a constant pool.
+ *
+ * @param sig new signature.
+ * @since 3.11
+ */
+ public void setSignature(String sig) {
+ int index = getConstPool().addUtf8Info(sig);
+ ByteArray.write16bit(index, info, 0);
+ }
+
+ /**
* Makes a copy. Class names are replaced according to the
* given <code>Map</code> object.
*
@@ -73,6 +85,107 @@ public class SignatureAttribute extends AttributeInfo {
return new SignatureAttribute(newCp, getSignature());
}
+ void renameClass(String oldname, String newname) {
+ String sig = renameClass(getSignature(), oldname, newname);
+ setSignature(sig);
+ }
+
+ void renameClass(Map classnames) {
+ String sig = renameClass(getSignature(), classnames);
+ setSignature(sig);
+ }
+
+ static String renameClass(String desc, String oldname, String newname) {
+ if (desc.indexOf(oldname) < 0)
+ return desc;
+
+ StringBuffer newdesc = new StringBuffer();
+ int head = 0;
+ int i = 0;
+ for (;;) {
+ int j = desc.indexOf('L', i);
+ if (j < 0)
+ break;
+
+ int k = j;
+ int p = 0;
+ char c;
+ boolean match = true;
+ try {
+ int len = oldname.length();
+ while (isNamePart(c = desc.charAt(++k)))
+ if (p >= len || c != oldname.charAt(p++))
+ match = false;
+ }
+ catch (IndexOutOfBoundsException e) { break; }
+ i = k + 1;
+ if (match && p == oldname.length()) {
+ newdesc.append(desc.substring(head, j));
+ newdesc.append('L');
+ newdesc.append(newname);
+ newdesc.append(c);
+ head = i;
+ }
+ }
+
+ if (head == 0)
+ return desc;
+ else {
+ int len = desc.length();
+ if (head < len)
+ newdesc.append(desc.substring(head, len));
+
+ return newdesc.toString();
+ }
+ }
+
+ static String renameClass(String desc, Map map) {
+ if (map == null)
+ return desc;
+
+ StringBuffer newdesc = new StringBuffer();
+ int head = 0;
+ int i = 0;
+ for (;;) {
+ int j = desc.indexOf('L', i);
+ if (j < 0)
+ break;
+
+ StringBuffer nameBuf = new StringBuffer();
+ int k = j;
+ char c;
+ try {
+ while (isNamePart(c = desc.charAt(++k)))
+ nameBuf.append(c);
+ }
+ catch (IndexOutOfBoundsException e) { break; }
+ i = k + 1;
+ String name = nameBuf.toString();
+ String name2 = (String)map.get(name);
+ if (name2 != null) {
+ newdesc.append(desc.substring(head, j));
+ newdesc.append('L');
+ newdesc.append(name2);
+ newdesc.append(c);
+ head = i;
+ }
+ }
+
+ if (head == 0)
+ return desc;
+ else {
+ int len = desc.length();
+ if (head < len)
+ newdesc.append(desc.substring(head, len));
+
+ return newdesc.toString();
+ }
+ }
+
+ private static boolean isNamePart(int c) {
+ return c != ';' && c != '<';
+ }
+
static private class Cursor {
int position = 0;