Search in sources :

Example 21 with MutableFieldDeclaration

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

the class DeclarationsTest method testAnnotation4.

/**
 * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=465007
 */
@Test
public void testAnnotation4() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@test.Annotation2 String foo");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@test.Annotation2(\"hubble\") String foo2");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@test.Annotation2(value=\"hubble\") String foo3");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final AnnotationReference anno = IterableExtensions.head(IterableExtensions.head(it.getTypeLookup().findClass("MyClass").getDeclaredFields()).getAnnotations());
        final AnnotationReference copied = it.getAnnotationReferenceProvider().newAnnotationReference(anno);
        Assert.assertTrue(((JvmAnnotationReferenceImpl) copied).getDelegate().getExplicitValues().isEmpty());
        final AnnotationReference anno2 = IterableExtensions.head(((MutableFieldDeclaration[]) Conversions.unwrapArray(it.getTypeLookup().findClass("MyClass").getDeclaredFields(), MutableFieldDeclaration.class))[1].getAnnotations());
        final AnnotationReference copied2 = it.getAnnotationReferenceProvider().newAnnotationReference(anno2);
        Assert.assertEquals(1, ((JvmAnnotationReferenceImpl) copied2).getDelegate().getExplicitValues().size());
        final AnnotationReference anno3 = IterableExtensions.head(((MutableFieldDeclaration[]) Conversions.unwrapArray(it.getTypeLookup().findClass("MyClass").getDeclaredFields(), MutableFieldDeclaration.class))[2].getAnnotations());
        final AnnotationReference copied3 = it.getAnnotationReferenceProvider().newAnnotationReference(anno3);
        Assert.assertEquals(1, ((JvmAnnotationReferenceImpl) copied3).getDelegate().getExplicitValues().size());
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) JvmAnnotationReferenceImpl(org.eclipse.xtend.core.macro.declaration.JvmAnnotationReferenceImpl) Test(org.junit.Test)

Example 22 with MutableFieldDeclaration

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

the class JvmTypeDeclarationImpl method addField.

public MutableFieldDeclaration addField(final String name, final Procedure1<MutableFieldDeclaration> initializer) {
    this.checkMutable();
    ConditionUtils.checkJavaIdentifier(name, "name");
    Preconditions.checkArgument((initializer != null), "initializer cannot be null");
    final JvmField newField = TypesFactory.eINSTANCE.createJvmField();
    newField.setSimpleName(name);
    newField.setVisibility(JvmVisibility.PRIVATE);
    this.getDelegate().getMembers().add(newField);
    MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(newField);
    final MutableFieldDeclaration mutableFieldDeclaration = ((MutableFieldDeclaration) _memberDeclaration);
    initializer.apply(mutableFieldDeclaration);
    return mutableFieldDeclaration;
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) MemberDeclaration(org.eclipse.xtend.lib.macro.declaration.MemberDeclaration) JvmField(org.eclipse.xtext.common.types.JvmField)

Example 23 with MutableFieldDeclaration

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

MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)23 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)16 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)16 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)12 Test (org.junit.Test)12 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)11 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)5 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)5 CompilationStrategy (org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)3 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)3 JvmField (org.eclipse.xtext.common.types.JvmField)3 Set (java.util.Set)2 EObject (org.eclipse.emf.ecore.EObject)2 MutableConstructorDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration)2 MutableTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration)2 MutableTypeParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration)2 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)2 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)2 Procedure0 (org.eclipse.xtext.xbase.lib.Procedures.Procedure0)2 Serializable (java.io.Serializable)1