Search in sources :

Example 1 with ToStringConfiguration

use of org.eclipse.xtend.lib.annotations.ToStringConfiguration 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 ToStringConfiguration

use of org.eclipse.xtend.lib.annotations.ToStringConfiguration in project xtext-lib by eclipse.

the class ToStringProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration it, @Extension final TransformationContext context) {
    AnnotationReference _findAnnotation = it.findAnnotation(context.findTypeGlobally(Data.class));
    boolean _tripleNotEquals = (_findAnnotation != null);
    if (_tripleNotEquals) {
        return;
    }
    @Extension final ToStringProcessor.Util util = new ToStringProcessor.Util(context);
    final AnnotationReference annotation = it.findAnnotation(context.findTypeGlobally(ToString.class));
    final ToStringConfiguration configuration = new ToStringConfiguration(annotation);
    boolean _hasToString = util.hasToString(it);
    if (_hasToString) {
        context.addWarning(annotation, "toString is already defined, this annotation has no effect.");
    } else {
        TypeReference _extendedClass = it.getExtendedClass();
        TypeReference _object = context.getObject();
        boolean _notEquals = (!Objects.equal(_extendedClass, _object));
        if (_notEquals) {
            util.addReflectiveToString(it, configuration);
        } else {
            final Function1<MutableFieldDeclaration, Boolean> _function = (MutableFieldDeclaration it_1) -> {
                return Boolean.valueOf(((context.isThePrimaryGeneratedJavaElement(it_1) && (!it_1.isStatic())) && (!it_1.isTransient())));
            };
            util.addToString(it, IterableExtensions.filter(it.getDeclaredFields(), _function), configuration);
        }
    }
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) ToStringConfiguration(org.eclipse.xtend.lib.annotations.ToStringConfiguration) Data(org.eclipse.xtend.lib.annotations.Data) Extension(org.eclipse.xtext.xbase.lib.Extension) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) ToString(org.eclipse.xtend.lib.annotations.ToString)

Aggregations

ToStringConfiguration (org.eclipse.xtend.lib.annotations.ToStringConfiguration)2 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)2 Extension (org.eclipse.xtext.xbase.lib.Extension)2 AccessorType (org.eclipse.xtend.lib.annotations.AccessorType)1 AccessorsProcessor (org.eclipse.xtend.lib.annotations.AccessorsProcessor)1 Data (org.eclipse.xtend.lib.annotations.Data)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 ToString (org.eclipse.xtend.lib.annotations.ToString)1 ToStringProcessor (org.eclipse.xtend.lib.annotations.ToStringProcessor)1 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)1 Element (org.eclipse.xtend.lib.macro.declaration.Element)1 FieldDeclaration (org.eclipse.xtend.lib.macro.declaration.FieldDeclaration)1 ResolvedConstructor (org.eclipse.xtend.lib.macro.declaration.ResolvedConstructor)1 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)1 Visibility (org.eclipse.xtend.lib.macro.declaration.Visibility)1