Search in sources :

Example 1 with TypeChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class RefactorProcessor method getConvertResolvedTypeToVarTypeProposal.

private static boolean getConvertResolvedTypeToVarTypeProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) {
    CompilationUnit astRoot = context.getASTRoot();
    IJavaElement root = astRoot.getJavaElement();
    if (root == null) {
        return false;
    }
    IJavaProject javaProject = root.getJavaProject();
    if (javaProject == null) {
        return false;
    }
    if (!JavaModelUtil.is10OrHigher(javaProject)) {
        return false;
    }
    SimpleName name = getSimpleNameForVariable(node);
    if (name == null) {
        return false;
    }
    IBinding binding = name.resolveBinding();
    if (!(binding instanceof IVariableBinding)) {
        return false;
    }
    IVariableBinding varBinding = (IVariableBinding) binding;
    if (varBinding.isField() || varBinding.isParameter()) {
        return false;
    }
    ASTNode varDeclaration = astRoot.findDeclaringNode(varBinding);
    if (varDeclaration == null) {
        return false;
    }
    Type type = null;
    Expression expression = null;
    ITypeBinding typeBinding = varBinding.getType();
    if (typeBinding == null) {
        return false;
    }
    ITypeBinding expressionTypeBinding = null;
    if (varDeclaration instanceof SingleVariableDeclaration) {
        SingleVariableDeclaration svDecl = (SingleVariableDeclaration) varDeclaration;
        type = svDecl.getType();
        expression = svDecl.getInitializer();
        if (expression != null) {
            expressionTypeBinding = expression.resolveTypeBinding();
        } else {
            ASTNode parent = svDecl.getParent();
            if (parent instanceof EnhancedForStatement) {
                EnhancedForStatement efStmt = (EnhancedForStatement) parent;
                expression = efStmt.getExpression();
                if (expression != null) {
                    ITypeBinding expBinding = expression.resolveTypeBinding();
                    if (expBinding != null) {
                        if (expBinding.isArray()) {
                            expressionTypeBinding = expBinding.getElementType();
                        } else {
                            // $NON-NLS-1$
                            ITypeBinding iterable = Bindings.findTypeInHierarchy(expBinding, "java.lang.Iterable");
                            if (iterable != null) {
                                ITypeBinding[] typeArguments = iterable.getTypeArguments();
                                if (typeArguments.length == 1) {
                                    expressionTypeBinding = typeArguments[0];
                                    expressionTypeBinding = Bindings.normalizeForDeclarationUse(expressionTypeBinding, context.getASTRoot().getAST());
                                }
                            }
                        }
                    }
                }
            }
        }
    } else if (varDeclaration instanceof VariableDeclarationFragment) {
        ASTNode parent = varDeclaration.getParent();
        expression = ((VariableDeclarationFragment) varDeclaration).getInitializer();
        if (expression != null) {
            expressionTypeBinding = expression.resolveTypeBinding();
        }
        if (parent instanceof VariableDeclarationStatement) {
            type = ((VariableDeclarationStatement) parent).getType();
        } else if (parent instanceof VariableDeclarationExpression) {
            VariableDeclarationExpression varDecl = (VariableDeclarationExpression) parent;
            // cannot convert a VariableDeclarationExpression with multiple fragments to var.
            if (varDecl.fragments().size() > 1) {
                return false;
            }
            type = varDecl.getType();
        }
    }
    if (type == null || type.isVar()) {
        return false;
    }
    if (expression == null || expression instanceof ArrayInitializer || expression instanceof LambdaExpression || expression instanceof MethodReference) {
        return false;
    }
    if (expressionTypeBinding == null || !expressionTypeBinding.isEqualTo(typeBinding)) {
        return false;
    }
    TypeChangeCorrectionProposal proposal = new TypeChangeCorrectionProposal(context.getCompilationUnit(), varBinding, astRoot, typeBinding, IProposalRelevance.CHANGE_VARIABLE);
    proposal.setKind(CodeActionKind.Refactor);
    proposals.add(proposal);
    return true;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) TypeChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) IJavaProject(org.eclipse.jdt.core.IJavaProject) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) MethodReference(org.eclipse.jdt.core.dom.MethodReference) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 2 with TypeChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class RefactorProcessor method getConvertVarTypeToResolvedTypeProposal.

private static boolean getConvertVarTypeToResolvedTypeProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) {
    CompilationUnit astRoot = context.getASTRoot();
    IJavaElement root = astRoot.getJavaElement();
    if (root == null) {
        return false;
    }
    IJavaProject javaProject = root.getJavaProject();
    if (javaProject == null) {
        return false;
    }
    if (!JavaModelUtil.is10OrHigher(javaProject)) {
        return false;
    }
    SimpleName name = getSimpleNameForVariable(node);
    if (name == null) {
        return false;
    }
    IBinding binding = name.resolveBinding();
    if (!(binding instanceof IVariableBinding)) {
        return false;
    }
    IVariableBinding varBinding = (IVariableBinding) binding;
    if (varBinding.isField() || varBinding.isParameter()) {
        return false;
    }
    ASTNode varDeclaration = astRoot.findDeclaringNode(varBinding);
    if (varDeclaration == null) {
        return false;
    }
    ITypeBinding typeBinding = varBinding.getType();
    if (typeBinding == null || typeBinding.isAnonymous() || typeBinding.isIntersectionType() || typeBinding.isWildcardType()) {
        return false;
    }
    Type type = null;
    if (varDeclaration instanceof SingleVariableDeclaration) {
        type = ((SingleVariableDeclaration) varDeclaration).getType();
    } else if (varDeclaration instanceof VariableDeclarationFragment) {
        ASTNode parent = varDeclaration.getParent();
        if (parent instanceof VariableDeclarationStatement) {
            type = ((VariableDeclarationStatement) parent).getType();
        } else if (parent instanceof VariableDeclarationExpression) {
            type = ((VariableDeclarationExpression) parent).getType();
        }
    }
    if (type == null || !type.isVar()) {
        return false;
    }
    TypeChangeCorrectionProposal proposal = new TypeChangeCorrectionProposal(context.getCompilationUnit(), varBinding, astRoot, typeBinding, false, IProposalRelevance.CHANGE_VARIABLE);
    proposal.setKind(CodeActionKind.Refactor);
    proposals.add(proposal);
    return true;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) TypeChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) IJavaProject(org.eclipse.jdt.core.IJavaProject) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 IType (org.eclipse.jdt.core.IType)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 IBinding (org.eclipse.jdt.core.dom.IBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 Type (org.eclipse.jdt.core.dom.Type)2 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 TypeChangeCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal)2 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)1 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)1 Expression (org.eclipse.jdt.core.dom.Expression)1 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)1