Search in sources :

Example 1 with ContextualVisibilityHelper

use of org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper in project xtext-xtend by eclipse.

the class XtendValidator method doCheckOverriddenMethods.

protected void doCheckOverriddenMethods(XtendTypeDeclaration xtendType, JvmGenericType inferredType, ResolvedFeatures resolvedFeatures, Set<EObject> flaggedOperations) {
    List<IResolvedOperation> operationsMissingImplementation = null;
    boolean doCheckAbstract = !inferredType.isAbstract();
    if (doCheckAbstract) {
        operationsMissingImplementation = Lists.newArrayList();
    }
    IVisibilityHelper visibilityHelper = new ContextualVisibilityHelper(this.visibilityHelper, resolvedFeatures.getType());
    boolean flaggedType = false;
    for (IResolvedOperation operation : resolvedFeatures.getAllOperations()) {
        JvmDeclaredType operationDeclaringType = operation.getDeclaration().getDeclaringType();
        if (operationDeclaringType != inferredType) {
            if (operationsMissingImplementation != null && operation.getDeclaration().isAbstract()) {
                operationsMissingImplementation.add(operation);
            }
            if (visibilityHelper.isVisible(operation.getDeclaration())) {
                String erasureSignature = operation.getResolvedErasureSignature();
                List<IResolvedOperation> declaredOperationsWithSameErasure = resolvedFeatures.getDeclaredOperations(erasureSignature);
                for (IResolvedOperation localOperation : declaredOperationsWithSameErasure) {
                    if (!localOperation.isOverridingOrImplementing(operation.getDeclaration()).isOverridingOrImplementing()) {
                        EObject source = findPrimarySourceElement(localOperation);
                        if (flaggedOperations.add(source)) {
                            if (operation.getDeclaration().isStatic() && !localOperation.getDeclaration().isStatic()) {
                                error("The instance method " + localOperation.getSimpleSignature() + " cannot override the static method " + operation.getSimpleSignature() + " of type " + getDeclaratorName(operation.getDeclaration()) + ".", source, nameFeature(source), DUPLICATE_METHOD);
                            } else {
                                error("Name clash: The method " + localOperation.getSimpleSignature() + " of type " + inferredType.getSimpleName() + " has the same erasure as " + // due to name transformations in JVM model inference
                                operation.getSimpleSignature() + " of type " + getDeclaratorName(operation.getDeclaration()) + " but does not override it.", source, nameFeature(source), DUPLICATE_METHOD);
                            }
                        }
                    }
                }
                if (operation instanceof ConflictingDefaultOperation && contributesToConflict(inferredType, (ConflictingDefaultOperation) operation) && !flaggedType) {
                    IResolvedOperation conflictingOperation = ((ConflictingDefaultOperation) operation).getConflictingOperations().get(0);
                    // Include the declaring class in the issue code in order to give better quick fixes
                    String[] uris = new String[] { getDeclaratorName(operation.getDeclaration()) + "|" + EcoreUtil.getURI(operation.getDeclaration()).toString(), getDeclaratorName(conflictingOperation.getDeclaration()) + "|" + EcoreUtil.getURI(conflictingOperation.getDeclaration()).toString() };
                    if (!operation.getDeclaration().isAbstract() && !conflictingOperation.getDeclaration().isAbstract()) {
                        error("The type " + inferredType.getSimpleName() + " inherits multiple implementations of the method " + conflictingOperation.getSimpleSignature() + " from " + getDeclaratorName(conflictingOperation.getDeclaration()) + " and " + getDeclaratorName(operation.getDeclaration()) + ".", xtendType, XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME, CONFLICTING_DEFAULT_METHODS, uris);
                    } else {
                        // At least one of the operations is non-abstract
                        IResolvedOperation abstractOp, nonabstractOp;
                        if (operation.getDeclaration().isAbstract()) {
                            abstractOp = operation;
                            nonabstractOp = conflictingOperation;
                        } else {
                            abstractOp = conflictingOperation;
                            nonabstractOp = operation;
                        }
                        error("The non-abstract method " + nonabstractOp.getSimpleSignature() + " inherited from " + getDeclaratorName(nonabstractOp.getDeclaration()) + " conflicts with the method " + abstractOp.getSimpleSignature() + " inherited from " + getDeclaratorName(abstractOp.getDeclaration()) + ".", xtendType, XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME, CONFLICTING_DEFAULT_METHODS, uris);
                    }
                    flaggedType = true;
                }
            }
        }
    }
    if (operationsMissingImplementation != null && !operationsMissingImplementation.isEmpty() && !flaggedType) {
        reportMissingImplementations(xtendType, inferredType, operationsMissingImplementation);
    }
}
Also used : ConflictingDefaultOperation(org.eclipse.xtext.xbase.typesystem.override.ConflictingDefaultOperation) ContextualVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper) EObject(org.eclipse.emf.ecore.EObject) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.IVisibilityHelper) RichString(org.eclipse.xtend.core.xtend.RichString) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation)

Example 2 with ContextualVisibilityHelper

use of org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper 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 3 with ContextualVisibilityHelper

use of org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper in project xtext-xtend by eclipse.

the class DispatchHelper method getAllDispatchCases.

/**
 * Return all the cases that are associated with the given dispatch operation.
 */
public List<JvmOperation> getAllDispatchCases(JvmOperation dispatcherOperation) {
    DispatchSignature dispatchSignature = new DispatchSignature(dispatcherOperation.getSimpleName(), dispatcherOperation.getParameters().size());
    JvmDeclaredType type = dispatcherOperation.getDeclaringType();
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, type);
    ContextualVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(visibilityHelper, owner.newParameterizedTypeReference(type));
    return getAllDispatchMethods(dispatchSignature, type, contextualVisibilityHelper);
}
Also used : ContextualVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Aggregations

ContextualVisibilityHelper (org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper)3 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)2 ITypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner)2 StandardTypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)2 EObject (org.eclipse.emf.ecore.EObject)1 RichString (org.eclipse.xtend.core.xtend.RichString)1 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)1 ConflictingDefaultOperation (org.eclipse.xtext.xbase.typesystem.override.ConflictingDefaultOperation)1 IResolvedOperation (org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation)1 IVisibilityHelper (org.eclipse.xtext.xbase.typesystem.util.IVisibilityHelper)1