Skip to content

Commit a2ddf1f

Browse files
committed
Switch to fast-reflect
1 parent 045cc78 commit a2ddf1f

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

luajava/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ plugins {
66

77
repositories {
88
mavenCentral()
9+
maven {
10+
url = uri("https://maven.pkg.github.com/lualink/fast-reflection")
11+
name = "FastReflection"
12+
credentials {
13+
username = findProperty("gpr.actor") ?: System.getenv("GITHUB_ACTOR")
14+
password = findProperty("gpr.token") ?: System.getenv("GITHUB_TOKEN")
15+
}
16+
}
17+
918
}
1019

1120
group = rootProject.group
@@ -14,6 +23,7 @@ version = rootProject.version
1423
dependencies {
1524
implementation 'org.jetbrains:annotations:24.1.0'
1625
implementation 'com.badlogicgames.gdx:gdx-jnigen-loader:2.5.2'
26+
implementation 'me.sunlan:fast-reflection:1.0.3'
1727
}
1828

1929
tasks.withType(JavaCompile) {

luajava/src/main/java/party/iroiro/luajava/JuaAPI.java

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
import party.iroiro.luajava.value.LuaValue;
3030

3131
import java.lang.reflect.*;
32+
import me.sunlan.fastreflection.FastMethod;
33+
import me.sunlan.fastreflection.FastField;
34+
import me.sunlan.fastreflection.FastConstructor;
35+
import me.sunlan.fastreflection.FastClass;
3236
import java.nio.ByteBuffer;
3337
import java.util.*;
3438
import java.util.regex.Pattern;
@@ -155,18 +159,24 @@ public static int loadLib(int id, String className, String methodName) {
155159
AbstractLua L = Jua.get(id);
156160
try {
157161
Class<?> clazz = ClassUtils.forName(className);
158-
Method method = clazz.getDeclaredMethod(methodName, Lua.class);
159-
if (method.getReturnType() == int.class) {
160-
//noinspection Convert2Lambda
162+
FastClass<?> fastClass = FastClass.create(clazz);
163+
FastMethod method;
164+
try {
165+
method = fastClass.getMethod(methodName, Lua.class);
166+
} catch (NoSuchMethodException e) {
167+
L.pushNil();
168+
L.push("\n no method '" + methodName + "': no such method");
169+
return 2;
170+
}
171+
// FastMethod.getReturnType() returns FastClass<?>, so get the raw class
172+
if (method.getReturnType().getRawClass() == int.class) {
161173
L.push(new JFunction() {
162174
@Override
163175
public int __call(Lua l) {
164176
try {
165177
return (Integer) method.invoke(null, l);
166-
} catch (IllegalAccessException e) {
178+
} catch (Throwable e) {
167179
return l.error(e);
168-
} catch (InvocationTargetException e) {
169-
return l.error(e.getCause());
170180
}
171181
}
172182
});
@@ -176,7 +186,7 @@ public int __call(Lua l) {
176186
L.push("\n no method '" + methodName + "': not returning int values");
177187
return 2;
178188
}
179-
} catch (ClassNotFoundException | NoSuchMethodException ignored) {
189+
} catch (ClassNotFoundException ignored) {
180190
L.pushNil();
181191
L.push("\n no method '" + methodName + "': no such method");
182192
return 2;
@@ -817,10 +827,10 @@ private static Object[] transformVarArgs(Executable executable, Object[] objects
817827

818828
private final static class OptionalField {
819829
@Nullable
820-
public final Field field;
830+
public final FastField fastField;
821831

822-
private OptionalField(@Nullable Field field) {
823-
this.field = field;
832+
private OptionalField(@Nullable FastField fastField) {
833+
this.fastField = fastField;
824834
}
825835
}
826836

@@ -845,20 +855,24 @@ private OptionalField(@Nullable Field field) {
845855
public static int fieldIndex(Lua L, Class<?> clazz, @Nullable Object object, String name) {
846856
try {
847857
OptionalField optionalField = OBJECT_FIELD_CACHE.get(clazz, name);
848-
Field field;
858+
FastField fastField;
849859
if (optionalField == null) {
850-
field = clazz.getField(name);
851-
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(field));
860+
FastClass<?> fastClass = FastClass.create(clazz);
861+
fastField = fastClass.getField(name);
862+
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(fastField));
852863
} else {
853-
field = optionalField.field;
854-
if (field == null) {
864+
fastField = optionalField.fastField;
865+
if (fastField == null) {
855866
return 2;
856867
}
857868
}
858-
Object obj = field.get(object);
869+
if (fastField == null) {
870+
return 2;
871+
}
872+
Object obj = fastField.get(object);
859873
L.push(obj, Lua.Conversion.SEMI);
860874
return 1;
861-
} catch (NoSuchFieldException | IllegalAccessException | NullPointerException ignored) {
875+
} catch (Throwable ignored) {
862876
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(null));
863877
return 2;
864878
}
@@ -877,21 +891,27 @@ private static int fieldNewIndex(int index, Class<?> clazz, Object object, Strin
877891
Lua L = Jua.get(index);
878892
try {
879893
OptionalField optionalField = OBJECT_FIELD_CACHE.get(clazz, name);
880-
Field field;
894+
FastField fastField;
881895
if (optionalField == null) {
882-
field = clazz.getField(name);
883-
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(field));
896+
FastClass<?> fastClass = FastClass.create(clazz);
897+
fastField = fastClass.getField(name);
898+
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(fastField));
884899
} else {
885-
field = optionalField.field;
886-
if (field == null) {
900+
fastField = optionalField.fastField;
901+
if (fastField == null) {
887902
return L.error(new NoSuchFieldException(name));
888903
}
889904
}
890-
Class<?> type = field.getType();
905+
if (fastField == null) {
906+
return L.error(new NoSuchFieldException(name));
907+
}
908+
// FastField does not have getType(), but has getDeclaringClass() and getName().
909+
// To get the type, use reflection on the underlying class:
910+
Class<?> type = fastField.getDeclaringClass().getRawClass().getDeclaredField(fastField.getName()).getType();
891911
Object o = convertFromLua(L, type, 3);
892-
field.set(object, o);
912+
fastField.set(object, o);
893913
return 0;
894-
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
914+
} catch (Throwable e) {
895915
OBJECT_FIELD_CACHE.put(clazz, name, new OptionalField(null));
896916
return L.error(e);
897917
}

0 commit comments

Comments
 (0)