Search in sources :

Example 1 with MutableParameterDeclaration

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

the class AbstractReusableActiveAnnotationTests method testParameterAnnotation.

@Test
public void testParameterAnnotation() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.util.List");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.RegisterGlobalsParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.RegisterGlobalsContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(typeof(ParamProcessor))");
    _builder.newLine();
    _builder.append("annotation Param { }");
    _builder.newLine();
    _builder.append("class ParamProcessor implements TransformationParticipant<MutableParameterDeclaration>, RegisterGlobalsParticipant<ParameterDeclaration> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableParameterDeclaration> params, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("params.forEach[");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("simpleName = simpleName+\'foo\'");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doRegisterGlobals(List<? extends ParameterDeclaration> annotatedSourceElements, extension RegisterGlobalsContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedSourceElements.forEach [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("context.registerInterface(\"demo.I\"+it.simpleName.toFirstUpper)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AbstractAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("class MyClass {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def void foo(@myannotation.Param String a, @myannotation.Param String b) {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.MyClass");
        final Function1<MutableParameterDeclaration, Boolean> _function_1 = (MutableParameterDeclaration it_1) -> {
            return Boolean.valueOf(it_1.getSimpleName().endsWith("foo"));
        };
        Assert.assertTrue(IterableExtensions.forall(IterableExtensions.head(clazz.getDeclaredMethods()).getParameters(), _function_1));
        Assert.assertNotNull(it.getTypeLookup().findInterface("demo.IA"));
        Assert.assertNotNull(it.getTypeLookup().findInterface("demo.IB"));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) MutableParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 2 with MutableParameterDeclaration

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

the class JvmExecutableDeclarationImpl method addParameter.

public MutableParameterDeclaration addParameter(final String name, final TypeReference type) {
    this.checkMutable();
    ConditionUtils.checkJavaIdentifier(name, "name");
    Preconditions.checkArgument((type != null), "type cannot be null");
    boolean _isInferred = type.isInferred();
    if (_isInferred) {
        throw new IllegalArgumentException("Cannot use inferred type as parameter type.");
    }
    final JvmFormalParameter param = TypesFactory.eINSTANCE.createJvmFormalParameter();
    param.setName(name);
    param.setParameterType(this.getCompilationUnit().toJvmTypeReference(type));
    this.getDelegate().getParameters().add(param);
    ParameterDeclaration _parameterDeclaration = this.getCompilationUnit().toParameterDeclaration(param);
    return ((MutableParameterDeclaration) _parameterDeclaration);
}
Also used : JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) ParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration) MutableTypeParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration) MutableParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration) TypeParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeParameterDeclaration)

Example 3 with MutableParameterDeclaration

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

the class AbstractReusableActiveAnnotationTests method testParameterAnnotation.

@Test
public void testParameterAnnotation() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.util.List");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(typeof(ParamProcessor))");
    _builder.newLine();
    _builder.append("annotation Param { }");
    _builder.newLine();
    _builder.append("class ParamProcessor implements TransformationParticipant<MutableParameterDeclaration> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableParameterDeclaration> params, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("params.forEach[");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("simpleName = simpleName+\'foo\'");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AbstractAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("class MyClass {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def void foo(@myannotation.Param String a, @myannotation.Param String b) {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.MyClass");
        final Function1<MutableParameterDeclaration, Boolean> _function_1 = (MutableParameterDeclaration it_1) -> {
            return Boolean.valueOf(it_1.getSimpleName().endsWith("foo"));
        };
        Assert.assertTrue(IterableExtensions.forall(IterableExtensions.head(clazz.getDeclaredMethods()).getParameters(), _function_1));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) MutableParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 4 with MutableParameterDeclaration

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

the class CheckMutableParameterDeclarationProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableParameterDeclaration> annotatedTargetElements, @Extension final TransformationContext context) {
    for (final MutableParameterDeclaration annotatedTargetElement : annotatedTargetElements) {
        final Procedure1<String> _function = (String identifier) -> {
            annotatedTargetElement.setSimpleName(identifier);
        };
        MutableAssert.assertValidJavaIdentifier("name", _function);
    }
}
Also used : MutableParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration)

Example 5 with MutableParameterDeclaration

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

