Search in sources :

Example 6 with ParameterInfo

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

the class ChangeSignatureProcessor method createComment.

private JDTRefactoringDescriptorComment createComment(String project, final String header) throws JavaModelException {
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    if (!fMethod.getElementName().equals(fMethodName))
        comment.addSetting(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_new_name_pattern, BasicElementLabels.getJavaElementName(fMethodName)));
    if (!isVisibilitySameAsInitial()) {
        String visibility = JdtFlags.getVisibilityString(fVisibility);
        if (//$NON-NLS-1$
        "".equals(visibility))
            visibility = RefactoringCoreMessages.ChangeSignatureRefactoring_default_visibility;
        comment.addSetting(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_new_visibility_pattern, visibility));
    }
    if (fReturnTypeInfo.isTypeNameChanged())
        comment.addSetting(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_new_return_type_pattern, BasicElementLabels.getJavaElementName(fReturnTypeInfo.getNewTypeName())));
    List<String> deleted = new ArrayList<String>();
    List<String> added = new ArrayList<String>();
    List<String> changed = new ArrayList<String>();
    for (final Iterator<ParameterInfo> iterator = fParameterInfos.iterator(); iterator.hasNext(); ) {
        final ParameterInfo info = iterator.next();
        if (info.isDeleted())
            deleted.add(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_deleted_parameter_pattern, new String[] { BasicElementLabels.getJavaElementName(info.getOldTypeName()), BasicElementLabels.getJavaElementName(info.getOldName()) }));
        else if (info.isAdded())
            added.add(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_added_parameter_pattern, new String[] { BasicElementLabels.getJavaElementName(info.getNewTypeName()), BasicElementLabels.getJavaElementName(info.getNewName()) }));
        else if (info.isRenamed() || info.isTypeNameChanged() || info.isVarargChanged())
            changed.add(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_changed_parameter_pattern, new String[] { BasicElementLabels.getJavaElementName(info.getOldTypeName()), BasicElementLabels.getJavaElementName(info.getOldName()) }));
    }
    if (!added.isEmpty())
        comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ChangeSignatureRefactoring_added_parameters, added.toArray(new String[added.size()])));
    if (!deleted.isEmpty())
        comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ChangeSignatureRefactoring_removed_parameters, deleted.toArray(new String[deleted.size()])));
    if (!changed.isEmpty())
        comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ChangeSignatureRefactoring_changed_parameters, changed.toArray(new String[changed.size()])));
    added.clear();
    deleted.clear();
    changed.clear();
    for (final Iterator<ExceptionInfo> iterator = fExceptionInfos.iterator(); iterator.hasNext(); ) {
        final ExceptionInfo info = iterator.next();
        if (info.isAdded())
            added.add(info.getElement().getElementName());
        else if (info.isDeleted())
            deleted.add(info.getElement().getElementName());
    }
    if (!added.isEmpty())
        comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ChangeSignatureRefactoring_added_exceptions, added.toArray(new String[added.size()])));
    if (!deleted.isEmpty())
        comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ChangeSignatureRefactoring_removed_exceptions, deleted.toArray(new String[deleted.size()])));
    return comment;
}
Also used : ArrayList(java.util.ArrayList) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) ExceptionInfo(org.eclipse.jdt.internal.corext.refactoring.ExceptionInfo) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)

Example 7 with ParameterInfo

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

the class ExtractMethodRefactoring method checkParameterNames.

/**
	 * Checks if the parameter names are valid.
	 * @return validation status
	 */
public RefactoringStatus checkParameterNames() {
    RefactoringStatus result = new RefactoringStatus();
    for (Iterator<ParameterInfo> iter = fParameterInfos.iterator(); iter.hasNext(); ) {
        ParameterInfo parameter = iter.next();
        result.merge(Checks.checkIdentifier(parameter.getNewName(), fCUnit));
        for (Iterator<ParameterInfo> others = fParameterInfos.iterator(); others.hasNext(); ) {
            ParameterInfo other = others.next();
            if (parameter != other && other.getNewName().equals(parameter.getNewName())) {
                result.addError(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_error_sameParameter, BasicElementLabels.getJavaElementName(other.getNewName())));
                return result;
            }
        }
        if (parameter.isRenamed() && fUsedNames.contains(parameter.getNewName())) {
            result.addError(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_error_nameInUse, BasicElementLabels.getJavaElementName(parameter.getNewName())));
            return result;
        }
    }
    return result;
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Example 8 with ParameterInfo

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

