Search in sources :

Example 86 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class ImportReferencesCollector method possibleStaticImportFound.

private void possibleStaticImportFound(Name name) {
    if (fStaticImports == null || fASTRoot == null) {
        return;
    }
    while (name.isQualifiedName()) {
        name = ((QualifiedName) name).getQualifier();
    }
    if (!isAffected(name)) {
        return;
    }
    IBinding binding = name.resolveBinding();
    SimpleName simpleName = (SimpleName) name;
    if (binding == null || binding instanceof ITypeBinding || !Modifier.isStatic(binding.getModifiers()) || simpleName.isDeclaration()) {
        return;
    }
    if (binding instanceof IVariableBinding) {
        IVariableBinding varBinding = (IVariableBinding) binding;
        if (varBinding.isField()) {
            varBinding = varBinding.getVariableDeclaration();
            ITypeBinding declaringClass = varBinding.getDeclaringClass();
            if (declaringClass != null && !declaringClass.isLocal()) {
                if (new ScopeAnalyzer(fASTRoot).isDeclaredInScope(varBinding, simpleName, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY))
                    return;
                fStaticImports.add(simpleName);
            }
        }
    } else if (binding instanceof IMethodBinding) {
        IMethodBinding methodBinding = ((IMethodBinding) binding).getMethodDeclaration();
        ITypeBinding declaringClass = methodBinding.getDeclaringClass();
        if (declaringClass != null && !declaringClass.isLocal()) {
            if (new ScopeAnalyzer(fASTRoot).isDeclaredInScope(methodBinding, simpleName, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY))
                return;
            fStaticImports.add(simpleName);
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 87 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class StubUtility method getBaseNameFromExpression.

private static String getBaseNameFromExpression(IJavaProject project, Expression assignedExpression, int variableKind) {
    String name = null;
    if (assignedExpression instanceof CastExpression) {
        assignedExpression = ((CastExpression) assignedExpression).getExpression();
    }
    if (assignedExpression instanceof Name) {
        Name simpleNode = (Name) assignedExpression;
        IBinding binding = simpleNode.resolveBinding();
        if (binding instanceof IVariableBinding)
            return getBaseName((IVariableBinding) binding, project);
        return ASTNodes.getSimpleNameIdentifier(simpleNode);
    } else if (assignedExpression instanceof MethodInvocation) {
        name = ((MethodInvocation) assignedExpression).getName().getIdentifier();
    } else if (assignedExpression instanceof SuperMethodInvocation) {
        name = ((SuperMethodInvocation) assignedExpression).getName().getIdentifier();
    } else if (assignedExpression instanceof FieldAccess) {
        return ((FieldAccess) assignedExpression).getName().getIdentifier();
    } else if (variableKind == NamingConventions.VK_STATIC_FINAL_FIELD && (assignedExpression instanceof StringLiteral || assignedExpression instanceof NumberLiteral)) {
        String string = assignedExpression instanceof StringLiteral ? ((StringLiteral) assignedExpression).getLiteralValue() : ((NumberLiteral) assignedExpression).getToken();
        StringBuffer res = new StringBuffer();
        boolean needsUnderscore = false;
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            if (Character.isJavaIdentifierPart(ch)) {
                if (res.length() == 0 && !Character.isJavaIdentifierStart(ch) || needsUnderscore) {
                    res.append('_');
                }
                res.append(ch);
                needsUnderscore = false;
            } else {
                needsUnderscore = res.length() > 0;
            }
        }
        if (res.length() > 0) {
            return res.toString();
        }
    }
    if (name != null) {
        for (int i = 0; i < KNOWN_METHOD_NAME_PREFIXES.length; i++) {
            String curr = KNOWN_METHOD_NAME_PREFIXES[i];
            if (name.startsWith(curr)) {
                if (name.equals(curr)) {
                    // don't suggest 'get' as variable name
                    return null;
                } else if (Character.isUpperCase(name.charAt(curr.length()))) {
                    return name.substring(curr.length());
                }
            }
        }
    }
    return name;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Name(org.eclipse.jdt.core.dom.Name) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 88 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class ModifierCorrectionSubProcessor method addNeedToEmulateProposal.

public static void addNeedToEmulateProposal(IInvocationContext context, IProblemLocation problem, Collection<ModifierChangeCorrectionProposal> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    IBinding binding = ((SimpleName) selectedNode).resolveBinding();
    if (binding instanceof IVariableBinding) {
        binding = ((IVariableBinding) binding).getVariableDeclaration();
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertofinal_description, BasicElementLabels.getJavaElementName(binding.getName()));
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, Modifier.FINAL, 0, IProposalRelevance.CHANGE_MODIFIER_OF_VARIABLE_TO_FINAL, image));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Image(org.eclipse.swt.graphics.Image)

Example 89 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class AssignToVariableAssistProposal method doAddField.

private ASTRewrite doAddField() {
    boolean isParamToField = fNodeToAssign.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION;
    ASTNode newTypeDecl = ASTResolving.findParentType(fNodeToAssign);
    if (newTypeDecl == null) {
        return null;
    }
    Expression expression = isParamToField ? ((SingleVariableDeclaration) fNodeToAssign).getName() : ((ExpressionStatement) fNodeToAssign).getExpression();
    AST ast = newTypeDecl.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    createImportRewrite((CompilationUnit) fNodeToAssign.getRoot());
    BodyDeclaration bodyDecl = ASTResolving.findParentBodyDeclaration(fNodeToAssign);
    Block body;
    if (bodyDecl instanceof MethodDeclaration) {
        body = ((MethodDeclaration) bodyDecl).getBody();
    } else if (bodyDecl instanceof Initializer) {
        body = ((Initializer) bodyDecl).getBody();
    } else {
        return null;
    }
    IJavaProject project = getCompilationUnit().getJavaProject();
    boolean isAnonymous = newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION;
    boolean isStatic = Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous;
    boolean isConstructorParam = isParamToField && fNodeToAssign.getParent() instanceof MethodDeclaration && ((MethodDeclaration) fNodeToAssign.getParent()).isConstructor();
    int modifiers = Modifier.PRIVATE;
    if (isStatic) {
        modifiers |= Modifier.STATIC;
    } else if (isConstructorParam) {
    //			String saveActionsKey= AbstractSaveParticipantPreferenceConfiguration.EDITOR_SAVE_PARTICIPANT_PREFIX + CleanUpPostSaveListener.POSTSAVELISTENER_ID;
    //			IScopeContext[] scopes= { InstanceScope.INSTANCE, new ProjectScope(project.getProject()) };
    //			boolean safeActionsEnabled= Platform.getPreferencesService().getBoolean(JavaPlugin.getPluginId(), saveActionsKey, false, scopes);
    }
    if (/*CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(
					CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS, project))
			&&*/
    CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL, project)) && CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS, project))) {
        int constructors = 0;
        if (newTypeDecl instanceof AbstractTypeDeclaration) {
            List<BodyDeclaration> bodyDeclarations = ((AbstractTypeDeclaration) newTypeDecl).bodyDeclarations();
            for (BodyDeclaration decl : bodyDeclarations) {
                if (decl instanceof MethodDeclaration && ((MethodDeclaration) decl).isConstructor()) {
                    constructors++;
                }
            }
        }
        if (constructors == 1) {
            modifiers |= Modifier.FINAL;
        }
    }
    VariableDeclarationFragment newDeclFrag = addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression);
    String varName = newDeclFrag.getName().getIdentifier();
    Assignment assignment = ast.newAssignment();
    assignment.setRightHandSide((Expression) rewrite.createCopyTarget(expression));
    boolean needsThis = StubUtility.useThisForFieldAccess(project);
    if (isParamToField) {
        needsThis |= varName.equals(((SimpleName) expression).getIdentifier());
    }
    SimpleName accessName = ast.newSimpleName(varName);
    if (needsThis) {
        FieldAccess fieldAccess = ast.newFieldAccess();
        fieldAccess.setName(accessName);
        if (isStatic) {
            String typeName = ((AbstractTypeDeclaration) newTypeDecl).getName().getIdentifier();
            fieldAccess.setExpression(ast.newSimpleName(typeName));
        } else {
            fieldAccess.setExpression(ast.newThisExpression());
        }
        assignment.setLeftHandSide(fieldAccess);
    } else {
        assignment.setLeftHandSide(accessName);
    }
    ASTNode selectionNode;
    if (isParamToField) {
        // assign parameter to field
        ExpressionStatement statement = ast.newExpressionStatement(assignment);
        int insertIdx = findAssignmentInsertIndex(body.statements());
        rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY).insertAt(statement, insertIdx, null);
        selectionNode = statement;
    } else {
        if (needsSemicolon(expression)) {
            rewrite.replace(expression, ast.newExpressionStatement(assignment), null);
        } else {
            rewrite.replace(expression, assignment, null);
        }
        selectionNode = fNodeToAssign;
    }
    addLinkedPosition(rewrite.track(newDeclFrag.getName()), false, KEY_NAME);
    if (!isParamToField) {
        FieldDeclaration fieldDeclaration = (FieldDeclaration) newDeclFrag.getParent();
        addLinkedPosition(rewrite.track(fieldDeclaration.getType()), false, KEY_TYPE);
    }
    addLinkedPosition(rewrite.track(accessName), true, KEY_NAME);
    IVariableBinding variableBinding = newDeclFrag.resolveBinding();
    if (variableBinding != null) {
        SimpleName[] linkedNodes = LinkedNodeFinder.findByBinding(fNodeToAssign.getRoot(), variableBinding);
        for (int i = 0; i < linkedNodes.length; i++) {
            addLinkedPosition(rewrite.track(linkedNodes[i]), false, KEY_NAME);
        }
    }
    setEndPosition(rewrite.track(selectionNode));
    return rewrite;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Assignment(org.eclipse.jdt.core.dom.Assignment) IJavaProject(org.eclipse.jdt.core.IJavaProject) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Initializer(org.eclipse.jdt.core.dom.Initializer) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 90 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class AssignToVariableAssistProposal method findAssignmentInsertIndex.

