Search in sources :

Example 1 with FieldDeclaration

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

the class DataProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration it, @Extension final TransformationContext context) {
    @Extension final DataProcessor.Util util = new DataProcessor.Util(context);
    @Extension final AccessorsProcessor.Util getterUtil = new AccessorsProcessor.Util(context);
    @Extension final EqualsHashCodeProcessor.Util ehUtil = new EqualsHashCodeProcessor.Util(context);
    @Extension final ToStringProcessor.Util toStringUtil = new ToStringProcessor.Util(context);
    @Extension final FinalFieldsConstructorProcessor.Util requiredArgsUtil = new FinalFieldsConstructorProcessor.Util(context);
    final Consumer<MutableFieldDeclaration> _function = (MutableFieldDeclaration it_1) -> {
        Element _primarySourceElement = context.getPrimarySourceElement(it_1);
        boolean _contains = ((FieldDeclaration) _primarySourceElement).getModifiers().contains(Modifier.VAR);
        if (_contains) {
            context.addError(it_1, "Cannot use the \'var\' keyword on a data field");
        }
        it_1.setFinal(true);
    };
    util.getDataFields(it).forEach(_function);
    if ((requiredArgsUtil.needsFinalFieldConstructor(it) || (it.findAnnotation(context.findTypeGlobally(FinalFieldsConstructor.class)) != null))) {
        requiredArgsUtil.addFinalFieldsConstructor(it);
    }
    boolean _hasHashCode = ehUtil.hasHashCode(it);
    boolean _not = (!_hasHashCode);
    if (_not) {
        ehUtil.addHashCode(it, util.getDataFields(it), ehUtil.hasSuperHashCode(it));
    }
    boolean _hasEquals = ehUtil.hasEquals(it);
    boolean _not_1 = (!_hasEquals);
    if (_not_1) {
        ehUtil.addEquals(it, util.getDataFields(it), ehUtil.hasSuperEquals(it));
    }
    boolean _hasToString = toStringUtil.hasToString(it);
    boolean _not_2 = (!_hasToString);
    if (_not_2) {
        ResolvedConstructor _superConstructor = requiredArgsUtil.getSuperConstructor(it);
        boolean _tripleEquals = (_superConstructor == null);
        if (_tripleEquals) {
            Iterable<? extends MutableFieldDeclaration> _dataFields = util.getDataFields(it);
            ToStringConfiguration _elvis = null;
            ToStringConfiguration _toStringConfig = toStringUtil.getToStringConfig(it);
            if (_toStringConfig != null) {
                _elvis = _toStringConfig;
            } else {
                ToStringConfiguration _toStringConfiguration = new ToStringConfiguration();
                _elvis = _toStringConfiguration;
            }
            toStringUtil.addToString(it, _dataFields, _elvis);
        } else {
            ToStringConfiguration _elvis_1 = null;
            ToStringConfiguration _toStringConfig_1 = toStringUtil.getToStringConfig(it);
            if (_toStringConfig_1 != null) {
                _elvis_1 = _toStringConfig_1;
            } else {
                ToStringConfiguration _toStringConfiguration_1 = new ToStringConfiguration();
                _elvis_1 = _toStringConfiguration_1;
            }
            toStringUtil.addReflectiveToString(it, _elvis_1);
        }
    }
    final Consumer<MutableFieldDeclaration> _function_1 = (MutableFieldDeclaration it_1) -> {
        boolean _shouldAddGetter = getterUtil.shouldAddGetter(it_1);
        if (_shouldAddGetter) {
            Visibility _elvis_2 = null;
            AccessorType _getterType = getterUtil.getGetterType(it_1);
            Visibility _visibility = null;
            if (_getterType != null) {
                _visibility = getterUtil.toVisibility(_getterType);
            }
            if (_visibility != null) {
                _elvis_2 = _visibility;
            } else {
                _elvis_2 = Visibility.PUBLIC;
            }
            getterUtil.addGetter(it_1, _elvis_2);
        }
    };
    util.getDataFields(it).forEach(_function_1);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) AccessorsProcessor(org.eclipse.xtend.lib.annotations.AccessorsProcessor) Element(org.eclipse.xtend.lib.macro.declaration.Element) ToStringConfiguration(org.eclipse.xtend.lib.annotations.ToStringConfiguration) EqualsHashCodeProcessor(org.eclipse.xtend.lib.annotations.EqualsHashCodeProcessor) FinalFieldsConstructorProcessor(org.eclipse.xtend.lib.annotations.FinalFieldsConstructorProcessor) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) FieldDeclaration(org.eclipse.xtend.lib.macro.declaration.FieldDeclaration) ResolvedConstructor(org.eclipse.xtend.lib.macro.declaration.ResolvedConstructor) Extension(org.eclipse.xtext.xbase.lib.Extension) ToStringProcessor(org.eclipse.xtend.lib.annotations.ToStringProcessor) FinalFieldsConstructor(org.eclipse.xtend.lib.annotations.FinalFieldsConstructor) Visibility(org.eclipse.xtend.lib.macro.declaration.Visibility) AccessorType(org.eclipse.xtend.lib.annotations.AccessorType)

