Skip to content

Commit 7908352

Browse files
committed
feat: wrap class for Field, Constructor and Method.
1 parent f9e8912 commit 7908352

9 files changed

Lines changed: 183 additions & 55 deletions

File tree

java/src/main/java/org/eu/smileyik/luajava/LuaJavaAPI.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@
4949

5050
import org.eu.smileyik.luajava.debug.LuaDebug;
5151
import org.eu.smileyik.luajava.exception.Result;
52-
import org.eu.smileyik.luajava.reflect.ConvertablePriority;
53-
import org.eu.smileyik.luajava.reflect.LuaInvokedMethod;
54-
import org.eu.smileyik.luajava.reflect.ReflectUtil;
55-
import org.eu.smileyik.luajava.reflect.SimpleReflectUtil;
52+
import org.eu.smileyik.luajava.reflect.*;
5653
import org.eu.smileyik.luajava.util.*;
5754

5855
import java.io.IOException;
@@ -186,13 +183,14 @@ public static int objectIndex(int luaState, Object obj, String methodName, boole
186183
} else {
187184
clazz = obj.getClass();
188185
}
189-
method = findMethod(luaStateFacade, clazz, methodName, objs, top);
186+
IExecutable<Method> methodWrapper = findMethod(luaStateFacade, clazz, methodName, objs, top);
190187

