Created
June 10, 2014 17:36
-
-
Save boberetezeke/ba919d3e45fca0f18a2d to your computer and use it in GitHub Desktop.
Test of JRuby/Java reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class A { | |
public void hello() { | |
System.out.println("hello from A"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class B { | |
public static void hello() { | |
System.out.println("hello from B"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class C { | |
public static void hello() { | |
System.out.println("hello from C"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Method; | |
public class Caller { | |
public static void main(String args[]) { | |
callbackTest(args[0]); | |
} | |
static void callbackTest(String classname) { | |
System.out.println("args = " + classname); | |
try { | |
Class c = Class.forName(classname); | |
System.out.println("class = "); | |
System.out.println(c); | |
Object o = c.newInstance(); | |
Method m = c.getMethod("hello"); | |
m.invoke(null); | |
} catch(Exception e) { | |
System.out.println("failed to invoke class method"); | |
System.out.println(e); | |
try { | |
Class c2 = Class.forName(classname); | |
Object o2 = c2.newInstance(); | |
Method m2 = c2.getMethod("hello"); | |
m2.invoke(o2); | |
} catch(Exception e2) { | |
System.out.println("failed to invoke object method"); | |
System.out.println(e2); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "caller.jar" | |
java_import "MyCallback" | |
require "jruby/core_ext" | |
class MyCallbackLocal | |
def hello | |
puts "hello from MyCallbackLocal" | |
end | |
end | |
MyCallbackLocal.become_java! | |
puts "trying with class inside jar" | |
Java::Default::Caller.callbackTest("A") | |
puts "trying with class outside jar" | |
Java::Default::Caller.callbackTest("C") | |
puts "trying with JRuby class that is compiled to .class with jrubyc" | |
Java::Default::Caller.callbackTest("MyCallbackJrubyc") | |
puts "trying with JRuby class that is compiled to .java with jrubyc and .class with javac" | |
Java::Default::Caller.callbackTest("MyCallbackJrubycJavac") | |
puts "trying with JRuby class defined locally" | |
Java::Default::Caller.callbackTest("MyCallbackLocal") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCallbackJrubyc | |
def hello | |
puts "hello from MyCallbackJrubyc" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCallbackJrubycJavac | |
def hello | |
puts "hello from MyCallbackJrubycJavac" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trying with class inside jar | |
args = A | |
class = | |
class A | |
failed to invoke class method | |
java.lang.NullPointerException | |
hello from A | |
trying with class outside jar | |
args = C | |
class = | |
class C | |
hello from C | |
trying with JRuby class that is compiled to .class with jrubyc | |
args = MyCallbackJrubyc | |
class = | |
class MyCallbackJrubyc | |
failed to invoke class method | |
java.lang.NoSuchMethodException: MyCallbackJrubyc.hello() | |
failed to invoke object method | |
java.lang.NoSuchMethodException: MyCallbackJrubyc.hello() | |
trying with JRuby class that is compiled to .java with jrubyc and .class with javac | |
args = MyCallbackJrubycJavac | |
class = | |
class MyCallbackJrubycJavac | |
failed to invoke class method | |
java.lang.NullPointerException | |
hello from MyCallbackJrubycJavac | |
trying with JRuby class defined locally | |
args = MyCallbackLocal | |
failed to invoke class method | |
java.lang.ClassNotFoundException: MyCallbackLocal | |
failed to invoke object method | |
java.lang.ClassNotFoundException: MyCallbackLocal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment