Search in sources :

Example 31 with Function1

use of org.eclipse.xtext.xbase.lib.Functions.Function1 in project xtext-xtend by eclipse.

the class AnnotationReferenceProviderImpl method newAnnotationReference.

@Override
public AnnotationReference newAnnotationReference(final AnnotationReference annotationReference, final Procedure1<AnnotationReferenceBuildContext> initializer) {
    Object _xblockexpression = null;
    {
        this.compilationUnit.checkCanceled();
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("annotationReference cannot be null");
        Preconditions.checkArgument((annotationReference != null), _builder);
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("initializer cannot be null");
        Preconditions.checkArgument((initializer != null), _builder_1);
        if ((annotationReference instanceof JvmAnnotationReferenceImpl)) {
            final JvmAnnotationReference baseJvmAnnotationReference = ((JvmAnnotationReferenceImpl) annotationReference).getDelegate();
            ConditionUtils.notRemoved(baseJvmAnnotationReference, "annotationReference");
            final JvmAnnotationReference newJvmAnnotationReference = this.createJvmAnnotationReference(baseJvmAnnotationReference.getAnnotation());
            AnnotationReferenceBuildContextImpl _annotationReferenceBuildContextImpl = new AnnotationReferenceBuildContextImpl();
            final Procedure1<AnnotationReferenceBuildContextImpl> _function = (AnnotationReferenceBuildContextImpl it) -> {
                it.setDelegate(newJvmAnnotationReference);
                it.setCompilationUnit(this.compilationUnit);
            };
            final AnnotationReferenceBuildContextImpl buildContext = ObjectExtensions.<AnnotationReferenceBuildContextImpl>operator_doubleArrow(_annotationReferenceBuildContextImpl, _function);
            final Function1<JvmAnnotationValue, String> _function_1 = (JvmAnnotationValue it) -> {
                String _elvis = null;
                String _valueName = it.getValueName();
                if (_valueName != null) {
                    _elvis = _valueName;
                } else {
                    _elvis = "value";
                }
                return _elvis;
            };
            List<String> _map = ListExtensions.<JvmAnnotationValue, String>map(baseJvmAnnotationReference.getExplicitValues(), _function_1);
            for (final String valueName : _map) {
                {
                    final Object value = ((JvmAnnotationReferenceImpl) annotationReference).getValue(valueName);
                    buildContext.set(valueName, value);
                }
            }
            initializer.apply(buildContext);
            return this.compilationUnit.toAnnotationReference(newJvmAnnotationReference);
        }
        _xblockexpression = null;
    }
    return ((AnnotationReference) _xblockexpression);
}
Also used : AnnotationReferenceBuildContextImpl(org.eclipse.xtend.core.macro.declaration.AnnotationReferenceBuildContextImpl) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) List(java.util.List) JvmAnnotationReferenceImpl(org.eclipse.xtend.core.macro.declaration.JvmAnnotationReferenceImpl) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) JvmAnnotationValue(org.eclipse.xtext.common.types.JvmAnnotationValue)

Example 32 with Function1

use of org.eclipse.xtext.xbase.lib.Functions.Function1 in project xtext-xtend by eclipse.

the class JvmTypeDeclarationImpl method findDeclaredConstructor.

public ConstructorDeclaration findDeclaredConstructor(final TypeReference... parameterTypes) {
    ConstructorDeclaration _xblockexpression = null;
    {
        ConditionUtils.checkIterable(((Iterable<?>) Conversions.doWrapArray(parameterTypes)), "parameterTypes");
        final Function1<ConstructorDeclaration, Boolean> _function = (ConstructorDeclaration constructor) -> {
            final Function1<ParameterDeclaration, TypeReference> _function_1 = (ParameterDeclaration it) -> {
                return it.getType();
            };
            List<TypeReference> _list = IterableExtensions.<TypeReference>toList(IterableExtensions.map(constructor.getParameters(), _function_1));
            List<TypeReference> _list_1 = IterableExtensions.<TypeReference>toList(((Iterable<TypeReference>) Conversions.doWrapArray(parameterTypes)));
            return Boolean.valueOf(Objects.equal(_list, _list_1));
        };
        _xblockexpression = IterableExtensions.findFirst(this.getDeclaredConstructors(), _function);
    }
    return _xblockexpression;
}
Also used : MutableConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration) ConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.ConstructorDeclaration) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) ParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration)

Example 33 with Function1

use of org.eclipse.xtext.xbase.lib.Functions.Function1 in project xtext-xtend by eclipse.

the class ResourceStorageTest method testFailedWrite.

