Search in sources :

Example 86 with JvmTypeReference

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

the class XtendHoverSignatureProvider method _signature.

protected String _signature(XtendParameter parameter, boolean typeAtEnd) {
    EObject container = parameter.eContainer();
    JvmTypeReference type = parameter.getParameterType();
    if (type != null) {
        String signature = parameter.getName();
        String signatureOfFather = getSimpleSignature(container);
        if (signatureOfFather != null) {
            signature += JavaElementLabels.CONCAT_STRING + signatureOfFather;
        }
        if (typeAtEnd)
            return signature + " : " + type.getSimpleName();
        return type.getSimpleName() + " " + signature;
    }
    return parameter.getName();
}
Also used : JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject)

Example 87 with JvmTypeReference

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

the class XtendQuickfixProvider method addThrowsDeclaration.

@Fix(org.eclipse.xtext.xbase.validation.IssueCodes.UNHANDLED_EXCEPTION)
public void addThrowsDeclaration(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData() != null && issue.getData().length > 0)
        acceptor.accept(issue, "Add throws declaration", "Add throws declaration", "fix_indent.gif", new ISemanticModification() {

            @Override
            public void apply(EObject element, IModificationContext context) throws Exception {
                String[] issueData = issue.getData();
                XtendExecutable xtendExecutable = EcoreUtil2.getContainerOfType(element, XtendExecutable.class);
                XtextResource xtextResource = (XtextResource) xtendExecutable.eResource();
                List<JvmType> exceptions = getExceptions(issueData, xtextResource);
                if (exceptions.size() > 0) {
                    int insertPosition;
                    if (xtendExecutable.getExpression() == null) {
                        ICompositeNode functionNode = NodeModelUtils.findActualNodeFor(xtendExecutable);
                        if (functionNode == null)
                            throw new IllegalStateException("functionNode may not be null");
                        insertPosition = functionNode.getEndOffset();
                    } else {
                        ICompositeNode expressionNode = NodeModelUtils.findActualNodeFor(xtendExecutable.getExpression());
                        if (expressionNode == null)
                            throw new IllegalStateException("expressionNode may not be null");
                        insertPosition = expressionNode.getOffset();
                    }
                    ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) xtendExecutable.eResource(), insertPosition, 0);
                    if (xtendExecutable.getExpression() == null)
                        appendable.append(" ");
                    EList<JvmTypeReference> thrownExceptions = xtendExecutable.getExceptions();
                    if (thrownExceptions.isEmpty())
                        appendable.append("throws ");
                    else
                        appendable.append(", ");
                    for (int i = 0; i < exceptions.size(); i++) {
                        appendable.append(exceptions.get(i));
                        if (i != exceptions.size() - 1) {
                            appendable.append(", ");
                        }
                    }
                    if (xtendExecutable.getExpression() != null)
                        appendable.append(" ");
                    appendable.commitChanges();
                }
            }
        });
}
Also used : ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) XtextResource(org.eclipse.xtext.resource.XtextResource) JvmType(org.eclipse.xtext.common.types.JvmType) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) XtendExecutable(org.eclipse.xtend.core.xtend.XtendExecutable) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 88 with JvmTypeReference

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

the class DispatchRenameSupport method addDispatcher.

