Search in sources :

Example 36 with JvmTypeParameter

use of org.eclipse.xtext.common.types.JvmTypeParameter 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 37 with JvmTypeParameter

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

the class SuperMemberImplementorTest method getContextType.

protected LightweightTypeReference getContextType() {
    JvmGenericType inferredType = associations.getInferredType(xtendClass);
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, inferredType);
    ParameterizedTypeReference contextType = owner.newParameterizedTypeReference(inferredType);
    for (JvmTypeParameter typeParamter : inferredType.getTypeParameters()) {
        contextType.addTypeArgument(owner.newParameterizedTypeReference(typeParamter));
    }
    return contextType;
}
Also used : ParameterizedTypeReference(org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 38 with JvmTypeParameter

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

the class MethodBuilderTest method testJavaTypeParameter.

@Test
public void testJavaTypeParameter() {
    AbstractMethodBuilder _createMethodBuilder = this._codeBuilderFactory.createMethodBuilder(this.getJavaClass());
    final Procedure1<AbstractMethodBuilder> _function = (AbstractMethodBuilder it) -> {
        it.setContext(this.getJavaClass());
        it.setMethodName("foo");
        JvmTypeParameter _createJvmTypeParameter = this._typesFactory.createJvmTypeParameter();
        final Procedure1<JvmTypeParameter> _function_1 = (JvmTypeParameter it_1) -> {
            it_1.setName("T");
        };
        JvmTypeParameter _doubleArrow = ObjectExtensions.<JvmTypeParameter>operator_doubleArrow(_createJvmTypeParameter, _function_1);
        JvmTypeParameter _createJvmTypeParameter_1 = this._typesFactory.createJvmTypeParameter();
        final Procedure1<JvmTypeParameter> _function_2 = (JvmTypeParameter it_1) -> {
            it_1.setName("U");
        };
        JvmTypeParameter _doubleArrow_1 = ObjectExtensions.<JvmTypeParameter>operator_doubleArrow(_createJvmTypeParameter_1, _function_2);
        it.setTypeParameters(Collections.<JvmTypeParameter>unmodifiableList(CollectionLiterals.<JvmTypeParameter>newArrayList(_doubleArrow, _doubleArrow_1)));
    };
    AbstractMethodBuilder _doubleArrow = ObjectExtensions.<AbstractMethodBuilder>operator_doubleArrow(_createMethodBuilder, _function);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<T,U> void foo() {");
    _builder.newLine();
    _builder.append("  ");
    _builder.append(AbstractBuilderTest.DEFAULT_BODY, "  ");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    this.assertBuilds(_doubleArrow, _builder.toString());
}
Also used : JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractMethodBuilder(org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder) Test(org.junit.Test) AbstractBuilderTest(org.eclipse.xtend.ide.tests.codebuilder.AbstractBuilderTest)

Example 39 with JvmTypeParameter

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

the class ActualTypeArgumentCollectorTest method assertOrigins.

public Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> assertOrigins(final Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> mapping, final String typeParamName, final int count) {
    final Set<JvmTypeParameter> allKeys = mapping.keySet();
    for (final JvmTypeParameter key : allKeys) {
        String _simpleName = key.getSimpleName();
        boolean _equals = Objects.equal(_simpleName, typeParamName);
        if (_equals) {
            final List<LightweightBoundTypeArgument> mappingData = mapping.get(key);
            final Function1<LightweightBoundTypeArgument, Object> _function = (LightweightBoundTypeArgument it) -> {
                return it.getOrigin();
            };
            Assert.assertEquals(count, IterableExtensions.<Object>toSet(ListExtensions.<LightweightBoundTypeArgument, Object>map(mappingData, _function)).size());
            return mapping;
        }
    }
    return mapping;
}
Also used : JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) LightweightBoundTypeArgument(org.eclipse.xtext.xbase.typesystem.references.LightweightBoundTypeArgument)

Example 40 with JvmTypeParameter

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

the class DeferredTypeParameterHintCollectorTest method hasNoHintsFor.

public void hasNoHintsFor(final Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> mapping, final String typeParamName) {
    final Set<JvmTypeParameter> allKeys = mapping.keySet();
    for (final JvmTypeParameter key : allKeys) {
        String _simpleName = key.getSimpleName();
        boolean _equals = Objects.equal(_simpleName, typeParamName);
        if (_equals) {
            LightweightTypeReference _typeReference = mapping.get(key).getTypeReference();
            final UnboundTypeReference unbound = ((UnboundTypeReference) _typeReference);
            boolean _isEmpty = unbound.getAllHints().isEmpty();
            boolean _not = (!_isEmpty);
            if (_not) {
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("Unexpected mapping for ");
                _builder.append(typeParamName);
                _builder.append(" in ");
                final Function1<JvmTypeParameter, String> _function = (JvmTypeParameter it) -> {
                    return it.getSimpleName();
                };
                Iterable<String> _map = IterableExtensions.<JvmTypeParameter, String>map(mapping.keySet(), _function);
                _builder.append(_map);
                Assert.fail(_builder.toString());
            }
        }
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) UnboundTypeReference(org.eclipse.xtext.xbase.typesystem.references.UnboundTypeReference)

Aggregations

JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)57 Test (org.junit.Test)20 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)16 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)14 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)13 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)12 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)12 JvmUpperBound (org.eclipse.xtext.common.types.JvmUpperBound)10 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)8 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)7 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)7 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)6 List (java.util.List)5 EList (org.eclipse.emf.common.util.EList)5 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)5 EObject (org.eclipse.emf.ecore.EObject)4 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)4 ArrayList (java.util.ArrayList)3 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)3 AbstractMethodBuilder (org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder)3