Search in sources :

Example 1 with ParameterInfo

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

the class ChangeSignatureProcessor method getParticipantArguments.

private ChangeMethodSignatureArguments getParticipantArguments() {
    ArrayList<Parameter> parameterList = new ArrayList<Parameter>();
    List<ParameterInfo> pis = getParameterInfos();
    String[] originalParameterTypeSigs = fMethod.getParameterTypes();
    for (Iterator<ParameterInfo> iter = pis.iterator(); iter.hasNext(); ) {
        ParameterInfo pi = iter.next();
        if (!pi.isDeleted()) {
            int oldIndex = pi.isAdded() ? -1 : pi.getOldIndex();
            String newName = pi.getNewName();
            String typeSig;
            if (pi.isTypeNameChanged()) {
                String newType = pi.getNewTypeName();
                if (pi.isNewVarargs()) {
                    //$NON-NLS-1$
                    newType = ParameterInfo.stripEllipsis(newType) + "[]";
                }
                typeSig = Signature.createTypeSignature(newType, false);
            } else {
                typeSig = originalParameterTypeSigs[pi.getOldIndex()];
            }
            String defaultValue = pi.getDefaultValue();
            parameterList.add(new Parameter(oldIndex, newName, typeSig, defaultValue));
        }
    }
    Parameter[] parameters = parameterList.toArray(new Parameter[parameterList.size()]);
    ArrayList<ThrownException> exceptionList = new ArrayList<ThrownException>();
    List<ExceptionInfo> exceptionInfos = getExceptionInfos();
    for (int i = 0; i < exceptionInfos.size(); i++) {
        ExceptionInfo ei = exceptionInfos.get(i);
        if (!ei.isDeleted()) {
            int oldIndex = ei.isAdded() ? -1 : i;
            String qualifiedTypeName = ei.getFullyQualifiedName();
            String newTypeSig = Signature.createTypeSignature(qualifiedTypeName, true);
            exceptionList.add(new ThrownException(oldIndex, newTypeSig));
        }
    }
    ThrownException[] exceptions = exceptionList.toArray(new ThrownException[exceptionList.size()]);
    String returnTypeSig;
    if (fReturnTypeInfo.isTypeNameChanged()) {
        returnTypeSig = Signature.createTypeSignature(fReturnTypeInfo.getNewTypeName(), false);
    } else {
        try {
            returnTypeSig = fMethod.getReturnType();
        } catch (JavaModelException e) {
            returnTypeSig = Signature.createTypeSignature(fReturnTypeInfo.getNewTypeName(), false);
        }
    }
    return new ChangeMethodSignatureArguments(fMethodName, returnTypeSig, fVisibility, parameters, exceptions, fDelegateUpdating);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) ArrayList(java.util.ArrayList) ChangeMethodSignatureArguments(org.eclipse.jdt.core.refactoring.participants.ChangeMethodSignatureArguments) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) Parameter(org.eclipse.jdt.core.refactoring.participants.ChangeMethodSignatureArguments.Parameter) MethodRefParameter(org.eclipse.jdt.core.dom.MethodRefParameter) ExceptionInfo(org.eclipse.jdt.internal.corext.refactoring.ExceptionInfo) ThrownException(org.eclipse.jdt.core.refactoring.participants.ChangeMethodSignatureArguments.ThrownException)

Example 2 with ParameterInfo

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

the class ChangeSignatureProcessor method createParameterInfoList.

private static List<ParameterInfo> createParameterInfoList(IMethod method) {
    try {
        String[] typeNames = method.getParameterTypes();
        String[] oldNames = method.getParameterNames();
        List<ParameterInfo> result = new ArrayList<ParameterInfo>(typeNames.length);
        for (int i = 0; i < oldNames.length; i++) {
            ParameterInfo parameterInfo;
            if (i == oldNames.length - 1 && Flags.isVarargs(method.getFlags())) {
                String varargSignature = typeNames[i];
                int arrayCount = Signature.getArrayCount(varargSignature);
                String baseSignature = Signature.getElementType(varargSignature);
                if (arrayCount > 1)
                    baseSignature = Signature.createArraySignature(baseSignature, arrayCount - 1);
                parameterInfo = new ParameterInfo(Signature.toString(baseSignature) + ParameterInfo.ELLIPSIS, oldNames[i], i);
            } else {
                parameterInfo = new ParameterInfo(Signature.toString(typeNames[i]), oldNames[i], i);
            }
            result.add(parameterInfo);
        }
        return result;
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
        return new ArrayList<ParameterInfo>(0);
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) ArrayList(java.util.ArrayList) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Example 3 with ParameterInfo

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

the class ChangeSignatureProcessor method getOldMethodParameters.

private String getOldMethodParameters() {
    StringBuffer buff = new StringBuffer();
    int i = 0;
    for (Iterator<ParameterInfo> iter = getNotAddedInfos().iterator(); iter.hasNext(); i++) {
        ParameterInfo info = iter.next();
        if (i != 0)
            //$NON-NLS-1$
            buff.append(", ");
        buff.append(createDeclarationString(info));
    }
    return buff.toString();
}
Also used : ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Example 4 with ParameterInfo

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

the class ChangeSignatureProcessor method checkForDuplicateParameterNames.

private void checkForDuplicateParameterNames(RefactoringStatus result) {
    Set<String> found = new HashSet<String>();
    Set<String> doubled = new HashSet<String>();
    for (Iterator<ParameterInfo> iter = getNotDeletedInfos().iterator(); iter.hasNext(); ) {
        ParameterInfo info = iter.next();
        String newName = info.getNewName();
        if (found.contains(newName) && !doubled.contains(newName)) {
            result.addFatalError(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_duplicate_name, BasicElementLabels.getJavaElementName(newName)));
            doubled.add(newName);
        } else {
            found.add(newName);
        }
    }
}
Also used : ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) HashSet(java.util.HashSet)

