From c5081c5de1a86f18ac63099b3170e4f769fd7931 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Sun, 8 Mar 2015 20:22:30 +0000 Subject: [PATCH] Convert anonymous classes to inner classes (gradle build support). --- README.html | 1 + src/core/org/luaj/vm2/LoadState.java | 13 +- src/jse/luajc.java | 24 ++-- .../org/luaj/vm2/lib/jse/CoerceJavaToLua.java | 133 ++++++++++-------- src/jse/org/luaj/vm2/lib/jse/JavaArray.java | 12 +- src/jse/org/luaj/vm2/lib/jse/JseProcess.java | 56 +++++--- src/jse/org/luaj/vm2/lib/jse/LuajavaLib.java | 62 ++++---- src/jse/org/luaj/vm2/luajc/BasicBlock.java | 81 ++++++++--- src/jse/org/luaj/vm2/luajc/VarInfo.java | 32 +++-- 9 files changed, 259 insertions(+), 155 deletions(-) diff --git a/README.html b/README.html index 5a7f4746..c92dbef4 100644 --- a/README.html +++ b/README.html @@ -966,6 +966,7 @@ Files are no longer hosted at LuaForge.   3.0.1 diff --git a/src/core/org/luaj/vm2/LoadState.java b/src/core/org/luaj/vm2/LoadState.java index 9f7377f7..992d887b 100644 --- a/src/core/org/luaj/vm2/LoadState.java +++ b/src/core/org/luaj/vm2/LoadState.java @@ -85,12 +85,7 @@ import org.luaj.vm2.compiler.DumpState; public class LoadState { /** Shared instance of Globals.Undumper to use loading prototypes from binary lua files */ - public static final Globals.Undumper instance = new Globals.Undumper() { - public Prototype undump(InputStream stream, String chunkname) - throws IOException { - return LoadState.undump(stream, chunkname); - } - }; + public static final Globals.Undumper instance = new GlobalsUndumper(); /** format corresponding to non-number-patched lua, all numbers are floats or doubles */ public static final int NUMBER_FORMAT_FLOATS_OR_DOUBLES = 0; @@ -437,4 +432,10 @@ public class LoadState { this.is = new DataInputStream( stream ); } + private static final class GlobalsUndumper implements Globals.Undumper { + public Prototype undump(InputStream stream, String chunkname) + throws IOException { + return LoadState.undump(stream, chunkname); + } + } } diff --git a/src/jse/luajc.java b/src/jse/luajc.java index ae562d3a..f54176b4 100644 --- a/src/jse/luajc.java +++ b/src/jse/luajc.java @@ -180,6 +180,21 @@ public class luajc { } } + private static final class LocalClassLoader extends ClassLoader { + private final Hashtable t; + + private LocalClassLoader(Hashtable t) { + this.t = t; + } + + public Class findClass(String classname) throws ClassNotFoundException { + byte[] bytes = (byte[]) t.get(classname); + if ( bytes != null ) + return defineClass(classname, bytes, 0, bytes.length); + return super.findClass(classname); + } + } + class InputFile { public String luachunkname; public String srcfilename; @@ -230,14 +245,7 @@ public class luajc { // try to load the files if ( loadclasses ) { - ClassLoader loader = new ClassLoader() { - public Class findClass(String classname) throws ClassNotFoundException { - byte[] bytes = (byte[]) t.get(classname); - if ( bytes != null ) - return defineClass(classname, bytes, 0, bytes.length); - return super.findClass(classname); - } - }; + ClassLoader loader = new LocalClassLoader(t); for ( Enumeration e = t.keys(); e.hasMoreElements(); ) { String classname = (String) e.nextElement(); try { diff --git a/src/jse/org/luaj/vm2/lib/jse/CoerceJavaToLua.java b/src/jse/org/luaj/vm2/lib/jse/CoerceJavaToLua.java index d991e6a7..8ca3ec36 100644 --- a/src/jse/org/luaj/vm2/lib/jse/CoerceJavaToLua.java +++ b/src/jse/org/luaj/vm2/lib/jse/CoerceJavaToLua.java @@ -62,53 +62,87 @@ import org.luaj.vm2.LuaValue; * @see LuajavaLib */ public class CoerceJavaToLua { - + static interface Coercion { public LuaValue coerce( Object javaValue ); }; + private static final class BoolCoercion implements Coercion { + public LuaValue coerce( Object javaValue ) { + Boolean b = (Boolean) javaValue; + return b.booleanValue()? LuaValue.TRUE: LuaValue.FALSE; + } + } + + private static final class IntCoercion implements Coercion { + public LuaValue coerce( Object javaValue ) { + Number n = (Number) javaValue; + return LuaInteger.valueOf( n.intValue() ); + } + } + + private static final class CharCoercion implements Coercion { + public LuaValue coerce( Object javaValue ) { + Character c = (Character) javaValue; + return LuaInteger.valueOf( c.charValue() ); + } + } + + private static final class DoubleCoercion implements Coercion { + public LuaValue coerce( Object javaValue ) { + Number n = (Number) javaValue; + return LuaDouble.valueOf( n.doubleValue() ); + } + } + + private static final class StringCoercion implements Coercion { + public LuaValue coerce( Object javaValue ) { + return LuaString.valueOf( javaValue.toString() ); + } + } + + private static final class BytesCoercion implements Coercion { + public LuaValue coerce( Object javaValue ) { + return LuaValue.valueOf((byte[]) javaValue); + } + } + + private static final class ClassCoercion implements Coercion { + public LuaValue coerce( Object javaValue ) { + return JavaClass.forClass((Class) javaValue); + } + } + + private static final class InstanceCoercion implements Coercion { + public LuaValue coerce(Object javaValue) { + return new JavaInstance(javaValue); + } + } + + private static final class ArrayCoercion implements Coercion { + public LuaValue coerce(Object javaValue) { + // should be userdata? + return new JavaArray(javaValue); + } + } + + private static final class LuaCoercion implements Coercion { + public LuaValue coerce( Object javaValue ) { + return (LuaValue) javaValue; + } + } + + static final Map COERCIONS = new HashMap(); static { - Coercion boolCoercion = new Coercion() { - public LuaValue coerce( Object javaValue ) { - Boolean b = (Boolean) javaValue; - return b.booleanValue()? LuaValue.TRUE: LuaValue.FALSE; - } - } ; - Coercion intCoercion = new Coercion() { - public LuaValue coerce( Object javaValue ) { - Number n = (Number) javaValue; - return LuaInteger.valueOf( n.intValue() ); - } - } ; - Coercion charCoercion = new Coercion() { - public LuaValue coerce( Object javaValue ) { - Character c = (Character) javaValue; - return LuaInteger.valueOf( c.charValue() ); - } - } ; - Coercion doubleCoercion = new Coercion() { - public LuaValue coerce( Object javaValue ) { - Number n = (Number) javaValue; - return LuaDouble.valueOf( n.doubleValue() ); - } - } ; - Coercion stringCoercion = new Coercion() { - public LuaValue coerce( Object javaValue ) { - return LuaString.valueOf( javaValue.toString() ); - } - } ; - Coercion bytesCoercion = new Coercion() { - public LuaValue coerce( Object javaValue ) { - return LuaValue.valueOf((byte[]) javaValue); - } - } ; - Coercion classCoercion = new Coercion() { - public LuaValue coerce( Object javaValue ) { - return JavaClass.forClass((Class) javaValue); - } - } ; + Coercion boolCoercion = new BoolCoercion() ; + Coercion intCoercion = new IntCoercion() ; + Coercion charCoercion = new CharCoercion() ; + Coercion doubleCoercion = new DoubleCoercion() ; + Coercion stringCoercion = new StringCoercion() ; + Coercion bytesCoercion = new BytesCoercion() ; + Coercion classCoercion = new ClassCoercion() ; COERCIONS.put( Boolean.class, boolCoercion ); COERCIONS.put( Byte.class, intCoercion ); COERCIONS.put( Character.class, charCoercion ); @@ -153,22 +187,9 @@ public class CoerceJavaToLua { return c.coerce(o); } - static final Coercion instanceCoercion = new Coercion() { - public LuaValue coerce(Object javaValue) { - return new JavaInstance(javaValue); - } - }; + static final Coercion instanceCoercion = new InstanceCoercion(); - // should be userdata? - static final Coercion arrayCoercion = new Coercion() { - public LuaValue coerce(Object javaValue) { - return new JavaArray(javaValue); - } - }; + static final Coercion arrayCoercion = new ArrayCoercion(); - static final Coercion luaCoercion = new Coercion() { - public LuaValue coerce( Object javaValue ) { - return (LuaValue) javaValue; - } - } ; + static final Coercion luaCoercion = new LuaCoercion() ; } diff --git a/src/jse/org/luaj/vm2/lib/jse/JavaArray.java b/src/jse/org/luaj/vm2/lib/jse/JavaArray.java index cc361964..532730cf 100644 --- a/src/jse/org/luaj/vm2/lib/jse/JavaArray.java +++ b/src/jse/org/luaj/vm2/lib/jse/JavaArray.java @@ -41,16 +41,18 @@ import org.luaj.vm2.lib.OneArgFunction; */ class JavaArray extends LuaUserdata { + private static final class LenFunction extends OneArgFunction { + public LuaValue call(LuaValue u) { + return LuaValue.valueOf(Array.getLength(((LuaUserdata)u).m_instance)); + } + } + static final LuaValue LENGTH = valueOf("length"); static final LuaTable array_metatable; static { array_metatable = new LuaTable(); - array_metatable.rawset(LuaValue.LEN, new OneArgFunction() { - public LuaValue call(LuaValue u) { - return LuaValue.valueOf(Array.getLength(((LuaUserdata)u).m_instance)); - } - }); + array_metatable.rawset(LuaValue.LEN, new LenFunction()); } JavaArray(Object instance) { diff --git a/src/jse/org/luaj/vm2/lib/jse/JseProcess.java b/src/jse/org/luaj/vm2/lib/jse/JseProcess.java index 63b24495..3fcd79f1 100644 --- a/src/jse/org/luaj/vm2/lib/jse/JseProcess.java +++ b/src/jse/org/luaj/vm2/lib/jse/JseProcess.java @@ -90,27 +90,43 @@ public class JseProcess { private Thread copyBytes(final InputStream input, final OutputStream output, final InputStream ownedInput, final OutputStream ownedOutput) { - Thread t = (new Thread() { - public void run() { - try { - byte[] buf = new byte[1024]; - int r; - try { - while ((r = input.read(buf)) >= 0) { - output.write(buf, 0, r); - } - } finally { - if (ownedInput != null) - ownedInput.close(); - if (ownedOutput != null) - ownedOutput.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - }); + Thread t = (new CopyThread(output, ownedOutput, ownedInput, input)); t.start(); return t; } + + private static final class CopyThread extends Thread { + private final OutputStream output; + private final OutputStream ownedOutput; + private final InputStream ownedInput; + private final InputStream input; + + private CopyThread(OutputStream output, OutputStream ownedOutput, + InputStream ownedInput, InputStream input) { + this.output = output; + this.ownedOutput = ownedOutput; + this.ownedInput = ownedInput; + this.input = input; + } + + public void run() { + try { + byte[] buf = new byte[1024]; + int r; + try { + while ((r = input.read(buf)) >= 0) { + output.write(buf, 0, r); + } + } finally { + if (ownedInput != null) + ownedInput.close(); + if (ownedOutput != null) + ownedOutput.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } diff --git a/src/jse/org/luaj/vm2/lib/jse/LuajavaLib.java b/src/jse/org/luaj/vm2/lib/jse/LuajavaLib.java index 7091f89a..46da493e 100644 --- a/src/jse/org/luaj/vm2/lib/jse/LuajavaLib.java +++ b/src/jse/org/luaj/vm2/lib/jse/LuajavaLib.java @@ -81,7 +81,7 @@ import org.luaj.vm2.lib.VarArgFunction; * @see http://www.keplerproject.org/luajava/manual.html#luareference */ public class LuajavaLib extends VarArgFunction { - + static final int INIT = 0; static final int BINDCLASS = 1; static final int NEWINSTANCE = 2; @@ -139,32 +139,7 @@ public class LuajavaLib extends VarArgFunction { ifaces[i] = classForName(args.checkjstring(i+1)); // create the invocation handler - InvocationHandler handler = new InvocationHandler() { - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - String name = method.getName(); - LuaValue func = lobj.get(name); - if ( func.isnil() ) - return null; - boolean isvarargs = ((method.getModifiers() & METHOD_MODIFIERS_VARARGS) != 0); - int n = args!=null? args.length: 0; - LuaValue[] v; - if ( isvarargs ) { - Object o = args[--n]; - int m = Array.getLength( o ); - v = new LuaValue[n+m]; - for ( int i=0; i