the class ExternalizedProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    Iterable<? extends MutableFieldDeclaration> _declaredFields = annotatedClass.getDeclaredFields();
    for (final MutableFieldDeclaration field : _declaredFields) {
        {
            final String initializer = this.getInitializerAsString(field);
            MessageFormat _xtrycatchfinallyexpression = null;
            try {
                _xtrycatchfinallyexpression = new MessageFormat(initializer);
            } catch (final Throwable _t) {
                if (_t instanceof IllegalArgumentException) {
                    final IllegalArgumentException e = (IllegalArgumentException) _t;
                    MessageFormat _xblockexpression = null;
                    {
                        Expression _initializer = field.getInitializer();
                        String _message = e.getMessage();
                        String _plus = ("invalid format : " + _message);
                        context.addError(_initializer, _plus);
                        _xblockexpression = new MessageFormat("");
                    }
                    _xtrycatchfinallyexpression = _xblockexpression;
                } else {
                    throw Exceptions.sneakyThrow(_t);
                }
            }
            final MessageFormat msgFormat = _xtrycatchfinallyexpression;
            final Format[] formats = msgFormat.getFormatsByArgumentIndex();
            int _length = msgFormat.getFormats().length;
            int _length_1 = formats.length;
            boolean _notEquals = (_length != _length_1);
            if (_notEquals) {
                context.addWarning(field.getInitializer(), "Unused placeholders. They should start at index 0.");
            }
            final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
                final Procedure2<Format, Integer> _function_1 = (Format format, Integer idx) -> {
                    TypeReference _switchResult = null;
                    boolean _matched = false;
                    if (format instanceof NumberFormat) {
                        _matched = true;
                        _switchResult = context.getPrimitiveInt();
                    }
                    if (!_matched) {
                        if (format instanceof DateFormat) {
                            _matched = true;
                            _switchResult = context.newTypeReference(Date.class);
                        }
                    }
                    if (!_matched) {
                        _switchResult = context.getString();
                    }
                    it.addParameter(("arg" + idx), _switchResult);
                };
                IterableExtensions.<Format>forEach(((Iterable<Format>) Conversions.doWrapArray(formats)), _function_1);
                it.setReturnType(context.getString());
                it.setDocComment(initializer);
                it.setStatic(true);
                final Iterable<? extends MutableParameterDeclaration> params = it.getParameters();
                StringConcatenationClient _client = new StringConcatenationClient() {

                    @Override
                    protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                        _builder.append("try {");
                        _builder.newLine();
                        _builder.append("\t");
                        _builder.append("String msg = RESOURCE_BUNDLE.getString(\"");
                        String _simpleName = field.getSimpleName();
                        _builder.append(_simpleName, "\t");
                        _builder.append("\");");
                        _builder.newLineIfNotEmpty();
                        {
                            int _length = formats.length;
                            boolean _greaterThan = (_length > 0);
                            if (_greaterThan) {
                                _builder.append("\t");
                                _builder.append("msg = ");
                                _builder.append(MessageFormat.class, "\t");
                                _builder.append(".format(msg,");
                                final Function1<MutableParameterDeclaration, String> _function = (MutableParameterDeclaration it_1) -> {
                                    return it_1.getSimpleName();
                                };
                                String _join = IterableExtensions.join(IterableExtensions.map(params, _function), ",");
                                _builder.append(_join, "\t");
                                _builder.append(");");
                                _builder.newLineIfNotEmpty();
                            }
                        }
                        _builder.append("\t");
                        _builder.append("return msg;");
                        _builder.newLine();
                        _builder.append("} catch (");
                        _builder.append(MissingResourceException.class);
                        _builder.append(" e) {");
                        _builder.newLineIfNotEmpty();
                        _builder.append("\t");
                        _builder.append("// TODO error logging");
                        _builder.newLine();
                        _builder.append("\t");
                        _builder.append("return \"");
                        _builder.append(initializer, "\t");
                        _builder.append("\";");
                        _builder.newLineIfNotEmpty();
                        _builder.append("}");
                        _builder.newLine();
                    }
                };
                it.setBody(_client);
                context.setPrimarySourceElement(it, field);
            };
            annotatedClass.addMethod(field.getSimpleName(), _function);
        }
    }
    final Consumer<MutableFieldDeclaration> _function = (MutableFieldDeclaration it) -> {
        it.remove();
    };
    annotatedClass.getDeclaredFields().forEach(_function);
    final Procedure1<MutableFieldDeclaration> _function_1 = (MutableFieldDeclaration it) -> {
        it.setStatic(true);
        it.setFinal(true);
        it.setType(context.newTypeReference(ResourceBundle.class));
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("ResourceBundle.getBundle(\"");
                String _qualifiedName = annotatedClass.getQualifiedName();
                _builder.append(_qualifiedName);
                _builder.append("\")");
            }
        };
        it.setInitializer(_client);
        context.setPrimarySourceElement(it, annotatedClass);
    };
    annotatedClass.addField("RESOURCE_BUNDLE", _function_1);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Format(java.text.Format) NumberFormat(java.text.NumberFormat) MessageFormat(java.text.MessageFormat) DateFormat(java.text.DateFormat) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) MessageFormat(java.text.MessageFormat) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) MutableParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration) Expression(org.eclipse.xtend.lib.macro.expression.Expression) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) DateFormat(java.text.DateFormat) NumberFormat(java.text.NumberFormat)

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