Search in sources :

Example 6 with ITypeReferenceOwner

use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner in project xtext-xtend by eclipse.

the class DispatchHelper method getDeclaredOrEnhancedDispatchMethods.

/**
 * Computes all the dispatch methods that are declared in the given type or altered
 * by additional cases in this type. The associated operations are sorted by according their parameter types
 * from left to right where the most special types occur before more common types. Ambiguous
 * ordering is resolved alphabetically.
 *
 * An exemplary order would look like this
 * <pre>
 *   method(String)
 *   method(Serializable)
 *   method(CharSequence)
 *   method(Object)
 * </pre>
 *
 * @return a mapping from {@link DispatchSignature signature} to sorted operations.
 */
public ListMultimap<DispatchSignature, JvmOperation> getDeclaredOrEnhancedDispatchMethods(JvmDeclaredType type) {
    ListMultimap<DispatchSignature, JvmOperation> result = Multimaps2.newLinkedHashListMultimap(2, 4);
    Iterable<JvmOperation> operations = type.getDeclaredOperations();
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, type);
    ContextualVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(visibilityHelper, owner.newParameterizedTypeReference(type));
    for (JvmOperation operation : operations) {
        if (isDispatchFunction(operation)) {
            DispatchSignature signature = new DispatchSignature(operation.getSimpleName().substring(1), operation.getParameters().size());
            if (!result.containsKey(signature)) {
                List<JvmOperation> allOperations = getAllDispatchMethods(signature, type, contextualVisibilityHelper);
                result.putAll(signature, allOperations);
            }
        }
    }
    return result;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) ContextualVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 7 with ITypeReferenceOwner

use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner in project xtext-xtend by eclipse.

the class ActualTypeArgumentCollectorTest method mappedBy.

public Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> mappedBy(final String typeParameters, final String... alternatingTypeReferences) {
    final JvmOperation operation = this.operation(typeParameters, alternatingTypeReferences);
    EList<JvmTypeParameter> _typeParameters = operation.getTypeParameters();
    ITypeReferenceOwner _owner = this.getOwner();
    final ActualTypeArgumentCollector collector = new ActualTypeArgumentCollector(_typeParameters, BoundTypeArgumentSource.INFERRED, _owner);
    int _size = ((List<String>) Conversions.doWrapArray(alternatingTypeReferences)).size();
    int _minus = (_size - 1);
    IntegerRange _withStep = new IntegerRange(0, _minus).withStep(2);
    for (final Integer i : _withStep) {
        collector.populateTypeParameterMapping(this.toLightweightTypeReference(operation.getParameters().get((i).intValue()).getParameterType()), this.toLightweightTypeReference(operation.getParameters().get(((i).intValue() + 1)).getParameterType()));
    }
    return collector.getTypeParameterMapping();
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) IntegerRange(org.eclipse.xtext.xbase.lib.IntegerRange) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) EList(org.eclipse.emf.common.util.EList) List(java.util.List) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) ActualTypeArgumentCollector(org.eclipse.xtext.xbase.typesystem.util.ActualTypeArgumentCollector)

Example 8 with ITypeReferenceOwner

use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method fixUnreachableIfBlock.

@Fix(IssueCodes.UNREACHABLE_IF_BLOCK)
public void fixUnreachableIfBlock(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Remove if block", "Remove if block", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XIfExpression ifExpression = EcoreUtil2.getContainerOfType(element, XIfExpression.class);
            if (ifExpression == null) {
                return;
            }
            ICompositeNode node = NodeModelUtils.findActualNodeFor(ifExpression);
            if (node == null) {
                return;
            }
            int[] offsetAndLength = getOffsetAndLength(ifExpression, node);
            context.getXtextDocument().replace(offsetAndLength[0], offsetAndLength[1], "");
        }
    });
    acceptor.accept(issue, "Move if block up", "Move if block up", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XIfExpression ifExpression = EcoreUtil2.getContainerOfType(element, XIfExpression.class);
            if (ifExpression == null) {
                return;
            }
            ICompositeNode node = NodeModelUtils.findActualNodeFor(ifExpression);
            if (node == null) {
                return;
            }
            XIfExpression firstIfExpression = getFirstIfExpression(ifExpression);
            if (firstIfExpression == null) {
                return;
            }
            XInstanceOfExpression actualIfPart = (XInstanceOfExpression) ifExpression.getIf();
            XAbstractFeatureCall actualFeatureCall = (XAbstractFeatureCall) actualIfPart.getExpression();
            JvmIdentifiableElement actualFeature = actualFeatureCall.getFeature();
            ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, firstIfExpression);
            LightweightTypeReference actualTypeReference = owner.toLightweightTypeReference(actualIfPart.getType());
            List<XExpression> ifParts = collectIfParts(firstIfExpression, new ArrayList<XExpression>());
            for (XExpression previousIfPart : ifParts) {
                if (actualIfPart == previousIfPart) {
                    return;
                }
                if (!(previousIfPart instanceof XInstanceOfExpression)) {
                    continue;
                }
                XInstanceOfExpression instanceOfExpression = (XInstanceOfExpression) previousIfPart;
                if (!(instanceOfExpression.getExpression() instanceof XAbstractFeatureCall)) {
                    continue;
                }
                XAbstractFeatureCall previousFeatureCall = (XAbstractFeatureCall) instanceOfExpression.getExpression();
                if (previousFeatureCall.getFeature() != actualFeature) {
                    continue;
                }
                LightweightTypeReference previousTypeReference = owner.toLightweightTypeReference(instanceOfExpression.getType());
                if (typesOrderUtil.isHandled(actualTypeReference, previousTypeReference)) {
                    ICompositeNode previousNode = NodeModelUtils.findActualNodeFor(instanceOfExpression.eContainer());
                    if (previousNode == null) {
                        return;
                    }
                    int[] offsetAndLength = getOffsetAndLength(ifExpression, node);
                    int offset = offsetAndLength[0];
                    int length = offsetAndLength[1];
                    int endOffset = offset + length;
                    String text = node.getRootNode().getText().substring(offset, endOffset).trim();
                    if (text.startsWith("else")) {
                        text = text.substring("else".length()).trim();
                    }
                    if (!text.endsWith("else")) {
                        text = text + " else ";
                    } else {
                        text = text + " ";
                    }
                    IXtextDocument document = context.getXtextDocument();
                    document.replace(offset, length, "");
                    document.replace(previousNode.getOffset(), 0, text);
                    return;
                }
            }
        }

        private XIfExpression getFirstIfExpression(XIfExpression ifExpression) {
            EObject container = ifExpression.eContainer();
            if (container instanceof XIfExpression) {
                XIfExpression parentIfExpression = (XIfExpression) container;
                if (parentIfExpression.getElse() == ifExpression) {
                    return getFirstIfExpression(parentIfExpression);
                }
            }
            return ifExpression;
        }

        private List<XExpression> collectIfParts(XIfExpression expression, List<XExpression> result) {
            result.add(expression.getIf());
            if (expression.getElse() instanceof XIfExpression) {
                collectIfParts((XIfExpression) expression.getElse(), result);
            }
            return result;
        }
    });
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) ArrayList(java.util.ArrayList) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) XInstanceOfExpression(org.eclipse.xtext.xbase.XInstanceOfExpression) XIfExpression(org.eclipse.xtext.xbase.XIfExpression) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XExpression(org.eclipse.xtext.xbase.XExpression) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 9 with ITypeReferenceOwner