191188
// If method is null means there isn't one receiving the given arguments
192-
if (method == null) {
189+
if (methodWrapper == null) {
193190
throw new LuaException("Invalid method call. No such method.");
194191
}
195192

193+
method = methodWrapper.getExecutable();
196194
Object ret;
197195
try {
198196
boolean isStatic = Modifier.isStatic(method.getModifiers());
@@ -202,9 +200,9 @@ public static int objectIndex(int luaState, Object obj, String methodName, boole
202200

203201
//if (obj instanceof Class)
204202
if (isStatic) {
205-
ret = method.invoke(null, objs);
203+
ret = methodWrapper.invoke(null, objs);
206204
} else {
207-
ret = method.invoke(obj, objs);
205+
ret = methodWrapper.invoke(obj, objs);
208206
}
209207
} catch (Exception e) {
210208
throw new LuaException(e);
@@ -280,12 +278,13 @@ public static int objectNewIndex(int luaState, Object obj, String fieldName)
280278
LuaStateFacade luaStateFacade = LuaStateFactory.getExistingState(luaState);
281279
// like a.b = 1
282280
Class<?> targetClass = obj instanceof Class<?> ? (Class<?>) obj : obj.getClass();
283-
Field field = reflectUtil.findFieldByName(targetClass, fieldName,
281+
IFieldAccessor fieldAccessor = reflectUtil.findFieldByName(targetClass, fieldName,
284282
false, false, false, luaStateFacade.isIgnoreNotPublic());
285-
if (field == null) {
283+
if (fieldAccessor == null) {
286284
throw new LuaException("Error accessing field " + fieldName);
287285
}
288286
// checkField method already checked the obj can access this field or not.
287+
Field field = fieldAccessor.getField();
289288
if (!field.canAccess(Modifier.isStatic(field.getModifiers()) ? null : obj)) {
290289
field.setAccessible(true);
291290
}
@@ -297,7 +296,7 @@ public static int objectNewIndex(int luaState, Object obj, String fieldName)
297296
throw new LuaException("Invalid type.");
298297
}
299298
try {
300-
field.set(obj, setObjRet.getValue());
299+
fieldAccessor.set(obj, setObjRet.getValue());
301300
} catch (IllegalAccessException e) {
302301
throw new LuaException("Field not accessible.", e);
303302
}
@@ -419,16 +418,17 @@ public static int checkField(int luaState, Object obj, String fieldName) throws
419418
LuaStateFacade luaStateFacade = LuaStateFactory.getExistingState(luaState);
420419
Class<?> targetClass = obj instanceof Class<?> ? (Class<?>) obj : obj.getClass();
421420
boolean isStatic = targetClass == obj;
422-
Field field = reflectUtil.findFieldByName(targetClass, fieldName,
421+
IFieldAccessor fieldAccessor = reflectUtil.findFieldByName(targetClass, fieldName,
423422
false, false, isStatic, luaStateFacade.isIgnoreNotPublic());
424-
if (field == null) return 0;
423+
if (fieldAccessor == null) return 0;
425424
try {
425+
Field field = fieldAccessor.getField();
426426
Object ret;
427427
if (field.canAccess(Modifier.isStatic(field.getModifiers()) ? null : obj)) {
428-
ret = field.get(obj);
428+
ret = fieldAccessor.get(obj);
429429
} else if (!luaStateFacade.isIgnoreNotPublic()) {
430430
field.setAccessible(true);
431-
ret = field.get(obj);
431+
ret = fieldAccessor.get(obj);
432432
} else {
433433
return 0;
434434
}
@@ -534,7 +534,7 @@ private static Object getObjInstance(LuaStateFacade luaStateFacade, LuaState L,
534534
int paramsCount = top - 1;
535535
Object[] objs = getLuaParams(luaStateFacade, paramsCount);
536536

537-
LuaInvokedMethod<Constructor<?>> result = reflectUtil.findConstructorByParams(
537+
LuaInvokedMethod<IExecutable<Constructor<?>>> result = reflectUtil.findConstructorByParams(
538538
clazz, objs, luaStateFacade.isIgnoreNotPublic(), false, false);
539539

540540
if (result == null) {
@@ -543,28 +543,28 @@ private static Object getObjInstance(LuaStateFacade luaStateFacade, LuaState L,
543543
result.getOverwriteParams().forEach((idx, obj) -> objs[idx] = obj);
544544
Object ret;
545545
try {
546-
ret = result.getExecutable().newInstance(objs);
546+
ret = result.getExecutable().invoke(null, objs);
547547
} catch (Exception e) {
548548
throw new LuaException(e);
549549
}
550550
return ret;
551551
}
552552

553-
private static Method findMethod(LuaStateFacade luaStateFacade, Class<?> clazz,
554-
String methodName, Object[] retObjs, int top) throws LuaException {
553+
private static IExecutable<Method> findMethod(LuaStateFacade luaStateFacade, Class<?> clazz,
554+
String methodName, Object[] retObjs, int top) throws LuaException {
555555
// Convert lua params to java params
556556
int paramsCount = top - 1;
557557
Object[] objs = getLuaParams(luaStateFacade, paramsCount);
558558

559-
LinkedList<LuaInvokedMethod<Method>> list = reflectUtil.findMethodByParams(
559+
LinkedList<LuaInvokedMethod<IExecutable<Method>>> list = reflectUtil.findMethodByParams(
560560
clazz, methodName, objs, luaStateFacade.isJustUseFirstMethod(),
561561
luaStateFacade.isIgnoreNotPublic(), false, false);
562562
if (list.isEmpty()) {
563563
return null;
564564
} else if (list.size() > 1) {
565565
throw new LuaException("Found multi result for method " + methodName + " in class " + clazz);
566566
}
567-
LuaInvokedMethod<Method> invokedMethod = list.getFirst();
567+
LuaInvokedMethod<IExecutable<Method>> invokedMethod = list.getFirst();
568568
invokedMethod.getOverwriteParams().forEach((idx, obj) -> objs[idx] = obj);
569569
System.arraycopy(objs, 0, retObjs, 0, objs.length);
570570
return invokedMethod.getExecutable();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.eu.smileyik.luajava.reflect;
2+
3+
import java.lang.reflect.Executable;
4+
import java.lang.reflect.InvocationTargetException;
5+
6+
public interface IExecutable <T extends Executable> {
7+
public Object invoke(Object instance, Object[] params) throws InvocationTargetException, IllegalAccessException, InstantiationException;
8+
9+
public T getExecutable();
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.eu.smileyik.luajava.reflect;
2+
3+
import java.lang.reflect.Field;
4+
5+
public interface IFieldAccessor {
6+
7+
public Object get(Object instance) throws IllegalAccessException;
8+
9+
public void set(Object instance, Object value) throws IllegalAccessException;
10+
11+
Field getField();
12+
}

java/src/main/java/org/eu/smileyik/luajava/reflect/LuaInvokedMethod.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323

2424
package org.eu.smileyik.luajava.reflect;
2525

26-
import java.lang.reflect.Executable;
2726
import java.util.HashMap;
2827
import java.util.Map;
2928

30-
public class LuaInvokedMethod<T extends Executable> {
29+
public class LuaInvokedMethod<T> {
3130
private T executable;
3231
private final Map<Integer, Object> overwriteParams;
3332

@@ -45,6 +44,10 @@ public void reset(T executable) {
4544
this.overwriteParams.clear();
4645
}
4746

47+
public void setExecutable(T executable) {
48+
this.executable = executable;
49+
}
50+
4851
public void clear() {
4952
this.executable = null;
5053
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.eu.smileyik.luajava.reflect;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.InvocationTargetException;
5+
6+
public class ReflectConstructor implements IExecutable<Constructor<?>> {
7+
8+
protected final Constructor<?> constructor;
9+
10+
public ReflectConstructor(Constructor<?> constructor) {
11+
this.constructor = constructor;
12+
}
13+
14+
@Override
15+
public Object invoke(Object instance, Object[] params) throws InvocationTargetException, IllegalAccessException, InstantiationException {
16+
return constructor.newInstance(params);
17+
}
18+
19+
@Override
20+
public Constructor<?> getExecutable() {
21+
return constructor;
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.eu.smileyik.luajava.reflect;
2+
3+
import java.lang.reflect.Field;
4+
5+
public class ReflectField implements IFieldAccessor {
6+
protected final Field field;
7+
8+
public ReflectField(Field field) {
9+
this.field = field;
10+
}
11+
12+
@Override
13+
public Object get(Object instance) throws IllegalAccessException {
14+
return field.get(instance);
15+
}
16+
17+
@Override
18+
public void set(Object instance, Object value) throws IllegalAccessException {
19+
field.set(instance, value);
20+
}
21+
22+
@Override
23+
public Field getField() {
24+
return field;
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.eu.smileyik.luajava.reflect;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
6+
public class ReflectMethod implements IExecutable<Method> {
7+
protected final Method method;
8+
public ReflectMethod(Method method) {
9+
this.method = method;
10+
}
11+
12+
@Override
13+
public Object invoke(Object instance, Object[] params) throws InvocationTargetException, IllegalAccessException {
14+
return method.invoke(instance, params);
15+
}
16+
17+
@Override
18+
public Method getExecutable() {
19+
return method;
20+
}
21+
}

java/src/main/java/org/eu/smileyik/luajava/reflect/ReflectUtil.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626
import org.eu.smileyik.luajava.util.ParamRef;
2727

28-
import java.lang.reflect.*;
28+
import java.lang.reflect.Constructor;
29+
import java.lang.reflect.Executable;
30+
import java.lang.reflect.Method;
31+
import java.lang.reflect.Modifier;
2932
import java.util.*;
3033
import java.util.function.Function;
3134

@@ -44,11 +47,11 @@ public interface ReflectUtil {
4447
* @param ignoreNotPublic ignore not public field
4548
* @return the target filed
4649
*/
47-
public Field findFieldByName(Class<?> clazz, String name,
48-
boolean ignoreFinal,
49-
boolean ignoreStatic,
50-
boolean ignoreNotStatic,
51-
boolean ignoreNotPublic);
50+
public IFieldAccessor findFieldByName(Class<?> clazz, String name,
51+
boolean ignoreFinal,
52+
boolean ignoreStatic,
53+
boolean ignoreNotStatic,
54+
boolean ignoreNotPublic);
5255

5356
/**
5457
* find constructor by params.
@@ -60,7 +63,7 @@ public Field findFieldByName(Class<?> clazz, String name,
6063
* @param ignoreNotStatic ..
6164
* @return result. my be null
6265
*/
63-
public LuaInvokedMethod<Constructor<?>> findConstructorByParams(
66+
public LuaInvokedMethod<IExecutable<Constructor<?>>> findConstructorByParams(
6467
Class<?> clazz, Object[] params, boolean ignoreNotPublic,
6568
boolean ignoreStatic, boolean ignoreNotStatic
6669
);
@@ -77,7 +80,7 @@ public LuaInvokedMethod<Constructor<?>> findConstructorByParams(
7780
* @param ignoreNotStatic skip the method witch is not static
7881
* @return list of suitable methods.
7982
*/
80-
public LinkedList<LuaInvokedMethod<Method>> findMethodByParams(
83+
public LinkedList<LuaInvokedMethod<IExecutable<Method>>> findMethodByParams(
8184
Class<?> clazz, String methodName,
8285
Object[] params, boolean justFirst,
8386
boolean ignoreNotPublic,
@@ -115,7 +118,7 @@ public boolean existsMethodByName(
115118
* @param ignoreNotStatic skip the method witch is not static
116119
* @return list of suitable methods.
117120
*/
118-
public LinkedList<LuaInvokedMethod<Method>> findMethodByParams(
121+
public LinkedList<LuaInvokedMethod<IExecutable<Method>>> findMethodByParams(
119122
ReflectExecutableCacheKey cacheKey,
120123
Class<?> clazz, String methodName,
121124
Object[] params, boolean justFirst,

0 commit comments

Comments
 (0)