Search in sources :

Example 76 with IVariableBinding

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

the class ScopeAnalyzer method isVisible.

/**
	 * Evaluates if the declaration is visible in a certain context.
	 * @param binding The binding of the declaration to examine
	 * @param context The context to test in
	 * @return Returns
	 */
public static boolean isVisible(IBinding binding, ITypeBinding context) {
    if (binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) {
        // all local variables found are visible
        return true;
    }
    ITypeBinding declaring = getDeclaringType(binding);
    if (declaring == null) {
        return false;
    }
    declaring = declaring.getTypeDeclaration();
    int modifiers = binding.getModifiers();
    if (Modifier.isPublic(modifiers) || declaring.isInterface()) {
        return true;
    } else if (Modifier.isProtected(modifiers) || !Modifier.isPrivate(modifiers)) {
        if (declaring.getPackage() == context.getPackage()) {
            return true;
        }
        return isTypeInScope(declaring, context, Modifier.isProtected(modifiers));
    }
    // private visibility
    return isTypeInScope(declaring, context, false);
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 77 with IVariableBinding

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

the class GetterSetterCorrectionSubProcessor method addGetterSetterProposal.

private static boolean addGetterSetterProposal(IInvocationContext context, ASTNode coveringNode, Collection<ICommandAccess> proposals, int relevance) {
    if (!(coveringNode instanceof SimpleName)) {
        return false;
    }
    SimpleName sn = (SimpleName) coveringNode;
    IBinding binding = sn.resolveBinding();
    if (!(binding instanceof IVariableBinding))
        return false;
    IVariableBinding variableBinding = (IVariableBinding) binding;
    if (!variableBinding.isField())
        return false;
    if (proposals == null)
        return true;
    ChangeCorrectionProposal proposal = getProposal(context.getCompilationUnit(), sn, variableBinding, relevance);
    if (proposal != null)
        proposals.add(proposal);
    return true;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal)

Example 78 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project flux 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 79 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding 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 80 with IVariableBinding

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

the class Bindings method findFieldInHierarchy.

/**
	 * Finds the field specified by <code>fieldName</code> in
	 * the type hierarchy denoted by the given type. Returns <code>null</code> if no such field
	 * exists. If the field is defined in more than one super type only the first match is
	 * returned. First the super class is examined and then the implemented interfaces.
	 * @param type The type to search the field in
	 * @param fieldName The name of the field to find
	 * @return the variable binding representing the field
	 */
public static IVariableBinding findFieldInHierarchy(ITypeBinding type, String fieldName) {
    IVariableBinding field = findFieldInType(type, fieldName);
    if (field != null)
        return field;
    ITypeBinding superClass = type.getSuperclass();
    if (superClass != null) {
        field = findFieldInHierarchy(superClass, fieldName);
        if (field != null)
            return field;
    }
    ITypeBinding[] interfaces = type.getInterfaces();
    for (int i = 0; i < interfaces.length; i++) {
        field = findFieldInHierarchy(interfaces[i], fieldName);
        if (// no private fields in interfaces
        field != null)
            return field;
    }
    return null;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

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