PriyaSabut
PriyaSabut

Reputation: 11

NullPointerException while invoking JvmModelInferrer/Generator for code generation from a Java Project

I am working on a project which has a requirement to generate Java class depending on the language(the language will have only function definitions) specified. And the JvmModelInferrer/IGenerater needed to called from a java project implicitly.

Followed the steps given in post How to invoke Xtext parser/JvmModelInferrer from a Java project, but unfortunately EclipseResourceFileSystemAccess2.generateFile() is throwing NullPointerException.

The code used to generate:

    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);

       }
    }

The exception is:

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)

Need your help in resolving the above mentioned issue.

Thanking you in anticipation.

Upvotes: 0

Views: 417

Answers (2)

PriyaSabut
PriyaSabut

Reputation: 11

The below code worked with JavaIoFileSystemAccess and 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);       
}

}

Thanks.

Upvotes: 1

Sebastian Zarnekow
Sebastian Zarnekow

Reputation: 6729

The EclipseResourceFileSystemAccess2 can only be used from within a fully initialized Eclipse environment (including workspace etc). If you want to write a standalone application (a class with a plain main method), you should rather obtain an instance of IFileSystemAccess from the injector similar to what you did with the generator.

Upvotes: 1

Related Questions