Search in sources :

Example 56 with Function1

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

the class JdtFindReferencesTest method testMethodJavaElements.

@Test
public void testMethodJavaElements() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("class Xtend {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("def foo() {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final XtendMember method = IterableExtensions.<XtendMember>head(IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(this._workbenchTestHelper.xtendFile("Xtend.xtend", _builder.toString()).getXtendTypes(), XtendClass.class)).getMembers());
        IResourcesSetupUtil.waitForBuild();
        Iterable<IJavaElement> _javaElements = this._jvmModelFindReferenceHandler.getJavaElements(method);
        final Procedure1<Iterable<IJavaElement>> _function = (Iterable<IJavaElement> it) -> {
            Assert.assertEquals(1, IterableExtensions.size(it));
            final Function1<IJavaElement, Boolean> _function_1 = (IJavaElement it_1) -> {
                return Boolean.valueOf(((it_1 instanceof IMethod) && Objects.equal(((IMethod) it_1).getElementName(), "foo")));
            };
            Assert.assertTrue(IterableExtensions.<IJavaElement>exists(it, _function_1));
        };
        ObjectExtensions.<Iterable<IJavaElement>>operator_doubleArrow(_javaElements, _function);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) IMethod(org.eclipse.jdt.core.IMethod) Test(org.junit.Test)

Example 57 with Function1

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

the class JdtFindReferencesTest method testFindPropertyRef.

@Test
public void testFindPropertyRef() {
    try {
        this._workbenchTestHelper.createFile("Xtend.xtend", "class Xtend { @Property public int foo = 0 }");
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("public class JavaRef {");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("private Xtend x = new Xtend();");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("private int bar = x.getFoo();");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public void baz() {");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("x.setFoo(1);");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("\t");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public int fooBar() {");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("return x._foo;");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        this._workbenchTestHelper.createFile("JavaRef.java", _builder.toString());
        IResourcesSetupUtil.waitForBuild();
        final IType type = JavaCore.create(this._workbenchTestHelper.getProject()).findType("Xtend");
        final IField field = type.getField("_foo");
        final IMethod getter = type.getMethod("getFoo", ((String[]) Conversions.unwrapArray(CollectionLiterals.<String>newArrayList(), String.class)));
        final IMethod setter = type.getMethod("setFoo", ((String[]) Conversions.unwrapArray(CollectionLiterals.<String>newArrayList("I"), String.class)));
        ArrayList<Object> _findReferences = this.findReferences(field, getter, setter);
        final Procedure1<ArrayList<Object>> _function = (ArrayList<Object> it) -> {
            Assert.assertEquals(5, it.size());
            final Function1<Object, Boolean> _function_1 = (Object it_1) -> {
                return Boolean.valueOf(((it_1 instanceof IField) && Objects.equal(((IField) it_1).getElementName(), "bar")));
            };
            Assert.assertTrue(IterableExtensions.<Object>exists(it, _function_1));
            final Function1<Object, Boolean> _function_2 = (Object it_1) -> {
                return Boolean.valueOf(((it_1 instanceof IMethod) && Objects.equal(((IMethod) it_1).getElementName(), "baz")));
            };
            Assert.assertTrue(IterableExtensions.<Object>exists(it, _function_2));
            final Function1<Object, Boolean> _function_3 = (Object it_1) -> {
                return Boolean.valueOf(((it_1 instanceof IMethod) && Objects.equal(((IMethod) it_1).getElementName(), "fooBar")));
            };
            Assert.assertTrue(IterableExtensions.<Object>exists(it, _function_3));
            final Function1<Object, Boolean> _function_4 = (Object it_1) -> {
                return Boolean.valueOf(((it_1 instanceof IMethod) && Objects.equal(((IMethod) it_1).getElementName(), "getFoo")));
            };
            Assert.assertTrue(IterableExtensions.<Object>exists(it, _function_4));
            final Function1<Object, Boolean> _function_5 = (Object it_1) -> {
                return Boolean.valueOf(((it_1 instanceof IMethod) && Objects.equal(((IMethod) it_1).getElementName(), "setFoo")));
            };
            Assert.assertTrue(IterableExtensions.<Object>exists(it, _function_5));
        };
        ObjectExtensions.<ArrayList<Object>>operator_doubleArrow(_findReferences, _function);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ArrayList(java.util.ArrayList) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test)

Example 58 with Function1

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

the class JdtFindReferencesTest method testClassJavaElements.

@Test
public void testClassJavaElements() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("class Xtend {");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final XtendTypeDeclaration clazz = IterableExtensions.<XtendTypeDeclaration>head(this._workbenchTestHelper.xtendFile("Xtend.xtend", _builder.toString()).getXtendTypes());
        IResourcesSetupUtil.waitForBuild();
        Iterable<IJavaElement> _javaElements = this._jvmModelFindReferenceHandler.getJavaElements(clazz);
        final Procedure1<Iterable<IJavaElement>> _function = (Iterable<IJavaElement> it) -> {
            Assert.assertEquals(1, IterableExtensions.size(it));
            final Function1<IJavaElement, Boolean> _function_1 = (IJavaElement it_1) -> {
                return Boolean.valueOf(((it_1 instanceof IType) && Objects.equal(((IType) it_1).getElementName(), "Xtend")));
            };
            Assert.assertTrue(IterableExtensions.<IJavaElement>exists(it, _function_1));
        };
        ObjectExtensions.<Iterable<IJavaElement>>operator_doubleArrow(_javaElements, _function);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test)

