【问题标题】:What is the --precompile flag supposed to do?--precompile 标志应该做什么?
【发布时间】:2016-11-18 20:31:06
【问题描述】:

我正在使用 Spoon 在带注释的类上生成代码,这样我就可以减少我正在从事的项目的样板和代码重复。

由于项目很小,我一直在寻找方法,因此我不必将整个项目分成两个甚至三个不同的项目,这些项目必须按顺序编译,这样我的 AnnotationProcessor 才可用在

查看the Spoon documentation 我找到了--precompile 标志,据我了解,它应该预编译整个源并将其添加到类路径中,以便Spoon 可以找到处理器和其他运行时的东西。

但是根据我在运行它时遇到的错误,它似乎不起作用,即使我添加了选项,我仍然缺少处理器。


[INFO] --- exec-maven-plugin:1.5.0:java (execute-spoon) @ JHTML5 ---
12:29:50.454 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - output: spooned
12:29:50.456 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - destination: C:\Data\CrumbleWorks_java\JHTML5\spooned-classes
12:29:50.457 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - source classpath: null
12:29:50.457 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - template classpath: []
12:29:50.466 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - output: spooned
12:29:50.466 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - destination: C:\Data\CrumbleWorks_java\JHTML5\spooned-classes
12:29:50.466 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - source classpath: null
12:29:50.467 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - template classpath: []
12:29:50.467 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - compiling sources: []
12:29:50.539 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - compile args: [-encoding, UTF-8, -cp, .;/C:/Data/CrumbleWorks_java/JHTML5/target/classes;/C:/Data/Maven/repo/fr/inria/gforge/spoon/spoon-core/5.4.0/spoon-core-5.4.0.jar;/C:/Data/Maven/repo/org/eclipse/jdt/org.eclipse.jdt.core/3.12.0.v20160516-2131/org.eclipse.jdt.core-3.12.0.v20160516-2131.jar;/C:/Data/Maven/repo/com/martiansoftware/jsap/2.1/jsap-2.1.jar;/C:/Data/Maven/repo/commons-io/commons-io/1.4/commons-io-1.4.jar;/C:/Data/Maven/repo/org/slf4j/log4j-over-slf4j/1.7.21/log4j-over-slf4j-1.7.21.jar;/C:/Data/Maven/repo/ch/qos/logback/logback-classic/1.1.7/logback-classic-1.1.7.jar;/C:/Data/Maven/repo/ch/qos/logback/logback-core/1.1.7/logback-core-1.1.7.jar;/C:/Data/Maven/repo/org/crumbleworks/forge/crumbutil/crumbutil/0.1.0/crumbutil-0.1.0.jar;/C:/Data/Maven/repo/org/slf4j/slf4j-api/1.7.21/slf4j-api-1.7.21.jar, -d, C:\Data\CrumbleWorks_java\JHTML5\spooned-classes, -1.8, -proc:none, -preserveAllLocals, -noExit, -enableJavadoc, .]
12:29:50.790 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - compiled in 323 ms
12:29:50.792 [spoon.Launcher.main()] INFO spoon.support.StandardEnvironment - Spoon version 5.4.0
12:29:50.792 [spoon.Launcher.main()] INFO spoon.support.StandardEnvironment - running Spoon...
12:29:50.792 [spoon.Launcher.main()] INFO spoon.support.StandardEnvironment - start processing...
12:29:50.792 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - building sources: []
12:29:50.792 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - built in 0 ms
12:29:50.792 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - building templates: []
12:29:50.792 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - built in 0 ms
12:29:50.793 [spoon.Launcher.main()] DEBUG spoon.support.StandardEnvironment - model built in 1
[WARNING]
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:294)
        at java.lang.Thread.run(Thread.java:745)
Caused by: spoon.SpoonException: Unable to load processor "org.crumbleworks.forge.jhtml5.spoon.AttributeProcessor" - Check your classpath. Did you use the --precompile option?
        at spoon.support.QueueProcessingManager.addProcessor(QueueProcessingManager.java:79)
        at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.instantiateAndProcess(JDTBasedSpoonCompiler.java:169)
        at spoon.Launcher.process(Launcher.java:719)
        at spoon.Launcher.run(Launcher.java:665)
        at spoon.Launcher.run(Launcher.java:106)
        at spoon.Launcher.main(Launcher.java:99)
        ... 6 more
Caused by: java.lang.ClassNotFoundException: org.crumbleworks.forge.jhtml5.spoon.AttributeProcessor
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at spoon.support.QueueProcessingManager.addProcessor(QueueProcessingManager.java:77)
        ... 11 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.202 s
[INFO] Finished at: 2016-10-31T12:29:50+01:00
[INFO] Final Memory: 14M/284M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:java (execute-spoon) on project JHTML5: An exception occured while executing the Java class. null: InvocationTargetException: Unable to load processor "org.crumbleworks.forge.jhtml5.spoon.AttributeProcessor" - Check your classpath. Did you use the --precompile option? -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

完整的maven错误输出

唉,这条线让我很困惑......

Caused by: spoon.SpoonException: Unable to load processor "org.crumbleworks.forge.jhtml5.spoon.AttributeProcessor" - Check your classpath. Did you use the --precompile option?

...因为 是的,我确实使用了 --precompile 选项。谢谢你的询问


<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>${exec-plugin.version}</version>
    <executions>
        <execution>
            <id>execute-spoon</id>
            <phase>process-sources</phase>
            <goals>
                <goal>java</goal>
            </goals>
            <configuration>
                <classpathScope>compile</classpathScope>
                <mainClass>spoon.Launcher</mainClass>
                <arguments>
                    <argument>--with-imports</argument>
                    <argument>--processors</argument>
                    <argument>org.crumbleworks.forge.jhtml5.spoon.AttributeProcessor</argument>
                    <argument>--enable-comments</argument>
                    <argument>--precompile</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

使用 exec-maven-plugin(执行任何 java 可执行文件)运行spoon的Maven插件声明


那么:--precompile 标志应该做什么?
分别是我做错了什么?


编辑
这是一个带有 maven-pom 的简化示例和描述如何在不使用 maven 的情况下运行和显示的自述文件:https://github.com/CrumbleWorks/Spoon-Precompile-Issue

【问题讨论】:

  • 你的类路径中有 org.crumbleworks.forge.jhtml5.spoon.AttributeProcessor.class 吗?
  • @MartinMonperrus 这是正在编译的代码的一部分
  • @MartinMonperrus 我添加了一个演示问题的示例项目
  • 这是一个错误,感谢您报告它。该修复程序将与github.com/INRIA/spoon/pull/923 合并。
  • 您仍然可以自己编译处理器并将其添加到类路径中。

标签: java compilation inria-spoon


【解决方案1】:

--precompile 选项编译作为输入给出的源代码,以便对于所有CtTypeReference 对象,可以使用getActualClass() 获得一个实际的类。此外,它还为任何人启用getActualClass() CtType

(这允许在同一个分析或转换中混合 Spoon 的反射和 Java 的反射)

【讨论】:

    猜你喜欢
    • 2011-02-23
    • 2018-09-18
    • 1970-01-01
    • 2022-01-22
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    相关资源
    最近更新 更多