Search in sources :

Example 16 with GeneratorConfig

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);
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) GeneratorConfig(org.eclipse.xtext.xbase.compiler.GeneratorConfig) Test(org.junit.Test)

Example 17 with GeneratorConfig

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);
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) GeneratorConfig(org.eclipse.xtext.xbase.compiler.GeneratorConfig) Test(org.junit.Test)

Example 18 with GeneratorConfig

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);
        }
    }
}
Also used : XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendInterface(org.eclipse.xtend.core.xtend.XtendInterface) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) GeneratorConfig(org.eclipse.xtext.xbase.compiler.GeneratorConfig) Check(org.eclipse.xtext.validation.Check)

Example 19 with GeneratorConfig

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;
}
Also used : GeneratorConfig(org.eclipse.xtext.xbase.compiler.GeneratorConfig)

Aggregations

GeneratorConfig (org.eclipse.xtext.xbase.compiler.GeneratorConfig)19 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)11 Test (org.junit.Test)11 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)3 IGeneratorConfigProvider (org.eclipse.xtext.xbase.compiler.IGeneratorConfigProvider)3 File (java.io.File)1 FileWriter (java.io.FileWriter)1 ArrayList (java.util.ArrayList)1 IProject (org.eclipse.core.resources.IProject)1 IStorage (org.eclipse.core.resources.IStorage)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 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)1 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)1 XtendInterface (org.eclipse.xtend.core.xtend.XtendInterface)1 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)1 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)1 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)1 Check (org.eclipse.xtext.validation.Check)1