Search in sources :

Example 26 with ParameterInfo

use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project che by eclipse.

the class ChangeSignatureProcessor method getNamesOfNotDeletedParameters.

private Set<String> getNamesOfNotDeletedParameters() {
    Set<String> result = new HashSet<String>();
    for (Iterator<ParameterInfo> iter = getNotDeletedInfos().iterator(); iter.hasNext(); ) {
        ParameterInfo info = iter.next();
        result.add(info.getNewName());
    }
    return result;
}
Also used : ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) HashSet(java.util.HashSet)

Example 27 with ParameterInfo

use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project che by eclipse.

the class ChangeSignatureProcessor method checkTypeVariables.

private RefactoringStatus checkTypeVariables() {
    if (fRippleMethods.length == 1)
        return null;
    RefactoringStatus result = new RefactoringStatus();
    if (fReturnTypeInfo.isTypeNameChanged() && fReturnTypeInfo.getNewTypeBinding() != null) {
        HashSet<ITypeBinding> typeVariablesCollector = new HashSet<ITypeBinding>();
        collectTypeVariables(fReturnTypeInfo.getNewTypeBinding(), typeVariablesCollector);
        if (typeVariablesCollector.size() != 0) {
            ITypeBinding first = typeVariablesCollector.iterator().next();
            String msg = Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_return_type_contains_type_variable, new String[] { BasicElementLabels.getJavaElementName(fReturnTypeInfo.getNewTypeName()), BasicElementLabels.getJavaElementName(first.getName()) });
            result.addError(msg);
        }
    }
    for (Iterator<ParameterInfo> iter = getNotDeletedInfos().iterator(); iter.hasNext(); ) {
        ParameterInfo info = iter.next();
        if (info.isTypeNameChanged() && info.getNewTypeBinding() != null) {
            HashSet<ITypeBinding> typeVariablesCollector = new HashSet<ITypeBinding>();
            collectTypeVariables(info.getNewTypeBinding(), typeVariablesCollector);
            if (typeVariablesCollector.size() != 0) {
                ITypeBinding first = typeVariablesCollector.iterator().next();
                String msg = Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_parameter_type_contains_type_variable, new String[] { BasicElementLabels.getJavaElementName(info.getNewTypeName()), BasicElementLabels.getJavaElementName(info.getNewName()), BasicElementLabels.getJavaElementName(first.getName()) });
                result.addError(msg);
            }
        }
    }
    return result;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) HashSet(java.util.HashSet)

Example 28 with ParameterInfo

use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project xtext-xtend by eclipse.

the class ExtractMethodRefactoring method getMethodBodyWithRenamedParameters.

protected String getMethodBodyWithRenamedParameters(ITextRegion expressionsRegion) throws BadLocationException {
    String expressionsAsString = document.get(expressionsRegion.getOffset(), expressionsRegion.getLength());
    List<ReplaceRegion> parameterRenames = newArrayList();
    for (final String parameterName : externalFeatureCalls.keySet()) {
        ParameterInfo parameter = find(parameterInfos, new Predicate<ParameterInfo>() {

            @Override
            public boolean apply(ParameterInfo info) {
                return equal(info.getOldName(), parameterName);
            }
        });
        if (parameter.isRenamed()) {
            for (XFeatureCall featureCall : externalFeatureCalls.get(parameterName)) {
                ITextRegion textRegion = locationInFileProvider.getSignificantTextRegion(featureCall, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, -1);
                parameterRenames.add(new ReplaceRegion(textRegion, parameter.getNewName()));
            }
        }
    }
    sort(parameterRenames, new Comparator<ReplaceRegion>() {

        @Override
        public int compare(ReplaceRegion o1, ReplaceRegion o2) {
            return o2.getOffset() - o1.getOffset();
        }
    });
    StringBuffer buffer = new StringBuffer(expressionsAsString);
    for (ReplaceRegion parameterRename : parameterRenames) {
        buffer.replace(parameterRename.getOffset() - expressionsRegion.getOffset(), parameterRename.getEndOffset() - expressionsRegion.getOffset(), parameterRename.getText());
    }
    expressionsAsString = buffer.toString();
    return expressionsAsString;
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) ITextRegion(org.eclipse.xtext.util.ITextRegion) RichString(org.eclipse.xtend.core.xtend.RichString) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Example 29 with ParameterInfo

use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project xtext-xtend by eclipse.

the class ExtractMethodRefactoring method createMethodCallEdit.

protected void createMethodCallEdit(DocumentRewriter.Section methodCallSection, ITextRegion expressionRegion) throws BadLocationException {
    if (firstExpression.eContainer() instanceof RichString) {
        methodCallSection.append("�");
    }
    if (isNeedsReturnExpression()) {
        JvmIdentifiableElement returnFeature = ((XFeatureCall) returnExpression).getFeature();
        if (isFinalFeature(returnFeature))
            methodCallSection.append("val ");
        else
            methodCallSection.append("var ");
        methodCallSection.append(returnFeature.getSimpleName()).append(" = ");
    }
    boolean needsSurroundingParentheses = false;
    if (firstExpression.eContainer() instanceof XMemberFeatureCall) {
        if (((XMemberFeatureCall) firstExpression.eContainer()).getMemberCallArguments().size() == 1) {
            String expressionExpanded = document.get(expressionRegion.getOffset() - 1, expressionRegion.getLength() + 2);
            if (!expressionExpanded.startsWith("(") || !expressionExpanded.endsWith(")")) {
                needsSurroundingParentheses = true;
                methodCallSection.append("(");
            }
        }
    }
    methodCallSection.append(methodName).append("(");
    boolean isFirst = true;
    for (ParameterInfo parameterInfo : getParameterInfos()) {
        if (!isFirst)
            methodCallSection.append(", ");
        isFirst = false;
        methodCallSection.append(parameterInfo.getOldName());
    }
    methodCallSection.append(")");
    if (needsSurroundingParentheses)
        methodCallSection.append(")");
    if (lastExpression.eContainer() instanceof RichString) {
        methodCallSection.append("�");
    }
}
Also used : RichString(org.eclipse.xtend.core.xtend.RichString) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) RichString(org.eclipse.xtend.core.xtend.RichString) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Example 30 with ParameterInfo

use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo 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)

Aggregations

ParameterInfo (org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)30 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)6 ArrayList (java.util.ArrayList)5 JavaModelException (org.eclipse.jdt.core.JavaModelException)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)4 ExceptionInfo (org.eclipse.jdt.internal.corext.refactoring.ExceptionInfo)4 RichString (org.eclipse.xtend.core.xtend.RichString)4 HashSet (java.util.HashSet)3 ExtractMethodRefactoring (org.eclipse.xtend.ide.refactoring.ExtractMethodRefactoring)3 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)3 Test (org.junit.Test)3 Expression (org.eclipse.jdt.core.dom.Expression)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)2