我正在开发一个项目,该项目需要根据指定的语言(语言只有函数定义)生成Java类。并且JvmModelInferrer / IGenerater需要隐式地从java项目调用。
遵循帖子How to invoke Xtext parser/JvmModelInferrer from a Java project中给出的步骤,但遗憾的是EclipseResourceFileSystemAccess2.generateFile()
正在投掷NullPointerException
。
用于生成的代码:
public class Generator {
private static IGenerator generator;
public static void main(String[] args) {
// this line registers the EMF for our DSL
Injector injector = new RequestDslStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet rs = new ResourceSetImpl();
File file = new File(
"D:/Xtext/XtextFormIntegration/runtime-XtextFormNC/XtextFormCar/lang.request");
Resource resource = rs.getResource(URI.createURI(file.toURI().toString()), true);
//setup the generator
generator = injector.getInstance(IGenerator.class);
//setup the file system access
final EclipseResourceFileSystemAccess2 fsa = new EclipseResourceFileSystemAccess2();
generator.doGenerate(resource, fsa);
}
}
例外是:
Exception in thread "main" java.lang.NullPointerException
at org.eclipse.xtext.builder.EclipseResourceFileSystemAccess2.generateFile(EclipseResourceFileSystemAccess2.java:156)
at org.eclipse.xtext.generator.AbstractFileSystemAccess.generateFile(AbstractFileSystemAccess.java:75)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator._internalDoGenerate(JvmModelGenerator.java:201)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.internalDoGenerate(JvmModelGenerator.java:1756)
at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.doGenerate(JvmModelGenerator.java:181)
at de.itemis.utils.jface.viewers.Generator.main(Generator.java:41)
需要您帮助解决上述问题。
在期待中感谢你。
答案 0 :(得分:1)
EclipseResourceFileSystemAccess2只能在完全初始化的Eclipse环境(包括工作空间等)中使用。如果你想编写一个独立的应用程序(一个带有普通main方法的类),你应该从注入器获得一个IFileSystemAccess实例,类似于你对生成器所做的那样。
答案 1 :(得分:1)
以下代码适用于JavaIoFileSystemAccess和IEncodingProvider。
public class Generator {
private static IGenerator generator;
public static void main(String[] args) {
// this line registers the EMF for our DSL
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet rs = new ResourceSetImpl();
File file = new File(
"D:/Xtext/asap.dsl");
Resource resource = rs.getResource(URI.createURI(file.toURI().toString()), true);
//setup the generator
generator = injector.getInstance(IGenerator.class);
//setup the file system access
final JavaIoFileSystemAccess jfsa = new JavaIoFileSystemAccess();
jfsa.setOutputPath("Test");
Guice.createInjector(new AbstractGenericModule() {
public Class<? extends IEncodingProvider> bindIEncodingProvider() {
return IEncodingProvider.Runtime.class;
}
}).injectMembers(jfsa);
generator.doGenerate(resource, jfsa);
}
}
感谢。