简体   繁体   English

如何进行AST转换?

[英]How to run AST transformation?

I'm trying to run the AST transformation from this example: 我正在尝试从此示例运行AST转换:

http://melix.github.io/ast-workshop/exercise1-hints.html http://melix.github.io/ast-workshop/exercise1-hints.html

But when I run the test I get my assertions failed. 但是,当我运行测试时,我的断言失败了。

I need to compile class AuthorAdderASTTransformation first? 我需要先编译类AuthorAdderASTTransformation吗? I even did that too, added a main method and compiled it. 我什至也这样做了,添加了main方法并对其进行了编译。 But still test fails saying: 但是仍然测试失败说:

groovy.lang.MissingPropertyException: No such property: $AUTHOR for class: org.gr8conf.AuthorAdderASTTransformationTest
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
    at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:86)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
    at org.gr8conf.AuthorAdderASTTransformationTest.testThatAuthorExists(AuthorAdderASTTransformationTest.groovy:5)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)


groovy.lang.MissingPropertyException: No such property: $AUTHOR for class: Foo
    at groovy.lang.MetaClassImpl.invokeStaticMissingProperty(MetaClassImpl.java:900)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1729)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1705)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3458)
    at org.codehaus.groovy.runtime.callsite.ClassMetaClassGetPropertySite.getProperty(ClassMetaClassGetPropertySite.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227)
    at TestScripttestASTTransformationShouldBeDebuggableFromIDE0.run(TestScripttestASTTransformationShouldBeDebuggableFromIDE0.groovy:4)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:518)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:556)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:537)
    at groovy.util.GroovyTestCase.assertScript(GroovyTestCase.java:203)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:272)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:52)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
    at org.gr8conf.AuthorAdderASTTransformationTest.testASTTransformationShouldBeDebuggableFromIDE(AuthorAdderASTTransformationTest.groovy:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

where I'm making the mistake? 我在哪里犯错?

The exception says that your test tried to read the property $AUTHOR, but it was not found. 异常表明您的测试尝试读取属性$ AUTHOR,但未找到。 Assuming the transform itself is right, and that you actually ask the right class, it means it was not run. 假设转换本身是正确的,并且您实际上要求的是正确的类,则意味着它没有运行。 I can only assume these two points, since you didn't give any information on that. 我只能假设这两点,因为您没有提供任何信息。

So then onto why it was not run. 那么,为什么不运行。 AuthorAdderASTTransformation needs to be precompiled when you compile Foo. 编译Foo时,需要预编译AuthorAdderASTTransformation。 But even that is not enough, there needs to be metadata for it as well. 但这还不够,还需要有元数据。 The page http://www.groovy-lang.org/metaprogramming.html#developing-ast-xforms suggest creating a jar for it. http://www.groovy-lang.org/metaprogramming.html#developing-ast-xforms页面建议为此创建一个jar。 While that is not strictly required it well prevent you forgetting the meta data file services correctly when compiling your normal source code. 尽管这不是严格要求的,但它可以防止您在编译常规源代码时正确地忘记元数据文件服务。

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

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