Search in sources :

Example 91 with JvmOperation

use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.

the class DispatchMethodRenameStrategy method initialize.

@Override
public boolean initialize(EObject xtendMethod, IRenameElementContext context) {
    Assert.isLegal(xtendMethod instanceof XtendFunction);
    Assert.isLegal(((XtendFunction) xtendMethod).isDispatch());
    Assert.isLegal(context instanceof DispatchMethodRenameContext);
    ResourceSet resourceSet = xtendMethod.eResource().getResourceSet();
    Map<URI, IJavaElement> jvm2JavaElements = ((DispatchMethodRenameContext) context).getJvm2JavaElements();
    for (URI dispatchOperationURI : jvm2JavaElements.keySet()) {
        JvmOperation dispatchOperation = (JvmOperation) resourceSet.getEObject(dispatchOperationURI, true);
        XtendFunction xtendDispatchMethod = associations.getXtendFunction(dispatchOperation);
        if (xtendDispatchMethod != null) {
            if (equal(xtendDispatchMethod.getName(), dispatchOperation.getSimpleName())) {
                // synthetic dispatcher
                dispatchers.add(dispatchOperation);
            } else {
                // xtend dispatch method
                XtendDispatchMethodChildStrategy xtendChildStrategy = childStrategyProvider.get();
                xtendChildStrategy.initialize(xtendDispatchMethod, context);
                children.add(xtendChildStrategy);
            }
        } else {
            // a dispatch method form a Java class
            JavaDispatchMethodChildStrategy jvmChildStrategy = javaStrategyProvider.get();
            jvmChildStrategy.initialize(dispatchOperation, context);
            children.add(jvmChildStrategy);
        }
    }
    return !children.isEmpty();
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) IJavaElement(org.eclipse.jdt.core.IJavaElement) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI)

Example 92 with JvmOperation

use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.

the class ExtractMethodRefactoring method appendMethodSignature.

protected void appendMethodSignature(ISourceAppender appendable) {
    if (visibility != JvmVisibility.PUBLIC)
        appendable.append(getVisibility().getName().toLowerCase()).append(" ");
    appendable.append("def ");
    if (isStatic)
        appendable.append("static ");
    if (!neededTypeParameters.isEmpty()) {
        JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
        if (operation != null) {
            appendable.append("<");
            boolean isFirst = true;
            for (JvmTypeParameter typeParameter : operation.getTypeParameters()) {
                if (neededTypeParameters.contains(typeParameter)) {
                    if (!isFirst)
                        appendable.append(", ");
                    isFirst = false;
                    appendable.append(typeParameter);
                }
            }
            appendable.append("> ");
        }
    }
    if (isExplicitlyDeclareReturnType) {
        appendable.append(returnType).append(" ");
    }
    appendable.append(methodName).append("(");
    boolean isFirst = true;
    for (ParameterInfo parameterInfo : getParameterInfos()) {
        if (!isFirst)
            appendable.append(", ");
        isFirst = false;
        appendable.append(parameter2type.get(parameterInfo)).append(" ").append(parameterInfo.getNewName());
    }
    appendable.append(")");
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Example 93 with JvmOperation

use of org.eclipse.xtext.common.types.JvmOperation 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)

Example 94 with JvmOperation

use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.

the class ResourceStorageTest method testUpstreamResourcesAreLoadedFromStorage.

@Test
public void testUpstreamResourcesAreLoadedFromStorage() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("package mypack");
        _builder.newLine();
        _builder.newLine();
        _builder.append("class MyClass {");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        this.helper.createFile("mypack/MyClass.xtend", _builder.toString());
        final IProject downStreamProject = WorkbenchTestHelper.createPluginProject("downstream", this.helper.getProject().getName());
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("package downstream");
        _builder_1.newLine();
        _builder_1.newLine();
        _builder_1.append("class SomeClass {");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("def void foo(mypack.MyClass myClass) {");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("}");
        _builder_1.newLine();
        _builder_1.append("}");
        _builder_1.newLine();
        final IFile downstreamFile = this.helper.createFileImpl("/downstream/src/downstream/SomeClass.xtend", _builder_1.toString());
        IResourcesSetupUtil.waitForBuild();
        final URI downstreamUri = this.uriMapper.getUri(downstreamFile);
        final ResourceSet resourceSet = this.resourceSetProvider.get(downStreamProject);
        SourceLevelURIsAdapter.setSourceLevelUris(resourceSet, Collections.<URI>unmodifiableList(CollectionLiterals.<URI>newArrayList(downstreamUri)));
        Resource _resource = resourceSet.getResource(downstreamUri, true);
        final StorageAwareResource downstreamResource = ((StorageAwareResource) _resource);
        EObject _get = downstreamResource.getContents().get(1);
        final JvmGenericType type = ((JvmGenericType) _get);
        final JvmType parameterType = IterableExtensions.<JvmFormalParameter>head(IterableExtensions.<JvmOperation>head(Iterables.<JvmOperation>filter(type.getMembers(), JvmOperation.class)).getParameters()).getParameterType().getType();
        Assert.assertNotNull(parameterType);
        Resource _eResource = parameterType.eResource();
        Assert.assertTrue(((StorageAwareResource) _eResource).isLoadedFromStorage());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) Resource(org.eclipse.emf.ecore.resource.Resource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) JvmType(org.eclipse.xtext.common.types.JvmType) URI(org.eclipse.emf.common.util.URI) AbsoluteURI(org.eclipse.xtext.generator.trace.AbsoluteURI) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) IProject(org.eclipse.core.resources.IProject) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Test(org.junit.Test)

Example 95 with JvmOperation

use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.

the class XtendJvmModelInferrer method appendSyntheticDispatchMethods.

protected void appendSyntheticDispatchMethods(XtendTypeDeclaration source, final JvmGenericType target) {
    ListMultimap<DispatchHelper.DispatchSignature, JvmOperation> methods = dispatchHelper.getDeclaredOrEnhancedDispatchMethods(target);
    for (DispatchHelper.DispatchSignature signature : methods.keySet()) {
        List<JvmOperation> operations = methods.get(signature);
        Iterable<JvmOperation> localOperations = Iterables.filter(operations, new Predicate<JvmOperation>() {

            @Override
            public boolean apply(JvmOperation input) {
                return target == input.eContainer();
            }
        });
        JvmOperation operation = deriveGenericDispatchOperationSignature(localOperations, target);
        if (operation != null) {
            dispatchHelper.markAsDispatcherFunction(operation);
            operation.setSimpleName(signature.getSimpleName());
            operation.setReturnType(jvmTypesBuilder.inferredType());
        }
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation)

Aggregations

JvmOperation (org.eclipse.xtext.common.types.JvmOperation)202 Test (org.junit.Test)127 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)101 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)73 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)71 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)62 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)49 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)38 EObject (org.eclipse.emf.ecore.EObject)30 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)26 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)22 XExpression (org.eclipse.xtext.xbase.XExpression)20 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)20 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)20 JvmField (org.eclipse.xtext.common.types.JvmField)18 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)15 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)15 JvmMember (org.eclipse.xtext.common.types.JvmMember)15 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)11 DispatchHelper (org.eclipse.xtend.core.jvmmodel.DispatchHelper)10