Search in sources :

Example 6 with MutableParameterDeclaration

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

Example 7 with MutableParameterDeclaration

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

the class TracedProcessor method doTransform.

@Override
public void doTransform(MutableMethodDeclaration annotatedMethod, @Extension TransformationContext context) {
    boolean useForDebugging = annotatedMethod.findAnnotation(context.findTypeGlobally(Traced.class)).getBooleanValue("useForDebugging");
    TypeReference traceSugar = context.newTypeReference(TracingSugar.class);
    TypeReference templateClient = context.newTypeReference(StringConcatenationClient.class);
    TypeReference nodeType = context.newTypeReference(IGeneratorNode.class);
    TypeReference eobjectType = context.newTypeReference(EObject.class);
    MutableClassDeclaration clazz = (MutableClassDeclaration) annotatedMethod.getDeclaringType();
    MutableFieldDeclaration field = IterableExtensions.findFirst(clazz.getDeclaredFields(), it -> Boolean.valueOf(traceSugar.isAssignableFrom(it.getType())));
    if (field == null) {
        context.addError(annotatedMethod, "@" + Traced.class.getSimpleName() + " methods can only declared in a class with a field of type " + TracingSugar.class);
        return;
    }
    MutableParameterDeclaration traceParam = IterableExtensions.findFirst(annotatedMethod.getParameters(), it -> eobjectType.isAssignableFrom(it.getType()));
    if (traceParam == null) {
        context.addError(annotatedMethod, "@" + Traced.class.getSimpleName() + " methods need at least one parameter of type " + EObject.class + ".");
        return;
    }
    clazz.addMethod("_" + annotatedMethod.getSimpleName(), it -> {
        for (MutableParameterDeclaration p : annotatedMethod.getParameters()) it.addParameter(p.getSimpleName(), p.getType());
        it.setReturnType(templateClient);
        it.setBody(annotatedMethod.getBody());
    });
    annotatedMethod.setReturnType(nodeType);
    annotatedMethod.setBody(new StringConcatenationClient() {

        @Override
        protected void appendTo(TargetStringConcatenation builder) {
            builder.append(ILocationData.class);
            builder.append(" _location = this.");
            builder.append(field.getSimpleName());
            builder.append(".location(");
            builder.append(traceParam.getSimpleName());
            builder.append(");");
            builder.newLineIfNotEmpty();
            builder.append(CompositeGeneratorNode.class);
            builder.append(" _traceNode = this.");
            builder.append(field.getSimpleName());
            builder.append(".trace(_location, ");
            builder.append(useForDebugging);
            builder.append(");");
            builder.newLineIfNotEmpty();
            builder.append("this.");
            builder.append(field.getSimpleName());
            builder.append(".appendTemplate(_traceNode, _");
            builder.append(annotatedMethod.getSimpleName());
            builder.append("(");
            builder.append(IterableExtensions.join(annotatedMethod.getParameters(), ",", it -> it.getSimpleName()));
            builder.append("));");
            builder.newLineIfNotEmpty();
            builder.append("return _traceNode;");
            builder.newLine();
        }
    });
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) MutableParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) ILocationData(org.eclipse.xtext.generator.trace.ILocationData)

Aggregations

MutableParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration)7 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)3 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)3 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)3 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)2 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)2 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)2 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)2 Test (org.junit.Test)2 DateFormat (java.text.DateFormat)1 Format (java.text.Format)1 MessageFormat (java.text.MessageFormat)1 NumberFormat (java.text.NumberFormat)1 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)1 MutableTypeParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration)1 ParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration)1 TypeParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.TypeParameterDeclaration)1 Visibility (org.eclipse.xtend.lib.macro.declaration.Visibility)1 Expression (org.eclipse.xtend.lib.macro.expression.Expression)1