Java Code Examples for org.eclipse.xtext.xbase.compiler.XbaseCompiler#compile()

The following examples show how to use org.eclipse.xtext.xbase.compiler.XbaseCompiler#compile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EvaluationCompilerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected String compileToJavaCode(String xtendCode) {
	XExpression model = null;
	ITreeAppendable appendable = new FakeTreeAppendable();
	try {
		model = expression(xtendCode, true);
		XbaseCompiler compiler = compilerProvider.get();
		LightweightTypeReference objectRef = new StandardTypeReferenceOwner(services, model).newReferenceToObject();
		compiler.compile(model, appendable, objectRef);
	} catch (Exception e) {
		throw new RuntimeException("Xtend compilation failed", e);
	} finally {
		if (model != null)
			cache.clear(model.eResource());
	}
	return appendable.getContent();
}
 
Example 2
Source File: ConstantInlineExpansionTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected String compileToJavaCode(String xtendCode) {
	XExpression model = null;
	ITreeAppendable appendable = new FakeTreeAppendable();
	try {
		model = expression(xtendCode, true);
		XbaseCompiler compiler = this.compilerProvider.get();
		LightweightTypeReference objectRef = new StandardTypeReferenceOwner(this.services, model).newReferenceToObject();
		compiler.compile(model, appendable, objectRef);
	} catch (Exception e) {
		throw new RuntimeException("Xtend compilation failed", e);
	} finally {
		if (model != null)
			this.cache.clear(model.eResource());
	}
	return appendable.getContent();
}
 
Example 3
Source File: CompilerTraceTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertTrace(final String javaCodeWithMarker, String xbaseCodeWithMarker) throws Exception {
	xbaseCodeWithMarker = " " + xbaseCodeWithMarker + " ";
	Matcher xbaseMatcher = p.matcher(xbaseCodeWithMarker);
	assertTrue(xbaseMatcher.matches());
	String xbaseGroup1 = xbaseMatcher.group(1);
	String xbaseGroup2 = xbaseMatcher.group(2);
	String xbaseGroup3 = xbaseMatcher.group(3);
	String actualCode = xbaseGroup1 + xbaseGroup2 + xbaseGroup3; 
	XExpression model = expression(actualCode,true);
	TreeAppendable appendable = new TreeAppendable(new ImportManager(true), converter, locationProvider, jvmModelAssociations, model, "  ", "\n");
	XbaseCompiler compiler = get(XbaseCompiler.class);
	LightweightTypeReference returnType = typeResolver.resolveTypes(model).getReturnType(model);
	if (returnType == null) {
		throw new IllegalStateException();
	}
	compiler.compile(model, appendable, returnType);
	String compiledJavaCode = appendable.getContent();
	Matcher javaMatcher = p.matcher(javaCodeWithMarker);
	assertTrue(javaMatcher.matches());
	String javaGroup1 = javaMatcher.group(1);
	String javaGroup2 = javaMatcher.group(2);
	String javaGroup3 = javaMatcher.group(3);
	String actualExpectation = javaGroup1 + javaGroup2 + javaGroup3;
	assertEquals(actualExpectation, compiledJavaCode);
	ITrace trace = new SimpleTrace(appendable.getTraceRegion());
	ILocationInResource location = trace.getBestAssociatedLocation(new TextRegion(javaGroup1.length(), javaGroup2.length()));
	if (location == null) {
		throw new IllegalStateException("location may not be null");
	}
	assertEquals(new TextRegion(xbaseGroup1.length(), xbaseGroup2.length()), location.getTextRegion());
}
 
Example 4
Source File: AbstractOutputComparingCompilerTests.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertCompilesTo(final CharSequence expectedJavaCode, final CharSequence xbaseCode,
		GeneratorConfig generatorConfig, boolean resolve) throws Exception {
	XExpression model = expression(xbaseCode.toString(), resolve);
	XbaseCompiler compiler = get(XbaseCompiler.class);
	FakeTreeAppendable tracing = createAppendable();
	tracing.setGeneratorConfig(generatorConfig);
	LightweightTypeReference returnType = typeResolver.resolveTypes(model).getReturnType(model);
	if (returnType == null) {
		throw new IllegalStateException();
	}
	compiler.compile(model, tracing, returnType);
	assertEquals(expectedJavaCode.toString().trim(), tracing.getContent().toString().trim());
}
 
Example 5
Source File: AbstractOutputComparingCompilerTests.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertCompilesToStatement(final CharSequence expectedJavaCode, final CharSequence xbaseCode) throws Exception {
	XExpression model = expression(xbaseCode.toString(),true);
	XbaseCompiler compiler = get(XbaseCompiler.class);
	ITreeAppendable tracing = createAppendable();
	StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(services, model);
	LightweightTypeReference voidRef = owner.newReferenceTo(Void.TYPE);
	compiler.compile(model, tracing, voidRef);
	assertEquals(expectedJavaCode, tracing.getContent());
}