@Test(expected = IOException.class)
public void testFailedWrite() throws Exception {
    final XtendFile file = this.file("class C{}");
    ByteArrayOutputStream _byteArrayOutputStream = new ByteArrayOutputStream();
    Resource _eResource = file.eResource();
    new BatchLinkableResourceStorageWritable(_byteArrayOutputStream, false) {

        @Override
        protected void writeAssociationsAdapter(final BatchLinkableResource resource, final OutputStream zipOut) throws IOException {
            final Function1<Adapter, Boolean> _function = (Adapter it) -> {
                return Boolean.valueOf((it instanceof JvmModelAssociator.Adapter));
            };
            final Adapter removeMe = IterableExtensions.<Adapter>findFirst(resource.eAdapters(), _function);
            Assert.assertTrue(resource.eAdapters().remove(removeMe));
            super.writeAssociationsAdapter(resource, zipOut);
        }
    }.writeResource(((StorageAwareResource) _eResource));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Resource(org.eclipse.emf.ecore.resource.Resource) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) BatchLinkableResourceStorageWritable(org.eclipse.xtext.xbase.resource.BatchLinkableResourceStorageWritable) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) DocumentationAdapter(org.eclipse.xtext.xbase.compiler.DocumentationAdapter) Adapter(org.eclipse.emf.common.notify.Adapter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) Test(org.junit.Test)

Example 34 with Function1

use of org.eclipse.xtext.xbase.lib.Functions.Function1 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 35 with Function1

use of org.eclipse.xtext.xbase.lib.Functions.Function1 in project xtext-xtend by eclipse.

the class AccessObjectProcessorTest method testWithoutPackage.

@Test
public void testWithoutPackage() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@org.eclipse.xtend.core.tests.macro.AccessObjectAnn");
    _builder.newLine();
    _builder.append("class A {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String field");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
        final TransformationContext ctx = it.getTransformationContext();
        final MutableClassDeclaration classA = ctx.findClass("A");
        Assert.assertNotNull(classA.findDeclaredMethod("getField"));
        final MutableClassDeclaration classPA = ctx.findClass("PA");
        final Function1<TypeReference, Boolean> _function_1 = (TypeReference it_1) -> {
            Type _type = it_1.getType();
            Type _type_1 = ctx.newTypeReference(Serializable.class).getType();
            return Boolean.valueOf(Objects.equal(_type, _type_1));
        };
        Assert.assertNotNull(IterableExtensions.findFirst(classPA.getImplementedInterfaces(), _function_1));
        final MutableClassDeclaration classGA = ctx.findClass("GA");
        final Function1<TypeReference, Boolean> _function_2 = (TypeReference it_1) -> {
            Type _type = it_1.getType();
            Type _type_1 = ctx.newTypeReference(Serializable.class).getType();
            return Boolean.valueOf(Objects.equal(_type, _type_1));
        };
        Assert.assertNotNull(IterableExtensions.findFirst(classGA.getImplementedInterfaces(), _function_2));
    };
    this._xtendCompilerTester.compile(_builder, _function);
}
Also used : Type(org.eclipse.xtend.lib.macro.declaration.Type) Serializable(java.io.Serializable) XtendCompilerTester(org.eclipse.xtend.core.compiler.batch.XtendCompilerTester) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) TransformationContext(org.eclipse.xtend.lib.macro.TransformationContext) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) AbstractActiveAnnotationTest(org.eclipse.xtend.core.tests.macro.AbstractActiveAnnotationTest) Test(org.junit.Test)

Aggregations

Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)131 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)80 Test (org.junit.Test)54 List (java.util.List)20 ArrayList (java.util.ArrayList)19 AbstractXtendCompilerTest (org.eclipse.xtend.core.tests.compiler.AbstractXtendCompilerTest)17 CompilationTestHelper (org.eclipse.xtext.xbase.testing.CompilationTestHelper)17 EList (org.eclipse.emf.common.util.EList)14 EObject (org.eclipse.emf.ecore.EObject)14 ColumnSource (com.robotoworks.mechanoid.db.sqliteModel.ColumnSource)13 Method (java.lang.reflect.Method)12 Set (java.util.Set)10 IMethod (org.eclipse.jdt.core.IMethod)10 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)9 ScreenListItemCell (org.applause.lang.applauseDsl.ScreenListItemCell)8 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)8 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)8 ColumnType (com.robotoworks.mechanoid.db.sqliteModel.ColumnType)7 URI (org.eclipse.emf.common.util.URI)7 Resource (org.eclipse.emf.ecore.resource.Resource)7