Example 59 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.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 60 with Function1

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

the class AbstractReusableActiveAnnotationTests method testTracing.

@Test
public void testTracing() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
    _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.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(GettersProcessor)");
    _builder.newLine();
    _builder.append("annotation Getters {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class GettersProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.declaredFields.filter[isThePrimaryGeneratedJavaElement].forEach [field|");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("cls.addMethod(\"get\" + field.simpleName.toFirstUpper) [");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("primarySourceElement = field");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("returnType = field.type");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("body = [\"return this.\" + field.simpleName + \";\"]");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("field.markAsRead");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/Getters.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.Getters");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@Getters");
    _builder_1.newLine();
    _builder_1.append("class Client {");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val int bar = 1");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def create new Integer(1) foo() {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _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 cls = it.getTypeLookup().findClass("myusercode.Client");
        final MutableFieldDeclaration barField = cls.findDeclaredField("bar");
        final Function1<MutableMethodDeclaration, Boolean> _function_1 = (MutableMethodDeclaration it_1) -> {
            return Boolean.valueOf(it_1.getSimpleName().startsWith("get"));
        };
        final Iterable<? extends MutableMethodDeclaration> getters = IterableExtensions.filter(cls.getDeclaredMethods(), _function_1);
        Assert.assertEquals(1, IterableExtensions.size(getters));
        final MutableMethodDeclaration getBar = IterableExtensions.head(getters);
        Assert.assertEquals("getBar", getBar.getSimpleName());
        final JvmField barJvmField = ((MutableJvmFieldDeclarationImpl) barField).getDelegate();
        final JvmOperation getBarJvmMethod = ((MutableJvmMethodDeclarationImpl) getBar).getDelegate();
        final Set<EObject> elementsAssociatedWithBarField = it.getJvmModelAssociations().getJvmElements(it.getJvmModelAssociations().getPrimarySourceElement(barJvmField));
        Assert.assertEquals(2, elementsAssociatedWithBarField.size());
        Assert.assertEquals(barJvmField, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[0]);
        Assert.assertEquals(getBarJvmMethod, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[1]);
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : Set(java.util.Set) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EObject(org.eclipse.emf.ecore.EObject) JvmField(org.eclipse.xtext.common.types.JvmField) 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