use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method fixUnreachableCatchBlock.

@Fix(IssueCodes.UNREACHABLE_CATCH_BLOCK)
public void fixUnreachableCatchBlock(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Remove catch block", "Remove catch block", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            remove(element, XCatchClause.class, context);
        }
    });
    acceptor.accept(issue, "Move catch block up", "Move catch block up", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XCatchClause catchClause = EcoreUtil2.getContainerOfType(element, XCatchClause.class);
            if (catchClause == null) {
                return;
            }
            ICompositeNode node = NodeModelUtils.findActualNodeFor(catchClause);
            if (node == null) {
                return;
            }
            XTryCatchFinallyExpression tryCatchFinallyExpression = EcoreUtil2.getContainerOfType(catchClause, XTryCatchFinallyExpression.class);
            if (tryCatchFinallyExpression == null) {
                return;
            }
            ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, tryCatchFinallyExpression);
            LightweightTypeReference actualTypeReference = owner.toLightweightTypeReference(catchClause.getDeclaredParam().getParameterType());
            for (XCatchClause previousCatchClause : tryCatchFinallyExpression.getCatchClauses()) {
                if (previousCatchClause == catchClause) {
                    return;
                }
                LightweightTypeReference previousTypeReference = owner.toLightweightTypeReference(previousCatchClause.getDeclaredParam().getParameterType());
                if (typesOrderUtil.isHandled(actualTypeReference, previousTypeReference)) {
                    ICompositeNode previousNode = NodeModelUtils.findActualNodeFor(previousCatchClause);
                    if (previousNode == null) {
                        return;
                    }
                    moveUp(node, previousNode, context);
                    return;
                }
            }
        }
    });
}
Also used : XCatchClause(org.eclipse.xtext.xbase.XCatchClause) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XTryCatchFinallyExpression(org.eclipse.xtext.xbase.XTryCatchFinallyExpression) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 10 with ITypeReferenceOwner

use of org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner in project xtext-xtend by eclipse.

the class CreateMemberQuickfixes method getReceiverType.

/* @Nullable */
protected LightweightTypeReference getReceiverType(XAbstractFeatureCall featureCall) {
    XExpression actualReceiver = featureCall.getActualReceiver();
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, featureCall);
    if (actualReceiver == null) {
        JvmDeclaredType callersType = getCallersType(featureCall);
        if (callersType != null)
            return owner.newParameterizedTypeReference(callersType);
    } else if (actualReceiver instanceof XAbstractFeatureCall && ((XAbstractFeatureCall) actualReceiver).isTypeLiteral()) {
        JvmType type = (JvmType) ((XAbstractFeatureCall) actualReceiver).getFeature();
        ParameterizedTypeReference reference = owner.newParameterizedTypeReference(type);
        return reference;
    } else {
        LightweightTypeReference typeRef = typeResolver.resolveTypes(featureCall).getActualType(actualReceiver);
        if (typeRef != null && typeRef.getType() instanceof JvmDeclaredType)
            return typeRef;
    }
    return null;
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) ParameterizedTypeReference(org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XExpression(org.eclipse.xtext.xbase.XExpression) JvmType(org.eclipse.xtext.common.types.JvmType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall)

Aggregations

ITypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner)16 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)11 StandardTypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)11 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)6 EObject (org.eclipse.emf.ecore.EObject)4 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)4 List (java.util.List)3 EList (org.eclipse.emf.common.util.EList)3 WrappedException (org.eclipse.emf.common.util.WrappedException)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)3 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)3 JvmType (org.eclipse.xtext.common.types.JvmType)3 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)3 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)3 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)3 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)3 ParameterizedTypeReference (org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference)3 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)2 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)2