Search in sources :

Example 6 with ParameterInfo

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

Example 7 with ParameterInfo

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

the class ExtractMethodRefactoring method createNewMethodDeclaration.

private MethodDeclaration createNewMethodDeclaration() {
    MethodDeclaration result = fAST.newMethodDeclaration();
    int modifiers = fVisibility;
    BodyDeclaration enclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
    boolean isDestinationInterface = isDestinationInterface();
    if (isDestinationInterface && !(enclosingBodyDeclaration instanceof MethodDeclaration && enclosingBodyDeclaration.getParent() == fDestination && Modifier.isPublic(enclosingBodyDeclaration.getModifiers()))) {
        modifiers = Modifier.NONE;
    }
    boolean shouldBeStatic = false;
    ASTNode currentParent = enclosingBodyDeclaration;
    do {
        if (currentParent instanceof BodyDeclaration) {
            shouldBeStatic = shouldBeStatic || JdtFlags.isStatic((BodyDeclaration) currentParent);
        }
        currentParent = currentParent.getParent();
    } while (!shouldBeStatic && currentParent != null && currentParent != fDestination);
    if (shouldBeStatic || fAnalyzer.getForceStatic() || forceStatic()) {
        modifiers |= Modifier.STATIC;
    } else if (isDestinationInterface) {
        modifiers |= Modifier.DEFAULT;
    }
    ITypeBinding[] typeVariables = computeLocalTypeVariables(modifiers);
    List<TypeParameter> typeParameters = result.typeParameters();
    for (int i = 0; i < typeVariables.length; i++) {
        TypeParameter parameter = fAST.newTypeParameter();
        parameter.setName(fAST.newSimpleName(typeVariables[i].getName()));
        ITypeBinding[] bounds = typeVariables[i].getTypeBounds();
        for (int j = 0; j < bounds.length; j++) {
            if (!"java.lang.Object".equals(bounds[j].getQualifiedName())) {
                parameter.typeBounds().add(fImportRewriter.addImport(bounds[j], fAST));
            }
        }
        typeParameters.add(parameter);
    }
    result.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, modifiers));
    result.setReturnType2((Type) ASTNode.copySubtree(fAST, fAnalyzer.getReturnType()));
    result.setName(fAST.newSimpleName(fMethodName));
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(enclosingBodyDeclaration, fImportRewriter);
    List<SingleVariableDeclaration> parameters = result.parameters();
    for (int i = 0; i < fParameterInfos.size(); i++) {
        ParameterInfo info = fParameterInfos.get(i);
        VariableDeclaration infoDecl = getVariableDeclaration(info);
        SingleVariableDeclaration parameter = fAST.newSingleVariableDeclaration();
        parameter.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, ASTNodes.getModifiers(infoDecl)));
        parameter.setType(ASTNodeFactory.newType(fAST, infoDecl, fImportRewriter, context));
        parameter.setName(fAST.newSimpleName(info.getNewName()));
        parameter.setVarargs(info.isNewVarargs());
        parameters.add(parameter);
    }
    List<Type> exceptions = result.thrownExceptionTypes();
    ITypeBinding[] exceptionTypes = fAnalyzer.getExceptions(fThrowRuntimeExceptions);
    for (int i = 0; i < exceptionTypes.length; i++) {
        ITypeBinding exceptionType = exceptionTypes[i];
        exceptions.add(fImportRewriter.addImport(exceptionType, fAST, context, TypeLocation.EXCEPTION));
    }
    return result;
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ParameterInfo(org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Aggregations

ParameterInfo (org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 ArrayList (java.util.ArrayList)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 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 Assignment (org.eclipse.jdt.core.dom.Assignment)1 Block (org.eclipse.jdt.core.dom.Block)1 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)1 Expression (org.eclipse.jdt.core.dom.Expression)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)1 Type (org.eclipse.jdt.core.dom.Type)1