Search in sources :

Example 1 with JvmModelGenerator

use of org.eclipse.xtext.xbase.compiler.JvmModelGenerator in project xtext-xtend by eclipse.

the class TestCaseCompiler method compile.

public void compile(String qualifiedName) throws IOException {
    ResourceSet set = injector.getInstance(ResourceSet.class);
    final String from = "src/" + qualifiedName.replace('.', '/') + ".xtend";
    final String to = "src-gen/" + qualifiedName.replace('.', '/') + ".java";
    Resource res = set.getResource(org.eclipse.emf.common.util.URI.createFileURI(from), true);
    EcoreUtil.resolveAll(res);
    if (!res.getErrors().isEmpty())
        throw new RuntimeException(res.getErrors().toString());
    final File file = new File(to);
    createFolders(file);
    FileWriter writer = new FileWriter(file);
    IXtendJvmAssociations associations = injector.getInstance(IXtendJvmAssociations.class);
    JvmModelGenerator generator = injector.getInstance(JvmModelGenerator.class);
    XtendFile xtendFile = (XtendFile) res.getContents().get(0);
    JvmGenericType inferredType = associations.getInferredType((XtendClass) xtendFile.getXtendTypes().get(0));
    GeneratorConfig config = injector.getInstance(IGeneratorConfigProvider.class).get(inferredType);
    CharSequence javaCode = generator.generateType(inferredType, config);
    writer.append(javaCode);
    writer.close();
    System.out.println("compiled " + from + " to " + to);
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmModelGenerator(org.eclipse.xtext.xbase.compiler.JvmModelGenerator) FileWriter(java.io.FileWriter) Resource(org.eclipse.emf.ecore.resource.Resource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) GeneratorConfig(org.eclipse.xtext.xbase.compiler.GeneratorConfig) IGeneratorConfigProvider(org.eclipse.xtext.xbase.compiler.IGeneratorConfigProvider) IXtendJvmAssociations(org.eclipse.xtend.core.jvmmodel.IXtendJvmAssociations) File(java.io.File) XtendFile(org.eclipse.xtend.core.xtend.XtendFile)

Aggregations

File (java.io.File)1 FileWriter (java.io.FileWriter)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 IXtendJvmAssociations (org.eclipse.xtend.core.jvmmodel.IXtendJvmAssociations)1 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)1 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)1 GeneratorConfig (org.eclipse.xtext.xbase.compiler.GeneratorConfig)1 IGeneratorConfigProvider (org.eclipse.xtext.xbase.compiler.IGeneratorConfigProvider)1 JvmModelGenerator (org.eclipse.xtext.xbase.compiler.JvmModelGenerator)1