private int findAssignmentInsertIndex(List<Statement> statements) {
    HashSet<String> paramsBefore = new HashSet<String>();
    List<SingleVariableDeclaration> params = ((MethodDeclaration) fNodeToAssign.getParent()).parameters();
    for (int i = 0; i < params.size() && (params.get(i) != fNodeToAssign); i++) {
        SingleVariableDeclaration decl = params.get(i);
        paramsBefore.add(decl.getName().getIdentifier());
    }
    int i = 0;
    for (i = 0; i < statements.size(); i++) {
        Statement curr = statements.get(i);
        switch(curr.getNodeType()) {
            case ASTNode.CONSTRUCTOR_INVOCATION:
            case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
                break;
            case ASTNode.EXPRESSION_STATEMENT:
                Expression expr = ((ExpressionStatement) curr).getExpression();
                if (expr instanceof Assignment) {
                    Assignment assignment = (Assignment) expr;
                    Expression rightHand = assignment.getRightHandSide();
                    if (rightHand instanceof SimpleName && paramsBefore.contains(((SimpleName) rightHand).getIdentifier())) {
                        IVariableBinding binding = Bindings.getAssignedVariable(assignment);
                        if (binding == null || binding.isField()) {
                            break;
                        }
                    }
                }
                return i;
            default:
                return i;
        }
    }
    return i;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Assignment(org.eclipse.jdt.core.dom.Assignment) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) HashSet(java.util.HashSet)

Aggregations

IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)106 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)49 IBinding (org.eclipse.jdt.core.dom.IBinding)48 SimpleName (org.eclipse.jdt.core.dom.SimpleName)43 ASTNode (org.eclipse.jdt.core.dom.ASTNode)30 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)29 Expression (org.eclipse.jdt.core.dom.Expression)24 ArrayList (java.util.ArrayList)22 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 Name (org.eclipse.jdt.core.dom.Name)13 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)13 Assignment (org.eclipse.jdt.core.dom.Assignment)12 CastExpression (org.eclipse.jdt.core.dom.CastExpression)12 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)12 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)12 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)12 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)11 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)11