From a2ddf1f709480fac2c8b29b56156e26e0411ba0b Mon Sep 17 00:00:00 2001 From: Saturn Date: Tue, 20 May 2025 22:50:08 +0000 Subject: [PATCH 1/2] Switch to fast-reflect --- luajava/build.gradle | 10 +++ .../java/party/iroiro/luajava/JuaAPI.java | 70 ++++++++++++------- 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/luajava/build.gradle b/luajava/build.gradle index 35c32641..fdc13276 100644 --- a/luajava/build.gradle +++ b/luajava/build.gradle @@ -6,6 +6,15 @@ plugins { repositories { mavenCentral() + maven { + url = uri("https://maven.pkg.github.com/lualink/fast-reflection") + name = "FastReflection" + credentials { + username = findProperty("gpr.actor") ?: System.getenv("GITHUB_ACTOR") + password = findProperty("gpr.token") ?: System.getenv("GITHUB_TOKEN") + } + } + } group = rootProject.group @@ -14,6 +23,7 @@ version = rootProject.version dependencies { implementation 'org.jetbrains:annotations:24.1.0' implementation 'com.badlogicgames.gdx:gdx-jnigen-loader:2.5.2' + implementation 'me.sunlan:fast-reflection:1.0.3' } tasks.withType(JavaCompile) { diff --git a/luajava/src/main/java/party/iroiro/luajava/JuaAPI.java b/luajava/src/main/java/party/iroiro/luajava/JuaAPI.java index cc0b7040..49395853 100644 --- a/luajava/src/main/java/party/iroiro/luajava/JuaAPI.java +++ b/luajava/src/main/java/party/iroiro/luajava/JuaAPI.java @@ -29,6 +29,10 @@ import party.iroiro.luajava.value.LuaValue; import java.lang.reflect.*; +import me.sunlan.fastreflection.FastMethod; +import me.sunlan.fastreflection.FastField; +import me.sunlan.fastreflection.FastConstructor; +import me.sunlan.fastreflection.FastClass; import java.nio.ByteBuffer; import java.util.*; import java.util.regex.Pattern; @@ -155,18 +159,24 @@ public static int loadLib(int id, String className, String methodName) { AbstractLua L = Jua.get(id); try { Class clazz = ClassUtils.forName(className); - Method method = clazz.getDeclaredMethod(methodName, Lua.class); - if (method.getReturnType() == int.class) { - //noinspection Convert2Lambda + FastClass fastClass = FastClass.create(clazz); + FastMethod method; + try { + method = fastClass.getMethod(methodName, Lua.class); + } catch (NoSuchMethodException e) { + L.pushNil(); + L.push("\n no method '" + methodName + "': no such method"); + return 2; + } + // FastMethod.getReturnType() returns FastClass, so get the raw class + if (method.getReturnType().getRawClass() == int.class) { L.push(new JFunction() { @Override public int __call(Lua l) { try { return (Integer) method.invoke(null, l); - } catch (IllegalAccessException e) { + } catch (Throwable e) { return l.error(e); - } catch (InvocationTargetException e) { - return l.error(e.getCause()); } } }); @@ -176,7 +186,7 @@ public int __call(Lua l) { L.push("\n no method '" + methodName + "': not returning int values"); return 2; } - } catch (ClassNotFoundException | NoSuchMethodException ignored) { + } catch (ClassNotFoundException ignored) { L.pushNil(); L.push("\n no method '" + methodName + "': no such method"); return 2; @@ -817,10 +827,10 @@ private static Object[] transformVarArgs(Executable executable, Object[] objects private final static class OptionalField { @Nullable - public final Field field; + public final FastField fastField; - private OptionalField(@Nullable Field field) { - this.field = field; + private OptionalField(@Nullable FastField fastField) { + this.fastField = fastField; } } @@ -845,20 +855,24 @@ private OptionalField(@Nullable Field field) { public static int fieldIndex(Lua L, Class clazz, @Nullable Object object, String name) { try { OptionalField optionalField = OBJECT_FIELD_CACHE.get(clazz, name); - Field field; + FastField fastField; if (optionalField == null) { - field = clazz.getField(name); - OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(field)); + FastClass fastClass = FastClass.create(clazz); + fastField = fastClass.getField(name); + OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(fastField)); } else { - field = optionalField.field; - if (field == null) { + fastField = optionalField.fastField; + if (fastField == null) { return 2; } } - Object obj = field.get(object); + if (fastField == null) { + return 2; + } + Object obj = fastField.get(object); L.push(obj, Lua.Conversion.SEMI); return 1; - } catch (NoSuchFieldException | IllegalAccessException | NullPointerException ignored) { + } catch (Throwable ignored) { OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(null)); return 2; } @@ -877,21 +891,27 @@ private static int fieldNewIndex(int index, Class clazz, Object object, Strin Lua L = Jua.get(index); try { OptionalField optionalField = OBJECT_FIELD_CACHE.get(clazz, name); - Field field; + FastField fastField; if (optionalField == null) { - field = clazz.getField(name); - OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(field)); + FastClass fastClass = FastClass.create(clazz); + fastField = fastClass.getField(name); + OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(fastField)); } else { - field = optionalField.field; - if (field == null) { + fastField = optionalField.fastField; + if (fastField == null) { return L.error(new NoSuchFieldException(name)); } } - Class type = field.getType(); + if (fastField == null) { + return L.error(new NoSuchFieldException(name)); + } + // FastField does not have getType(), but has getDeclaringClass() and getName(). + // To get the type, use reflection on the underlying class: + Class type = fastField.getDeclaringClass().getRawClass().getDeclaredField(fastField.getName()).getType(); Object o = convertFromLua(L, type, 3); - field.set(object, o); + fastField.set(object, o); return 0; - } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) { + } catch (Throwable e) { OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(null)); return L.error(e); } From 2db692e56c44ba19077ffdab5b0a37facaf72a61 Mon Sep 17 00:00:00 2001 From: Saturn Date: Fri, 23 May 2025 17:43:14 +0000 Subject: [PATCH 2/2] Update build.gradle --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index e2e95aa5..30a65593 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'party.iroiro.luajava' -version(System.getenv('IS_RELEASE') == 'true' ? '4.0.2' : '4.0.3-SNAPSHOT') +version(System.getenv('IS_RELEASE') == 'true' ? '4.0.2' : '4.0.5-SNAPSHOT') buildscript { repositories {