Search in sources :

Example 1 with MutableTypeDeclaration

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

the class AccessObjectProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableClassDeclaration> annotatedSourceClasses, @Extension final TransformationContext ctx) {
    final Consumer<MutableClassDeclaration> _function = (MutableClassDeclaration it) -> {
        final Consumer<MutableFieldDeclaration> _function_1 = (MutableFieldDeclaration field) -> {
            MutableTypeDeclaration _declaringType = field.getDeclaringType();
            String _firstUpper = StringExtensions.toFirstUpper(field.getSimpleName());
            String _plus = ("get" + _firstUpper);
            final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it_1) -> {
                it_1.setReturnType(field.getType());
                final CompilationStrategy _function_3 = (CompilationStrategy.CompilationContext it_2) -> {
                    StringConcatenation _builder = new StringConcatenation();
                    _builder.append("return this.");
                    String _simpleName = field.getSimpleName();
                    _builder.append(_simpleName);
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                    return _builder;
                };
                it_1.setBody(_function_3);
            };
            _declaringType.addMethod(_plus, _function_2);
        };
        it.getDeclaredFields().forEach(_function_1);
        String _qualifiedName = it.getQualifiedName();
        int _length = it.getQualifiedName().length();
        int _length_1 = it.getSimpleName().length();
        int _minus = (_length - _length_1);
        final String pkg = _qualifiedName.substring(0, _minus);
        final TypeReference ser = ctx.newTypeReference(Serializable.class);
        if ((ser == null)) {
            ctx.addError(it, "Cannot find Serializable");
        }
        String _simpleName = it.getSimpleName();
        final String PVersionName = ((pkg + "P") + _simpleName);
        final MutableClassDeclaration p = ctx.findClass(PVersionName);
        if ((p == null)) {
            ctx.addError(it, (("Class " + PVersionName) + " not found"));
        }
        if (((p != null) && (ser != null))) {
            final LinkedList<TypeReference> pIfcs = new LinkedList<TypeReference>();
            pIfcs.add(ser);
            p.setImplementedInterfaces(pIfcs);
        }
        String _simpleName_1 = it.getSimpleName();
        final String GVersionName = ((pkg + "G") + _simpleName_1);
        final MutableClassDeclaration g = ctx.findClass(GVersionName);
        if ((g == null)) {
            ctx.addError(it, (("Class " + GVersionName) + " not found"));
        }
        if (((g != null) && (ser != null))) {
            final LinkedList<TypeReference> gIfcs = new LinkedList<TypeReference>();
            gIfcs.add(ser);
            g.setImplementedInterfaces(gIfcs);
        }
    };
    annotatedSourceClasses.forEach(_function);
}
Also used : Serializable(java.io.Serializable) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) LinkedList(java.util.LinkedList) Consumer(java.util.function.Consumer) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) CompilationStrategy(org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)

Example 2 with MutableTypeDeclaration

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

MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)2 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)2 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)2 MutableTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration)2 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)2 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 Serializable (java.io.Serializable)1 LinkedList (java.util.LinkedList)1 Consumer (java.util.function.Consumer)1 CompilationStrategy (org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)1 MutableParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration)1 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)1