Search in sources :

Example 1 with Element

use of org.eclipse.xtend.lib.macro.declaration.Element in project xtext-xtend by eclipse.

the class AnnotationProcessor method inferencePhase.

public Object inferencePhase(final ActiveAnnotationContext ctx, final CancelIndicator monitor) {
    Object _xblockexpression = null;
    {
        final Stopwatches.StoppedTask task = Stopwatches.forTask("[macros] inferencePhase (AnnotationProcessor.inferencePhase)");
        task.start();
        Object _xtrycatchfinallyexpression = null;
        try {
            Object _switchResult = null;
            Object _processorInstance = ctx.getProcessorInstance();
            final Object processor = _processorInstance;
            boolean _matched = false;
            if (processor instanceof TransformationParticipant) {
                _matched = true;
                Object _xblockexpression_1 = null;
                {
                    final TransformationContextImpl modifyCtx = this.modifyContextProvider.get();
                    modifyCtx.setUnit(ctx.getCompilationUnit());
                    final Runnable _function = () -> {
                        final Function1<XtendAnnotationTarget, MutableNamedElement> _function_1 = (XtendAnnotationTarget it) -> {
                            Declaration _switchResult_1 = null;
                            boolean _matched_1 = false;
                            if (it instanceof XtendMember) {
                                _matched_1 = true;
                                _switchResult_1 = ctx.getCompilationUnit().toXtendMemberDeclaration(((XtendMember) it));
                            }
                            if (!_matched_1) {
                                if (it instanceof XtendParameter) {
                                    _matched_1 = true;
                                    _switchResult_1 = ctx.getCompilationUnit().toXtendParameterDeclaration(((XtendParameter) it));
                                }
                            }
                            final Declaration xtendMember = _switchResult_1;
                            Element _primaryGeneratedJavaElement = modifyCtx.getPrimaryGeneratedJavaElement(xtendMember);
                            return ((MutableNamedElement) _primaryGeneratedJavaElement);
                        };
                        final List<MutableNamedElement> map = ListExtensions.<XtendAnnotationTarget, MutableNamedElement>map(ctx.getAnnotatedSourceElements(), _function_1);
                        ((TransformationParticipant<MutableNamedElement>) processor).doTransform(map, modifyCtx);
                    };
                    _xblockexpression_1 = this.runWithCancelIndiciator(ctx, monitor, _function);
                }
                _switchResult = _xblockexpression_1;
            }
            _xtrycatchfinallyexpression = _switchResult;
        } finally {
            task.stop();
        }
        _xblockexpression = _xtrycatchfinallyexpression;
    }
    return _xblockexpression;
}
Also used : MutableNamedElement(org.eclipse.xtend.lib.macro.declaration.MutableNamedElement) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) MutableNamedElement(org.eclipse.xtend.lib.macro.declaration.MutableNamedElement) NamedElement(org.eclipse.xtend.lib.macro.declaration.NamedElement) Element(org.eclipse.xtend.lib.macro.declaration.Element) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) XtendParameter(org.eclipse.xtend.core.xtend.XtendParameter) XtendAnnotationTarget(org.eclipse.xtend.core.xtend.XtendAnnotationTarget) TransformationParticipant(org.eclipse.xtend.lib.macro.TransformationParticipant) List(java.util.List) Declaration(org.eclipse.xtend.lib.macro.declaration.Declaration) TransformationContextImpl(org.eclipse.xtend.core.macro.TransformationContextImpl)

Example 2 with Element

use of org.eclipse.xtend.lib.macro.declaration.Element in project xtext-xtend by eclipse.

the class AnnotationProcessor method validationPhase.

