Remap Type#getTypeName (#463)

This commit is contained in:
IzzelAliz 2022-03-27 16:47:48 +08:00
parent 58ea87a299
commit 9ee76a45b5
No known key found for this signature in database
GPG Key ID: EE50E123A11D8338
2 changed files with 56 additions and 0 deletions

View File

@ -96,6 +96,7 @@ public class ArclightRedirectAdapter implements PluginTransformer {
modify(classOf("jdk.internal.misc.Unsafe"), "defineClass", "unsafeDefineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class);
modify(classOf("jdk.internal.misc.Unsafe"), "defineClass0", "unsafeDefineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class);
modify(MethodHandles.Lookup.class, "defineClass", "lookupDefineClass", byte[].class);
redirect(java.lang.reflect.Type.class, "getTypeName", "typeGetName");
}
public static Object[] runHandle(ClassLoaderRemapper remapper, Method method, Object src, Object[] param) {

View File

@ -16,7 +16,10 @@ import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.WildcardType;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
@ -25,6 +28,7 @@ import java.security.Permissions;
import java.security.ProtectionDomain;
import java.security.SecureClassLoader;
import java.util.Enumeration;
import java.util.StringJoiner;
@SuppressWarnings("unused")
public class ArclightReflectionHandler extends ClassLoader {
@ -166,6 +170,57 @@ public class ArclightReflectionHandler extends ClassLoader {
return handlePackageGetName(pkg.getName());
}
// srg -> bukkit
public static String redirectTypeGetName(java.lang.reflect.Type type) {
if (type instanceof Class cl) {
if (cl.isArray()) {
return redirectTypeGetName(cl) + "[]";
}
return redirectClassGetName(cl);
} else if (type instanceof WildcardType wType) {
StringBuilder sb;
java.lang.reflect.Type[] bounds;
if (wType.getLowerBounds().length == 0) {
if (wType.getUpperBounds().length == 0 || Object.class == wType.getUpperBounds()[0]) {
return "?";
}
bounds = wType.getUpperBounds();
sb = new StringBuilder("? extends ");
} else {
bounds = wType.getLowerBounds();
sb = new StringBuilder("? super ");
}
for (int i = 0; i < bounds.length; i++) {
if (i > 0) {
sb.append(" & ");
}
sb.append(redirectTypeGetName(bounds[i]));
}
return sb.toString();
} else if (type instanceof ParameterizedType pType) {
var sb = new StringBuilder();
if (pType.getOwnerType() != null) {
sb.append(redirectTypeGetName(pType.getOwnerType()));
sb.append("$");
sb.append(redirectClassGetSimpleName((Class<?>) pType.getRawType()));
} else {
sb.append(redirectTypeGetName(pType.getRawType()));
}
if (pType.getActualTypeArguments() != null) {
var sj = new StringJoiner(", ", "<", ">");
sj.setEmptyValue("");
for (var t : pType.getActualTypeArguments()) {
sj.add(redirectTypeGetName(t));
}
sb.append(sj);
}
return sb.toString();
} else if (type instanceof GenericArrayType arrayType) {
return redirectTypeGetName(arrayType.getGenericComponentType()) + "[]";
}
return type.getTypeName();
}
// bukkit -> srg
public static Class<?> redirectClassForName(String cl) throws ClassNotFoundException {
return redirectClassForName(cl, true, Unsafe.getCallerClass().getClassLoader());