Search in sources :

Example 36 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.

the class DeclarationsTest method testRemove.

@Test
public void testRemove() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def void m() {}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
        final ClassDeclaration c = ((ClassDeclaration) _head);
        final MutableClassDeclaration mutable = it.getTypeLookup().findClass(c.getQualifiedName());
        final Consumer<MutableMemberDeclaration> _function_1 = (MutableMemberDeclaration it_1) -> {
            it_1.remove();
        };
        mutable.getDeclaredMembers().forEach(_function_1);
        Assert.assertTrue(IterableExtensions.isEmpty(mutable.getDeclaredMembers()));
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : MutableMemberDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMemberDeclaration) ClassDeclaration(org.eclipse.xtend.lib.macro.declaration.ClassDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Consumer(java.util.function.Consumer) 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) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 37 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.

the class DeclarationsTest method testAnnotationReferenceValues_2.

@Test
public void testAnnotationReferenceValues_2() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package foo");
    _builder.newLine();
    _builder.append("@test.Annotation(");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("intValue = 2 / 2 + 2 * 3 - 4 % 1,");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("longValue = 42 + 4 + 6 * 42 - 4 / 45,");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("stringValue = \'foo\' + \'baz\',");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("booleanArrayValue = #[true, false],");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("intArrayValue = #[ -1, 34 + 45, 2 - 6 ],");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("longArrayValue = #[42, 5 * -3],");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("stringArrayValue = #[\'foo\', \'bla\' + \'buzz\'],");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("typeValue = String,");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("typeArrayValue = #[String, Integer],");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("annotation2Value = @test.Annotation2(\'foo\' + \'wuppa\'),");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("annotation2ArrayValue = #[@test.Annotation2, @test.Annotation2(\'foo\'+\'wuppa\')]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append(") class Bar {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration baseClass = it.getTypeLookup().findClass("foo.Bar");
        final AnnotationReference annoRef = IterableExtensions.head(baseClass.getAnnotations());
        Assert.assertEquals((((2 / 2) + (2 * 3)) - (4 % 1)), annoRef.getIntValue("intValue"));
        Assert.assertEquals((((42 + 4) + (6 * 42)) - (4 / 45)), annoRef.getLongValue("longValue"));
        Assert.assertEquals("foobaz", annoRef.getStringValue("stringValue"));
        final boolean[] bools = annoRef.getBooleanArrayValue("booleanArrayValue");
        Assert.assertTrue(bools[0]);
        Assert.assertFalse(bools[1]);
        Assert.assertArrayEquals(new int[] { (-1), (34 + 45), (2 - 6) }, annoRef.getIntArrayValue("intArrayValue"));
        final TypeReference[] type = annoRef.getClassArrayValue("typeArrayValue");
        Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(Integer.class), type[1]);
        final AnnotationReference anno = annoRef.getAnnotationValue("annotation2Value");
        Assert.assertEquals("foowuppa", anno.getStringValue("value"));
        final AnnotationReference[] annoArray = annoRef.getAnnotationArrayValue("annotation2ArrayValue");
        Assert.assertEquals("HUBBA BUBBA!", (annoArray[0]).getValue("value"));
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 38 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.

the class DeclarationsTest method testMutableInterfaceDeclaration.

@Test
public void testMutableInterfaceDeclaration() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package foo");
    _builder.newLine();
    _builder.newLine();
    _builder.append("interface MyInterface {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableInterfaceDeclaration genInterface = it.getTypeLookup().findInterface("foo.MyInterface");
        final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it_1) -> {
        };
        final MutableMethodDeclaration m = genInterface.addMethod("newMethod", _function_1);
        Assert.assertTrue(m.isAbstract());
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Test(org.junit.Test)

Example 39 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl 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 40 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.

the class DeclarationsTest method testAnnotation.

@Test
public void testAnnotation() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("@SuppressWarnings(\"unused\")");
    _builder.newLine();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@com.google.inject.Inject(optional=true) MyClass foo");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        Assert.assertNull(it.getPackageName());
        TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
        final ClassDeclaration clazz = ((ClassDeclaration) _head);
        Assert.assertEquals("MyClass", clazz.getQualifiedName());
        final AnnotationReference suppressWarning = IterableExtensions.head(clazz.getAnnotations());
        final AnnotationTypeDeclaration supressWarningsDeclaration = suppressWarning.getAnnotationTypeDeclaration();
        Assert.assertEquals("java.lang.SuppressWarnings", supressWarningsDeclaration.getQualifiedName());
        Assert.assertEquals("unused", suppressWarning.getStringArrayValue("value")[0]);
        Assert.assertEquals(2, IterableExtensions.size(supressWarningsDeclaration.getAnnotations()));
        final AnnotationTypeElementDeclaration valueProperty = IterableExtensions.<AnnotationTypeElementDeclaration>head(Iterables.<AnnotationTypeElementDeclaration>filter(supressWarningsDeclaration.getDeclaredMembers(), AnnotationTypeElementDeclaration.class));
        Assert.assertEquals("String[]", valueProperty.getType().toString());
        Assert.assertEquals("value", valueProperty.getSimpleName());
        MemberDeclaration _head_1 = IterableExtensions.head(clazz.getDeclaredMembers());
        final FieldDeclaration field = ((FieldDeclaration) _head_1);
        final AnnotationReference inject = IterableExtensions.head(field.getAnnotations());
        Object _value = inject.getValue("optional");
        Assert.assertTrue((((Boolean) _value)).booleanValue());
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : AnnotationTypeElementDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeElementDeclaration) 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) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) AccessibleObject(java.lang.reflect.AccessibleObject) 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)

Aggregations

CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)139 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)132 Test (org.junit.Test)130 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)91 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)22 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)17 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)15 EObject (org.eclipse.emf.ecore.EObject)12 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)11 Resource (org.eclipse.emf.ecore.resource.Resource)10 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)10 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)10 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)10 List (java.util.List)9 Element (org.eclipse.xtend.lib.macro.declaration.Element)9 Type (org.eclipse.xtend.lib.macro.declaration.Type)9 TypeDeclaration (org.eclipse.xtend.lib.macro.declaration.TypeDeclaration)9 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)9 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)9 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8