Hello everybody,
When trying to call private classes methods that implements interfaces such as ( Iterator or ResultSet ) via java reflection, you will get an exception like :
Exception in thread "main" org.luaj.vm2.LuaError: coercion error java.lang.IllegalAccessException: Class org.luaj.vm2.lib.jse.JavaMethod can not access a member of class java.util.ArrayList$Itr with modifiers "public"
I added this little piece of code to to invokeMethod(Object instance, Varargs args) in org.luaj.vm2.lib.jse.JavaMethod and this solved the problem :
Class interf[]= method.getDeclaringClass().getInterfaces();
for(int i= 0; i< interf.length; ++i)
{
if( ( java.sql.ResultSet.class== interf[i] ) || ( java.util.Iterator.class == interf[i] ) )
{
try
{
method= interf[i].getMethod(method.getName(), method.getParameterTypes());
}
catch (NoSuchMethodException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SecurityException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
Here is a slightly improved version of the previous code :
Class<?> cls= null;
if( java.util.Iterator.class.isAssignableFrom( method.getDeclaringClass() ) )
{
cls= java.util.Iterator.class;
}
else
if( java.sql.ResultSet.class.isAssignableFrom( method.getDeclaringClass() ) )
cls= java.sql.ResultSet.class;
if( cls!= null )
{
try
{
method= cls.getMethod(method.getName(), method.getParameterTypes());
}
catch (NoSuchMethodException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SecurityException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}