【发布时间】:2021-03-16 21:13:17
【问题描述】:
有没有办法在不完全编译的情况下为 CtCodeSnippetStatement 构建 CtElement?我有 sn-ps,我希望能够扫描并进一步处理,然后将它们插入到具有其余依赖项(sn-p 可能引用的方法/全局变量)的类中。
【问题讨论】:
标签: inria-spoon
有没有办法在不完全编译的情况下为 CtCodeSnippetStatement 构建 CtElement?我有 sn-ps,我希望能够扫描并进一步处理,然后将它们插入到具有其余依赖项(sn-p 可能引用的方法/全局变量)的类中。
【问题讨论】:
标签: inria-spoon
如果你所说的“没有完全编译”的意思是你不希望 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 中的元素将返回 null 和 getDeclaration,并返回 null 和 getTypeDeclaration 以访问用户定义的类。
【讨论】: