Search in sources :

Example 1 with Problem

use of org.eclipse.xtend.lib.macro.services.Problem in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testValidationPhase.

@Test
public void testValidationPhase() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.ClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.ValidationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(ValidatingProcessor)");
    _builder.newLine();
    _builder.append("annotation ValidatingAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class ValidatingProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addWarning(\"Foo\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addWarning(\"Bar\")");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doValidate(ClassDeclaration cls, extension ValidationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addWarning(\"Baz\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addWarning(\"There were \" + cls.problems.size + \" problems\")");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/ValidatingAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.*");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@ValidatingAnnotation");
    _builder_1.newLine();
    _builder_1.append("class Foo {");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration cls = it.getTypeLookup().findClass("myusercode.Foo");
        final List<? extends Problem> problems = it.getProblemSupport().getProblems(cls);
        Assert.assertEquals(4, problems.size());
        Assert.assertEquals("Foo", problems.get(0).getMessage());
        Assert.assertEquals("Bar", problems.get(1).getMessage());
        Assert.assertEquals("Baz", problems.get(2).getMessage());
        Assert.assertEquals("There were 3 problems", problems.get(3).getMessage());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) List(java.util.List) Problem(org.eclipse.xtend.lib.macro.services.Problem) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 2 with Problem

use of org.eclipse.xtend.lib.macro.services.Problem in project xtext-xtend by eclipse.

the class AnnotationWithNestedEnumProcessorTest method testBug487559_01.

@Test
public void testBug487559_01() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import org.eclipse.xtend.core.tests.macro.AnnotationWithNestedEnum");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@org.eclipse.xtend.core.tests.macro.AnnotationWithNestedEnum");
    _builder.newLine();
    _builder.append("class Client {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
        final Function1<Problem, String> _function_1 = (Problem it_1) -> {
            return it_1.getMessage();
        };
        Assert.assertTrue(IterableExtensions.join(ListExtensions.map(it.getAllProblems(), _function_1), ", "), it.getAllProblems().isEmpty());
    };
    this._xtendCompilerTester.compile(_builder, _function);
}
Also used : XtendCompilerTester(org.eclipse.xtend.core.compiler.batch.XtendCompilerTester) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) Problem(org.eclipse.xtend.lib.macro.services.Problem) Test(org.junit.Test)

Example 3 with Problem

use of org.eclipse.xtend.lib.macro.services.Problem in project xtext-xtend by eclipse.

the class Bug464136Test method testThrownErrorInPreValidation.

@Test
public void testThrownErrorInPreValidation() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("@org.eclipse.xtend.core.tests.macro.Bug464136 class C {}");
    _builder.newLine();
    final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
        final List<? extends Problem> problems = it.getAllProblems();
        final Function1<Problem, String> _function_1 = (Problem it_1) -> {
            return it_1.getMessage();
        };
        Assert.assertEquals(ListExtensions.map(problems, _function_1).toString(), 1, problems.size());
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("Error during annotation processing:");
        _builder_1.newLine();
        _builder_1.append("java.lang.LinkageError: Just a test :-/");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("at org.eclipse.xtend.core.tests.macro.Bug464136Processor.lambda$doTransform$0(Bug464136Processor.java:22)");
        _builder_1.newLine();
        final String messageJava = _builder_1.toString();
        StringConcatenation _builder_2 = new StringConcatenation();
        _builder_2.append("Error during annotation processing:");
        _builder_2.newLine();
        _builder_2.append("java.lang.LinkageError: Just a test :-/");
        _builder_2.newLine();
        _builder_2.append("\t");
        _builder_2.append("at org.eclipse.xtend.core.tests.macro.Bug464136Processor.lambda$0(Bug464136Processor.java:22)");
        _builder_2.newLine();
        final String messageEclipse = _builder_2.toString();
        if (((!Objects.equal(messageJava, IterableExtensions.head(problems).getMessage())) && (!Objects.equal(messageEclipse, IterableExtensions.head(problems).getMessage())))) {
            StringConcatenation _builder_3 = new StringConcatenation();
            _builder_3.append("Expected one of the following problem messages:");
            _builder_3.newLine();
            _builder_3.newLine();
            _builder_3.append(messageJava);
            _builder_3.newLineIfNotEmpty();
            _builder_3.newLine();
            _builder_3.append(messageEclipse);
            _builder_3.newLineIfNotEmpty();
            _builder_3.newLine();
            _builder_3.append("But got:");
            _builder_3.newLine();
            _builder_3.newLine();
            String _message = IterableExtensions.head(problems).getMessage();
            _builder_3.append(_message);
            _builder_3.newLineIfNotEmpty();
            Assert.fail(_builder_3.toString());
        }
    };
    this._xtendCompilerTester.compile(_builder, _function);
}
Also used : XtendCompilerTester(org.eclipse.xtend.core.compiler.batch.XtendCompilerTester) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) List(java.util.List) Problem(org.eclipse.xtend.lib.macro.services.Problem) Test(org.junit.Test)