Example 5 with ParameterInfo

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

the class ChangeSignatureProcessor method createDescriptor.

public JavaRefactoringDescriptor createDescriptor() {
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fMethod.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    ChangeMethodSignatureDescriptor descriptor = null;
    try {
        final String description = Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fMethod.getElementName()));
        final String header = Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_descriptor_description, new String[] { getOldMethodSignature(), getNewMethodSignature() });
        final JDTRefactoringDescriptorComment comment = createComment(project, header);
        descriptor = RefactoringSignatureDescriptorFactory.createChangeMethodSignatureDescriptor(project, description, comment.asString(), arguments, getDescriptorFlags());
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fMethod));
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fMethodName);
        arguments.put(ATTRIBUTE_DELEGATE, Boolean.valueOf(fDelegateUpdating).toString());
        arguments.put(ATTRIBUTE_DEPRECATE, Boolean.valueOf(fDelegateDeprecation).toString());
        if (fReturnTypeInfo.isTypeNameChanged())
            arguments.put(ATTRIBUTE_RETURN, fReturnTypeInfo.getNewTypeName());
        try {
            if (!isVisibilitySameAsInitial())
                arguments.put(ATTRIBUTE_VISIBILITY, new Integer(fVisibility).toString());
        } catch (JavaModelException exception) {
            JavaPlugin.log(exception);
        }
        int count = 1;
        for (final Iterator<ParameterInfo> iterator = fParameterInfos.iterator(); iterator.hasNext(); ) {
            final ParameterInfo info = iterator.next();
            final StringBuffer buffer = new StringBuffer(64);
            if (info.isAdded())
                //$NON-NLS-1$
                buffer.append("{added}");
            else
                buffer.append(info.getOldTypeName());
            //$NON-NLS-1$
            buffer.append(" ");
            if (info.isAdded())
                //$NON-NLS-1$
                buffer.append("{added}");
            else
                buffer.append(info.getOldName());
            //$NON-NLS-1$
            buffer.append(" ");
            buffer.append(info.getOldIndex());
            //$NON-NLS-1$
            buffer.append(" ");
            if (info.isDeleted())
                //$NON-NLS-1$
                buffer.append("{deleted}");
            else
                //$NON-NLS-1$//$NON-NLS-2$
                buffer.append(info.getNewTypeName().replaceAll(" ", ""));
            //$NON-NLS-1$
            buffer.append(" ");
            if (info.isDeleted())
                //$NON-NLS-1$
                buffer.append("{deleted}");
            else
                buffer.append(info.getNewName());
            //$NON-NLS-1$
            buffer.append(" ");
            buffer.append(info.isDeleted());
            arguments.put(ATTRIBUTE_PARAMETER + count, buffer.toString());
            final String value = info.getDefaultValue();
            if (//$NON-NLS-1$
            value != null && !"".equals(value))
                arguments.put(ATTRIBUTE_DEFAULT + count, value);
            count++;
        }
        count = 1;
        for (final Iterator<ExceptionInfo> iterator = fExceptionInfos.iterator(); iterator.hasNext(); ) {
            final ExceptionInfo info = iterator.next();
            arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + count, JavaRefactoringDescriptorUtil.elementToHandle(project, info.getElement()));
            arguments.put(ATTRIBUTE_KIND + count, new Integer(info.getKind()).toString());
            count++;
        }
    } catch (JavaModelException exception) {
        JavaPlugin.log(exception);
        return null;
    }
    return descriptor;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) IJavaProject(org.eclipse.jdt.core.IJavaProject) ChangeMethodSignatureDescriptor(org.eclipse.jdt.core.refactoring.descriptors.ChangeMethodSignatureDescriptor) ExceptionInfo(org.eclipse.jdt.internal.corext.refactoring.ExceptionInfo)

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