在Java 7源代码级别编译GWT 2.6.1

时间:2014-05-30 15:22:31

标签: java gwt ant

我最近将我的GWT项目更新为2.6.1,并开始使用Java 7语法,因为2.6现在支持Java 7。

但是,当我尝试编译时,我收到编译器错误,例如

[ERROR] Line 42: '<>' operator is not allowed for source level below 1.7

完整输出

Compiling module com.Project
   Validating units:
      Ignored 85 units with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
   Computing all possible rebind results for 'com.google.gwt.useragent.client.UserAgentAsserter'
      Rebinding com.google.gwt.useragent.client.UserAgentAsserter
         Checking rule <generate-with class='com.google.gwt.editor.rebind.SimpleBeanEditorDriverGenerator'/>
            [WARN] Detected warnings related to 'com.google.gwt.editor.client.SimpleBeanEditorDriver'.   Are validation-api-<version>.jar and validation-api-<version>-sources.jar on the classpath?
            Specify -logLevel DEBUG to see all errors.
            [WARN] Unknown type 'com.google.gwt.editor.client.SimpleBeanEditorDriver' specified in deferred binding rule
   [ERROR] Errors in 'file:/C:/workspace/Project/src/com/project/ProjectFile.java'
      [ERROR] Line 42: '<>' operator is not allowed for source level below 1.7
   [ERROR] Errors in 'file:/C:/workspace/Project/src/com/project/AnotherProjectFile.java'
      [ERROR] Line 188: Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted
C:\workspace\Project\build.xml:51: Java returned: 1
    at org.apache.tools.ant.taskdefs.Java.execute(Java.java:111)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.Main.runBuild(Main.java:809)
    at org.apache.tools.ant.Main.startAnt(Main.java:217)
    at org.apache.tools.ant.Main.start(Main.java:180)
    at org.apache.tools.ant.Main.main(Main.java:268)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)
C:\workspace\Project\build.xml:51: Java returned: 1
    at org.apache.tools.ant.taskdefs.Java.execute(Java.java:111)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.Main.runBuild(Main.java:809)
    at org.apache.tools.ant.Main.startAnt(Main.java:217)
    at org.apache.tools.ant.Main.start(Main.java:180)
    at org.apache.tools.ant.Main.main(Main.java:268)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)
C:\workspace\Project\build.xml:51: Java returned: 1
    at org.apache.tools.ant.taskdefs.Java.execute(Java.java:111)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.Main.runBuild(Main.java:809)
    at org.apache.tools.ant.Main.startAnt(Main.java:217)
    at org.apache.tools.ant.Main.start(Main.java:180)
    at org.apache.tools.ant.Main.main(Main.java:268)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)

如何指定GWT编译器以1.7为目标?我的印象是默认会这样做,但我猜不是。我试图清理项目,包括删除gwt-unitCache目录但无济于事。

这是我的Ant编译目标。

<target name="compile" depends="prepare">
    <javac includeantruntime="false"
           debug="on"
           debuglevel="lines,vars,source"
           srcdir="${src.dir}"
           destdir="${build.dir}"
           source="7"
           target="7">
        <classpath refid="project.classpath"/>
    </javac>
</target>

<target name="gwt-compile" depends="compile">
    <java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
        <classpath>
            <!-- src dir is added to ensure the module.xml file(s) are on the classpath -->
            <pathelement location="${src.dir}"/>
            <pathelement location="${build.dir}"/>
            <path refid="project.classpath"/>
        </classpath>
        <jvmarg value="-Xmx256M"/>
        <arg value="-sourceLevel"/><arg value="7"/>
        <arg value="${gwt.module.name}"/>
    </java>
</target>

3 个答案:

答案 0 :(得分:4)

GWT编译器(和SuperDevMode的CodeServer)有一个-sourceLevel参数,可用于指定语言级别,类似于-source的{​​{1}}参数。

javac的默认值取决于您当前的JVM,因此如果您使用JDK 1.7,它应默认为Java 7.

明确的做法是将-sourceLevel(或-sourceLevel 7)传递给GWT,将1.7(或-source 7)传递给1.7(使用javac Ant任务中的source="7"

答案 1 :(得分:3)

我们只花了两天时间查找类似的bug,结果是由于使用错误版本的java(1.6)部分编译gwt代码。这留下了gwt-unitCache中的一些数据,这些数据以某种方式迫使gwt编译器继续期望代码为1.6(因此钻石操作员警告),即使java版本更改为1.7。通过删除gwt-unitCache来解决问题。

我知道OP说他已经尝试了这个并没有解决他的问题,但我想提一下,因为它可能会帮助其他有类似问题的人。此外,这也可能表明如果OP正在使用其他预编译的gwt代码,这可能会影响gwt编译器使用的java版本。

答案 2 :(得分:2)

最终,解决方案最终成为了zpon和Thomas Broyer的答案的组合。

首先,删除gwt-unitCache目录。

然后,在javac Ant任务中,明确指定源级别

<javac includeantruntime="false"
               debug="on"
               debuglevel="lines,vars,source"
               srcdir="${src.dir}"
               destdir="${build.dir}"
               source="7"
               target="7">
   <classpath refid="project.classpath"/>
</javac>

还指定GWT编译器的源级别

<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
    <classpath>
        <pathelement location="${src.dir}"/>
        <pathelement location="${build.dir}"/>
        <path refid="project.classpath"/>
    </classpath>
    <jvmarg value="-Xmx256M"/>
    <arg value="-sourceLevel"/><arg value="7"/>
    <arg value="${gwt.module.name}"/>
</java>