【问题标题】:Building Spoon CtElements for a CtCodeSnippetStatement without strict compiling在没有严格编译的情况下为 CtCodeSnippetStatement 构建 Spoon CtElements
【发布时间】:2021-03-16 21:13:17
【问题描述】:

有没有办法在不完全编译的情况下为 CtCodeSnippetStatement 构建 CtElement?我有 sn-ps,我希望能够扫描并进一步处理,然后将它们插入到具有其余依赖项(sn-p 可能引用的方法/全局变量)的类中。

【问题讨论】:

    标签: inria-spoon


    【解决方案1】:

    如果你所说的“没有完全编译”的意思是你不希望 Spoon 解析编译后的 sn-p 中的类型引用,那么你可以尝试:

    Environment env = new StandardEnvironment();
    env.setInputClassLoader(new URLClassLoader(new URL[]{}, null));
    Factory factory = new FactoryImpl(new DefaultCoreFactory(),env);
    CtCodeSnippetStatement statement = factory.createCodeSnippetStatement("pkg.MyClass myClass = new pkg.MyClass();");
    CtStatement compiledStatement = statement.compile();
    

    这里使用的环境没有输入资源,它会阻止系统类加载器访问类路径。 compiledStatement 中的元素将返回 nullgetDeclaration,并返回 nullgetTypeDeclaration 以访问用户定义的类。

    【讨论】:

    • 这正是我想要的,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    相关资源
    最近更新 更多