Search in sources :

Example 36 with TypeReference

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

Example 37 with TypeReference

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

the class TracedProcessor method doTransform.

@Override
public void doTransform(final MutableMethodDeclaration annotatedMethod, @Extension final TransformationContext context) {
    final boolean useForDebugging = annotatedMethod.findAnnotation(context.findTypeGlobally(Traced.class)).getBooleanValue("useForDebugging");
    final TypeReference traceSugar = context.newTypeReference(TracingSugar.class);
    final TypeReference templateClient = context.newTypeReference(StringConcatenationClient.class);
    final TypeReference nodeType = context.newTypeReference(IGeneratorNode.class);
    final TypeReference eobjectType = context.newTypeReference(EObject.class);
    MutableTypeDeclaration _declaringType = annotatedMethod.getDeclaringType();
    final MutableClassDeclaration clazz = ((MutableClassDeclaration) _declaringType);
    final Function1<MutableFieldDeclaration, Boolean> _function = (MutableFieldDeclaration it) -> {
        return Boolean.valueOf(traceSugar.isAssignableFrom(it.getType()));
    };
    final MutableFieldDeclaration field = IterableExtensions.findFirst(clazz.getDeclaredFields(), _function);
    if ((field == null)) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("@");
        String _simpleName = Traced.class.getSimpleName();
        _builder.append(_simpleName);
        _builder.append(" methods can only declared in a class with a field of type ");
        _builder.append(TracingSugar.class);
        context.addError(annotatedMethod, _builder.toString());
        return;
    }
    final Function1<MutableParameterDeclaration, Boolean> _function_1 = (MutableParameterDeclaration it) -> {
        return Boolean.valueOf(eobjectType.isAssignableFrom(it.getType()));
    };
    final MutableParameterDeclaration traceParam = IterableExtensions.findFirst(annotatedMethod.getParameters(), _function_1);
    if ((traceParam == null)) {
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("@");
        String _simpleName_1 = Traced.class.getSimpleName();
        _builder_1.append(_simpleName_1);
        _builder_1.append(" methods need at least one parameter of type ");
        _builder_1.append(EObject.class);
        _builder_1.append(".");
        context.addError(annotatedMethod, _builder_1.toString());
        return;
    }
    String _simpleName_2 = annotatedMethod.getSimpleName();
    String _plus = ("_" + _simpleName_2);
    final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it) -> {
        Iterable<? extends MutableParameterDeclaration> _parameters = annotatedMethod.getParameters();
        for (final MutableParameterDeclaration p : _parameters) {
            it.addParameter(p.getSimpleName(), p.getType());
        }
        it.setReturnType(templateClient);
        it.setBody(annotatedMethod.getBody());
    };
    clazz.addMethod(_plus, _function_2);
    annotatedMethod.setReturnType(nodeType);
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append(ILocationData.class);
            _builder.append(" _location = this.");
            String _simpleName = field.getSimpleName();
            _builder.append(_simpleName);
            _builder.append(".location(");
            String _simpleName_1 = traceParam.getSimpleName();
            _builder.append(_simpleName_1);
            _builder.append(");");
            _builder.newLineIfNotEmpty();
            _builder.append(CompositeGeneratorNode.class);
            _builder.append(" _traceNode = this.");
            String _simpleName_2 = field.getSimpleName();
            _builder.append(_simpleName_2);
            _builder.append(".trace(_location, ");
            _builder.append(useForDebugging);
            _builder.append(");");
            _builder.newLineIfNotEmpty();
            _builder.append("this.");
            String _simpleName_3 = field.getSimpleName();
            _builder.append(_simpleName_3);
            _builder.append(".appendTemplate(_traceNode, _");
            String _simpleName_4 = annotatedMethod.getSimpleName();
            _builder.append(_simpleName_4);
            _builder.append("(");
            final Function1<MutableParameterDeclaration, CharSequence> _function = (MutableParameterDeclaration it) -> {
                return it.getSimpleName();
            };
            String _join = IterableExtensions.join(annotatedMethod.getParameters(), ",", _function);
            _builder.append(_join);
            _builder.append("));");
            _builder.newLineIfNotEmpty();
            _builder.append("return _traceNode;");
            _builder.newLine();
        }
    };
    annotatedMethod.setBody(_client);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration) MutableTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

Aggregations

TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)37 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)13 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)10 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)10 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)8 Test (org.junit.Test)8 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)7 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)6 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)5 MutableTypeParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration)5 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)4 Type (org.eclipse.xtend.lib.macro.declaration.Type)4 TypeParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.TypeParameterDeclaration)4 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)4 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)4 Serializable (java.io.Serializable)3 EObject (org.eclipse.emf.ecore.EObject)3 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)3 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)3 JvmUpperBound (org.eclipse.xtext.common.types.JvmUpperBound)3