Search in sources :

Example 11 with Element

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

the class AbstractReusableActiveAnnotationTests method testTracing3.

@Test
public void testTracing3() {
    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.extendedClass");
    _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 extendsClause = it.getTracability().getPrimarySourceElement(cls.getExtendedClass());
        Assert.assertTrue((extendsClause instanceof TypeReference));
        Assert.assertEquals(extendsClause, 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) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 12 with Element

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

the class AssociatorImpl method setPrimarySourceElement.

@Override
public void setPrimarySourceElement(final MutableElement javaElement, final Element sourceElement) {
    final Element primarySourceElement = this.unit.getTracability().getPrimarySourceElement(sourceElement);
    EObject _switchResult = null;
    boolean _matched = false;
    if (primarySourceElement instanceof TypeReferenceImpl) {
        _matched = true;
        _switchResult = ((TypeReferenceImpl) primarySourceElement).getSource();
    }
    if (!_matched) {
        if (primarySourceElement instanceof AbstractElementImpl) {
            _matched = true;
            _switchResult = ((AbstractElementImpl<?>) primarySourceElement).getDelegate();
        }
    }
    final EObject delegate = _switchResult;
    this.unit.getJvmModelAssociator().associate(delegate, ((AbstractElementImpl<?>) javaElement).getDelegate());
}
Also used : AbstractElementImpl(org.eclipse.xtend.core.macro.declaration.AbstractElementImpl) TypeReferenceImpl(org.eclipse.xtend.core.macro.declaration.TypeReferenceImpl) MutableElement(org.eclipse.xtend.lib.macro.declaration.MutableElement) Element(org.eclipse.xtend.lib.macro.declaration.Element) EObject(org.eclipse.emf.ecore.EObject)

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