the class ExtractMethodRefactoring method createCallNodes.

//---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
    List<ASTNode> result = new ArrayList<ASTNode>(2);
    IVariableBinding[] locals = fAnalyzer.getCallerLocals();
    for (int i = 0; i < locals.length; i++) {
        result.add(createDeclaration(locals[i], null));
    }
    MethodInvocation invocation = fAST.newMethodInvocation();
    invocation.setName(fAST.newSimpleName(fMethodName));
    ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration());
    RefactoringStatus status = new RefactoringStatus();
    while (fDestination != typeNode) {
        fAnalyzer.checkInput(status, fMethodName, typeNode);
        if (!status.isOK()) {
            SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName());
            if ((modifiers & Modifier.STATIC) == 0) {
                ThisExpression thisExpression = fAST.newThisExpression();
                thisExpression.setQualifier(destinationTypeName);
                invocation.setExpression(thisExpression);
            } else {
                invocation.setExpression(destinationTypeName);
            }
            break;
        }
        typeNode = typeNode.getParent();
    }
    List<Expression> arguments = invocation.arguments();
    for (int i = 0; i < fParameterInfos.size(); i++) {
        ParameterInfo parameter = fParameterInfos.get(i);
        arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter)));
    }
    if (fLinkedProposalModel != null) {
        LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
        nameGroup.addPosition(fRewriter.track(invocation.getName()), false);
    }
    ASTNode call;
    int returnKind = fAnalyzer.getReturnKind();
    switch(returnKind) {
        case ExtractMethodAnalyzer.ACCESS_TO_LOCAL:
            IVariableBinding binding = fAnalyzer.getReturnLocal();
            if (binding != null) {
                VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation);
                call = decl;
            } else {
                Assignment assignment = fAST.newAssignment();
                assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName()));
                assignment.setRightHandSide(invocation);
                call = assignment;
            }
            break;
        case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE:
            ReturnStatement rs = fAST.newReturnStatement();
            rs.setExpression(invocation);
            call = rs;
            break;
        default:
            call = invocation;
    }
    if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) {
        call = fAST.newExpressionStatement((Expression) call);
    }
    result.add(call);
    // return;
    if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) {
        result.add(fAST.newReturnStatement());
    }
    return result.toArray(new ASTNode[result.size()]);
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Assignment(org.eclipse.jdt.core.dom.Assignment) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 9 with ParameterInfo

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

the class ExtractMethodRefactoring method appendMethodSignature.

protected void appendMethodSignature(ISourceAppender appendable) {
    if (visibility != JvmVisibility.PUBLIC)
        appendable.append(getVisibility().getName().toLowerCase()).append(" ");
    appendable.append("def ");
    if (isStatic)
        appendable.append("static ");
    if (!neededTypeParameters.isEmpty()) {
        JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
        if (operation != null) {
            appendable.append("<");
            boolean isFirst = true;
            for (JvmTypeParameter typeParameter : operation.getTypeParameters()) {
                if (neededTypeParameters.contains(typeParameter)) {
                    if (!isFirst)
                        appendable.append(", ");
                    isFirst = false;
                    appendable.append(typeParameter);
                }
            }
            appendable.append("> ");
        }
    }
    if (isExplicitlyDeclareReturnType) {
        appendable.append(returnType).append(" ");
    }
    appendable.append(methodName).append("(");
    boolean isFirst = true;
    for (ParameterInfo parameterInfo : getParameterInfos()) {
        if (!isFirst)
            appendable.append(", ");
        isFirst = false;
        appendable.append(parameter2type.get(parameterInfo)).append(" ").append(parameterInfo.getNewName());
    }
    appendable.append(")");
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Example 10 with ParameterInfo

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

the class ExtractMethodRefactoring method validateParameters.

public RefactoringStatus validateParameters() {
    RefactoringStatus status = new RefactoringStatus();
    Set<String> namesSoFar = newHashSet();
    for (ParameterInfo parameterInfo : parameterInfos) {
        String newName = parameterInfo.getNewName();
        if (namesSoFar.contains(newName))
            status.addError("Duplicate parameter name '" + newName + "'");
        if (!equal(newName, parameterInfo.getOldName()) && localFeatureNames.contains(newName))
            status.addError("'" + newName + "' is already used as a name in the selected code");
        nameUtil.checkNewFeatureName(newName, false, status);
        namesSoFar.add(newName);
    }
    // TODO: vararg order
    return status;
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) RichString(org.eclipse.xtend.core.xtend.RichString) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

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