Groovy Java交叉编译实例(Groovy Java cross compilation instanceof)

编程入门 行业动态 更新时间:2024-10-28 20:20:40
Groovy Java交叉编译实例(Groovy Java cross compilation instanceof)

我正在寻找一种从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 = false

Obviously, 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(); // nice

In 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 yeah

instanceof 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

更多推荐

本文发布于:2023-07-23 00:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   Java   Groovy   instanceof   compilation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!