Search in sources :

Example 16 with TypeReference

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

the class DeclarationsTest method testTypeReferences.

@Test
public void testTypeReferences() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package foo");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final TypeReference anyType = it.getTypeReferenceProvider().getAnyType();
        Assert.assertTrue(anyType.isAnyType());
        final TypeReference stringType = it.getTypeReferenceProvider().getString();
        final TypeReference charsequenceType = it.getTypeReferenceProvider().newTypeReference(CharSequence.class.getName());
        Assert.assertTrue(charsequenceType.isAssignableFrom(stringType));
        Assert.assertTrue(stringType.isAssignableFrom(anyType));
        Assert.assertFalse(stringType.isAssignableFrom(charsequenceType));
        this.checkPrimitive(it.getTypeReferenceProvider().getPrimitiveBoolean(), "java.lang.Boolean");
        this.checkPrimitive(it.getTypeReferenceProvider().getPrimitiveInt(), "java.lang.Integer");
        this.checkPrimitive(it.getTypeReferenceProvider().getPrimitiveLong(), "java.lang.Long");
        this.checkPrimitive(it.getTypeReferenceProvider().getPrimitiveShort(), "java.lang.Short");
        this.checkPrimitive(it.getTypeReferenceProvider().getPrimitiveChar(), "java.lang.Character");
        this.checkPrimitive(it.getTypeReferenceProvider().getPrimitiveByte(), "java.lang.Byte");
        this.checkPrimitive(it.getTypeReferenceProvider().getPrimitiveFloat(), "java.lang.Float");
        this.checkPrimitive(it.getTypeReferenceProvider().getPrimitiveDouble(), "java.lang.Double");
        final TypeReference primitiveVoid = it.getTypeReferenceProvider().getPrimitiveVoid();
        Assert.assertTrue(primitiveVoid.isVoid());
        final TypeReference listOfStringType = it.getTypeReferenceProvider().getList(it.getTypeReferenceProvider().getString());
        final TypeReference setOfString = it.getTypeReferenceProvider().getSet(IterableExtensions.<TypeReference>head(listOfStringType.getActualTypeArguments()));
        Assert.assertEquals("List<String>", listOfStringType.toString());
        Assert.assertEquals("String", IterableExtensions.<TypeReference>head(listOfStringType.getActualTypeArguments()).toString());
        Assert.assertEquals("Set<String>", setOfString.toString());
        Assert.assertEquals("String", IterableExtensions.<TypeReference>head(setOfString.getActualTypeArguments()).toString());
        Assert.assertEquals("Set<?>", it.getTypeReferenceProvider().getSet(it.getTypeReferenceProvider().newWildcardTypeReference()).toString());
        Assert.assertEquals("Set<? extends List<String>>", it.getTypeReferenceProvider().getSet(it.getTypeReferenceProvider().newWildcardTypeReference(listOfStringType)).toString());
        Assert.assertEquals("Set<? super List<String>>", it.getTypeReferenceProvider().getSet(it.getTypeReferenceProvider().newWildcardTypeReferenceWithLowerBound(listOfStringType)).toString());
    };
    this.asCompilationUnit(this.validFile(_builder), _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) Test(org.junit.Test)

Example 17 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 18 with TypeReference

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

the class AccessObjectProcessorTest method testWithoutPackage.

@Test
public void testWithoutPackage() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@org.eclipse.xtend.core.tests.macro.AccessObjectAnn");
    _builder.newLine();
    _builder.append("class A {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String field");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
        final TransformationContext ctx = it.getTransformationContext();
        final MutableClassDeclaration classA = ctx.findClass("A");
        Assert.assertNotNull(classA.findDeclaredMethod("getField"));
        final MutableClassDeclaration classPA = ctx.findClass("PA");
        final Function1<TypeReference, Boolean> _function_1 = (TypeReference it_1) -> {
            Type _type = it_1.getType();
            Type _type_1 = ctx.newTypeReference(Serializable.class).getType();
            return Boolean.valueOf(Objects.equal(_type, _type_1));
        };
        Assert.assertNotNull(IterableExtensions.findFirst(classPA.getImplementedInterfaces(), _function_1));
        final MutableClassDeclaration classGA = ctx.findClass("GA");
        final Function1<TypeReference, Boolean> _function_2 = (TypeReference it_1) -> {
            Type _type = it_1.getType();
            Type _type_1 = ctx.newTypeReference(Serializable.class).getType();
            return Boolean.valueOf(Objects.equal(_type, _type_1));
        };
        Assert.assertNotNull(IterableExtensions.findFirst(classGA.getImplementedInterfaces(), _function_2));
    };
    this._xtendCompilerTester.compile(_builder, _function);
}
Also used : Type(org.eclipse.xtend.lib.macro.declaration.Type) Serializable(java.io.Serializable) XtendCompilerTester(org.eclipse.xtend.core.compiler.batch.XtendCompilerTester) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) TransformationContext(org.eclipse.xtend.lib.macro.TransformationContext) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) AbstractActiveAnnotationTest(org.eclipse.xtend.core.tests.macro.AbstractActiveAnnotationTest) Test(org.junit.Test)