protected boolean addDispatcher(JvmGenericType type, DispatchHelper.DispatchSignature signature, final IAcceptor<JvmOperation> acceptor, final Set<JvmGenericType> processedTypes, ResourceSet tempResourceSet) {
    if (processedTypes.contains(type))
        return false;
    processedTypes.add(type);
    boolean needProcessSubclasses = false;
    JvmOperation dispatcher = dispatchHelper.getDispatcherOperation(type, signature);
    if (dispatcher != null) {
        needProcessSubclasses = true;
        acceptor.accept(dispatcher);
    }
    for (JvmOperation dispatchCase : dispatchHelper.getLocalDispatchCases(type, signature)) {
        needProcessSubclasses = true;
        acceptor.accept(dispatchCase);
    }
    for (JvmTypeReference superTypeRef : type.getSuperTypes()) {
        JvmType superType = superTypeRef.getType();
        if (superType instanceof JvmGenericType)
            needProcessSubclasses |= addDispatcher((JvmGenericType) superType, signature, acceptor, processedTypes, tempResourceSet);
    }
    if (needProcessSubclasses) {
        for (JvmGenericType subType : getSubTypes(type, tempResourceSet)) needProcessSubclasses |= addDispatcher(subType, signature, acceptor, processedTypes, tempResourceSet);
    }
    return needProcessSubclasses;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 89 with JvmTypeReference

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

the class ExtractMethodRefactoring method checkInitialConditions.

@Override
public RefactoringStatus checkInitialConditions(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    StatusWrapper status = statusProvider.get();
    IResolvedTypes resolvedTypes = typeResolver.resolveTypes(firstExpression, new CancelIndicator() {

        @Override
        public boolean isCanceled() {
            return pm.isCanceled();
        }
    });
    try {
        Set<String> calledExternalFeatureNames = newHashSet();
        returnType = calculateReturnType(resolvedTypes);
        if (returnType != null && !equal("void", returnType.getIdentifier()))
            returnExpression = lastExpression;
        boolean isReturnAllowed = isEndOfOriginalMethod();
        for (EObject element : EcoreUtil2.eAllContents(originalMethod.getExpression())) {
            if (pm.isCanceled()) {
                throw new OperationCanceledException();
            }
            boolean isLocalExpression = EcoreUtil.isAncestor(expressions, element);
            if (element instanceof XFeatureCall) {
                XFeatureCall featureCall = (XFeatureCall) element;
                JvmIdentifiableElement feature = featureCall.getFeature();
                LightweightTypeReference featureType = resolvedTypes.getActualType(featureCall);
                boolean isLocalFeature = EcoreUtil.isAncestor(expressions, feature);
                if (!isLocalFeature && isLocalExpression) {
                    // call-out
                    if (feature instanceof JvmFormalParameter || feature instanceof XVariableDeclaration) {
                        if (!calledExternalFeatureNames.contains(feature.getSimpleName())) {
                            calledExternalFeatureNames.add(feature.getSimpleName());
                            ParameterInfo parameterInfo = new ParameterInfo(featureType.getIdentifier(), feature.getSimpleName(), parameterInfos.size());
                            parameterInfos.add(parameterInfo);
                            parameter2type.put(parameterInfo, featureType);
                        }
                        externalFeatureCalls.put(feature.getSimpleName(), featureCall);
                    }
                } else if (isLocalFeature && !isLocalExpression) {
                    // call-in
                    if (returnExpression != null) {
                        status.add(RefactoringStatus.FATAL, "Ambiguous return value: Multiple local variables are accessed in subsequent code.");
                        break;
                    }
                    returnExpression = featureCall;
                    returnType = featureType;
                }
            } else if (isLocalExpression) {
                if (element instanceof XReturnExpression && !isReturnAllowed) {
                    status.add(RefactoringStatus.FATAL, "Extracting method would break control flow due to return statements.");
                    break;
                } else if (element instanceof JvmTypeReference) {
                    JvmType type = ((JvmTypeReference) element).getType();
                    if (type instanceof JvmTypeParameter) {
                        JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
                        if (operation != null) {
                            List<JvmTypeParameter> typeParameters = operation.getTypeParameters();
                            if (typeParameters.contains(type))
                                neededTypeParameters.add((JvmTypeParameter) type);
                        }
                    }
                } else if (element instanceof JvmFormalParameter)
                    localFeatureNames.add(((JvmFormalParameter) element).getName());
                else if (element instanceof XVariableDeclaration)
                    localFeatureNames.add(((XVariableDeclaration) element).getIdentifier());
            }
        }
    } catch (OperationCanceledException e) {
        throw e;
    } catch (Exception exc) {
        handleException(exc, status);
    }
    return status.getRefactoringStatus();
}
Also used : XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) StatusWrapper(org.eclipse.xtext.ui.refactoring.impl.StatusWrapper) RichString(org.eclipse.xtend.core.xtend.RichString) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) JvmType(org.eclipse.xtext.common.types.JvmType) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) XReturnExpression(org.eclipse.xtext.xbase.XReturnExpression)

Example 90 with JvmTypeReference

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

the class XtendUnsugaredHoverTest method testBug380361_2.

@Test
public void testBug380361_2() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package testpackage");
    _builder.newLine();
    _builder.append("import static extension testpackage.Baz.*");
    _builder.newLine();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String fieldInFoo");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("extension Extension");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def void foo(Bar it) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("fieldInBar");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("fieldInExtension");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("fieldInFoo");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("staticFieldInBaz");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Bar {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public String fieldInBar");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Baz {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public static String staticFieldInBaz");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile xtendFile = this.testHelper.xtendFile(XtendUnsugaredHoverTest.FILEPATH, _builder.toString());
    XtendMember _get = IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(xtendFile.getXtendTypes(), XtendClass.class)).getMembers().get(2);
    final XtendFunction function = ((XtendFunction) _get);
    XExpression _expression = function.getExpression();
    final XBlockExpression block = ((XBlockExpression) _expression);
    final XExpression call = block.getExpressions().get(0);
    final XExpression call2 = block.getExpressions().get(1);
    final XExpression call3 = block.getExpressions().get(2);
    final XExpression call4 = block.getExpressions().get(3);
    final XtendTypeDeclaration foo = IterableExtensions.<XtendTypeDeclaration>head(xtendFile.getXtendTypes());
    XtendMember _get_1 = foo.getMembers().get(1);
    final XtendField extensionField = ((XtendField) _get_1);
    final JvmTypeReference extensionFieldType = extensionField.getType();
    Assert.assertEquals("testpackage.Extension", extensionFieldType.getIdentifier());
    Assert.assertEquals("it.fieldInBar", this.serializer.computeUnsugaredExpression(call));
    Assert.assertEquals("this._extension.fieldInExtension", this.serializer.computeUnsugaredExpression(call2));
    Assert.assertEquals("this.fieldInFoo", this.serializer.computeUnsugaredExpression(call3));
    Assert.assertEquals("", this.serializer.computeUnsugaredExpression(call4));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XExpression(org.eclipse.xtext.xbase.XExpression) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Aggregations

JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)130 Test (org.junit.Test)58 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)46 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)38 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)36 JvmType (org.eclipse.xtext.common.types.JvmType)28 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)23 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)18 XExpression (org.eclipse.xtext.xbase.XExpression)18 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)17 EObject (org.eclipse.emf.ecore.EObject)15 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)12 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)12 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)12 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)12 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)8 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)8 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)8 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8 JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)8