Search in sources :

Example 11 with XtendFunction

use of org.eclipse.xtend.core.xtend.XtendFunction in project xtext-xtend by eclipse.

the class XtendValidator method doCheckFunctionOverrides.

protected void doCheckFunctionOverrides(EObject sourceElement, IResolvedOperation resolved, List<IResolvedOperation> allInherited) {
    boolean overrideProblems = false;
    List<IResolvedOperation> exceptionMismatch = null;
    for (IResolvedOperation inherited : allInherited) {
        if (inherited.getOverrideCheckResult().hasProblems()) {
            overrideProblems = true;
            EnumSet<OverrideCheckDetails> details = inherited.getOverrideCheckResult().getDetails();
            if (details.contains(OverrideCheckDetails.IS_FINAL)) {
                error("Attempt to override final method " + inherited.getSimpleSignature(), sourceElement, nameFeature(sourceElement), OVERRIDDEN_FINAL);
            } else if (details.contains(OverrideCheckDetails.REDUCED_VISIBILITY)) {
                error("Cannot reduce the visibility of the overridden method " + inherited.getSimpleSignature(), sourceElement, nameFeature(sourceElement), OVERRIDE_REDUCES_VISIBILITY);
            } else if (details.contains(OverrideCheckDetails.EXCEPTION_MISMATCH)) {
                if (exceptionMismatch == null)
                    exceptionMismatch = Lists.newArrayListWithCapacity(allInherited.size());
                exceptionMismatch.add(inherited);
            } else if (details.contains(OverrideCheckDetails.RETURN_MISMATCH)) {
                error("The return type is incompatible with " + inherited.getSimpleSignature(), sourceElement, returnTypeFeature(sourceElement), INCOMPATIBLE_RETURN_TYPE);
            } else if (details.contains(OverrideCheckDetails.SYNCHRONIZED_MISMATCH)) {
                warning("The overridden method is synchronized, the current one is not synchronized.", sourceElement, nameFeature(sourceElement), MISSING_SYNCHRONIZED);
            }
        }
    }
    if (exceptionMismatch != null) {
        createExceptionMismatchError(resolved, sourceElement, exceptionMismatch);
    }
    if (sourceElement instanceof XtendFunction) {
        XtendFunction function = (XtendFunction) sourceElement;
        if (!overrideProblems && !function.isOverride() && !function.isStatic()) {
            error("The method " + resolved.getSimpleSignature() + " of type " + getDeclaratorName(resolved) + " must use override keyword since it actually overrides a supertype method.", function, XTEND_FUNCTION__NAME, MISSING_OVERRIDE);
        }
        if (!overrideProblems && function.isOverride() && function.isStatic()) {
            for (IResolvedOperation inherited : allInherited) {
                error("The method " + resolved.getSimpleSignature() + " of type " + getDeclaratorName(resolved) + " shadows the method " + resolved.getSimpleSignature() + " of type " + getDeclaratorName(inherited) + ", but does not override it.", function, XTEND_FUNCTION__NAME, function.getModifiers().indexOf("override"), OBSOLETE_OVERRIDE);
            }
        }
        if (function.isOverride()) {
            for (XAnnotation anno : function.getAnnotations()) {
                if (anno != null && anno.getAnnotationType() != null && Override.class.getName().equals(anno.getAnnotationType().getIdentifier())) {
                    warning("Superfluous @Override annotation", anno, null, OBSOLETE_ANNOTATION_OVERRIDE);
                }
            }
        }
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation) OverrideCheckDetails(org.eclipse.xtext.xbase.typesystem.override.IOverrideCheckResult.OverrideCheckDetails)

Example 12 with XtendFunction

use of org.eclipse.xtend.core.xtend.XtendFunction in project xtext-xtend by eclipse.

the class XtendValidator method addDispatchError.

protected void addDispatchError(JvmOperation jvmOperation, String message, String modifier, String ISSUE_ID) {
    XtendFunction function = associations.getXtendFunction(jvmOperation);
    if (function != null) {
        int modifierIndex = -1;
        if (modifier != null) {
            modifierIndex = function.getModifiers().indexOf(modifier);
        } else {
            for (int i = 0; i < function.getModifiers().size(); ++i) {
                if (visibilityModifers.contains(function.getModifiers().get(i))) {
                    modifierIndex = i;
                    break;
                }
            }
        }
        if (modifierIndex == -1)
            modifierIndex = function.getModifiers().indexOf("dispatch");
        error(message, function, XTEND_MEMBER__MODIFIERS, modifierIndex, ISSUE_ID);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction)

Example 13 with XtendFunction

use of org.eclipse.xtend.core.xtend.XtendFunction in project xtext-xtend by eclipse.

the class ProblemSupportImpl method getSignificantFeature.

public EStructuralFeature getSignificantFeature(final EObject obj) {
    EStructuralFeature _switchResult = null;
    boolean _matched = false;
    if (obj instanceof XtendTypeDeclaration) {
        _matched = true;
        _switchResult = XtendPackage.eINSTANCE.getXtendTypeDeclaration_Name();
    }
    if (!_matched) {
        if (obj instanceof XtendField) {
            _matched = true;
            _switchResult = XtendPackage.eINSTANCE.getXtendField_Name();
        }
    }
    if (!_matched) {
        if (obj instanceof XtendFunction) {
            _matched = true;
            _switchResult = XtendPackage.eINSTANCE.getXtendFunction_Name();
        }
    }
    if (!_matched) {
        if (obj instanceof JvmFormalParameter) {
            _matched = true;
            _switchResult = TypesPackage.eINSTANCE.getJvmFormalParameter_Name();
        }
    }
    if (!_matched) {
        if (obj instanceof XAnnotation) {
            _matched = true;
            _switchResult = XAnnotationsPackage.eINSTANCE.getXAnnotation_AnnotationType();
        }
    }
    return _switchResult;
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) XtendField(org.eclipse.xtend.core.xtend.XtendField)

Example 14 with XtendFunction

use of org.eclipse.xtend.core.xtend.XtendFunction in project xtext-xtend by eclipse.

the class ConstantExpressionsInterpreterTest method evaluatesWithException.

protected void evaluatesWithException(final String expression, final Procedure1<? super ConstantExpressionEvaluationException> exceptionAssertions) {
    try {
        final XtendFunction function = this.function((("def void testFoo() { " + expression) + " }"));
        XExpression _expression = function.getExpression();
        final XExpression expr = IterableExtensions.<XExpression>head(((XBlockExpression) _expression).getExpressions());
        try {
            this.interpreter.evaluate(expr, null);
            Assert.fail("exception expected");
        } catch (final Throwable _t) {
            if (_t instanceof ConstantExpressionEvaluationException) {
                final ConstantExpressionEvaluationException e = (ConstantExpressionEvaluationException) _t;
                exceptionAssertions.apply(e);
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) ConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 15 with XtendFunction

use of org.eclipse.xtend.core.xtend.XtendFunction in project xtext-xtend by eclipse.

the class ConstantExpressionsInterpreterTest method evaluatesTo.

protected void evaluatesTo(final Pair<String, String> typeAndExpression, final Procedure1<? super Object> assertions) {
    try {
        final String type = typeAndExpression.getKey();
        final String expression = typeAndExpression.getValue();
        String _elvis = null;
        if (type != null) {
            _elvis = type;
        } else {
            _elvis = "void";
        }
        String _plus = ("def " + _elvis);
        String _plus_1 = (_plus + " testFoo() { ");
        String _plus_2 = (_plus_1 + expression);
        String _plus_3 = (_plus_2 + " }");
        final XtendFunction function = this.function(_plus_3);
        XExpression _expression = function.getExpression();
        final XExpression expr = IterableExtensions.<XExpression>head(((XBlockExpression) _expression).getExpressions());
        JvmTypeReference _xifexpression = null;
        if ((type != null)) {
            _xifexpression = function.getReturnType();
        }
        final Object value = this.interpreter.evaluate(expr, _xifexpression);
        assertions.apply(value);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XExpression(org.eclipse.xtext.xbase.XExpression)

Aggregations

XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)441 Test (org.junit.Test)374 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)248 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)198 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)182 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)116 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)101 XExpression (org.eclipse.xtext.xbase.XExpression)95 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)91 XMemberFeatureCall (org.eclipse.xtext.xbase.XMemberFeatureCall)78 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)72 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)60 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)46 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)39 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)30 EObject (org.eclipse.emf.ecore.EObject)28 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)23 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)23 RichString (org.eclipse.xtend.core.xtend.RichString)17 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)16