aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/bytecode/SignatureAttribute.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/javassist/bytecode/SignatureAttribute.java')
-rw-r--r--src/main/javassist/bytecode/SignatureAttribute.java164
1 files changed, 139 insertions, 25 deletions
diff --git a/src/main/javassist/bytecode/SignatureAttribute.java b/src/main/javassist/bytecode/SignatureAttribute.java
index 1a8a62dc..ff160f82 100644
--- a/src/main/javassist/bytecode/SignatureAttribute.java
+++ b/src/main/javassist/bytecode/SignatureAttribute.java
@@ -19,6 +19,7 @@ package javassist.bytecode;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -110,42 +111,34 @@ public class SignatureAttribute extends AttributeInfo {
}
static String renameClass(String desc, Map<String,String> map) {
- if (map == null)
+ if (map == null || map.isEmpty())
return desc;
StringBuilder newdesc = new StringBuilder();
int head = 0;
int i = 0;
for (;;) {
- int j = desc.indexOf('L', i);
+ final int j = desc.indexOf('L', i);
if (j < 0)
break;
- StringBuilder nameBuf = new StringBuilder();
- int k = j;
- char c;
- try {
- while ((c = desc.charAt(++k)) != ';') {
- nameBuf.append(c);
- if (c == '<') {
- while ((c = desc.charAt(++k)) != '>')
- nameBuf.append(c);
+ final ArrayList<StringBuilder> nameBufs = new ArrayList<>();
+ final ArrayList<StringBuilder> genericParamBufs = new ArrayList<>();
+ i = parseClassName(nameBufs, genericParamBufs, desc, j) + 1;
+ if (i < 0)
+ break;
- nameBuf.append(c);
- }
- }
- }
- catch (IndexOutOfBoundsException e) { break; }
- i = k + 1;
- String name = nameBuf.toString();
- String name2 = map.get(name);
- if (name2 != null) {
- newdesc.append(desc.substring(head, j));
- newdesc.append('L');
- newdesc.append(name2);
- newdesc.append(c);
- head = i;
+ String name = String.join("$", nameBufs.toArray(new StringBuilder[0]));
+ String newname = map.get(name);
+ if (newname != null) {
+ if (makeNewClassName(desc, map, name, newname, newdesc, head, j,
+ nameBufs, genericParamBufs))
+ head = i;
}
+ else
+ if (replaceTypeArguments(desc, map, newdesc, head, j,
+ nameBufs, genericParamBufs))
+ head = i;
}
if (head == 0)
@@ -157,6 +150,127 @@ public class SignatureAttribute extends AttributeInfo {
return newdesc.toString();
}
+ private static int parseClassName(ArrayList<StringBuilder> nameBufs,
+ ArrayList<StringBuilder> genericParamBufs,
+ String desc, int j)
+ {
+ StringBuilder nameBuf = new StringBuilder();
+ StringBuilder genericParamBuf = new StringBuilder();
+ int k = j;
+ char c;
+ try {
+ while ((c = desc.charAt(++k)) != ';') {
+ if (c == '<') {
+ genericParamBuf.append(c);
+ int level = 1;
+ while (level > 0) {
+ c = desc.charAt(++k);
+ genericParamBuf.append(c);
+ if (c == '<')
+ ++level;
+ else if (c == '>')
+ --level;
+ }
+ }
+ else if (c == '.') {
+ nameBufs.add(nameBuf);
+ genericParamBufs.add(genericParamBuf);
+ nameBuf = new StringBuilder();
+ genericParamBuf = new StringBuilder();
+ }
+ else
+ nameBuf.append(c);
+ }
+ }
+ catch (IndexOutOfBoundsException e) {
+ return -2; // error
+ }
+
+ nameBufs.add(nameBuf);
+ genericParamBufs.add(genericParamBuf);
+ return k;
+ }
+
+ private static boolean makeNewClassName(String desc,
+ Map<String,String> map, String name,
+ String newname, StringBuilder newdesc,
+ int head, int j,
+ ArrayList<StringBuilder> nameBufs,
+ ArrayList<StringBuilder> genericParamBufs)
+ {
+ final String[] nameSplit = name.split("\\$");
+ final String[] newnameSplit = newname.split("\\$");
+ if (nameSplit.length == newnameSplit.length) {
+ final String[] newnames = new String[nameBufs.size()];
+ for (int start = 0, z = 0; z < nameBufs.size(); z++) {
+ final int toAggregate = (int) nameBufs.get(z).chars().filter(ch -> ch == '$').count() + 1;
+ String s = String.join("$", Arrays.copyOfRange(newnameSplit, start, start + toAggregate));
+ start += toAggregate;
+ newnames[z] = s;
+ }
+
+ newdesc.append(desc.substring(head, j));
+ newdesc.append('L');
+ for (int z = 0; z < newnames.length; z++) {
+ if (z > 0)
+ newdesc.append('.');
+
+ newdesc.append(newnames[z]);
+ final String newgenericParam;
+ final StringBuilder genericParamBufCurrent = genericParamBufs.get(z);
+ if (genericParamBufCurrent.length() > 0)
+ newgenericParam = "<" + renameClass(genericParamBufCurrent.substring(1, genericParamBufCurrent.length() - 1), map) + ">";
+ else
+ newgenericParam = genericParamBufCurrent.toString(); //empty string
+
+ newdesc.append(newgenericParam);
+ }
+ newdesc.append(';'); //the final semicolon
+ return true;
+ }
+ else
+ return false;
+ }
+
+ private static boolean replaceTypeArguments(String desc,
+ Map<String,String> map,
+ StringBuilder newdesc,
+ int head, int j,
+ ArrayList<StringBuilder> nameBufs,
+ ArrayList<StringBuilder> genericParamBufs)
+ {
+ final ArrayList<String> newGenericParamBufs = new ArrayList<String>();
+ boolean changed = false;
+ for (int z = 0; z < genericParamBufs.size(); z++) {
+ final String newGenericParam;
+ final StringBuilder genericParamBufCurrent = genericParamBufs.get(z);
+ if (genericParamBufCurrent.length() > 0) {
+ newGenericParam = "<" + renameClass(genericParamBufCurrent.substring(1, genericParamBufCurrent.length() - 1), map) + ">";
+ changed = changed || !genericParamBufCurrent.toString().equals(newGenericParam);
+ }
+ else
+ newGenericParam = genericParamBufCurrent.toString(); //empty string
+
+ newGenericParamBufs.add(newGenericParam);
+ }
+
+ if (changed) {
+ newdesc.append(desc.substring(head, j));
+ newdesc.append('L');
+ for (int z = 0; z < genericParamBufs.size(); z++) {
+ if (z > 0)
+ newdesc.append('.');
+
+ newdesc.append(nameBufs.get(z));
+ newdesc.append(newGenericParamBufs.get(z));
+ }
+ newdesc.append(';');
+ return true;
+ }
+ else
+ return false;
+ }
+
@SuppressWarnings("unused")
private static boolean isNamePart(int c) {
return c != ';' && c != '<';