Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Test of JRuby/Java reflection

View A.java
1 2 3 4 5
public class A {
public void hello() {
System.out.println("hello from A");
}
}
View A.java
1 2 3 4 5
public class B {
public static void hello() {
System.out.println("hello from B");
}
}
View A.java
1 2 3 4 5
public class C {
public static void hello() {
System.out.println("hello from C");
}
}
View A.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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);
}
}
}
}
View A.java
1 2 3 4 5
class MyCallbackJrubyc
def hello
puts "hello from MyCallbackJrubyc"
end
end
View A.java
1 2 3 4 5
class MyCallbackJrubycJavac
def hello
puts "hello from MyCallbackJrubycJavac"
end
end
View A.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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")
View A.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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
Something went wrong with that request. Please try again.