Search in sources :

Example 96 with IBinding

use of org.eclipse.jdt.core.dom.IBinding in project flux by eclipse.

the class ScopeAnalyzer method getDeclarationsInScope.

public IBinding[] getDeclarationsInScope(int offset, int flags) {
    org.eclipse.jdt.core.dom.NodeFinder finder = new org.eclipse.jdt.core.dom.NodeFinder(fRoot, offset, 0);
    ASTNode node = finder.getCoveringNode();
    if (node == null) {
        return NO_BINDING;
    }
    if (node instanceof SimpleName) {
        return getDeclarationsInScope((SimpleName) node, flags);
    }
    try {
        ITypeBinding binding = Bindings.getBindingOfParentType(node);
        DefaultBindingRequestor requestor = new DefaultBindingRequestor(binding, flags);
        addLocalDeclarations(node, offset, flags, requestor);
        if (binding != null) {
            addTypeDeclarations(binding, flags, requestor);
        }
        List<IBinding> result = requestor.getResult();
        return result.toArray(new IBinding[result.size()]);
    } finally {
        clearLists();
    }
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 97 with IBinding

use of org.eclipse.jdt.core.dom.IBinding 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)

Example 98 with IBinding

use of org.eclipse.jdt.core.dom.IBinding in project flux by eclipse.

the class StubUtility method getParameterTypeNamesForSeeTag.

/*
	 * Returns the parameters type names used in see tags. Currently, these are always fully qualified.
	 */
private static String[] getParameterTypeNamesForSeeTag(IMethod overridden) {
    try {
        ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
        parser.setProject(overridden.getJavaProject());
        IBinding[] bindings = parser.createBindings(new IJavaElement[] { overridden }, null);
        if (bindings.length == 1 && bindings[0] instanceof IMethodBinding) {
            return getParameterTypeNamesForSeeTag((IMethodBinding) bindings[0]);
        }
    } catch (IllegalStateException e) {
    // method does not exist
    }
    // fall back code. Not good for generic methods!
    String[] paramTypes = overridden.getParameterTypes();
    String[] paramTypeNames = new String[paramTypes.length];
    for (int i = 0; i < paramTypes.length; i++) {
        paramTypeNames[i] = Signature.toString(Signature.getTypeErasure(paramTypes[i]));
    }
    return paramTypeNames;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IBinding(org.eclipse.jdt.core.dom.IBinding) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 99 with IBinding

use of org.eclipse.jdt.core.dom.IBinding in project processing by processing.

the class ASTUtils method findAllOccurrences.

protected static List<SimpleName> findAllOccurrences(ASTNode root, String bindingKey) {
    List<SimpleName> occurences = new ArrayList<>();
    root.getRoot().accept(new ASTVisitor() {

        @Override
        public boolean visit(SimpleName name) {
            IBinding binding = resolveBinding(name);
            if (binding != null && bindingKey.equals(binding.getKey())) {
                occurences.add(name);
            }
            return super.visit(name);
        }
    });
    return occurences;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 100 with IBinding

use of org.eclipse.jdt.core.dom.IBinding in project processing by processing.

the class ASTUtils method resolveBinding.

public static IBinding resolveBinding(SimpleName node) {
    IBinding binding = node.resolveBinding();
    if (binding == null)
        return null;
    // Fix constructor call/declaration being resolved as type
    if (binding.getKind() == IBinding.TYPE) {
        ASTNode context = node;
        // stop if context is type argument (parent is also Name/Type, but unrelated)
        while (isNameOrType(context) && !context.getLocationInParent().getId().equals("typeArguments")) {
            context = context.getParent();
        }
        switch(context.getNodeType()) {
            case ASTNode.METHOD_DECLARATION:
                MethodDeclaration decl = (MethodDeclaration) context;
                if (decl.isConstructor()) {
                    binding = decl.resolveBinding();
                }
                break;
            case ASTNode.CLASS_INSTANCE_CREATION:
                ClassInstanceCreation cic = (ClassInstanceCreation) context;
                binding = cic.resolveConstructorBinding();
                break;
        }
    }
    if (binding == null)
        return null;
    // Normalize parametrized and raw bindings into generic bindings
    switch(binding.getKind()) {
        case IBinding.TYPE:
            ITypeBinding type = (ITypeBinding) binding;
            if (type.isParameterizedType() || type.isRawType()) {
                binding = type.getErasure();
            }
            break;
        case IBinding.METHOD:
            IMethodBinding method = (IMethodBinding) binding;
            ITypeBinding declaringClass = method.getDeclaringClass();
            if (declaringClass.isParameterizedType() || declaringClass.isRawType()) {
                IMethodBinding[] methods = declaringClass.getErasure().getDeclaredMethods();
                IMethodBinding generic = Arrays.stream(methods).filter(method::overrides).findAny().orElse(null);
                if (generic != null)
                    method = generic;
            }
            if (method.isParameterizedMethod() || method.isRawMethod()) {
                method = method.getMethodDeclaration();
            }
            binding = method;
            break;
    }
    return binding;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

IBinding (org.eclipse.jdt.core.dom.IBinding)100 SimpleName (org.eclipse.jdt.core.dom.SimpleName)57 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)45 ASTNode (org.eclipse.jdt.core.dom.ASTNode)40 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)40 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)25 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)24 Name (org.eclipse.jdt.core.dom.Name)20 Expression (org.eclipse.jdt.core.dom.Expression)18 ArrayList (java.util.ArrayList)16 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)14 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)13 CastExpression (org.eclipse.jdt.core.dom.CastExpression)12 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)11 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)11 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)11 AST (org.eclipse.jdt.core.dom.AST)10 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)10