4949
5050import org .eu .smileyik .luajava .debug .LuaDebug ;
5151import 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 .*;
5653import org .eu .smileyik .luajava .util .*;
5754
5855import 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 ();
0 commit comments