Skip to content

Commit

Permalink
增加findGetter/findStaticGetter/findSetter/findStaticSetter的remap
Browse files Browse the repository at this point in the history
  • Loading branch information
Luohuayu committed Oct 26, 2020
1 parent eb3ed0a commit d12a501
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/main/java/catserver/server/remapper/RemapRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ public class RemapRules {
remapVirtualMethodToStatic.put("java/lang/reflect/Field;getName", ReflectionMethods.class);
remapVirtualMethodToStatic.put("java/lang/reflect/Method;getName", ReflectionMethods.class);
remapVirtualMethodToStatic.put("java/lang/ClassLoader;loadClass", ReflectionMethods.class);
remapVirtualMethodToStatic.put("java/lang/invoke/MethodHandles$Lookup;findVirtual", MethodHandleMethods.class);
remapVirtualMethodToStatic.put("java/lang/invoke/MethodHandles$Lookup;findStatic", MethodHandleMethods.class);
remapVirtualMethodToStatic.put("java/lang/invoke/MethodHandles$Lookup;findVirtual", MethodHandleMethods.class);
remapVirtualMethodToStatic.put("java/lang/invoke/MethodHandles$Lookup;findSpecial", MethodHandleMethods.class);
remapVirtualMethodToStatic.put("java/lang/invoke/MethodHandles$Lookup;findGetter", MethodHandleMethods.class);
remapVirtualMethodToStatic.put("java/lang/invoke/MethodHandles$Lookup;findSetter", MethodHandleMethods.class);
remapVirtualMethodToStatic.put("java/lang/invoke/MethodHandles$Lookup;findStaticGetter", MethodHandleMethods.class);
remapVirtualMethodToStatic.put("java/lang/invoke/MethodHandles$Lookup;findStaticSetter", MethodHandleMethods.class);
remapVirtualMethodToStatic.put("java/lang/invoke/MethodHandles$Lookup;unreflect", MethodHandleMethods.class);

remapSuperClass.put("java/net/URLClassLoader", CatURLClassLoader.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ public class MethodHandleMethods {
}
}

public static MethodHandle findSpecial(MethodHandles.Lookup lookup, Class<?> refc, String name, MethodType type, Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException {
// MethodHandles$Lookup.findStatic
public static MethodHandle findStatic(MethodHandles.Lookup lookup, Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
if (refc.getName().startsWith("net.minecraft.")) {
name = RemapUtils.mapMethod(refc, name, type.parameterArray());
} else {
Class<?> remappedClass = RemapRules.getStaticMethodTarget((refc.getName().replace(".", "/") + ";" + name));
if (remappedClass != null) {
refc = remappedClass;
}
}
return lookup.findSpecial(refc, name, type, specialCaller);
return lookup.findStatic(refc, name, type);
}

// MethodHandles$Lookup.findVirtual
public static MethodHandle findVirtual(MethodHandles.Lookup lookup, Class<?> refc, String name, MethodType oldType) throws NoSuchMethodException, IllegalAccessException {
if (refc.getName().startsWith("net.minecraft.")) {
name = RemapUtils.mapMethod(refc, name, oldType.parameterArray());
Expand All @@ -51,23 +58,53 @@ public static MethodHandle findVirtual(MethodHandles.Lookup lookup, Class<?> ref
return lookup.findVirtual(refc, name, oldType);
}

public static MethodHandle findStatic(MethodHandles.Lookup lookup, Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
// MethodHandles$Lookup.findSpecial
public static MethodHandle findSpecial(MethodHandles.Lookup lookup, Class<?> refc, String name, MethodType type, Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException {
if (refc.getName().startsWith("net.minecraft.")) {
name = RemapUtils.mapMethod(refc, name, type.parameterArray());
} else {
Class<?> remappedClass = RemapRules.getStaticMethodTarget((refc.getName().replace(".", "/") + ";" + name));
if (remappedClass != null) {
refc = remappedClass;
}
}
return lookup.findStatic(refc, name, type);
return lookup.findSpecial(refc, name, type, specialCaller);
}

public static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader) {
// MethodHandles$Lookup.findGetter
public static MethodHandle findGetter(MethodHandles.Lookup lookup, Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
if (refc.getName().startsWith("net.minecraft.")) {
name = RemapUtils.mapFieldName(refc, name);
}
return lookup.findGetter(refc, name, type);
}

// MethodHandles$Lookup.findSetter
public static MethodHandle findSetter(MethodHandles.Lookup lookup, Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
if (refc.getName().startsWith("net.minecraft.")) {
name = RemapUtils.mapFieldName(refc, name);
}
return lookup.findSetter(refc, name, type);
}

// MethodHandles$Lookup.findStaticGetter
public static MethodHandle findStaticGetter(MethodHandles.Lookup lookup, Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
if (refc.getName().startsWith("net.minecraft.")) {
name = RemapUtils.mapFieldName(refc, name);
}
return lookup.findStaticGetter(refc, name, type);
}

// MethodHandles$Lookup.findStaticSetter
public static MethodHandle findStaticSetter(MethodHandles.Lookup lookup, Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
if (refc.getName().startsWith("net.minecraft.")) {
name = RemapUtils.mapFieldName(refc, name);
}
return lookup.findStaticSetter(refc, name, type);
}

// MethodType.fromMethodDescriptorString
private static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader) {
String remapDesc = map.getOrDefault(descriptor, descriptor);
return MethodType.fromMethodDescriptorString(remapDesc, loader);
}

// MethodHandles$Lookup.unreflect
public static MethodHandle unreflect(MethodHandles.Lookup lookup, Method m) throws IllegalAccessException {
Class<?> remappedClass = RemapRules.getVirtualMethodToStaticTarget((m.getDeclaringClass().getName().replace(".", "/") + ";" + m.getName()));
if (remappedClass != null) {
Expand All @@ -90,9 +127,7 @@ private static Method getClassReflectionMethod(MethodHandles.Lookup lookup, Clas
return remappedClass.getMethod(originalMethod.getName(), newParArr);
}


public static void loadMappings(BufferedReader reader) throws IOException {

private static void loadMappings(BufferedReader reader) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
int commentIndex = line.indexOf('#');
Expand All @@ -108,5 +143,4 @@ public static void loadMappings(BufferedReader reader) throws IOException {
map.put(firDesc, secDesc);
}
}

}

0 comments on commit d12a501

Please sign in to comment.