Search in sources :

Example 1 with Visibility

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

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

the class ExtractProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    final MutableInterfaceDeclaration interfaceType = context.findInterface(this.getInterfaceName(annotatedClass));
    context.setPrimarySourceElement(interfaceType, annotatedClass);
    Iterable<? extends TypeReference> _implementedInterfaces = annotatedClass.getImplementedInterfaces();
    TypeReference _newTypeReference = context.newTypeReference(interfaceType);
    Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_implementedInterfaces, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(_newTypeReference)));
    annotatedClass.setImplementedInterfaces(_plus);
    Iterable<? extends MutableMethodDeclaration> _declaredMethods = annotatedClass.getDeclaredMethods();
    for (final MutableMethodDeclaration method : _declaredMethods) {
        Visibility _visibility = method.getVisibility();
        boolean _equals = Objects.equal(_visibility, Visibility.PUBLIC);
        if (_equals) {
            final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
                it.setDocComment(method.getDocComment());
                it.setReturnType(method.getReturnType());
                Iterable<? extends MutableParameterDeclaration> _parameters = method.getParameters();
                for (final MutableParameterDeclaration p : _parameters) {
                    it.addParameter(p.getSimpleName(), p.getType());
                }
                it.setExceptions(((TypeReference[]) Conversions.unwrapArray(method.getExceptions(), TypeReference.class)));
                context.setPrimarySourceElement(it, method);
            };
            interfaceType.addMethod(method.getSimpleName(), _function);
        }
    }
}
Also used : MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration) Visibility(org.eclipse.xtend.lib.macro.declaration.Visibility) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

Aggregations

Visibility (org.eclipse.xtend.lib.macro.declaration.Visibility)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 Element (org.eclipse.xtend.lib.macro.declaration.Element)1 FieldDeclaration (org.eclipse.xtend.lib.macro.declaration.FieldDeclaration)1 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)1 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)1 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)1 MutableParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration)1 ResolvedConstructor (org.eclipse.xtend.lib.macro.declaration.ResolvedConstructor)1 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)1 Extension (org.eclipse.xtext.xbase.lib.Extension)1