Search in sources :

Example 1 with CompilationUnit

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

the class DeclarationsTest method testMutableClassDeclaration.

@Test
public void testMutableClassDeclaration() {
    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) -> {
        final MutableClassDeclaration genClazz = it.getTypeLookup().findClass("foo.MyClass");
        final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it_1) -> {
            CompilationUnit _compilationUnit = genClazz.getCompilationUnit();
            it_1.setReturnType(((CompilationUnitImpl) _compilationUnit).getTypeReferenceProvider().getString());
            it_1.setVisibility(Visibility.PRIVATE);
            final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_2) -> {
                StringConcatenation _builder_1 = new StringConcatenation();
                _builder_1.append("return \"foo\";");
                _builder_1.newLine();
                return _builder_1;
            };
            it_1.setBody(_function_2);
        };
        genClazz.addMethod("newMethod", _function_1);
        final MutableMethodDeclaration mutableMethod = genClazz.findDeclaredMethod("newMethod");
        Assert.assertSame(mutableMethod, ((Object[]) Conversions.unwrapArray(genClazz.getDeclaredMethods(), Object.class))[1]);
        Assert.assertEquals("String", mutableMethod.getReturnType().toString());
        Assert.assertEquals(Visibility.PRIVATE, mutableMethod.getVisibility());
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : CompilationUnit(org.eclipse.xtend.lib.macro.declaration.CompilationUnit) 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) AccessibleObject(java.lang.reflect.AccessibleObject) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) CompilationStrategy(org.eclipse.xtend.lib.macro.declaration.CompilationStrategy) Test(org.junit.Test)

Aggregations

AccessibleObject (java.lang.reflect.AccessibleObject)1 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)1 CompilationStrategy (org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)1 CompilationUnit (org.eclipse.xtend.lib.macro.declaration.CompilationUnit)1 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)1 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)1 Test (org.junit.Test)1