AST转换中的ClassNotFoundException

时间:2016-10-04 20:14:17

标签: gradle groovy

在我班上我有这样的代码

import com.lapots.game.journey.core.platform.ManagedPlatform

class ApplicationMain {

    def lifeFramework = ManagedPlatform["lifeFramework"]

}

我决定实现类似于bean注入的AST

@Managed("lifeFramework")
def lifeFramework

它应该为具有此类注释的字段设置初始值表达式。基本上替换为

def lifeFramework = com.lapots.game.journey.core.platform.ManagedPlatform.getAt("lifeFramework")

它与原版中的内容不太相似,但遗憾的是我不知道如何隐含地添加import[]表达式(无论如何都将其替换为getAt)。< / p>

这是我的注释声明

@Retention(RetentionPolicy.SOURCE)
@Target([ElementType.FIELD])
@GroovyASTTransformationClass(["com.lapots.game.journey.ast.managed.ManagedASTTransformation"])
public @interface Managed {
    String value()
}

这是实施

import static org.codehaus.groovy.ast.tools.GeneralUtils.callX
import static org.codehaus.groovy.ast.tools.GeneralUtils.args

@CompileStatic
@GroovyASTTransformation(phase=CompilePhase.SEMANTIC_ANALYSIS)
class ManagedASTTransformation implements ASTTransformation {

    private static final String DEFAULT_MEMBER = "value"
    private static final String MANAGED_STORAGE = "com.lapots.game.journey.core.platform.ManagedPlatform"

    @Override
    void visit(ASTNode[] nodes, SourceUnit source) {
        AnnotatedNode parent = (AnnotatedNode) nodes[1]
        AnnotationNode node = (AnnotationNode) nodes[0]

        if (parent in FieldNode) {
            def beanName = node.getMember(DEFAULT_MEMBER).text
            final FieldNode fieldNode = (FieldNode) parent
            fieldNode.setInitialValueExpression(createInvocationExpression(beanName))
        }
    }

    private StaticMethodCallExpression createInvocationExpression(String bean) {
        def classNode = new ClassNode(Class.forName(MANAGED_STORAGE))
        return callX(classNode, "getAt", args(bean))
    }
}

我的AST转换位于子项目ast内。

ast\
   src\main\groovy\...\Managed

我的ApplicationMain班级和整体ManagedPlatform位于子项目core

core\
   src\main\groovy\...\ApplicationMain
                      \ManagedPlatform

该子项目的ast子项目为compile依赖

dependencies {
   compile project(":ast")
}

当我尝试运行我的应用程序时,我收到此错误

java.lang.ClassNotFoundException: com.lapots.game.journey.core.platform.ManagedPlatform
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:677)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:775)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.lapots.game.journey.ast.managed.ManagedASTTransformation.createInvocationExpression(ManagedASTTransformation.groovy:49)
    at com.lapots.game.journey.ast.managed.ManagedASTTransformation.visit(ManagedASTTransformation.groovy:44)
    at org.codehaus.groovy.transform.ASTTransformationVisitor.visitClass(ASTTransformationVisitor.java:134)
    at org.codehaus.groovy.transform.ASTTransformationVisitor$2.call(ASTTransformationVisitor.java:178)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1053)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:591)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:525)
    at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:173)
    at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:59)
    at org.gradle.api.internal.tasks.compile.daemon.CompilerDaemonServer.execute(CompilerDaemonServer.java:31)
    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:497)
    at org.gradle.process.internal.worker.request.WorkerAction.run(WorkerAction.java:87)
    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:497)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:377)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

似乎无法找到ManagedPlatform类(可能因为它位于另一个子项目中)

如何让AST从另一个subproject看到我的课程?或者可能有另一种解决方案?

0 个答案:

没有答案