Example 19 with TypeReference

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

the class CheckMutableInterfaceDeclarationProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableInterfaceDeclaration> annotatedTargetElements, @Extension final TransformationContext context) {
    for (final MutableInterfaceDeclaration annotatedTargetElement : annotatedTargetElements) {
        {
            final Procedure1<String> _function = (String identifier) -> {
                annotatedTargetElement.addTypeParameter(identifier).remove();
            };
            MutableAssert.assertValidJavaIdentifier("name", _function);
            final Procedure0 _function_1 = () -> {
                annotatedTargetElement.addTypeParameter("T", ((TypeReference[]) null));
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "upperBounds cannot be null", _function_1);
            final Procedure0 _function_2 = () -> {
                annotatedTargetElement.addTypeParameter("T", new TypeReference[] { null });
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "upperBounds cannot contain null", _function_2);
            annotatedTargetElement.addTypeParameter("T", ((TypeReference[]) Conversions.unwrapArray(CollectionLiterals.<TypeReference>emptyList(), TypeReference.class))).remove();
            final Procedure0 _function_3 = () -> {
                annotatedTargetElement.setExtendedInterfaces(null);
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "superinterfaces cannot be null", _function_3);
            final Procedure0 _function_4 = () -> {
                annotatedTargetElement.setExtendedInterfaces(Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList((TypeReference) null)));
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "superinterfaces cannot contain null", _function_4);
            annotatedTargetElement.setExtendedInterfaces(CollectionLiterals.<TypeReference>emptyList());
            final Procedure1<AnnotationReferenceBuildContext> _function_5 = (AnnotationReferenceBuildContext it) -> {
                final Procedure0 _function_6 = () -> {
                    it.set(null, null);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_6);
                final Procedure0 _function_7 = () -> {
                    it.set(null, "foo");
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_7);
                final Procedure0 _function_8 = () -> {
                    String[] _xblockexpression = null;
                    {
                        final String[] array = { "foo" };
                        _xblockexpression = array;
                    }
                    it.set(null, _xblockexpression);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_8);
                final Procedure0 _function_9 = () -> {
                    it.set(null, Boolean.valueOf(true));
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_9);
                final Procedure0 _function_10 = () -> {
                    it.set(null, ((boolean[]) ((boolean[]) Conversions.unwrapArray(Collections.<Boolean>unmodifiableList(CollectionLiterals.<Boolean>newArrayList(Boolean.valueOf(true))), boolean.class))));
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_10);
                final Procedure0 _function_11 = () -> {
                    it.set(null, Integer.valueOf(0));
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_11);
                final Procedure0 _function_12 = () -> {
                    it.set(null, ((int[]) ((int[]) Conversions.unwrapArray(Collections.<Integer>unmodifiableList(CollectionLiterals.<Integer>newArrayList(Integer.valueOf(0))), int.class))));
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_12);
                final Procedure0 _function_13 = () -> {
                    String[] _xblockexpression = null;
                    {
                        final String[] array = { "foo" };
                        _xblockexpression = array;
                    }
                    it.set("doesNotExist", _xblockexpression);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "The annotation property \'doesNotExist\' is not declared on the annotation type \'java.lang.Deprecated\'.", _function_13);
            };
            final AnnotationReference annotationReference = annotatedTargetElement.addAnnotation(context.newAnnotationReference(Deprecated.class, _function_5));
            annotatedTargetElement.removeAnnotation(annotationReference);
            final Procedure1<AnnotationReferenceBuildContext> _function_6 = (AnnotationReferenceBuildContext it) -> {
                String[] _xblockexpression = null;
                {
                    final String[] array = { "foo" };
                    _xblockexpression = array;
                }
                it.set("value", _xblockexpression);
                final Procedure0 _function_7 = () -> {
                    int[] _xblockexpression_1 = null;
                    {
                        final int[] array = { 1 };
                        _xblockexpression_1 = array;
                    }
                    it.set("value", _xblockexpression_1);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "int[] is not applicable at this location. Expected java.lang.String[]", _function_7);
            };
            final AnnotationReference otherAnnotationReference = annotatedTargetElement.addAnnotation(context.newAnnotationReference(SuppressWarnings.class, _function_6));
            annotatedTargetElement.removeAnnotation(otherAnnotationReference);
            final Procedure1<AnnotationReferenceBuildContext> _function_7 = (AnnotationReferenceBuildContext it) -> {
                it.set("validators", new TypeReference[0]);
                it.set("validators", context.newTypeReference(String.class));
                final Procedure0 _function_8 = () -> {
                    it.set("validators", new String[0]);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "java.lang.String[] is not applicable at this location. Expected org.eclipse.xtend.lib.macro.declaration.TypeReference[]", _function_8);
                final Procedure0 _function_9 = () -> {
                    it.set("validators", new Object[0]);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot set annotation values of type java.lang.Object[]", _function_9);
            };
            final AnnotationReference composedChecksReference = annotatedTargetElement.addAnnotation(context.newAnnotationReference(ComposedChecks.class, _function_7));
            annotatedTargetElement.removeAnnotation(composedChecksReference);
        }
    }
}
Also used : MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) Procedure0(org.eclipse.xtext.xbase.lib.Procedures.Procedure0) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) AnnotationReferenceBuildContext(org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

Example 20 with TypeReference

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

the class CheckMutableMethodDeclarationProcessor method doTransform.

@Override
public void doTransform(final MutableMethodDeclaration annotatedMethod, @Extension final TransformationContext context) {
    final Procedure1<String> _function = (String identifier) -> {
        annotatedMethod.addTypeParameter(identifier).remove();
    };
    MutableAssert.assertValidJavaIdentifier("name", _function);
    final Procedure0 _function_1 = () -> {
        annotatedMethod.addTypeParameter("T", ((TypeReference[]) null));
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "upperBounds cannot be null", _function_1);
    final Procedure0 _function_2 = () -> {
        annotatedMethod.addTypeParameter("T", new TypeReference[] { null });
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "upperBounds cannot contain null", _function_2);
    annotatedMethod.addTypeParameter("T", ((TypeReference[]) Conversions.unwrapArray(CollectionLiterals.<TypeReference>emptyList(), TypeReference.class))).remove();
    final Procedure1<String> _function_3 = (String identifier) -> {
        annotatedMethod.addParameter(identifier, context.newTypeReference(String.class)).remove();
    };
    MutableAssert.assertValidJavaIdentifier("name", _function_3);
    final Procedure0 _function_4 = () -> {
        annotatedMethod.addParameter("foo", null);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "type cannot be null", _function_4);
    final Procedure0 _function_5 = () -> {
        annotatedMethod.setReturnType(null);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "returnType cannot be null", _function_5);
    final Procedure1<String> _function_6 = (String identifier) -> {
        annotatedMethod.setSimpleName(identifier);
    };
    MutableAssert.assertValidJavaIdentifier("name", _function_6);
    final Procedure0 _function_7 = () -> {
        annotatedMethod.setExceptions(null);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "exceptions cannot be null", _function_7);
    final Procedure0 _function_8 = () -> {
        annotatedMethod.setExceptions(new TypeReference[] { null });
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "exceptions cannot contain null", _function_8);
    annotatedMethod.setExceptions(((TypeReference[]) Conversions.unwrapArray(CollectionLiterals.<TypeReference>emptyList(), TypeReference.class)));
}
Also used : Procedure0(org.eclipse.xtext.xbase.lib.Procedures.Procedure0) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

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