[Feat] Add MappedClass MappedMethod MappedField and MappingRegistry

This commit is contained in:
xiaozhou233 committed 2026-09-03 18:14:02 +08:00
1 parent da8aa7bc10
commit 66fcce19b5
4 files changed
+382

No files matched your search

@@ -0,0 +1,60 @@
package cn.xiaozhou233.orangex.mapping;
import lombok.Getter;
import java.util.LinkedHashSet;
import java.util.Set;
public class MappedClass {
@Getter
private final String sourceName;
private Class<?> targetClass;
private boolean mapped;
public MappedClass(String sourceName) {
this.sourceName = sourceName;
this.mapped = false;
}
public Class<?> getTargetClass(ClassLoader... preferredLoaders) {
if(mapped) {
return targetClass;
}
targetClass = findClass(sourceName, preferredLoaders);
mapped = true;
return targetClass;
}
private static Class<?> findClass(String internalName, ClassLoader... preferredLoaders) {
String binaryName = internalName.replace('/', '.');
Set<ClassLoader> loaders = candidateLoaders(preferredLoaders);
for (ClassLoader loader : loaders) {
try {
return Class.forName(binaryName, false, loader);
} catch (ClassNotFoundException | LinkageError ignored) {
}
}
return null;
}
private static Set<ClassLoader> candidateLoaders(ClassLoader... preferredLoaders) {
Set<ClassLoader> loaders = new LinkedHashSet<>();
if (preferredLoaders != null) {
for (ClassLoader loader : preferredLoaders) {
if (loader != null) loaders.add(loader);
}
}
ClassLoader context = Thread.currentThread().getContextClassLoader();
if (context != null) loaders.add(context);
ClassLoader self = MappedClass.class.getClassLoader();
if (self != null) loaders.add(self);
try {
ClassLoader system = ClassLoader.getSystemClassLoader();
if (system != null) loaders.add(system);
} catch (SecurityException ignored) {
}
return loaders;
}
}
@@ -0,0 +1,132 @@
package cn.xiaozhou233.orangex.mapping;
import lombok.Getter;
import java.lang.reflect.Field;
public class MappedField {
private final MappedClass owner;
@Getter
private final String sourceName;
private final Class<?> fieldType;
private Field targetField;
private boolean mapped;
public MappedField(MappedClass owner, String sourceName, Class<?> fieldType) {
this.owner = owner;
this.sourceName = sourceName;
this.fieldType = fieldType;
}
public Field getTargetField(MappingRegistry registry) {
if (mapped) {
return targetField;
}
Class<?> ownerClass = owner.getTargetClass();
if (ownerClass == null) return null;
String runtimeName = registry.lookupField(owner.getSourceName(), sourceName);
if (runtimeName == null) runtimeName = sourceName;
try {
targetField = findField(ownerClass, runtimeName);
} catch (Exception ignored) {
}
return targetField;
}
public Object get(Object instance) {
if (targetField == null) return null;
try {
return targetField.get(instance);
} catch (Exception e) {
throw new RuntimeException("Failed to get field " + sourceName, e);
}
}
public void set(Object instance, Object value) {
if (targetField == null) return;
try {
targetField.set(instance, value);
} catch (Exception e) {
throw new RuntimeException("Failed to set field " + sourceName, e);
}
}
public int getInt(Object instance) {
if (targetField == null) return 0;
try {
return targetField.getInt(instance);
} catch (Exception e) {
throw new RuntimeException("Failed to get int field " + sourceName, e);
}
}
public void setInt(Object instance, int value) {
if (targetField == null) return;
try {
targetField.setInt(instance, value);
} catch (Exception e) {
throw new RuntimeException("Failed to set int field " + sourceName, e);
}
}
public boolean getBoolean(Object instance) {
if (targetField == null) return false;
try {
return targetField.getBoolean(instance);
} catch (Exception e) {
throw new RuntimeException("Failed to get boolean field " + sourceName, e);
}
}
public void setBoolean(Object instance, boolean value) {
if (targetField == null) return;
try {
targetField.setBoolean(instance, value);
} catch (Exception e) {
throw new RuntimeException("Failed to set boolean field " + sourceName, e);
}
}
public double getDouble(Object instance) {
if (targetField == null) return 0;
try {
return targetField.getDouble(instance);
} catch (Exception e) {
throw new RuntimeException("Failed to get double field " + sourceName, e);
}
}
public float getFloat(Object instance) {
if (targetField == null) return 0;
try {
return targetField.getFloat(instance);
} catch (Exception e) {
throw new RuntimeException("Failed to get float field " + sourceName, e);
}
}
public long getLong(Object instance) {
if (targetField == null) return 0;
try {
return targetField.getLong(instance);
} catch (Exception e) {
throw new RuntimeException("Failed to get long field " + sourceName, e);
}
}
private static Field findField(Class<?> clazz, String name) throws NoSuchFieldException {
try {
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
return field;
} catch (NoSuchFieldException e) {
if (clazz.getSuperclass() != null) {
return findField(clazz.getSuperclass(), name);
}
throw e;
}
}
}
@@ -0,0 +1,92 @@
package cn.xiaozhou233.orangex.mapping;
import lombok.Getter;
import java.lang.reflect.Method;
public class MappedMethod {
private final MappedClass owner;
@Getter
private final String sourceName;
private final Class<?> returnType;
private final Class<?>[] paramTypes;
private Method targetMethod;
private boolean mapped;
public MappedMethod(MappedClass owner, String sourceName, Class<?> returnType, Class<?>... paramTypes) {
this.owner = owner;
this.sourceName = sourceName;
this.returnType = returnType;
this.paramTypes = paramTypes;
}
public Method getTargetMethod(MappingRegistry registry) {
if(mapped) {
return targetMethod;
}
Class<?> ownerClass = owner.getTargetClass();
if (ownerClass == null) return null;
String runtimeName = registry.lookupMethod(owner.getSourceName(), sourceName, paramTypes);
if (runtimeName == null) runtimeName = sourceName;
try {
targetMethod = findMethod(ownerClass, runtimeName, paramTypes);
if (targetMethod != null) {
targetMethod.setAccessible(true);
}
} catch (Exception ignored) {
}
return targetMethod;
}
public Object invoke(Object instance, Object... args) {
if (targetMethod == null) return null;
try {
return targetMethod.invoke(instance, args);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke method " + sourceName, e);
}
}
public void invokeVoid(Object instance, Object... args) {
if (targetMethod == null) return;
try {
targetMethod.invoke(instance, args);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke void method " + sourceName, e);
}
}
public boolean invokeBoolean(Object instance, Object... args) {
if (targetMethod == null) return false;
try {
return (boolean) targetMethod.invoke(instance, args);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke boolean method " + sourceName, e);
}
}
public int invokeInt(Object instance, Object... args) {
if (targetMethod == null) return 0;
try {
return (int) targetMethod.invoke(instance, args);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke int method " + sourceName, e);
}
}
private static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
try {
Method method = clazz.getDeclaredMethod(name, paramTypes);
method.setAccessible(true);
return method;
} catch (NoSuchMethodException e) {
if (clazz.getSuperclass() != null) {
return findMethod(clazz.getSuperclass(), name, paramTypes);
}
return null;
}
}
}
@@ -0,0 +1,98 @@
package cn.xiaozhou233.orangex.mapping;
import java.util.HashMap;
import java.util.Map;
public class MappingRegistry {
private final Map<String, String> classMappings = new HashMap<>();
private final Map<String, String> fieldMappings = new HashMap<>();
private final Map<String, String> methodMappings = new HashMap<>();
public void registerClass(String sourceName, String runtimeName) {
classMappings.put(sourceName, runtimeName);
}
public void registerField(String ownerAndName, String runtimeName) {
fieldMappings.put(ownerAndName, runtimeName);
}
public void registerMethod(String ownerAndDescriptor, String runtimeName) {
methodMappings.put(ownerAndDescriptor, runtimeName);
}
public void registerField(String ownerName, String fieldName, String runtimeName) {
fieldMappings.put(ownerName + "/" + fieldName, runtimeName);
}
public void registerMethod(String ownerName, String methodName, String descriptor, String runtimeName) {
methodMappings.put(ownerName + "/" + methodName + " " + descriptor, runtimeName);
}
public String lookupClass(String sourceName) {
return classMappings.get(sourceName);
}
public String lookupField(String ownerName, String fieldName) {
String result = fieldMappings.get(ownerName + "/" + fieldName);
if (result == null) {
result = fieldMappings.get(fieldName);
}
return result;
}
public String lookupMethod(String ownerName, String methodName, Class<?>[] paramTypes) {
String descriptor = buildDescriptor(paramTypes);
String result = methodMappings.get(ownerName + "/" + methodName + " " + descriptor);
if (result == null) {
result = methodMappings.get(methodName + " " + descriptor);
}
if (result == null) {
result = methodMappings.get(methodName);
}
return result;
}
private static String buildDescriptor(Class<?>[] paramTypes) {
if (paramTypes == null || paramTypes.length == 0) {
return "()";
}
StringBuilder sb = new StringBuilder("(");
for (Class<?> type : paramTypes) {
if (type == Integer.TYPE) sb.append('I');
else if (type == Boolean.TYPE) sb.append('Z');
else if (type == Float.TYPE) sb.append('F');
else if (type == Double.TYPE) sb.append('D');
else if (type == Long.TYPE) sb.append('J');
else if (type == Short.TYPE) sb.append('S');
else if (type == Byte.TYPE) sb.append('B');
else if (type == Character.TYPE) sb.append('C');
else if (type == Void.TYPE) sb.append('V');
else sb.append('L').append(type.getName().replace('.', '/')).append(';');
}
sb.append(')');
return sb.toString();
}
public void loadFrom(MappingData data) {
for (Map.Entry<String, String> entry : data.getClassName().entrySet()) {
classMappings.put(entry.getValue(), entry.getKey());
}
for (Map.Entry<String, String> entry : data.getFieldName().entrySet()) {
fieldMappings.put(entry.getValue(), entry.getKey());
}
for (Map.Entry<String, String> entry : data.getMethodName().entrySet()) {
methodMappings.put(entry.getValue(), entry.getKey());
}
}
public void loadIdentityFor(String mcpClassName) {
String internalName = mcpClassName.replace('.', '/');
classMappings.put(internalName, internalName);
}
public void clear() {
classMappings.clear();
fieldMappings.clear();
methodMappings.clear();
}
}