Search in sources :

Example 66 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.

the class ActiveAnnotationContextProvider method computeContext.

public ActiveAnnotationContexts computeContext(final XtendFile file) {
    final Stopwatches.StoppedTask task = Stopwatches.forTask("[macros] findActiveAnnotations (ActiveAnnotationContextProvider.computeContext)");
    task.start();
    try {
        final ActiveAnnotationContexts result = ActiveAnnotationContexts.installNew(file.eResource());
        final CompilationUnitImpl compilationUnit = this.compilationUnitProvider.get();
        compilationUnit.setXtendFile(file);
        result.compilationUnit = compilationUnit;
        final IAcceptor<Pair<JvmAnnotationType, XAnnotation>> _function = (Pair<JvmAnnotationType, XAnnotation> it) -> {
            boolean _containsKey = result.getContexts().containsKey(it.getKey());
            boolean _not = (!_containsKey);
            if (_not) {
                final ActiveAnnotationContext fa = new ActiveAnnotationContext();
                fa.setCompilationUnit(compilationUnit);
                final JvmType processorType = this._xAnnotationExtensions.getProcessorType(it.getKey());
                try {
                    final Object processorInstance = this._processorInstanceForJvmTypeProvider.getProcessorInstance(processorType);
                    if ((processorInstance == null)) {
                        String _identifier = processorType.getIdentifier();
                        String _plus = ("Couldn\'t instantiate the annotation processor of type \'" + _identifier);
                        String _plus_1 = (_plus + "\'. This is usually the case when the processor resides in the same project as the annotated element.");
                        throw new IllegalStateException(_plus_1);
                    }
                    fa.setProcessorInstance(processorInstance);
                } catch (final Throwable _t) {
                    if (_t instanceof VirtualMachineError) {
                        final VirtualMachineError e = (VirtualMachineError) _t;
                        throw e;
                    } else if (_t instanceof Throwable) {
                        final Throwable e_1 = (Throwable) _t;
                        this.operationCanceledManager.propagateAsErrorIfCancelException(e_1);
                        String _switchResult = null;
                        boolean _matched = false;
                        if (e_1 instanceof ExceptionInInitializerError) {
                            _matched = true;
                            _switchResult = ((ExceptionInInitializerError) e_1).getException().getMessage();
                        }
                        if (!_matched) {
                            _switchResult = e_1.getMessage();
                        }
                        final String msg = _switchResult;
                        EList<Resource.Diagnostic> _errors = file.eResource().getErrors();
                        StringConcatenation _builder = new StringConcatenation();
                        _builder.append("Problem while loading annotation processor: ");
                        _builder.append(msg);
                        XAnnotation _value = it.getValue();
                        EObjectDiagnosticImpl _eObjectDiagnosticImpl = new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.PROCESSING_ERROR, _builder.toString(), _value, XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, (-1), null);
                        _errors.add(_eObjectDiagnosticImpl);
                    } else {
                        throw Exceptions.sneakyThrow(_t);
                    }
                }
                result.getContexts().put(it.getKey(), fa);
            }
            List<XtendAnnotationTarget> _annotatedSourceElements = result.getContexts().get(it.getKey()).getAnnotatedSourceElements();
            XtendAnnotationTarget _annotatedTarget = this._xAnnotationExtensions.getAnnotatedTarget(it.getValue());
            _annotatedSourceElements.add(_annotatedTarget);
        };
        this.searchAnnotatedElements(file, _function);
        return result;
    } catch (final Throwable _t) {
        if (_t instanceof Throwable) {
            final Throwable e = (Throwable) _t;
            this.operationCanceledManager.propagateAsErrorIfCancelException(e);
            boolean _matched = false;
            if (e instanceof VirtualMachineError) {
                _matched = true;
                throw ((VirtualMachineError) e);
            }
            if (!_matched) {
                if (e instanceof LinkageError) {
                    _matched = true;
                    throw ((LinkageError) e);
                }
            }
            ActiveAnnotationContextProvider.logger.warn("Error finding the elements to be processed by active annotations", e);
            return ActiveAnnotationContexts.installNew(file.eResource());
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    } finally {
        task.stop();
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) Stopwatches(org.eclipse.xtext.util.internal.Stopwatches) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) Resource(org.eclipse.emf.ecore.resource.Resource) JvmType(org.eclipse.xtext.common.types.JvmType) EList(org.eclipse.emf.common.util.EList) EObjectDiagnosticImpl(org.eclipse.xtext.validation.EObjectDiagnosticImpl) XtendAnnotationTarget(org.eclipse.xtend.core.xtend.XtendAnnotationTarget) ActiveAnnotationContexts(org.eclipse.xtend.core.macro.ActiveAnnotationContexts) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ActiveAnnotationContext(org.eclipse.xtend.core.macro.ActiveAnnotationContext) EObject(org.eclipse.emf.ecore.EObject) EList(org.eclipse.emf.common.util.EList) List(java.util.List) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 67 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.

