我正在寻找一种从Java检查的方法,一些对象是一些Groovy类的实例。
由于编译过程(包括Java类存根生成),每个Groovy类看起来都是不可能的。 因此,使用Groovy类的instanceof将返回false。
在Watches窗口中,我可以测试它。
ModelNode.class = {java.lang.Class@4830}"class ModelNode" component.getClass() = {java.lang.Class@3073}"class ModelNode" component instanceof ModelNode = false显然,有两个不同的类 。 而Java看到生成了一个。 所以,我正在寻找一种很好的方法来检查一些针对Groovy类的对象。
我尝试了reflexion和getSuperclass()方法,看起来有两个绝对不同的类,派生自同一个超类。
component.getClass().getSuperclass() = {java.lang.Class@871}"class javax.swing.tree.DefaultMutableTreeNode" ModelNode.class.getSuperclass() = {java.lang.Class@871}"class javax.swing.tree.DefaultMutableTreeNode"此外,由于类似的原因, 我不能在instanceof之后投出任何东西。 所以,我一定是做错了 。
有没有办法在Java中使用Groovy对象。 没有Java代码方法所需的Java端接口。
PS最后一句是关于这样的解决方案。
在Java中
interface A { foo(); } assert(object instanceof B) // false; ((A)object).foo(); // fail assert(object instanceof A) // true; ((A)object).foo(); // nice在Groovy中
class B extends SMTH implements A { ... }I'm looking a way to check from Java, that some object is an instance of some Groovy class.
It's looks like it's impossible, due to compilation process, including Java class stubs generation, for each Groovy class. So, instanceof with Groovy class will return false.
In Watches window I can test it.
ModelNode.class = {java.lang.Class@4830}"class ModelNode" component.getClass() = {java.lang.Class@3073}"class ModelNode" component instanceof ModelNode = falseObviously, there are two different classes. And Java see generated one. So, I'm looking a nice way to check some object against Groovy class.
I'v tried reflexion and getSuperclass() method, and it's looks like there are two absolutely different classes, derived from same superclass.
component.getClass().getSuperclass() = {java.lang.Class@871}"class javax.swing.tree.DefaultMutableTreeNode" ModelNode.class.getSuperclass() = {java.lang.Class@871}"class javax.swing.tree.DefaultMutableTreeNode"Moreover I can't cast anything after instanceof, due to similar reasons. So, I must be doing smth wrong.
Is there a way to use Groovy objects in Java. Without having Java-side interface for ech necessary from java code method.
P.S. Last sentence is about such solution.
In Java
interface A { foo(); } assert(object instanceof B) // false; ((A)object).foo(); // fail assert(object instanceof A) // true; ((A)object).foo(); // niceIn Groovy
class B extends SMTH implements A { ... }最满意答案
instanceof GroovyObject ?
我创建了以下Java类:
import groovy.lang.GroovyObject; public class A { public static void main(String[] args) { B b = new B(); if (b instanceof GroovyObject) { System.out.println("b is a groovyobject"); b.yeah(); } else { throw new RuntimeException("b is not a groovyobject"); } } }以下Groovy类:
class B { def yeah() { println "hell yeah" } }结果如下:
$ javac -cp ~/groovy/embeddable/groovy-all-2.1.3.jar:. A.java $ java -cp ~/groovy/embeddable/groovy-all-2.1.3.jar:. A b is a groovyobject hell yeahinstanceof GroovyObject?
I created the following Java class:
import groovy.lang.GroovyObject; public class A { public static void main(String[] args) { B b = new B(); if (b instanceof GroovyObject) { System.out.println("b is a groovyobject"); b.yeah(); } else { throw new RuntimeException("b is not a groovyobject"); } } }And the following Groovy class:
class B { def yeah() { println "hell yeah" } }With the following result:
$ javac -cp ~/groovy/embeddable/groovy-all-2.1.3.jar:. A.java $ java -cp ~/groovy/embeddable/groovy-all-2.1.3.jar:. A b is a groovyobject hell yeah更多推荐
发布评论