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);
}
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);
}
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);
}
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);
}
Aggregations