Java Code Examples for org.eclipse.xtext.xbase.compiler.GeneratorConfig#setJavaSourceVersion()

The following examples show how to use org.eclipse.xtext.xbase.compiler.GeneratorConfig#setJavaSourceVersion() . 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: CompilationTestHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Parses, validates and compiles the given source. Calls the given acceptor for each
 * resource which is generated from the source.
 *  
 * @param resourceSet - the {@link ResourceSet} to use
 * @param acceptor gets called once for each file generated in {@link IGenerator}
 */
public void compile(final ResourceSet resourceSet, IAcceptor<Result> acceptor) {
	try {
		List<Resource> resourcesToCheck = newArrayList(resourceSet.getResources());
		if (generatorConfigProvider instanceof GeneratorConfigProvider) {
			GeneratorConfigProvider configProvider = (GeneratorConfigProvider) generatorConfigProvider;
			GeneratorConfig config = generatorConfigProvider.get(null);
			config.setJavaSourceVersion(javaCompiler.getJavaVersion());
			GeneratorConfig existent = configProvider.install(resourceSet, config);
			if (existent != null) {
				existent.setJavaSourceVersion(javaCompiler.getJavaVersion());
				configProvider.install(resourceSet, existent);
			}
		}
		Result result = resultProvider.get();
		result.setJavaCompiler(javaCompiler);
		result.setCheckMode(getCheckMode());
		result.setResources(resourcesToCheck);
		result.setResourceSet(resourceSet);
		result.setOutputConfigurations(getOutputConfigurations());
		result.doGenerate();
		acceptor.accept(result);
	} catch (Exception e) {
		Exceptions.throwUncheckedException(e);
	}
}
 
Example 2
Source File: CompilerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug472265_01_lamda() throws Exception {
	GeneratorConfig generatorConfig = generatorConfigProvider.get(null);
	generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8);
	assertCompilesTo("final closures.IAcceptors.IAcceptor _function = (String x) -> {\n"
			+ "};\n"
			+ "closures.IAcceptors.IAcceptor a = _function;",
			"{var closures.IAcceptors.IAcceptor a = [x|]}",
			generatorConfig);
}
 
Example 3
Source File: CompilerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug472265_02_lamda() throws Exception {
	GeneratorConfig generatorConfig = generatorConfigProvider.get(null);
	generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8);
	assertCompilesTo("final closures.IAcceptors.IAcceptor2 _function = (String[] x) -> {\n"
			+ "};\n"
			+ "closures.IAcceptors.IAcceptor2 a = _function;",
			"{var closures.IAcceptors.IAcceptor2 a = [x|]}",
			generatorConfig);
}
 
Example 4
Source File: CompilerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug472265_03_lamda() throws Exception {
	GeneratorConfig generatorConfig = generatorConfigProvider.get(null);
	generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8);
	assertCompilesTo("final closures.IAcceptors.IAcceptor3 _function = (String... x) -> {\n"
			+ "};\n"
			+ "closures.IAcceptors.IAcceptor3 a = _function;",
			"{var closures.IAcceptors.IAcceptor3 a = [x|]}",
			generatorConfig);
}
 
Example 5
Source File: CompilerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug472265_04_lamda() throws Exception {
	GeneratorConfig generatorConfig = generatorConfigProvider.get(null);
	generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8);
	assertCompilesTo("final closures.IAcceptors.IAcceptor4 _function = (String x, String[] y) -> {\n"
			+ "};\n"
			+ "closures.IAcceptors.IAcceptor4 a = _function;",
			"{var closures.IAcceptors.IAcceptor4 a = [x,y|]}",
			generatorConfig);
}
 
Example 6
Source File: CompilerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug472265_05_lamda() throws Exception {
	GeneratorConfig generatorConfig = generatorConfigProvider.get(null);
	generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8);
	assertCompilesTo("final closures.IAcceptors.IAcceptor5 _function = (String x, String... y) -> {\n"
			+ "};\n"
			+ "closures.IAcceptors.IAcceptor5 a = _function;",
			"{var closures.IAcceptors.IAcceptor5 a = [x,y|]}",
			generatorConfig);
}
 
