Allow access to public members of private inner classes where possible
This commit is contained in:
@@ -32,6 +32,7 @@ import org.luaj.vm2.compiler.DumpLoadEndianIntTest;
|
||||
import org.luaj.vm2.compiler.RegressionTests;
|
||||
import org.luaj.vm2.compiler.SimpleTests;
|
||||
import org.luaj.vm2.lib.jse.LuaJavaCoercionTest;
|
||||
import org.luaj.vm2.lib.jse.LuajavaAccessibleMembersTest;
|
||||
import org.luaj.vm2.lib.jse.LuajavaClassMembersTest;
|
||||
import org.luaj.vm2.vm1.Luajvm1CompatibilityTest;
|
||||
|
||||
@@ -74,6 +75,7 @@ public class AllTests {
|
||||
|
||||
// library tests
|
||||
TestSuite lib = new TestSuite("Library Tests");
|
||||
lib.addTestSuite(LuajavaAccessibleMembersTest.class);
|
||||
lib.addTestSuite(LuajavaClassMembersTest.class);
|
||||
lib.addTestSuite(LuaJavaCoercionTest.class);
|
||||
lib.addTestSuite(RequireClassTest.class);
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.luaj.vm2.lib.jse;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.security.Permission;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.vm2.LuaFunction;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.compiler.LuaC;
|
||||
import org.luaj.vm2.lib.jse.JsePlatform;
|
||||
|
||||
public class LuajavaAccessibleMembersTest extends TestCase {
|
||||
|
||||
private LuaTable _G;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
_G = JsePlatform.standardGlobals();
|
||||
}
|
||||
|
||||
private String invokeScript(String script) {
|
||||
try {
|
||||
InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") );
|
||||
LuaFunction c = LuaC.instance.load( is, "script", _G );
|
||||
return c.call().tojstring();
|
||||
} catch ( Exception e ) {
|
||||
fail("exception: "+e );
|
||||
return "failed";
|
||||
}
|
||||
}
|
||||
|
||||
public void testAccessFromPrivateClassImplementedMethod() {
|
||||
assertEquals("privateImpl-aaa-interface_method(bar)", invokeScript(
|
||||
"b = luajava.newInstance('"+TestClass.class.getName()+"');" +
|
||||
"a = b:create_PrivateImpl('aaa');" +
|
||||
"return a:interface_method('bar');"));
|
||||
}
|
||||
|
||||
public void testAccessFromPrivateClassPublicMethod() {
|
||||
assertEquals("privateImpl-aaa-public_method", invokeScript(
|
||||
"b = luajava.newInstance('"+TestClass.class.getName()+"');" +
|
||||
"a = b:create_PrivateImpl('aaa');" +
|
||||
"return a:public_method();"));
|
||||
}
|
||||
|
||||
public void testAccessFromPrivateClassGetPublicField() {
|
||||
assertEquals("aaa", invokeScript(
|
||||
"b = luajava.newInstance('"+TestClass.class.getName()+"');" +
|
||||
"a = b:create_PrivateImpl('aaa');" +
|
||||
"return a.public_field;"));
|
||||
}
|
||||
|
||||
public void testAccessFromPrivateClassSetPublicField() {
|
||||
assertEquals("foo", invokeScript(
|
||||
"b = luajava.newInstance('"+TestClass.class.getName()+"');" +
|
||||
"a = b:create_PrivateImpl('aaa');" +
|
||||
"a.public_field = 'foo';" +
|
||||
"return a.public_field;"));
|
||||
}
|
||||
|
||||
public void testAccessFromPrivateClassPublicConcstructor() {
|
||||
assertEquals("privateImpl-constructor", invokeScript(
|
||||
"b = luajava.newInstance('"+TestClass.class.getName()+"');" +
|
||||
"c = b:get_PrivateImplClass();" +
|
||||
"return luajava.new(c);"));
|
||||
}
|
||||
}
|
||||
18
test/junit/org/luaj/vm2/lib/jse/TestClass.java
Normal file
18
test/junit/org/luaj/vm2/lib/jse/TestClass.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.luaj.vm2.lib.jse;
|
||||
|
||||
public class TestClass {
|
||||
private static class PrivateImpl implements TestInterface {
|
||||
public String public_field;
|
||||
public PrivateImpl() {
|
||||
this.public_field = "privateImpl-constructor";
|
||||
}
|
||||
PrivateImpl(String f) {
|
||||
this.public_field = f;
|
||||
}
|
||||
public String public_method() { return "privateImpl-"+public_field+"-public_method"; }
|
||||
public String interface_method(String x) { return "privateImpl-"+public_field+"-interface_method("+x+")"; }
|
||||
public String toString() { return public_field; }
|
||||
}
|
||||
public TestInterface create_PrivateImpl(String f) { return new PrivateImpl(f); }
|
||||
public Class get_PrivateImplClass() { return PrivateImpl.class; }
|
||||
}
|
||||
5
test/junit/org/luaj/vm2/lib/jse/TestInterface.java
Normal file
5
test/junit/org/luaj/vm2/lib/jse/TestInterface.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package org.luaj.vm2.lib.jse;
|
||||
|
||||
public interface TestInterface {
|
||||
String interface_method(String x);
|
||||
}
|
||||
Reference in New Issue
Block a user