Search in sources :

Example 56 with LightweightTypeReference

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

the class CreateMemberQuickfixes method newGetterQuickfixes.

protected void newGetterQuickfixes(String name, XAbstractFeatureCall call, final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
    JvmDeclaredType callersType = getCallersType(call);
    LightweightTypeReference receiverType = getReceiverType(call);
    LightweightTypeReference fieldType = getNewMemberType(call);
    if (callersType != null && receiverType != null) {
        newMethodQuickfixes(receiverType, getAccessorMethodName("get", name), fieldType, Collections.<LightweightTypeReference>emptyList(), call, callersType, issue, issueResolutionAcceptor);
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 57 with LightweightTypeReference

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

the class ExtractMethodRefactoring method calculateReturnType.

protected LightweightTypeReference calculateReturnType(IResolvedTypes resolvedTypes) {
    List<LightweightTypeReference> returnTypes = newArrayList();
    for (XExpression expression : expressions) {
        LightweightTypeReference expressionReturnType = resolvedTypes.getReturnType(expression, expression != lastExpression);
        if (expressionReturnType != null)
            returnTypes.add(expressionReturnType);
    }
    StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(typeComputationServices, xtendClass);
    return typeComputationServices.getTypeConformanceComputer().getCommonSuperType(returnTypes, owner);
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XExpression(org.eclipse.xtext.xbase.XExpression) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 58 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference 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 59 with LightweightTypeReference

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

the class SuperMemberImplementorTest method checkImplementConstructor.

protected void checkImplementConstructor(final String firstParamType, String implementCode) {
    StringBuilderBasedAppendable appendable = new StringBuilderBasedAppendable();
    JvmConstructor constructor = Iterables.find(superClass.getDeclaredConstructors(), new Predicate<JvmConstructor>() {

        @Override
        public boolean apply(JvmConstructor c) {
            if (firstParamType == null)
                return c.getParameters().isEmpty();
            if (c.getParameters().size() >= 1) {
                return firstParamType.equals(c.getParameters().get(0).getParameterType().getSimpleName());
            }
            return false;
        }
    });
    LightweightTypeReference contextType = getContextType();
    ResolvedConstructor resolvedConstructor = new ResolvedConstructor(constructor, contextType);
    implementor.appendConstructorFromSuper(xtendClass, resolvedConstructor, appendable);
    String code = appendable.toString();
    if (!equalsIgnoreWhitespace(implementCode, code))
        assertEquals(implementCode, code);
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable) ResolvedConstructor(org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor)

Example 60 with LightweightTypeReference

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

the class SuperMemberImplementorTest method checkOverrideMethodCode.

protected void checkOverrideMethodCode(String operationName, String overrideCode) {
    StringBuilderBasedAppendable appendable = new StringBuilderBasedAppendable();
    LightweightTypeReference contextType = getContextType();
    IResolvedOperation resolvedOperation = new BottomResolvedOperation((JvmOperation) findExecutable(implementedInterface, operationName), contextType, new OverrideTester());
    implementor.appendOverrideFunction(xtendClass, resolvedOperation, appendable);
    String code = appendable.toString();
    if (!equalsIgnoreWhitespace(overrideCode, code))
        assertEquals(overrideCode, code);
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable) OverrideTester(org.eclipse.xtext.xbase.typesystem.override.OverrideTester) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation) BottomResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.BottomResolvedOperation)

Aggregations

LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)83 Test (org.junit.Test)28 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)23 XExpression (org.eclipse.xtext.xbase.XExpression)22 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)20 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)19 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)16 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)13 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)10 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)10 IFeatureCallArguments (org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArguments)10 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)9 XNumberLiteral (org.eclipse.xtext.xbase.XNumberLiteral)9 JvmType (org.eclipse.xtext.common.types.JvmType)8 ITypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner)8 EObject (org.eclipse.emf.ecore.EObject)7 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)7 StandardTypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)7 RichString (org.eclipse.xtend.core.xtend.RichString)6 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)6