Search in sources :

Example 96 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 97 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 98 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)

Example 99 with IVariableBinding

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

the class JdtTypeElement method getEnclosedElements.

@Override
public List<? extends Element> getEnclosedElements() {
    ITypeBinding decl = (ITypeBinding) binding;
    List<Element> toReturn = new ArrayList<>();
    for (IVariableBinding i : decl.getDeclaredFields()) {
        toReturn.add(BindingConverter.getElement(i));
    }
    for (IMethodBinding i : decl.getDeclaredMethods()) {
        toReturn.add(BindingConverter.getElement(i));
    }
    for (ITypeBinding i : decl.getDeclaredTypes()) {
        toReturn.add(BindingConverter.getElement(i));
    }
    return toReturn;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ArrayList(java.util.ArrayList) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 100 with IVariableBinding

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

the class StructHelperClass method resolve.

@Override
public boolean resolve(IMarshalContext context) {
    if (BindingManager.DEBUG)
        Sharpen.Debug("RESOLVE STRUCT: %s", getName());
    if (_resolved)
        return !_resolveFailed;
    final List<MemberInfo> members = _template.getMembers();
    _fieldInfo = new ElementInfo[members.size()];
    for (int i = 0; i < members.size(); i++) {
        final MemberInfo member = members.get(i);
        IVariableBinding field = findField(getType(), member.getName());
        if (field == null) {
            Sharpen.Log(Level.SEVERE, "No such field '%s' in type '%s'", member.getName(), BindingUtils.qualifiedName(getType()));
            _resolveFailed = true;
            continue;
        }
        if (member.getValue() != null)
            continue;
        MarshalInfo marshal;
        if (member.getMarshalInfo() != null)
            marshal = member.getMarshalInfo().resolve(field.getType());
        else
            marshal = context.getMarshalInfo(field.getType());
        if (marshal == null) {
            Sharpen.Log(Level.SEVERE, "Missing marshal info for type '%s' of field '%s'", BindingUtils.qualifiedName(field.getType()), member.getName());
            _resolveFailed = true;
            continue;
        }
        _fieldInfo[i] = new ElementInfo(field.getType(), marshal, null, Flags.ELEMENT);
        if (BindingManager.DEBUG)
            Sharpen.Debug("STRUCT MEMBER: %s - %s", member.getName(), BindingUtils.qualifiedName(field.getType()));
        if (!marshal.resolve(context, field.getType())) {
            Sharpen.Log(Level.SEVERE, "Failed to resolve field '%s' in '%s'", member.getName(), BindingUtils.qualifiedName(getType()));
            _resolveFailed = true;
            continue;
        }
        if (!marshal.isPrimitiveType()) {
            Sharpen.Log(Level.SEVERE, "Field '%s' in '%s' is not a primitive type", member.getName(), BindingUtils.qualifiedName(getType()));
            _resolveFailed = true;
            continue;
        }
        if (BindingManager.DEBUG)
            Sharpen.Debug("STRUCT MEMBER DONE: %s - %s", member.getName(), BindingUtils.qualifiedName(field.getType()));
    }
    if (BindingManager.DEBUG)
        Sharpen.Debug("RESOLVE STRUCT DONE: %s%s", getName(), _resolveFailed ? " - ERROR" : "");
    _resolved = true;
    return true;
}
Also used : MemberInfo(sharpen.xobotos.api.interop.NativeStruct.MemberInfo) ElementInfo(sharpen.xobotos.api.interop.NativeMethodBuilder.ElementInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) MarshalInfo(sharpen.xobotos.api.interop.marshal.MarshalInfo)

Aggregations

IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)101 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)48 IBinding (org.eclipse.jdt.core.dom.IBinding)45 SimpleName (org.eclipse.jdt.core.dom.SimpleName)41 ASTNode (org.eclipse.jdt.core.dom.ASTNode)30 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)28 ArrayList (java.util.ArrayList)22 Expression (org.eclipse.jdt.core.dom.Expression)22 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 Name (org.eclipse.jdt.core.dom.Name)13 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 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)11 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)11 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)10 AST (org.eclipse.jdt.core.dom.AST)9 Assignment (org.eclipse.jdt.core.dom.Assignment)9