Search in sources :

Example 21 with TypeReference

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

the class InferredTypeReferencesCheckProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration it, @Extension final TransformationContext context) {
    final TypeReference type = it.findDeclaredField("foo").getType();
    Assert.assertTrue(type.isInferred());
    final Procedure0 _function = () -> {
        it.setExtendedClass(type);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as extended class.", _function);
    final Procedure0 _function_1 = () -> {
        Iterable<? extends TypeReference> _implementedInterfaces = it.getImplementedInterfaces();
        Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_implementedInterfaces, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(type)));
        it.setImplementedInterfaces(_plus);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as implemented interface.", _function_1);
    final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it_1) -> {
        it_1.setReturnType(type);
        final Procedure0 _function_3 = () -> {
            it_1.addParameter("bar", type);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_3);
        final Procedure0 _function_4 = () -> {
            it_1.setExceptions(type);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as exception type.", _function_4);
        final Procedure0 _function_5 = () -> {
            it_1.addTypeParameter("T", type);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_5);
        MutableTypeParameterDeclaration _addTypeParameter = it_1.addTypeParameter("T");
        final Procedure1<MutableTypeParameterDeclaration> _function_6 = (MutableTypeParameterDeclaration it_2) -> {
            final Procedure0 _function_7 = () -> {
                Iterable<? extends TypeReference> _upperBounds = it_2.getUpperBounds();
                Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_upperBounds, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(type)));
                it_2.setUpperBounds(_plus);
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_7);
        };
        ObjectExtensions.<MutableTypeParameterDeclaration>operator_doubleArrow(_addTypeParameter, _function_6);
    };
    it.addMethod("bar", _function_2);
    final Procedure1<MutableFieldDeclaration> _function_3 = (MutableFieldDeclaration field) -> {
        field.setType(type);
    };
    it.addField("bar2", _function_3);
    MutableInterfaceDeclaration _findInterface = context.findInterface("Bar");
    final Procedure1<MutableInterfaceDeclaration> _function_4 = (MutableInterfaceDeclaration it_1) -> {
        final Procedure0 _function_5 = () -> {
            Iterable<? extends TypeReference> _extendedInterfaces = it_1.getExtendedInterfaces();
            Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_extendedInterfaces, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(type)));
            it_1.setExtendedInterfaces(_plus);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as extended interface.", _function_5);
        final Procedure0 _function_6 = () -> {
            it_1.addTypeParameter("T", type);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_6);
        MutableTypeParameterDeclaration _addTypeParameter = it_1.addTypeParameter("T");
        final Procedure1<MutableTypeParameterDeclaration> _function_7 = (MutableTypeParameterDeclaration it_2) -> {
            final Procedure0 _function_8 = () -> {
                Iterable<? extends TypeReference> _upperBounds = it_2.getUpperBounds();
                Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_upperBounds, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(type)));
                it_2.setUpperBounds(_plus);
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_8);
        };
        ObjectExtensions.<MutableTypeParameterDeclaration>operator_doubleArrow(_addTypeParameter, _function_7);
    };
    ObjectExtensions.<MutableInterfaceDeclaration>operator_doubleArrow(_findInterface, _function_4);
    final Procedure0 _function_5 = () -> {
        it.addTypeParameter("T", type);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_5);
    MutableTypeParameterDeclaration _addTypeParameter = it.addTypeParameter("T");
    final Procedure1<MutableTypeParameterDeclaration> _function_6 = (MutableTypeParameterDeclaration it_1) -> {
        final Procedure0 _function_7 = () -> {
            Iterable<? extends TypeReference> _upperBounds = it_1.getUpperBounds();
            Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_upperBounds, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(type)));
            it_1.setUpperBounds(_plus);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_7);
    };
    ObjectExtensions.<MutableTypeParameterDeclaration>operator_doubleArrow(_addTypeParameter, _function_6);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableTypeParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration) MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) Procedure0(org.eclipse.xtext.xbase.lib.Procedures.Procedure0) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

Example 22 with TypeReference

use of org.eclipse.xtend.lib.macro.declaration.TypeReference 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 23 with TypeReference

use of org.eclipse.xtend.lib.macro.declaration.TypeReference 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 24 with TypeReference

use of org.eclipse.xtend.lib.macro.declaration.TypeReference 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 25 with TypeReference

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

the class AccessObjectProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableClassDeclaration> annotatedSourceClasses, @Extension final TransformationContext ctx) {
    final Consumer<MutableClassDeclaration> _function = (MutableClassDeclaration it) -> {
        final Consumer<MutableFieldDeclaration> _function_1 = (MutableFieldDeclaration field) -> {
            MutableTypeDeclaration _declaringType = field.getDeclaringType();
            String _firstUpper = StringExtensions.toFirstUpper(field.getSimpleName());
            String _plus = ("get" + _firstUpper);
            final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it_1) -> {
                it_1.setReturnType(field.getType());
                final CompilationStrategy _function_3 = (CompilationStrategy.CompilationContext it_2) -> {
                    StringConcatenation _builder = new StringConcatenation();
                    _builder.append("return this.");
                    String _simpleName = field.getSimpleName();
                    _builder.append(_simpleName);
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                    return _builder;
                };
                it_1.setBody(_function_3);
            };
            _declaringType.addMethod(_plus, _function_2);
        };
        it.getDeclaredFields().forEach(_function_1);
        String _qualifiedName = it.getQualifiedName();
        int _length = it.getQualifiedName().length();
        int _length_1 = it.getSimpleName().length();
        int _minus = (_length - _length_1);
        final String pkg = _qualifiedName.substring(0, _minus);
        final TypeReference ser = ctx.newTypeReference(Serializable.class);
        if ((ser == null)) {
            ctx.addError(it, "Cannot find Serializable");
        }
        String _simpleName = it.getSimpleName();
        final String PVersionName = ((pkg + "P") + _simpleName);
        final MutableClassDeclaration p = ctx.findClass(PVersionName);
        if ((p == null)) {
            ctx.addError(it, (("Class " + PVersionName) + " not found"));
        }
        if (((p != null) && (ser != null))) {
            final LinkedList<TypeReference> pIfcs = new LinkedList<TypeReference>();
            pIfcs.add(ser);
            p.setImplementedInterfaces(pIfcs);
        }
        String _simpleName_1 = it.getSimpleName();
        final String GVersionName = ((pkg + "G") + _simpleName_1);
        final MutableClassDeclaration g = ctx.findClass(GVersionName);
        if ((g == null)) {
            ctx.addError(it, (("Class " + GVersionName) + " not found"));
        }
        if (((g != null) && (ser != null))) {
            final LinkedList<TypeReference> gIfcs = new LinkedList<TypeReference>();
            gIfcs.add(ser);
            g.setImplementedInterfaces(gIfcs);
        }
    };
    annotatedSourceClasses.forEach(_function);
}
Also used : Serializable(java.io.Serializable) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) LinkedList(java.util.LinkedList) Consumer(java.util.function.Consumer) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) CompilationStrategy(org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)

Aggregations

TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)37 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)13 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)10 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)10 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)8 Test (org.junit.Test)8 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)7 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)6 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)5 MutableTypeParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration)5 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)4 Type (org.eclipse.xtend.lib.macro.declaration.Type)4 TypeParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.TypeParameterDeclaration)4 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)4 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)4 Serializable (java.io.Serializable)3 EObject (org.eclipse.emf.ecore.EObject)3 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)3 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)3 JvmUpperBound (org.eclipse.xtext.common.types.JvmUpperBound)3