Example 2 with FieldDeclaration

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

the class DeclarationsTest method testAnnotation2.

@Test
public void testAnnotation2() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@com.google.inject.Inject() MyClass foo");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
        final ClassDeclaration sourceClazz = ((ClassDeclaration) _head);
        final MutableClassDeclaration javaClass = it.getTypeLookup().findClass("MyClass");
        Assert.assertEquals(javaClass.getQualifiedName(), sourceClazz.getQualifiedName());
        final FieldDeclaration field = IterableExtensions.head(sourceClazz.getDeclaredFields());
        Assert.assertEquals(Boolean.FALSE, IterableExtensions.head(field.getAnnotations()).getValue("optional"));
        final MutableFieldDeclaration javaField = IterableExtensions.head(javaClass.getDeclaredFields());
        Object _value = IterableExtensions.head(javaField.getAnnotations()).getValue("optional");
        Assert.assertFalse((((Boolean) _value)).booleanValue());
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : ClassDeclaration(org.eclipse.xtend.lib.macro.declaration.ClassDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) 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) Test(org.junit.Test)

Example 3 with FieldDeclaration

use of org.eclipse.xtend.lib.macro.declaration.FieldDeclaration 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 4 with FieldDeclaration

use of org.eclipse.xtend.lib.macro.declaration.FieldDeclaration 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)

Example 5 with FieldDeclaration

use of org.eclipse.xtend.lib.macro.declaration.FieldDeclaration 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)

Aggregations

FieldDeclaration (org.eclipse.xtend.lib.macro.declaration.FieldDeclaration)6 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)6 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)5 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)5 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)4 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)4 TypeDeclaration (org.eclipse.xtend.lib.macro.declaration.TypeDeclaration)4 Test (org.junit.Test)4 AccessibleObject (java.lang.reflect.AccessibleObject)3 MemberDeclaration (org.eclipse.xtend.lib.macro.declaration.MemberDeclaration)2 MutableMemberDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMemberDeclaration)2 AccessorType (org.eclipse.xtend.lib.annotations.AccessorType)1 AccessorsProcessor (org.eclipse.xtend.lib.annotations.AccessorsProcessor)1 EqualsHashCodeProcessor (org.eclipse.xtend.lib.annotations.EqualsHashCodeProcessor)1 FinalFieldsConstructor (org.eclipse.xtend.lib.annotations.FinalFieldsConstructor)1 FinalFieldsConstructorProcessor (org.eclipse.xtend.lib.annotations.FinalFieldsConstructorProcessor)1 ToStringConfiguration (org.eclipse.xtend.lib.annotations.ToStringConfiguration)1 ToStringProcessor (org.eclipse.xtend.lib.annotations.ToStringProcessor)1 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)1