编译组合groovy和java的错误

编译组合groovy和java的错误

本文介绍了编译组合groovy和java的错误(使用maven)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的域对象使用groovy来摆脱setter / getters的样板代码等等。
但是我有一个使用AST转换的问题,特别是生成的构造函数。



这里有一些最低限度的复制代码:

App.java



  package experiment.groovy; 

public class App {
public static void main(String [] args){
示例示例= new示例(Alex);
System.out.println(example.getName());




$ b

Example.groovy



  package experiment.groovy 

import groovy.transform.Canonical

@Canonical
class示例{
字符串名称;
int id;

$ / code>



项目结构





pom.xml



 < project xmlns =http://maven.apache。 org / POM / 4.0.0
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =
http:// maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
< modelVersion> 4.0.0< / modelVersion>
< groupId> experiment.groovy< / groupId>
< artifactId> groovy-ast< / artifactId>
< version> 1.0-SNAPSHOT< / version>
<依赖关系>
< dependency>
< groupId> org.codehaus.groovy< / groupId>
< artifactId> groovy-all< / artifactId>
< version> 2.3.0< / version>
< /依赖关系>
< /依赖关系>
< build>
< plugins>
< plugin>
< groupId> org.codehaus.gmaven< / groupId>
< artifactId> gmaven-plugin< / artifactId>
<执行次数>
<执行>
<目标>
< goal> generateStubs< / goal>
< goal>编译< / goal>
< goal> generateTestStubs< / goal>
< goal> testCompile< / goal>
< /目标>
< /执行>
< /执行次数>
< / plugin>
< / plugins>
< / build>
< / project>

我也尝试过使用 groovy-eclipse-compiler

 < build> 
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-compiler-plugin< / artifactId>
< version> 3.1< / version>
<配置>
< compilerId> groovy-eclipse-compiler< / compilerId>
< source> 1.7< / source>
< target> 1.7< / target>
< / configuration>
<依赖关系>
< dependency>
< groupId> org.codehaus.groovy< / groupId>
< artifactId> groovy-eclipse-compiler< / artifactId>
< version> 2.8.0-01< / version>
< /依赖关系>
< dependency>
< groupId> org.codehaus.groovy< / groupId>
< artifactId> groovy-eclipse-batch< / artifactId>
< version> 2.1.8-01< / version>
< /依赖关系>
< /依赖关系>
< / plugin>
< / plugins>
< / build>

mvn compile 失败,错误。

  1。错误在C:\DEV\Groovy\src\main\java\experiment\groovy\ZApp.java(在第5行)
[错误]编译错误:
示例example = new示例(ToDelete);
[INFO] ------------------------------------------- ------------------
^^^^^^^^^^^^^^^^^^^^^^^
[错误]找到1个错误和0个警告。
构造函数Example(String)未定义

所以问题是:我应该改变什么,或者,可能,这是不可能的?

PS在将来,如果它很重要,我会将 javax.persistence.Entity 注释添加到Example类。

解决方案

我会在Groovy用户了。



编辑3:查看Guillaume的回答。基本上,Java应用程序存根是在应用AST转换之前生成的,因此Java无法看到添加了@Canonical的构造函数。你的解决方案是可行的,因为它将Java绑定到类文件,而不是存根。另一种方法是使用适用于Maven的Groovy-Eclipse编译器插件


I want to use groovy for my domain object to get rid of boilerplate code of setter/getters and so on.But I have a issue with using AST transformations, particularly with generated constructors.

Here some minimum code for reproduction:

App.java

package experiment.groovy;

public class App {
    public static void main(String[] args) {
        Example example = new Example("Alex");
        System.out.println(example.getName());
    }
}

Example.groovy

package experiment.groovy

import groovy.transform.Canonical

@Canonical
class Example {
    String name;
    int id;
}

Project structure

http://i60.tinypic.com/2eebx1e.gif

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>experiment.groovy</groupId>
    <artifactId>groovy-ast</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>generateStubs</goal>
                            <goal>compile</goal>
                            <goal>generateTestStubs</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I also tried with groovy-eclipse-compiler:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <compilerId>groovy-eclipse-compiler</compilerId>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-compiler</artifactId>
                    <version>2.8.0-01</version>
                </dependency>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-batch</artifactId>
                    <version>2.1.8-01</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

And mvn compile failing with error.

1. ERROR in C:\DEV\Groovy\src\main\java\experiment\groovy\ZApp.java (at line 5)
[ERROR] COMPILATION ERROR :
    Example example = new Example("ToDelete");
[INFO] -------------------------------------------------------------
                      ^^^^^^^^^^^^^^^^^^^^^^^
[ERROR] Found 1 error and 0 warnings.
The constructor Example(String) is undefined

So the question is: what should I change, OR, may be, it is impossible?

P.S. In future, if it is important, I will add javax.persistence.Entity annotation to the Example class.

解决方案

I'd ask about this on the Groovy user mailing list. I think the issue is that the Java stub generated contains getters and setters for 'id' and 'name', but doesn't include the constructors from the AST. I'm not sure whether that's a bug or a known limitation. But I thought things like this were supposed to work, so I'm leaning more towards the former.

Edit: This obviously wouldn't apply to the Groovy-Eclipse plugin for Maven since it doesn't use stubs. I don't know enough about it to say what might cause this there.

Edit 2: I couldn't contain my curiosity. I've gone ahead and asked on the mailing list.

Edit 3: See Guillaume's answer here. Basically the Java stubs are generated before the AST transformation has been applied, so Java isn't able to see the constructors @Canonical added. Your solution works because it's tying Java to the class file, not the stub. An alternative would be to use the Groovy-Eclipse Compiler Plugin for Maven.

这篇关于编译组合groovy和java的错误(使用maven)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:40