Skip to content

@boberetezeke /A.java
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Test of JRuby/Java reflection
public class A {
public void hello() {
System.out.println("hello from A");
}
}
public class B {
public static void hello() {
System.out.println("hello from B");
}
}
public class C {
public static void hello() {
System.out.println("hello from C");
}
}
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);
}
}
}
}
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")
class MyCallbackJrubyc
def hello
puts "hello from MyCallbackJrubyc"
end
end
class MyCallbackJrubycJavac
def hello
puts "hello from MyCallbackJrubycJavac"
end
end
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.