Search in sources :

Example 6 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 7 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)

Example 8 with Problem

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

the class Bug456264Test method testAddError.

@Test
public void testAddError() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("@org.eclipse.xtend.core.tests.macro.Bug456264 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());
        Assert.assertEquals("My error message", IterableExtensions.head(problems).getMessage());
        final List<? extends Problem> specificProblems = it.getProblems(IterableExtensions.head(it.getCompilationUnit().getSourceTypeDeclarations()));
        Assert.assertEquals(specificProblems.toString(), 1, problems.size());
        Assert.assertEquals("My error message", IterableExtensions.head(specificProblems).getMessage());
    };
    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) AbstractActiveAnnotationTest(org.eclipse.xtend.core.tests.macro.AbstractActiveAnnotationTest) Test(org.junit.Test)

Example 9 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) ProblemImpl(org.eclipse.xtend.core.macro.declaration.ProblemImpl) Problem(org.eclipse.xtend.lib.macro.services.Problem)

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 AbstractActiveAnnotationTest (org.eclipse.xtend.core.tests.macro.AbstractActiveAnnotationTest)3 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)3 Resource (org.eclipse.emf.ecore.resource.Resource)2 ProblemImpl (org.eclipse.xtend.core.macro.declaration.ProblemImpl)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 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