Search in sources :

Example 76 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 77 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 78 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 79 with IBinding

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

the class InferTypeArgumentsConstraintCreator method endVisit.

@Override
public void endVisit(SimpleName node) {
    if (node.resolveBoxing()) {
        ImmutableTypeVariable2 boxed = fTCModel.makeImmutableTypeVariable(node.resolveTypeBinding(), node);
        setConstraintVariable(node, boxed);
        return;
    }
    IBinding binding = node.resolveBinding();
    if (binding instanceof IVariableBinding) {
        //TODO: code is similar to handling of method return value
        IVariableBinding variableBinding = (IVariableBinding) binding;
        ITypeBinding declaredVariableType = variableBinding.getVariableDeclaration().getType();
        if (declaredVariableType.isTypeVariable()) {
            Expression receiver = getSimpleNameReceiver(node);
            if (receiver != null) {
                ConstraintVariable2 receiverCv = getConstraintVariable(receiver);
                // the type variable must come from the receiver!
                Assert.isNotNull(receiverCv);
                ConstraintVariable2 elementCv = fTCModel.getElementVariable(receiverCv, declaredVariableType);
                // [retVal] =^= Elem[receiver]:
                setConstraintVariable(node, elementCv);
                return;
            }
        } else if (declaredVariableType.isParameterizedType()) {
            Expression receiver = getSimpleNameReceiver(node);
            if (receiver != null) {
                ConstraintVariable2 receiverCv = getConstraintVariable(receiver);
                if (receiverCv != null) {
                    //						ITypeBinding genericVariableType= declaredVariableType.getTypeDeclaration();
                    ConstraintVariable2 returnTypeCv = fTCModel.makeParameterizedTypeVariable(declaredVariableType);
                    setConstraintVariable(node, returnTypeCv);
                    // Elem[retVal] =^= Elem[receiver]
                    TType declaredVariableTType = fTCModel.createTType(declaredVariableType);
                    fTCModel.createTypeVariablesEqualityConstraints(receiverCv, Collections.<String, IndependentTypeVariable2>emptyMap(), returnTypeCv, declaredVariableTType);
                    return;
                }
            }
        } else {
        //TODO: array...
        //logUnexpectedNode(node, null);
        }
        // default:
        VariableVariable2 cv = fTCModel.makeVariableVariable(variableBinding);
        setConstraintVariable(node, cv);
    }
// TODO else?
}
Also used : ImmutableTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ImmutableTypeVariable2) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) VariableVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.VariableVariable2) IndependentTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2)

Example 80 with IBinding

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

the class IntroduceIndirectionRefactoring method checkInitialConditions.

// ********** CONDITION CHECKING **********
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    try {
        pm.beginTask(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_activation, 1);
        fRewrites = new HashMap<ICompilationUnit, CompilationUnitRewrite>();
        if (fTargetMethod == null) {
            if (fSelectionStart == 0)
                return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
            // if a text selection exists, source is available.
            CompilationUnit selectionCURoot;
            ASTNode selectionNode;
            if (fSelectionCompilationUnit != null) {
                // compilation unit - could use CuRewrite later on
                selectionCURoot = getCachedCURewrite(fSelectionCompilationUnit).getRoot();
                selectionNode = getSelectedNode(fSelectionCompilationUnit, selectionCURoot, fSelectionStart, fSelectionLength);
            } else {
                // binary class file - no cu rewrite
                ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
                parser.setResolveBindings(true);
                parser.setSource(fSelectionClassFile);
                selectionCURoot = (CompilationUnit) parser.createAST(null);
                selectionNode = getSelectedNode(null, selectionCURoot, fSelectionStart, fSelectionLength);
            }
            if (selectionNode == null)
                return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
            IMethodBinding targetMethodBinding = null;
            if (selectionNode.getNodeType() == ASTNode.METHOD_INVOCATION) {
                targetMethodBinding = ((MethodInvocation) selectionNode).resolveMethodBinding();
            } else if (selectionNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
                targetMethodBinding = ((MethodDeclaration) selectionNode).resolveBinding();
            } else if (selectionNode.getNodeType() == ASTNode.SUPER_METHOD_INVOCATION) {
                // Allow invocation on super methods calls. makes sense as other
                // calls or even only the declaration can be updated.
                targetMethodBinding = ((SuperMethodInvocation) selectionNode).resolveMethodBinding();
            } else {
                return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
            }
            // resolve generics
            fTargetMethodBinding = targetMethodBinding.getMethodDeclaration();
            fTargetMethod = (IMethod) fTargetMethodBinding.getJavaElement();
            //allow single updating mode if an invocation was selected and the invocation can be updated
            if (selectionNode instanceof MethodInvocation && fSelectionCompilationUnit != null)
                fSelectionMethodInvocation = (MethodInvocation) selectionNode;
        } else {
            if (fTargetMethod.getDeclaringType().isAnnotation())
                return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_annotation);
            if (fTargetMethod.getCompilationUnit() != null) {
                // source method
                CompilationUnit selectionCURoot = getCachedCURewrite(fTargetMethod.getCompilationUnit()).getRoot();
                MethodDeclaration declaration = ASTNodeSearchUtil.getMethodDeclarationNode(fTargetMethod, selectionCURoot);
                fTargetMethodBinding = declaration.resolveBinding().getMethodDeclaration();
            } else {
                // binary method - no CURewrite available (and none needed as we cannot update the method anyway)
                ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
                parser.setProject(fTargetMethod.getJavaProject());
                IBinding[] bindings = parser.createBindings(new IJavaElement[] { fTargetMethod }, null);
                fTargetMethodBinding = ((IMethodBinding) bindings[0]).getMethodDeclaration();
            }
        }
        if (fTargetMethod == null || fTargetMethodBinding == null || (!RefactoringAvailabilityTester.isIntroduceIndirectionAvailable(fTargetMethod)))
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
        if (fTargetMethod.getDeclaringType().isLocal() || fTargetMethod.getDeclaringType().isAnonymous())
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_for_local_or_anonymous_types);
        if (fTargetMethod.isConstructor())
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_for_constructors);
        if (fIntermediaryMethodName == null)
            fIntermediaryMethodName = fTargetMethod.getElementName();
        if (fIntermediaryType == null) {
            if (fSelectionCompilationUnit != null && !fSelectionCompilationUnit.isReadOnly())
                fIntermediaryType = getEnclosingInitialSelectionMember().getDeclaringType();
            else if (!fTargetMethod.isBinary() && !fTargetMethod.isReadOnly())
                fIntermediaryType = fTargetMethod.getDeclaringType();
        }
        return new RefactoringStatus();
    } finally {
        pm.done();
    }
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IBinding(org.eclipse.jdt.core.dom.IBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Aggregations

IBinding (org.eclipse.jdt.core.dom.IBinding)103 SimpleName (org.eclipse.jdt.core.dom.SimpleName)59 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)48 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)41 ASTNode (org.eclipse.jdt.core.dom.ASTNode)40 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)25 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)25 Name (org.eclipse.jdt.core.dom.Name)20 Expression (org.eclipse.jdt.core.dom.Expression)19 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 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)11 AST (org.eclipse.jdt.core.dom.AST)10 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10