Search in sources :

Example 11 with ScopeAnalyzer

use of org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer in project che by eclipse.

the class MissingReturnTypeInLambdaCorrectionProposal method computeProposals.

@Override
protected Expression computeProposals(AST ast, ITypeBinding returnBinding, int returnOffset, CompilationUnit root, Expression result) {
    ScopeAnalyzer analyzer = new ScopeAnalyzer(root);
    IBinding[] bindings = analyzer.getDeclarationsInScope(returnOffset, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY);
    org.eclipse.jdt.core.dom.NodeFinder finder = new org.eclipse.jdt.core.dom.NodeFinder(root, returnOffset, 0);
    ASTNode varDeclFrag = ASTResolving.findAncestor(finder.getCoveringNode(), ASTNode.VARIABLE_DECLARATION_FRAGMENT);
    IVariableBinding varDeclFragBinding = null;
    if (varDeclFrag != null)
        varDeclFragBinding = ((VariableDeclarationFragment) varDeclFrag).resolveBinding();
    for (int i = 0; i < bindings.length; i++) {
        IVariableBinding curr = (IVariableBinding) bindings[i];
        ITypeBinding type = curr.getType();
        // Bindings are compared to make sure that a lambda does not return a variable which is yet to be initialised.
        if (type != null && type.isAssignmentCompatible(returnBinding) && testModifier(curr) && !Bindings.equals(curr, varDeclFragBinding)) {
            if (result == null) {
                result = ast.newSimpleName(curr.getName());
            }
            addLinkedPositionProposal(RETURN_EXPRESSION_KEY, curr.getName(), null);
        }
    }
    return result;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)

Example 12 with ScopeAnalyzer

use of org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer in project che by eclipse.

the class ExtractToNullCheckedLocalProposal method proposeLocalName.

String proposeLocalName(SimpleName fieldName, CompilationUnit root, IJavaProject javaProject) {
    // don't propose names that are already in use:
    Collection<String> variableNames = new ScopeAnalyzer(root).getUsedVariableNames(this.enclosingMethod.getStartPosition(), this.enclosingMethod.getLength());
    String[] names = new String[variableNames.size() + 1];
    variableNames.toArray(names);
    // don't propose the field name itself, either:
    String identifier = fieldName.getIdentifier();
    names[names.length - 1] = identifier;
    return StubUtility.getLocalNameSuggestions(javaProject, identifier, 0, names)[0];
}
Also used : ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)

Example 13 with ScopeAnalyzer

use of org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer in project che by eclipse.

the class ConvertLoopOperation method getUsedVariableNames.

protected String[] getUsedVariableNames() {
    final List<String> results = new ArrayList<String>();
    ForStatement forStatement = getForStatement();
    CompilationUnit root = (CompilationUnit) forStatement.getRoot();
    Collection<String> variableNames = new ScopeAnalyzer(root).getUsedVariableNames(forStatement.getStartPosition(), forStatement.getLength());
    results.addAll(variableNames);
    forStatement.accept(new GenericVisitor() {

        @Override
        public boolean visit(SingleVariableDeclaration node) {
            results.add(node.getName().getIdentifier());
            return super.visit(node);
        }

        @Override
        public boolean visit(VariableDeclarationFragment fragment) {
            results.add(fragment.getName().getIdentifier());
            return super.visit(fragment);
        }
    });
    results.addAll(Arrays.asList(fUsedNames));
    return results.toArray(new String[results.size()]);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ArrayList(java.util.ArrayList) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer) ForStatement(org.eclipse.jdt.core.dom.ForStatement) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

Example 14 with ScopeAnalyzer

use of org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer in project che by eclipse.

the class ASTResolving method getUsedVariableNames.

public static String[] getUsedVariableNames(ASTNode node) {
    CompilationUnit root = (CompilationUnit) node.getRoot();
    Collection<String> res = (new ScopeAnalyzer(root)).getUsedVariableNames(node.getStartPosition(), node.getLength());
    return res.toArray(new String[res.size()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)

Example 15 with ScopeAnalyzer

use of org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer in project flux 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)

Aggregations

ScopeAnalyzer (org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)19 IBinding (org.eclipse.jdt.core.dom.IBinding)10 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 ArrayList (java.util.ArrayList)4 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)4 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)3 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)3 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)3 RenameNodeCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.RenameNodeCorrectionProposal)3 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 AST (org.eclipse.jdt.core.dom.AST)2 Expression (org.eclipse.jdt.core.dom.Expression)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 HashSet (java.util.HashSet)1 List (java.util.List)1