How do I reference a Class object in JRuby?
I need to pass the object class to a method (gson.fromJson)
but JRuby says: 'org.jruby.gen.InterfaceImpl624212141@187ef71' is not a Class
How do I reference a Class (e.g. Bob.class) in JRuby?
include Java
开发者_StackOverflow中文版include_class Java::com.google.gson.Gson
include_class Java::com.bobmanager.Bob
def json_load(filename)
gson = Gson.new
jsonBob = IO.read filename
$dto = gson.fromJson jsonBob, Bob.class # <-- here is the problem: Bob.class
end
json_load 'C:/json/1152.json'
Produces the following:
<stack_trace_array message='org.jruby.embed.EvalFailedException: Native Exception: 'class java.lang.IllegalArgumentException'; Message: Type 'org.jruby.gen.InterfaceImpl624212141@187ef71' is not a Class, ParameterizedType, or GenericArrayType. Can't extract class.; StackTrace: java.lang.IllegalArgumentException: Type 'org.jruby.gen.InterfaceImpl624212141@187ef71' is not a Class, ParameterizedType, or GenericArrayType. Can't extract class.
at com.google.gson.TypeUtils.toRawClass(TypeUtils.java:83)
at com.google.gson.TypeInfo.<init>(TypeInfo.java:34)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:102)
at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51)
at com.google.gson.Gson.fromJson(Gson.java:568)
at com.google.gson.Gson.fromJson(Gson.java:515)
at com.google.gson.Gson.fromJson(Gson.java:484)
at com.google.gson.Gson.fromJson(Gson.java:434)
' >
Try using Bob.java_class
rather than Bob.class
精彩评论