Search in sources :

Example 1 with ResolvedMethod

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

the class TracedAccessorsProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    annotatedClass.setExtendedClass(context.newTypeReference(TracingSugar.class));
    final TypeReference iterableType = context.newTypeReference(Iterable.class, context.newWildcardTypeReference());
    final TypeReference annotationType = context.newTypeReference(TracedAccessors.class);
    AnnotationReference _findAnnotation = annotatedClass.findAnnotation(annotationType.getType());
    TypeReference[] _classArrayValue = null;
    if (_findAnnotation != null) {
        _classArrayValue = _findAnnotation.getClassArrayValue("value");
    }
    final TypeReference[] factories = _classArrayValue;
    if ((factories == null)) {
        return;
    }
    final Function1<TypeReference, Type> _function = (TypeReference it) -> {
        return it.getType();
    };
    Iterable<InterfaceDeclaration> _filter = Iterables.<InterfaceDeclaration>filter(ListExtensions.<TypeReference, Type>map(((List<TypeReference>) Conversions.doWrapArray(factories)), _function), InterfaceDeclaration.class);
    for (final InterfaceDeclaration f : _filter) {
        final Function1<MethodDeclaration, Boolean> _function_1 = (MethodDeclaration it) -> {
            return Boolean.valueOf((it.getSimpleName().startsWith("create") && IterableExtensions.isEmpty(it.getParameters())));
        };
        final Function1<MethodDeclaration, TypeReference> _function_2 = (MethodDeclaration it) -> {
            return it.getReturnType();
        };
        Iterable<TypeReference> _map = IterableExtensions.map(IterableExtensions.filter(f.getDeclaredMethods(), _function_1), _function_2);
        for (final TypeReference t : _map) {
            final Function1<ResolvedMethod, Boolean> _function_3 = (ResolvedMethod it) -> {
                return Boolean.valueOf(this.isSupportedGetter(it));
            };
            final Function1<ResolvedMethod, Boolean> _function_4 = (ResolvedMethod it) -> {
                boolean _isAssignableFrom = iterableType.isAssignableFrom(it.getDeclaration().getReturnType());
                return Boolean.valueOf((!_isAssignableFrom));
            };
            Iterable<? extends ResolvedMethod> _filter_1 = IterableExtensions.filter(IterableExtensions.filter(t.getAllResolvedMethods(), _function_3), _function_4);
            for (final ResolvedMethod getter : _filter_1) {
                {
                    final TypeReference rt = getter.getResolvedReturnType();
                    boolean _contains = TracedAccessorsProcessor.TYPES_WITH_GOOD_TO_STRING.contains(rt.getType().getSimpleName().toLowerCase());
                    if (_contains) {
                        final Procedure1<MutableMethodDeclaration> _function_5 = (MutableMethodDeclaration it) -> {
                            it.setReturnType(context.newTypeReference(IGeneratorNode.class));
                            it.addParameter("target", t);
                            StringConcatenationClient _client = new StringConcatenationClient() {

                                @Override
                                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                    _builder.append(EStructuralFeature.class);
                                    _builder.append(" feature = target.eClass().getEStructuralFeature(\"");
                                    String _featureName = TracedAccessorsProcessor.this.featureName(getter);
                                    _builder.append(_featureName);
                                    _builder.append("\");");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append(ILocationData.class);
                                    _builder.append(" location = this.location(target, feature, -1);");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append(CompositeGeneratorNode.class);
                                    _builder.append(" trace = this.trace(location);");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append("this.append(trace, target.");
                                    String _simpleName = getter.getDeclaration().getSimpleName();
                                    _builder.append(_simpleName);
                                    _builder.append("());");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append("return trace;");
                                    _builder.newLine();
                                }
                            };
                            it.setBody(_client);
                        };
                        annotatedClass.addMethod(this.tracerName(getter), _function_5);
                        final Procedure1<MutableMethodDeclaration> _function_6 = (MutableMethodDeclaration it) -> {
                            it.setReturnType(context.newTypeReference(IGeneratorNode.class));
                            it.addParameter("target", t);
                            it.addParameter("useForDebugging", context.newTypeReference(Boolean.TYPE));
                            StringConcatenationClient _client = new StringConcatenationClient() {

                                @Override
                                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                    _builder.append(EStructuralFeature.class);
                                    _builder.append(" feature = target.eClass().getEStructuralFeature(\"");
                                    String _featureName = TracedAccessorsProcessor.this.featureName(getter);
                                    _builder.append(_featureName);
                                    _builder.append("\");");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append(ILocationData.class);
                                    _builder.append(" location = this.location(target, feature, -1);");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append(CompositeGeneratorNode.class);
                                    _builder.append(" trace = this.trace(location, useForDebugging);");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append("this.append(trace, target.");
                                    String _simpleName = getter.getDeclaration().getSimpleName();
                                    _builder.append(_simpleName);
                                    _builder.append("());");
                                    _builder.newLineIfNotEmpty();
                                    _builder.append("return trace;");
                                    _builder.newLine();
                                }
                            };
                            it.setBody(_client);
                        };
                        annotatedClass.addMethod(this.tracerName(getter), _function_6);
                    }
                    final Procedure1<MutableMethodDeclaration> _function_7 = (MutableMethodDeclaration it) -> {
                        it.setReturnType(context.newTypeReference(IGeneratorNode.class));
                        it.addParameter("target", t);
                        final TypeReference stringProvider = context.newTypeReference(Function.class, rt, context.getString());
                        it.addParameter("stringProvider", stringProvider);
                        StringConcatenationClient _client = new StringConcatenationClient() {

                            @Override
                            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                _builder.append(EStructuralFeature.class);
                                _builder.append(" feature = target.eClass().getEStructuralFeature(\"");
                                String _featureName = TracedAccessorsProcessor.this.featureName(getter);
                                _builder.append(_featureName);
                                _builder.append("\");");
                                _builder.newLineIfNotEmpty();
                                _builder.append(ILocationData.class);
                                _builder.append(" location = this.location(target, feature, -1);");
                                _builder.newLineIfNotEmpty();
                                _builder.append(CompositeGeneratorNode.class);
                                _builder.append(" trace = this.trace(location);");
                                _builder.newLineIfNotEmpty();
                                _builder.append("this.append(trace, stringProvider.apply(target.");
                                String _simpleName = getter.getDeclaration().getSimpleName();
                                _builder.append(_simpleName);
                                _builder.append("()));");
                                _builder.newLineIfNotEmpty();
                                _builder.append("return trace;");
                                _builder.newLine();
                            }
                        };
                        it.setBody(_client);
                    };
                    annotatedClass.addMethod(this.tracerName(getter), _function_7);
                }
            }
        }
    }
}
Also used : MethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MethodDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) InterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.InterfaceDeclaration) IGeneratorNode(org.eclipse.xtext.generator.trace.node.IGeneratorNode) Type(org.eclipse.xtend.lib.macro.declaration.Type) ResolvedMethod(org.eclipse.xtend.lib.macro.declaration.ResolvedMethod) TracingSugar(org.eclipse.xtext.generator.trace.node.TracingSugar) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

Aggregations

AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)1 InterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.InterfaceDeclaration)1 MethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MethodDeclaration)1 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)1 ResolvedMethod (org.eclipse.xtend.lib.macro.declaration.ResolvedMethod)1 Type (org.eclipse.xtend.lib.macro.declaration.Type)1 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)1 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)1 IGeneratorNode (org.eclipse.xtext.generator.trace.node.IGeneratorNode)1 TracingSugar (org.eclipse.xtext.generator.trace.node.TracingSugar)1 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)1