public Object validationPhase(final ActiveAnnotationContext ctx, final CancelIndicator monitor) {
    Object _xblockexpression = null;
    {
        final Stopwatches.StoppedTask task = Stopwatches.forTask("[macros] validationPhase (AnnotationProcessor.validationPhase)");
        task.start();
        Object _xtrycatchfinallyexpression = null;
        try {
            Object _switchResult = null;
            Object _processorInstance = ctx.getProcessorInstance();
            final Object processor = _processorInstance;
            boolean _matched = false;
            if (processor instanceof ValidationParticipant) {
                _matched = true;
                Object _xblockexpression_1 = null;
                {
                    final ValidationContextImpl validationContext = this.validationContextProvider.get();
                    validationContext.setUnit(ctx.getCompilationUnit());
                    final Runnable _function = () -> {
                        final Function1<XtendAnnotationTarget, NamedElement> _function_1 = (XtendAnnotationTarget it) -> {
                            Declaration _switchResult_1 = null;
                            boolean _matched_1 = false;
                            if (it instanceof XtendMember) {
                                _matched_1 = true;
                                _switchResult_1 = ctx.getCompilationUnit().toXtendMemberDeclaration(((XtendMember) it));
                            }
                            if (!_matched_1) {
                                if (it instanceof XtendParameter) {
                                    _matched_1 = true;
                                    _switchResult_1 = ctx.getCompilationUnit().toXtendParameterDeclaration(((XtendParameter) it));
                                }
                            }
                            final Declaration xtendMember = _switchResult_1;
                            Element _primaryGeneratedJavaElement = validationContext.getPrimaryGeneratedJavaElement(xtendMember);
                            return ((NamedElement) _primaryGeneratedJavaElement);
                        };
                        final List<NamedElement> map = ListExtensions.<XtendAnnotationTarget, NamedElement>map(ctx.getAnnotatedSourceElements(), _function_1);
                        ((ValidationParticipant<NamedElement>) processor).doValidate(map, validationContext);
                    };
                    _xblockexpression_1 = this.runWithCancelIndiciator(ctx, monitor, _function);
                }
                _switchResult = _xblockexpression_1;
            }
            _xtrycatchfinallyexpression = _switchResult;
        } finally {
            task.stop();
        }
        _xblockexpression = _xtrycatchfinallyexpression;
    }
    return _xblockexpression;
}
Also used : ValidationParticipant(org.eclipse.xtend.lib.macro.ValidationParticipant) ValidationContextImpl(org.eclipse.xtend.core.macro.ValidationContextImpl) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) MutableNamedElement(org.eclipse.xtend.lib.macro.declaration.MutableNamedElement) NamedElement(org.eclipse.xtend.lib.macro.declaration.NamedElement) Element(org.eclipse.xtend.lib.macro.declaration.Element) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) XtendParameter(org.eclipse.xtend.core.xtend.XtendParameter) XtendAnnotationTarget(org.eclipse.xtend.core.xtend.XtendAnnotationTarget) List(java.util.List) Declaration(org.eclipse.xtend.lib.macro.declaration.Declaration) MutableNamedElement(org.eclipse.xtend.lib.macro.declaration.MutableNamedElement) NamedElement(org.eclipse.xtend.lib.macro.declaration.NamedElement)

Example 3 with Element

use of org.eclipse.xtend.lib.macro.declaration.Element in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testTracing4.

@Test
public void testTracing4() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _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.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnoProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnno {}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnoProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addMethod(\"foo\") [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("primarySourceElement = cls.annotations.head");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("body = [\"return;\"]");
    _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/MyAnno.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnno");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnno");
    _builder_1.newLine();
    _builder_1.append("class Client extends Object{}");
    _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.Client");
        final MutableMethodDeclaration fooMethod = cls.findDeclaredMethod("foo");
        final Element anno = it.getTracability().getPrimarySourceElement(IterableExtensions.head(cls.getAnnotations()));
        Assert.assertTrue((anno instanceof AnnotationReference));
        Assert.assertEquals(anno, it.getTracability().getPrimarySourceElement(((Element) fooMethod)));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Element(org.eclipse.xtend.lib.macro.declaration.Element) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 4 with Element

use of org.eclipse.xtend.lib.macro.declaration.Element in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testTracing2.

@Test
public void testTracing2() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _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.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnoProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnno {}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnoProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addMethod(\"foo\") [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("primarySourceElement = cls.typeParameters.head");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("body = [\"return;\"]");
    _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/MyAnno.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnno");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnno");
    _builder_1.newLine();
    _builder_1.append("class Client<A> {}");
    _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.Client");
        final MutableMethodDeclaration fooMethod = cls.findDeclaredMethod("foo");
        final Element typeParameter = it.getTracability().getPrimarySourceElement(IterableExtensions.head(cls.getTypeParameters()));
        Assert.assertEquals(typeParameter, it.getTracability().getPrimarySourceElement(fooMethod));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Element(org.eclipse.xtend.lib.macro.declaration.Element) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 5 with Element

use of org.eclipse.xtend.lib.macro.declaration.Element 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)

Aggregations

Element (org.eclipse.xtend.lib.macro.declaration.Element)12 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)9 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)8 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)8 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)8 Test (org.junit.Test)8 List (java.util.List)2 TransformationContextImpl (org.eclipse.xtend.core.macro.TransformationContextImpl)2 XtendAnnotationTarget (org.eclipse.xtend.core.xtend.XtendAnnotationTarget)2 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)2 XtendParameter (org.eclipse.xtend.core.xtend.XtendParameter)2 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)2 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)2 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)2 Declaration (org.eclipse.xtend.lib.macro.declaration.Declaration)2 EnumerationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration)2 InterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.InterfaceDeclaration)2 MethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MethodDeclaration)2 MutableAnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableAnnotationTypeDeclaration)2 MutableConstructorDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration)2