Example 4 with Problem

use of org.eclipse.xtend.lib.macro.services.Problem in project xtext-xtend by eclipse.

the class ProblemSupportImpl method getProblems.

@Override
public List<? extends Problem> getProblems(final Element element) {
    this.checkCanceled();
    final Pair<Resource, EObject> resAndObj = this.getResourceAndEObject(element);
    final Resource resource = resAndObj.getKey();
    EList<Resource.Diagnostic> _errors = resource.getErrors();
    EList<Resource.Diagnostic> _warnings = resource.getWarnings();
    final Iterable<EObjectDiagnosticImpl> issues = Iterables.<EObjectDiagnosticImpl>filter((Iterables.<Resource.Diagnostic>concat(_errors, _warnings)), EObjectDiagnosticImpl.class);
    final Function1<EObjectDiagnosticImpl, Boolean> _function = (EObjectDiagnosticImpl diag) -> {
        EObject _problematicObject = diag.getProblematicObject();
        EObject _value = resAndObj.getValue();
        return Boolean.valueOf(Objects.equal(_problematicObject, _value));
    };
    final Function1<EObjectDiagnosticImpl, Problem> _function_1 = (EObjectDiagnosticImpl diag) -> {
        String _code = diag.getCode();
        String _message = diag.getMessage();
        Problem.Severity _translateSeverity = this.translateSeverity(diag.getSeverity());
        ProblemImpl _problemImpl = new ProblemImpl(_code, _message, _translateSeverity);
        return ((Problem) _problemImpl);
    };
    final Iterable<Problem> result = IterableExtensions.<EObjectDiagnosticImpl, Problem>map(IterableExtensions.<EObjectDiagnosticImpl>filter(issues, _function), _function_1);
    return IterableExtensions.<Problem>toList(result);
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) Severity(org.eclipse.xtext.diagnostics.Severity) EObjectDiagnosticImpl(org.eclipse.xtext.validation.EObjectDiagnosticImpl) EObject(org.eclipse.emf.ecore.EObject) Problem(org.eclipse.xtend.lib.macro.services.Problem)

Example 5 with Problem

use of org.eclipse.xtend.lib.macro.services.Problem in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testValidateLater.

@Test
public void testValidateLater() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractFieldProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(ValidateLaterProcessor)");
    _builder.newLine();
    _builder.append("annotation ValidateLater {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class ValidateLaterProcessor extends AbstractFieldProcessor {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableFieldDeclaration it, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("validateLater[ |");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("if (type == primitiveBoolean)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("addWarning(\"The type was inferred and boolean\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/ValidateLater.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.*");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("class Foo {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@ValidateLater val foo = true");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration cls = it.getTypeLookup().findClass("myusercode.Foo");
        final List<? extends Problem> problems = it.getProblemSupport().getProblems(IterableExtensions.head(cls.getDeclaredFields()));
        Assert.assertEquals(1, problems.size());
        Assert.assertEquals("The type was inferred and boolean", problems.get(0).getMessage());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) List(java.util.List) Problem(org.eclipse.xtend.lib.macro.services.Problem) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Aggregations

Problem (org.eclipse.xtend.lib.macro.services.Problem)9 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)7 Test (org.junit.Test)7 List (java.util.List)6 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)5 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)4 XtendCompilerTester (org.eclipse.xtend.core.compiler.batch.XtendCompilerTester)3 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)3 Resource (org.eclipse.emf.ecore.resource.Resource)2 Severity (org.eclipse.xtext.diagnostics.Severity)2 EObjectDiagnosticImpl (org.eclipse.xtext.validation.EObjectDiagnosticImpl)2 Function (com.google.common.base.Function)1 IOException (java.io.IOException)1 EObject (org.eclipse.emf.ecore.EObject)1 TransformationContextImpl (org.eclipse.xtend.core.macro.TransformationContextImpl)1 ProblemImpl (org.eclipse.xtend.core.macro.declaration.ProblemImpl)1 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)1 Element (org.eclipse.xtend.lib.macro.declaration.Element)1 Result (org.eclipse.xtext.xbase.testing.CompilationTestHelper.Result)1