the class XtendCompilerTester method compile.

public void compile(CharSequence source, final IAcceptor<CompilationResult> acceptor) {
    try {
        compilationTestHelper.configureFreshWorkspace();
        compilationTestHelper.compile(source, new IAcceptor<CompilationTestHelper.Result>() {

            @Override
            public void accept(final Result t) {
                acceptor.accept(new CompilationResult() {

                    @Override
                    public Map<String, String> getGeneratedCode() {
                        return t.getGeneratedCode();
                    }

                    @Override
                    public String getGeneratedCode(String typeName) {
                        return t.getGeneratedCode(typeName);
                    }

                    @Override
                    public String getSingleGeneratedCode() {
                        return t.getSingleGeneratedCode();
                    }

                    @Override
                    public Class<?> getCompiledClass() {
                        return t.getCompiledClass();
                    }

                    @Override
                    public Class<?> getCompiledClass(String className) {
                        return t.getCompiledClass(className);
                    }

                    @Override
                    public List<? extends Problem> getProblems(Element element) {
                        return getTransformationContext().getProblems(element);
                    }

                    @Override
                    public List<? extends Problem> getAllProblems() {
                        Resource resource = compilationUnitImpl.getXtendFile().eResource();
                        Iterable<EObjectDiagnosticImpl> issues = Iterables.filter(Iterables.concat(resource.getErrors(), resource.getWarnings()), EObjectDiagnosticImpl.class);
                        List<Problem> result = Lists.newArrayList(Iterables.transform(issues, new Function<EObjectDiagnosticImpl, Problem>() {

                            @Override
                            public Problem apply(EObjectDiagnosticImpl diag) {
                                ProblemImpl result = new ProblemImpl(diag.getCode(), diag.getMessage(), translateSeverity(diag.getSeverity()));
                                return result;
                            }

                            private org.eclipse.xtend.lib.macro.services.Problem.Severity translateSeverity(Severity severity) {
                                switch(severity) {
                                    case ERROR:
                                        return Problem.Severity.ERROR;
                                    case WARNING:
                                        return Problem.Severity.WARNING;
                                    case INFO:
                                        return Problem.Severity.INFO;
                                    case IGNORE:
                                        return Problem.Severity.IGNORE;
                                    default:
                                        throw new IllegalArgumentException(String.valueOf(severity));
                                }
                            }
                        }));
                        return result;
                    }

                    @Override
                    public Map<String, CharSequence> getAllGeneratedResources() {
                        return t.getAllGeneratedResources();
                    }

                    CompilationUnitImpl compilationUnitImpl;

                    {
                        compilationUnitImpl = compilationUnitProvider.get();
                        XtendFile xtendFile = (XtendFile) t.getResourceSet().getResources().get(0).getContents().get(0);
                        compilationUnitImpl.setXtendFile(xtendFile);
                    }

                    @Override
                    public CompilationUnit getCompilationUnit() {
                        return compilationUnitImpl;
                    }

                    @Override
                    public TransformationContext getTransformationContext() {
                        TransformationContextImpl transformationContextImpl = new TransformationContextImpl();
                        transformationContextImpl.setUnit(compilationUnitImpl);
                        return transformationContextImpl;
                    }
                });
            }
        });
    } catch (IOException e) {
        Exceptions.sneakyThrow(e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) Element(org.eclipse.xtend.lib.macro.declaration.Element) Resource(org.eclipse.emf.ecore.resource.Resource) Severity(org.eclipse.xtext.diagnostics.Severity) IOException(java.io.IOException) Result(org.eclipse.xtext.xbase.testing.CompilationTestHelper.Result) Function(com.google.common.base.Function) EObjectDiagnosticImpl(org.eclipse.xtext.validation.EObjectDiagnosticImpl) ProblemImpl(org.eclipse.xtend.core.macro.declaration.ProblemImpl) Problem(org.eclipse.xtend.lib.macro.services.Problem) TransformationContextImpl(org.eclipse.xtend.core.macro.TransformationContextImpl)

Example 68 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testInferredMethodReturnType.

@Test
public void testInferredMethodReturnType() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractMethodProcessor");
    _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.MutableMethodDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnotationProcessor extends AbstractMethodProcessor {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableMethodDeclaration annotatedMethod, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedMethod.declaringType.addField(annotatedMethod.simpleName + \'_field\') [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("type = annotatedMethod.returnType");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnnotation");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("class Client {");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def bar() {");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.append("1");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def create new Integer(1) foo() {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _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 foo = it.getTypeLookup().findClass("myusercode.Client");
        final TypeReference barType = foo.findDeclaredField("bar_field").getType();
        this.assertSameType(barType, it.getTypeReferenceProvider().getPrimitiveInt());
        this.assertSameType(barType, foo.findDeclaredMethod("bar").getReturnType());
        final TypeReference fooType = foo.findDeclaredField("foo_field").getType();
        this.assertSameType(fooType, it.getTypeReferenceProvider().newTypeReference(Integer.class));
        this.assertSameType(fooType, foo.findDeclaredMethod("foo").getReturnType());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 69 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testValidation.

@Test
public void testValidation() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.util.List");
    _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.TransformationParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableNamedElement");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(typeof(ValidatedProcessor))");
    _builder.newLine();
    _builder.append("annotation Validated { }");
    _builder.newLine();
    _builder.append("class ValidatedProcessor implements TransformationParticipant<MutableNamedElement> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableNamedElement> annotatedMethods, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedMethods.forEach [ ele |");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("switch ele {");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("MutableFieldDeclaration : ele.addWarning(\'field-warning\')");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("default : ele.addWarning(\'warning\')");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("}");
    _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/AbstractAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("class MyClass {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@myannotation.Validated");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def void foo() {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@myannotation.Validated");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("String name");
    _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 type = it.getTypeLookup().findClass("myusercode.MyClass");
        final MutableMethodDeclaration method = IterableExtensions.head(type.getDeclaredMethods());
        final MutableFieldDeclaration field = IterableExtensions.head(type.getDeclaredFields());
        Assert.assertEquals("field-warning", IterableExtensions.head(it.getProblemSupport().getProblems(field)).getMessage());
        Assert.assertEquals("warning", IterableExtensions.head(it.getProblemSupport().getProblems(method)).getMessage());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 70 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testChangeJavaDoc.

@Test
public void testChangeJavaDoc() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.util.List");
    _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.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(typeof(ChangeDocProcessor))");
    _builder.newLine();
    _builder.append("annotation ChangeDoc{ }");
    _builder.newLine();
    _builder.append("class ChangeDocProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration clazz, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("clazz.docComment = clazz.docComment.toLowerCase");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/ChangeDocAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("/**");
    _builder_1.newLine();
    _builder_1.append(" ");
    _builder_1.append("* MAKE ME LOWER CASE!");
    _builder_1.newLine();
    _builder_1.append(" ");
    _builder_1.append("*/");
    _builder_1.newLine();
    _builder_1.append("@myannotation.ChangeDoc class MyClass {");
    _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 clazz = it.getTypeLookup().findClass("myusercode.MyClass");
        Assert.assertEquals("make me lower case!", clazz.getDocComment());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Aggregations

CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)139 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)130 Test (org.junit.Test)128 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)90 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)21 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)17 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)15 EObject (org.eclipse.emf.ecore.EObject)13 Resource (org.eclipse.emf.ecore.resource.Resource)11 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)11 Type (org.eclipse.xtend.lib.macro.declaration.Type)10 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)10 List (java.util.List)9 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)9 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)9 Element (org.eclipse.xtend.lib.macro.declaration.Element)9 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)9 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)9 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8 AccessibleObject (java.lang.reflect.AccessibleObject)7