use of org.eclipse.xtext.xbase.compiler.GeneratorConfig in project xtext-xtend by eclipse.
the class CompilerBugDispatchWithOverrideTest method testDispatchWithOverrideJava5HasNoAnnotation.
@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);
}
use of org.eclipse.xtext.xbase.compiler.GeneratorConfig in project xtext-xtend by eclipse.
the class CompilerBugDispatchWithOverrideTest method testDispatchWithOverrideJava6HasAnnotation.
@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);
}
use of org.eclipse.xtext.xbase.compiler.GeneratorConfig in project xtext-xtend by eclipse.
the class XtendValidator method checkModifiers.
@Check
protected void checkModifiers(XtendFunction method) {
XtendTypeDeclaration declaringType = method.getDeclaringType();
if (declaringType instanceof XtendClass || declaringType instanceof AnonymousClass) {
methodModifierValidator.checkModifiers(method, "method " + method.getName());
int abstractIndex = method.getModifiers().indexOf("abstract");
int nativeIndex = method.getModifiers().indexOf("native");
if (method.getExpression() != null) {
if (abstractIndex != -1) {
error("Method " + method.getName() + " with a body cannot be abstract", XTEND_MEMBER__MODIFIERS, abstractIndex, INVALID_MODIFIER);
} else if (method.isNative())
error("Native methods do not specify a body", XTEND_FUNCTION__NAME, -1, INVALID_MODIFIER);
} else if (nativeIndex == -1) {
int finalIndex = method.getModifiers().indexOf("final");
if (finalIndex != -1)
error("Abstract method " + method.getName() + " cannot be final", XTEND_MEMBER__MODIFIERS, finalIndex, INVALID_MODIFIER);
int privateIndex = method.getModifiers().indexOf("private");
if (privateIndex != -1)
error("Abstract method " + method.getName() + " cannot be private", XTEND_MEMBER__MODIFIERS, privateIndex, INVALID_MODIFIER);
int staticIndex = method.getModifiers().indexOf("static");
if (staticIndex != -1)
error("Abstract method " + method.getName() + " cannot be static", XTEND_MEMBER__MODIFIERS, staticIndex, INVALID_MODIFIER);
}
} else if (declaringType instanceof XtendInterface) {
// The validator for interface methods is created lazily when the generator configuration is loaded
GeneratorConfig config = getGeneratorConfig(method);
methodInInterfaceModifierValidator.checkModifiers(method, "method " + method.getName());
int abstractIndex = method.getModifiers().indexOf("abstract");
if (config.getJavaSourceVersion().isAtLeast(JAVA8) && method.getExpression() != null && abstractIndex != -1) {
error("Method " + method.getName() + " with a body cannot be abstract", XTEND_MEMBER__MODIFIERS, abstractIndex, INVALID_MODIFIER);
}
}
}
use of org.eclipse.xtext.xbase.compiler.GeneratorConfig in project xtext-xtend by eclipse.
the class XtendValidator method getGeneratorConfig.
protected GeneratorConfig getGeneratorConfig(EObject element) {
GeneratorConfig result = (GeneratorConfig) getContext().get(GeneratorConfig.class);
if (result == null) {
result = generatorConfigProvider.get(element);
getContext().put(GeneratorConfig.class, result);
if (result.getJavaSourceVersion().isAtLeast(JAVA8)) {
methodInInterfaceModifierValidator = new ModifierValidator(newArrayList("public", "abstract", "static", "def", "override"), this);
} else {
methodInInterfaceModifierValidator = new ModifierValidator(newArrayList("public", "abstract", "def", "override"), this);
}
}
return result;
}
Aggregations