Example 7
Source File: CompilerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug472265_06_lamda() throws Exception {
	GeneratorConfig generatorConfig = generatorConfigProvider.get(null);
	generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8);
	assertCompilesTo("final closures.IAcceptors.IAcceptor6 _function = (String[] x, String[] y) -> {\n"
			+ "};\n"
			+ "closures.IAcceptors.IAcceptor6 a = _function;",
			"{var closures.IAcceptors.IAcceptor6 a = [x,y|]}",
			generatorConfig);
}
 
Example 8
Source File: CompilerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug472265_07_lamda() throws Exception {
	GeneratorConfig generatorConfig = generatorConfigProvider.get(null);
	generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8);
	assertCompilesTo("final closures.IAcceptors.IAcceptor7 _function = (String[] x, String... y) -> {\n"
			+ "};\n"
			+ "closures.IAcceptors.IAcceptor7 a = _function;",
			"{var closures.IAcceptors.IAcceptor7 a = [x,y|]}",
			generatorConfig);
}
 
Example 9
Source File: XbaseBuilderPreferenceAccess.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void loadBuilderPreferences(GeneratorConfig generatorConfig, Object context) {
	IPreferenceStore preferenceStore = preferenceStoreAccess.getContextPreferenceStore(context);
	generatorConfig.setGenerateSyntheticSuppressWarnings(preferenceStore.getBoolean(PREF_GENERATE_SUPPRESS_WARNINGS));
	generatorConfig.setGenerateGeneratedAnnotation(preferenceStore.getBoolean(PREF_GENERATE_GENERATED));
	if (generatorConfig.isGenerateGeneratedAnnotation()) {
		generatorConfig.setIncludeDateInGeneratedAnnotation(preferenceStore.getBoolean(PREF_DATE_IN_GENERATED));
		generatorConfig.setGeneratedAnnotationComment(preferenceStore.getString(PREF_GENERATED_COMMENT));
	}
	generatorConfig.setJavaSourceVersion(getJavaVersion(context, preferenceStore));
}
 
Example 10
Source File: SARLGeneratorConfigProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Invoked for creating the default generator configuration.
 *
 * @return the configuration.
 */
protected GeneratorConfig createDefaultGeneratorConfig() {
	final GeneratorConfig config = new GeneratorConfig();
	if (this.defaultVersion == null) {
		this.defaultVersion = JavaVersion.fromQualifier(System.getProperty("java.specification.version")); //$NON-NLS-1$
		if (this.defaultVersion != null) {
			config.setJavaSourceVersion(this.defaultVersion);
		}
	} else {
		config.setJavaSourceVersion(this.defaultVersion);
	}
	return config;
}
 
Example 11
Source File: JavaVersionModule.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public GeneratorConfig get(EObject context) {
	GeneratorConfig config = new GeneratorConfig();
	config.setJavaSourceVersion(targetVersion);
	return config;
}
 
Example 12
Source File: AbstractOutputComparingCompilerTests.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void assertCompilesTo(final CharSequence expectedJavaCode, final CharSequence xbaseCode,
		JavaVersion javaVersion) throws Exception {
	GeneratorConfig generatorConfig = generatorConfigProvider.get(null);
	generatorConfig.setJavaSourceVersion(javaVersion);
	assertCompilesTo(expectedJavaCode, xbaseCode, generatorConfig);
}
 
