简体   繁体   English

Groovy特性支持Java联合编译吗?

[英]Do Groovy traits support joint compilation with Java?

I am quite excited about traits in Groovy, but I can't get them to work at all with joint compilation. 我对Groovy中的特征感到非常兴奋,但是我无法让它们与联合编译一起使用。 Example: 例:

Bird.groovy: Bird.groovy:

trait FlyingAbility {
  String fly() { "I'm flying!" }
}

class Bird implements FlyingAbility {}

JavaClass.java: JavaClass.java:

public class JavaClass {
  public static void main(String[] args) {
    Bird b = new Bird();
    // FlyingAbility f = new Bird();
  }
}

The generated stub is: 生成的存根为:

public class Bird
  extends java.lang.Object  implements
    FlyingAbility {
;
public  groovy.lang.MetaClass getMetaClass() { return (groovy.lang.MetaClass)null;}
public  void setMetaClass(groovy.lang.MetaClass mc) { }
public  java.lang.Object invokeMethod(java.lang.String method, java.lang.Object arguments) { return null;}
public  java.lang.Object getProperty(java.lang.String property) { return null;}
public  void setProperty(java.lang.String property, java.lang.Object value) { }
}

This results in the java compiler complaining that FlyingAbility is not defined (and is not an interface). 这导致Java编译器抱怨未定义FlyingAbility(并且它不是接口)。 I find it hard to believe this is not supported: am I missing something? 我很难相信这是不支持的:我缺少什么吗?

I find it hard to believe this is not supported: am I missing something? 我很难相信这是不支持的:我缺少什么吗?

You haven't indicated which version of Groovy you are using and there may have been a bug at some point, but the answer to your question is that it is supported. 您尚未指示正在使用哪个Groovy版本,并且有时可能存在错误,但问题的答案是它受支持。

See the project at https://github.com/jeffbrown/traitandjava . 请参阅https://github.com/jeffbrown/traitandjava上的项目。 The relevant files are: 相关文件是:

https://github.com/jeffbrown/traitandjava/blob/master/src/main/groovy/demo/FlyingAbility.groovy https://github.com/jeffbrown/traitandjava/blob/master/src/main/groovy/demo/FlyingAbility.groovy

https://github.com/jeffbrown/traitandjava/blob/master/src/main/groovy/demo/Bird.groovy https://github.com/jeffbrown/traitandjava/blob/master/src/main/groovy/demo/Bird.groovy

https://github.com/jeffbrown/traitandjava/blob/master/src/main/groovy/demo/BirdKeeper.java https://github.com/jeffbrown/traitandjava/blob/master/src/main/groovy/demo/BirdKeeper.java

https://github.com/jeffbrown/traitandjava/blob/master/src/test/groovy/demo/BirdKeeperSpec.groovy https://github.com/jeffbrown/traitandjava/blob/master/src/test/groovy/demo/BirdKeeperSpec.groovy

That project is using Groovy 2.4.4 and it all works. 该项目正在使用Groovy 2.4.4,并且一切正常。

I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM