Search in sources :

Example 6 with TypeDeclaration

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

the class DeclarationsTest method testSimpleClassWithField.

@Test
public void testSimpleClassWithField() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package foo");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyClass extends Object implements java.io.Serializable {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("MyClass foo");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        Assert.assertEquals("foo", it.getPackageName());
        TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
        final ClassDeclaration clazz = ((ClassDeclaration) _head);
        Assert.assertEquals("foo.MyClass", clazz.getQualifiedName());
        Assert.assertEquals("Object", clazz.getExtendedClass().toString());
        Assert.assertEquals("Serializable", IterableExtensions.head(clazz.getImplementedInterfaces()).toString());
        MemberDeclaration _head_1 = IterableExtensions.head(clazz.getDeclaredMembers());
        final FieldDeclaration field = ((FieldDeclaration) _head_1);
        Assert.assertEquals("foo", field.getSimpleName());
        Assert.assertSame(it.getTypeLookup().findClass("foo.MyClass"), field.getType().getType());
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : ClassDeclaration(org.eclipse.xtend.lib.macro.declaration.ClassDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMemberDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMemberDeclaration) MemberDeclaration(org.eclipse.xtend.lib.macro.declaration.MemberDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeDeclaration) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) FieldDeclaration(org.eclipse.xtend.lib.macro.declaration.FieldDeclaration) Test(org.junit.Test)

Example 7 with TypeDeclaration

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

the class DeclarationsTest method testIsAssignable.

@Test
public void testIsAssignable() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package foo");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class BaseClass implements InterfaceA {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.append("class SubType extends BaseClass implements InterfaceA {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.append("interface InterfaceA {}");
    _builder.newLine();
    _builder.append("interface InterfaceB {}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        TypeDeclaration _get = ((TypeDeclaration[]) Conversions.unwrapArray(it.getSourceTypeDeclarations(), TypeDeclaration.class))[0];
        final ClassDeclaration baseClass = ((ClassDeclaration) _get);
        final MutableClassDeclaration subClass = it.getTypeLookup().findClass("foo.SubType");
        TypeDeclaration _get_1 = ((TypeDeclaration[]) Conversions.unwrapArray(it.getSourceTypeDeclarations(), TypeDeclaration.class))[2];
        final InterfaceDeclaration interfaceA = ((InterfaceDeclaration) _get_1);
        TypeDeclaration _get_2 = ((TypeDeclaration[]) Conversions.unwrapArray(it.getSourceTypeDeclarations(), TypeDeclaration.class))[3];
        final InterfaceDeclaration interfaceB = ((InterfaceDeclaration) _get_2);
        final Type object = it.getTypeReferenceProvider().getObject().getType();
        Assert.assertTrue(object.isAssignableFrom(baseClass));
        Assert.assertTrue(object.isAssignableFrom(subClass));
        Assert.assertTrue(object.isAssignableFrom(interfaceA));
        Assert.assertTrue(object.isAssignableFrom(interfaceB));
        Assert.assertTrue(baseClass.isAssignableFrom(baseClass));
        Assert.assertTrue(baseClass.isAssignableFrom(subClass));
        Assert.assertFalse(baseClass.isAssignableFrom(interfaceB));
        Assert.assertFalse(baseClass.isAssignableFrom(interfaceA));
        Assert.assertFalse(baseClass.isAssignableFrom(object));
        Assert.assertTrue(interfaceA.isAssignableFrom(baseClass));
        Assert.assertTrue(interfaceA.isAssignableFrom(subClass));
        Assert.assertTrue(interfaceA.isAssignableFrom(interfaceA));
        Assert.assertFalse(interfaceA.isAssignableFrom(interfaceB));
        Assert.assertFalse(interfaceA.isAssignableFrom(object));
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : Type(org.eclipse.xtend.lib.macro.declaration.Type) ClassDeclaration(org.eclipse.xtend.lib.macro.declaration.ClassDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) InterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.InterfaceDeclaration) MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 8 with TypeDeclaration

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

the class DeclarationsTest method testXtendClassWithMethodFieldAndConstructor.

@Test
public void testXtendClassWithMethodFieldAndConstructor() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package foo");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyClass<T extends CharSequence> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String myField");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("new(String initial) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("this.myField = initial");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def <T2 extends CharSequence> MyClass myMethod(T2 a, T b) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("myField = myField + a + b");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return this");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        Assert.assertEquals("foo", it.getPackageName());
        TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
        final ClassDeclaration clazz = ((ClassDeclaration) _head);
        final MutableClassDeclaration genClazz = it.getTypeLookup().findClass("foo.MyClass");
        Assert.assertEquals("foo.MyClass", clazz.getQualifiedName());
        Assert.assertNull(clazz.getExtendedClass());
        Assert.assertTrue(IterableExtensions.isEmpty(clazz.getImplementedInterfaces()));
        Assert.assertEquals(3, IterableExtensions.size(clazz.getDeclaredMembers()));
        Assert.assertEquals("T", IterableExtensions.head(clazz.getTypeParameters()).getSimpleName());
        Assert.assertEquals("CharSequence", IterableExtensions.head(IterableExtensions.head(clazz.getTypeParameters()).getUpperBounds()).toString());
        Assert.assertSame(clazz, IterableExtensions.head(clazz.getTypeParameters()).getTypeParameterDeclarator());
        final FieldDeclaration field = IterableExtensions.head(clazz.getDeclaredFields());
        Assert.assertSame(clazz, field.getDeclaringType());
        Assert.assertEquals("myField", field.getSimpleName());
        Assert.assertEquals("String", field.getType().toString());
        Assert.assertFalse(field.isFinal());
        final ConstructorDeclaration constructor = IterableExtensions.head(clazz.getDeclaredConstructors());
        Assert.assertSame(clazz, constructor.getDeclaringType());
        Assert.assertEquals("MyClass", constructor.getSimpleName());
        Assert.assertEquals("initial", IterableExtensions.head(constructor.getParameters()).getSimpleName());
        Assert.assertEquals("String", IterableExtensions.head(constructor.getParameters()).getType().toString());
        final MethodDeclaration method = IterableExtensions.head(clazz.getDeclaredMethods());
        final MutableMethodDeclaration genMethod = IterableExtensions.head(genClazz.getDeclaredMethods());
        Assert.assertSame(clazz, method.getDeclaringType());
        Assert.assertEquals("a", IterableExtensions.head(method.getParameters()).getSimpleName());
        Assert.assertEquals("T2", IterableExtensions.head(method.getParameters()).getType().toString());
        Assert.assertSame(IterableExtensions.head(genMethod.getTypeParameters()), IterableExtensions.head(method.getParameters()).getType().getType());
        Assert.assertEquals("T", ((ParameterDeclaration[]) Conversions.unwrapArray(method.getParameters(), ParameterDeclaration.class))[1].getType().toString());
        Assert.assertSame(IterableExtensions.head(genClazz.getTypeParameters()), ((ParameterDeclaration[]) Conversions.unwrapArray(method.getParameters(), ParameterDeclaration.class))[1].getType().getType());
        Assert.assertSame(genClazz, method.getReturnType().getType());
        Assert.assertEquals("T2", IterableExtensions.head(method.getTypeParameters()).getSimpleName());
        Assert.assertEquals("CharSequence", IterableExtensions.head(IterableExtensions.head(method.getTypeParameters()).getUpperBounds()).toString());
        Assert.assertSame(IterableExtensions.head(method.getTypeParameters()), IterableExtensions.head(method.getTypeParameters()));
        Assert.assertSame(method, IterableExtensions.head(method.getTypeParameters()).getTypeParameterDeclarator());
        Assert.assertSame(field, ((Object[]) Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[0]);
        Assert.assertSame(constructor, ((Object[]) Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[1]);
        Assert.assertSame(method, ((Object[]) Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[2]);
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : ClassDeclaration(org.eclipse.xtend.lib.macro.declaration.ClassDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) ConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.ConstructorDeclaration) MethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MethodDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AccessibleObject(java.lang.reflect.AccessibleObject) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) FieldDeclaration(org.eclipse.xtend.lib.macro.declaration.FieldDeclaration) ParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration) Test(org.junit.Test)

Example 9 with TypeDeclaration

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

the class DeclarationsTest method testNestedClass.

@Test
public void testNestedClass() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package p");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Outer {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static class Inner {}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        Assert.assertEquals("p", it.getPackageName());
        TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
        final ClassDeclaration outer = ((ClassDeclaration) _head);
        Assert.assertEquals("p.Outer", outer.getQualifiedName());
        final ClassDeclaration inner = IterableExtensions.head(outer.getDeclaredClasses());
        Assert.assertEquals("Inner", inner.getSimpleName());
        Assert.assertEquals("p.Outer.Inner", inner.getQualifiedName());
        Assert.assertNotNull(it.getTypeLookup().findClass("p.Outer.Inner"));
        Assert.assertNotNull(it.getTypeLookup().findTypeGlobally("p.Outer.Inner"));
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : ClassDeclaration(org.eclipse.xtend.lib.macro.declaration.ClassDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeDeclaration) Test(org.junit.Test)

Aggregations

TypeDeclaration (org.eclipse.xtend.lib.macro.declaration.TypeDeclaration)9 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)8 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)7 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)7 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)7 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)7 Test (org.junit.Test)7 FieldDeclaration (org.eclipse.xtend.lib.macro.declaration.FieldDeclaration)4 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)4 AccessibleObject (java.lang.reflect.AccessibleObject)3 MutableMemberDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMemberDeclaration)3 MemberDeclaration (org.eclipse.xtend.lib.macro.declaration.MemberDeclaration)2 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)2 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)2 Consumer (java.util.function.Consumer)1 JvmAnnotationTypeDeclarationImpl (org.eclipse.xtend.core.macro.declaration.JvmAnnotationTypeDeclarationImpl)1 JvmClassDeclarationImpl (org.eclipse.xtend.core.macro.declaration.JvmClassDeclarationImpl)1 JvmEnumerationTypeDeclarationImpl (org.eclipse.xtend.core.macro.declaration.JvmEnumerationTypeDeclarationImpl)1 JvmInterfaceDeclarationImpl (org.eclipse.xtend.core.macro.declaration.JvmInterfaceDeclarationImpl)1 JvmTypeDeclarationImpl (org.eclipse.xtend.core.macro.declaration.JvmTypeDeclarationImpl)1