Example 13
Source File: CompilerTests2.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testEmptySwitch_11() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("{");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("val String x = null");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("switch (x) {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("Object _xblockexpression = null;");
    _builder_1.newLine();
    _builder_1.append("{");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("final String x = null;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("Object _switchResult = null;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("if (x != null) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("switch (x) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("_xblockexpression = _switchResult;");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("return _xblockexpression;");
    _builder_1.newLine();
    GeneratorConfig _generatorConfig = new GeneratorConfig();
    final Procedure1<GeneratorConfig> _function = (GeneratorConfig it) -> {
      it.setJavaSourceVersion(JavaVersion.JAVA8);
    };
    GeneratorConfig _doubleArrow = ObjectExtensions.<GeneratorConfig>operator_doubleArrow(_generatorConfig, _function);
    this.compilesTo(_builder, _builder_1, _doubleArrow);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 14
Source File: CompilerBugDispatchWithOverrideTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testDispatchWithOverrideJava6HasAnnotation() {
  final GeneratorConfig generatorConfig = this.generatorConfigProvider.get(null);
  generatorConfig.setJavaSourceVersion(JavaVersion.JAVA6);
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class Something extends AbstractSomething {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("override dispatch void m(String x) {}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  _builder.newLine();
  _builder.append("class AbstractSomething {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("protected def dispatch void m(String x) {}");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("protected def dispatch void m(Integer x) {}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  StringConcatenation _builder_1 = new StringConcatenation();
  _builder_1.append("import java.util.Arrays;");
  _builder_1.newLine();
  _builder_1.newLine();
  _builder_1.append("@SuppressWarnings(\"all\")");
  _builder_1.newLine();
  _builder_1.append("public class Something extends AbstractSomething {");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("@Override");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("protected void _m(final String x) {");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("}");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("@Override");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("public void m(final Object x) {");
  _builder_1.newLine();
  _builder_1.append("    ");
  _builder_1.append("if (x instanceof Integer) {");
  _builder_1.newLine();
  _builder_1.append("      ");
  _builder_1.append("_m((Integer)x);");
  _builder_1.newLine();
  _builder_1.append("      ");
  _builder_1.append("return;");
  _builder_1.newLine();
  _builder_1.append("    ");
  _builder_1.append("} else if (x instanceof String) {");
  _builder_1.newLine();
  _builder_1.append("      ");
  _builder_1.append("_m((String)x);");
  _builder_1.newLine();
  _builder_1.append("      ");
  _builder_1.append("return;");
  _builder_1.newLine();
  _builder_1.append("    ");
  _builder_1.append("} else {");
  _builder_1.newLine();
  _builder_1.append("      ");
  _builder_1.append("throw new IllegalArgumentException(\"Unhandled parameter types: \" +");
  _builder_1.newLine();
  _builder_1.append("        ");
  _builder_1.append("Arrays.<Object>asList(x).toString());");
  _builder_1.newLine();
  _builder_1.append("    ");
  _builder_1.append("}");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("}");
  _builder_1.newLine();
  _builder_1.append("}");
  _builder_1.newLine();
  this.assertCompilesTo(_builder, _builder_1);
}
 
Example 15
Source File: CompilerBugDispatchWithOverrideTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testDispatchWithOverrideJava5HasNoAnnotation() {
  final GeneratorConfig generatorConfig = this.generatorConfigProvider.get(null);
  generatorConfig.setJavaSourceVersion(JavaVersion.JAVA5);
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class Something extends AbstractSomething {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("override dispatch void m(String x) {}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  _builder.newLine();
  _builder.append("class AbstractSomething {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("protected def dispatch void m(String x) {}");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("protected def dispatch void m(Integer x) {}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  StringConcatenation _builder_1 = new StringConcatenation();
  _builder_1.append("import java.util.Arrays;");
  _builder_1.newLine();
  _builder_1.newLine();
  _builder_1.append("@SuppressWarnings(\"all\")");
  _builder_1.newLine();
  _builder_1.append("public class Something extends AbstractSomething {");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("protected void _m(final String x) {");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("}");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("public void m(final Object x) {");
  _builder_1.newLine();
  _builder_1.append("    ");
  _builder_1.append("if (x instanceof Integer) {");
  _builder_1.newLine();
  _builder_1.append("      ");
  _builder_1.append("_m((Integer)x);");
  _builder_1.newLine();
  _builder_1.append("      ");
  _builder_1.append("return;");
  _builder_1.newLine();
  _builder_1.append("    ");
  _builder_1.append("} else if (x instanceof String) {");
  _builder_1.newLine();
  _builder_1.append("      ");
  _builder_1.append("_m((String)x);");
  _builder_1.newLine();
  _builder_1.append("      ");
  _builder_1.append("return;");
  _builder_1.newLine();
  _builder_1.append("    ");
  _builder_1.append("} else {");
  _builder_1.newLine();
  _builder_1.append("      ");
  _builder_1.append("throw new IllegalArgumentException(\"Unhandled parameter types: \" +");
  _builder_1.newLine();
  _builder_1.append("        ");
  _builder_1.append("Arrays.<Object>asList(x).toString());");
  _builder_1.newLine();
  _builder_1.append("    ");
  _builder_1.append("}");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("}");
  _builder_1.newLine();
  _builder_1.append("}");
  _builder_1.newLine();
  this.assertCompilesTo(_builder, _builder_1);
}