Example usage for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding superclass

List of usage examples for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding superclass

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding superclass.

Prototype

@Override
    public ReferenceBinding superclass() 

Source Link

Usage

From source file:com.android.tools.lint.EcjParser.java

License:Apache License

/** Computes the super method, if any, given a method binding */
private static MethodBinding findSuperMethodBinding(@NonNull MethodBinding binding) {
    try {/*from ww  w . j a v  a 2  s  .  c  om*/
        ReferenceBinding superclass = binding.declaringClass.superclass();
        while (superclass != null) {
            MethodBinding[] methods = superclass.getMethods(binding.selector, binding.parameters.length);
            for (MethodBinding method : methods) {
                if (method.areParameterErasuresEqual(binding)) {
                    return method;
                }
            }

            superclass = superclass.superclass();
        }
    } catch (Exception ignore) {
        // Work around ECJ bugs; see https://code.google.com/p/android/issues/detail?id=172268
    }

    return null;
}

From source file:com.android.tools.lint.psi.EcjPsiBinaryClass.java

License:Apache License

private PsiField[] getFields(boolean includeInherited) {
    if (mBinding instanceof ReferenceBinding) {
        ReferenceBinding cls = (ReferenceBinding) mBinding;
        if (includeInherited) {
            List<EcjPsiBinaryField> result = null;
            while (cls != null) {
                FieldBinding[] fields = cls.fields();
                if (fields != null) {
                    int count = fields.length;
                    if (count > 0) {
                        if (result == null) {
                            result = Lists.newArrayListWithExpectedSize(count);
                        }//from www . j  a v a 2  s . c  o m
                        for (FieldBinding field : fields) {
                            if ((field.modifiers & Modifier.PRIVATE) != 0 && cls != mBinding) {
                                // Ignore parent fields that are private
                                continue;
                            }

                            // See if this field looks like it's masked
                            boolean masked = false;
                            for (EcjPsiBinaryField f : result) {
                                FieldBinding mb = f.getBinding();
                                if (Arrays.equals(mb.readableName(), field.readableName())) {
                                    masked = true;
                                    break;
                                }
                            }
                            if (masked) {
                                continue;
                            }

                            result.add(new EcjPsiBinaryField(mManager, field));
                        }
                    }
                }
                cls = cls.superclass();
            }

            return result != null ? result.toArray(PsiField.EMPTY_ARRAY) : PsiField.EMPTY_ARRAY;
        } else {
            FieldBinding[] fields = cls.fields();
            if (fields != null) {
                int count = fields.length;
                List<EcjPsiBinaryField> result = Lists.newArrayListWithExpectedSize(count);
                for (FieldBinding field : fields) {
                    result.add(new EcjPsiBinaryField(mManager, field));
                }
                return result.toArray(PsiField.EMPTY_ARRAY);
            }
        }
    }

    return PsiField.EMPTY_ARRAY;
}

From source file:com.android.tools.lint.psi.EcjPsiBinaryClass.java

License:Apache License

@Nullable
@Override/*from w  ww .  ja v a  2 s.com*/
public PsiField findFieldByName(String name, boolean checkBases) {
    if (mBinding instanceof ReferenceBinding) {
        ReferenceBinding cls = (ReferenceBinding) mBinding;
        while (cls != null) {
            FieldBinding[] fields = cls.fields();
            if (fields != null) {
                for (FieldBinding field : fields) {
                    if ((field.modifiers & Modifier.PRIVATE) != 0 && cls != mBinding) {
                        // Ignore parent methods that are private
                        continue;
                    }

                    if (EcjParser.sameChars(name, field.name)) {
                        return new EcjPsiBinaryField(mManager, field);
                    }
                }
            }
            if (checkBases) {
                cls = cls.superclass();
            } else {
                break;
            }
        }
    }

    return null;
}

From source file:com.android.tools.lint.psi.EcjPsiBinaryClass.java

License:Apache License

@NonNull
private PsiMethod[] findMethods(@Nullable String name, boolean includeInherited, boolean includeConstructors) {
    if (mBinding instanceof ReferenceBinding) {
        ReferenceBinding cls = (ReferenceBinding) mBinding;
        if (includeInherited) {
            List<EcjPsiBinaryMethod> result = null;
            while (cls != null) {
                MethodBinding[] methods = name != null ? cls.getMethods(name.toCharArray()) : cls.methods();
                if (methods != null) {
                    int count = methods.length;
                    if (count > 0) {
                        if (result == null) {
                            result = Lists.newArrayListWithExpectedSize(count);
                        }/*  w  ww  . j a  va 2s  . c o m*/
                        for (MethodBinding method : methods) {
                            if ((method.modifiers & Modifier.PRIVATE) != 0 && cls != mBinding) {
                                // Ignore parent methods that are private
                                continue;
                            }

                            if (includeConstructors || !method.isConstructor()) {
                                // See if this method looks like it's masked
                                boolean masked = false;
                                for (PsiMethod m : result) {
                                    MethodBinding mb = ((EcjPsiBinaryMethod) m).getBinding();
                                    if (mb.areParameterErasuresEqual(method)) {
                                        masked = true;
                                        break;
                                    }
                                }
                                if (masked) {
                                    continue;
                                }
                                result.add(new EcjPsiBinaryMethod(mManager, method));
                            }
                        }
                    }
                }
                cls = cls.superclass();
            }

            return result != null ? result.toArray(PsiMethod.EMPTY_ARRAY) : PsiMethod.EMPTY_ARRAY;
        } else {
            MethodBinding[] methods = name != null ? cls.getMethods(name.toCharArray()) : cls.methods();
            if (methods != null) {
                int count = methods.length;
                List<EcjPsiBinaryMethod> result = Lists.newArrayListWithExpectedSize(count);
                for (MethodBinding method : methods) {
                    if (includeConstructors || !method.isConstructor()) {
                        result.add(new EcjPsiBinaryMethod(mManager, method));
                    }
                }
                return result.toArray(PsiMethod.EMPTY_ARRAY);
            }
        }
    }

    return PsiMethod.EMPTY_ARRAY;
}

From source file:com.android.tools.lint.psi.EcjPsiBinaryClass.java

License:Apache License

@SuppressWarnings("SameParameterValue")
private PsiAnnotation[] findAnnotations(boolean includeSuper) {
    List<PsiAnnotation> all = Lists.newArrayListWithExpectedSize(4);
    ExternalAnnotationRepository manager = mManager.getAnnotationRepository();

    if (mBinding instanceof ReferenceBinding) {
        ReferenceBinding cls = (ReferenceBinding) mBinding;
        while (cls != null) {
            AnnotationBinding[] annotations;
            try {
                annotations = cls.getAnnotations();
            } catch (AbortCompilation e) {
                // ECJ  will in some cases go and attempt to load the standard annotations
                // as part of the annotation lookup above (in
                // AnnotationBinding#addStandardAnnotations), but if for some reason the
                // class path is incorrect, this will throw an AbortCompilation. In this
                // case, just ignore the annotations.
                annotations = new AnnotationBinding[0];
            }/*from   www  .  j  a va2s . c  o m*/
            int count = annotations.length;
            if (count > 0) {
                all = Lists.newArrayListWithExpectedSize(count);
                for (AnnotationBinding annotation : annotations) {
                    if (annotation != null) {
                        all.add(new EcjPsiBinaryAnnotation(mManager, this, annotation));
                    }
                }
            }

            // Look for external annotations
            if (manager != null) {
                Collection<PsiAnnotation> external = manager.getAnnotations(cls);
                if (external != null) {
                    all.addAll(external);
                }
            }

            if (!includeSuper) {
                break;
            }

            cls = cls.superclass();
        }
    }

    return EcjPsiManager.ensureUnique(all);
}

From source file:com.android.tools.lint.psi.EcjPsiBinaryMethod.java

License:Apache License

@NonNull
@Override/*from www .ja  va 2s  .  c  om*/
public String getName() {
    if (mName == null) {
        if (mMethodBinding.isConstructor()) {
            ReferenceBinding cls = mMethodBinding.declaringClass;
            while (cls != null && cls.isAnonymousType()) {
                cls = cls.superclass();
            }
            if (cls != null) {
                char[][] compoundName = cls.compoundName;
                mName = new String(compoundName[compoundName.length - 1]);
                return mName;
            }
        }
        mName = new String(mMethodBinding.selector);
    }
    return mName;
}

From source file:com.android.tools.lint.psi.EcjPsiJavaEvaluator.java

License:Apache License

@Override
public boolean extendsClass(@Nullable PsiClass cls, @NonNull String className, boolean strict) {
    ReferenceBinding binding;
    if (cls instanceof EcjPsiClass) {
        TypeDeclaration declaration = (TypeDeclaration) ((EcjPsiClass) cls).mNativeNode;
        binding = declaration.binding;/*  ww w.  j  a  v  a 2s . com*/
    } else if (cls instanceof EcjPsiBinaryClass) {
        binding = ((EcjPsiBinaryClass) cls).getTypeBinding();
    } else {
        return false;
    }
    if (strict) {
        try {
            binding = binding.superclass();
        } catch (AbortCompilation ignore) {
            // Encountered symbol that couldn't be resolved (e.g. compiled class references
            // class not found on the classpath
            return false;
        }
    }

    for (; binding != null; binding = binding.superclass()) {
        if (equalsCompound(className, binding.compoundName)) {
            return true;
        }
    }

    return false;
}

From source file:com.android.tools.lint.psi.EcjPsiJavaEvaluator.java

License:Apache License

@Override
public boolean implementsInterface(@NonNull PsiClass cls, @NonNull String interfaceName, boolean strict) {
    ReferenceBinding binding;
    if (cls instanceof EcjPsiClass) {
        TypeDeclaration declaration = (TypeDeclaration) ((EcjPsiClass) cls).mNativeNode;
        binding = declaration.binding;//w  w  w.j a  v  a  2 s  .c  om
    } else if (cls instanceof EcjPsiBinaryClass) {
        binding = ((EcjPsiBinaryClass) cls).getTypeBinding();
    } else {
        return false;
    }
    if (strict) {
        try {
            binding = binding.superclass();
        } catch (AbortCompilation ignore) {
            // Encountered symbol that couldn't be resolved (e.g. compiled class references
            // class not found on the classpath
            return false;
        }
    }
    return isInheritor(binding, interfaceName);
}

From source file:com.android.tools.lint.psi.EcjPsiJavaEvaluator.java

License:Apache License

/** Checks whether the given class extends or implements a class with the given name */
private static boolean isInheritor(@Nullable ReferenceBinding cls, @NonNull String name) {
    while (cls != null) {
        ReferenceBinding[] interfaces = cls.superInterfaces();
        for (ReferenceBinding binding : interfaces) {
            if (isInheritor(binding, name)) {
                return true;
            }//from  ww w  .j  a  v  a  2s.co  m
        }

        if (equalsCompound(name, cls.compoundName)) {
            return true;
        }

        try {
            cls = cls.superclass();
        } catch (AbortCompilation ignore) {
            // Encountered symbol that couldn't be resolved (e.g. compiled class references
            // class not found on the classpath
            break;
        }
    }

    return false;
}

From source file:com.android.tools.lint.psi.EcjPsiJavaEvaluator.java

License:Apache License

@NonNull
@Override/*w  ww  .j a v  a  2 s  . c o m*/
public PsiAnnotation[] getAllAnnotations(@NonNull PsiModifierListOwner owner, boolean inHierarchy) {
    if (!inHierarchy) {
        return getDirectAnnotations(owner);
    }

    PsiModifierList modifierList = owner.getModifierList();
    if (modifierList == null) {
        return getDirectAnnotations(owner);
    }

    if (owner instanceof PsiMethod) {
        MethodBinding method;
        if (owner instanceof EcjPsiMethod) {
            EcjPsiMethod psiMethod = (EcjPsiMethod) owner;
            AbstractMethodDeclaration declaration = (AbstractMethodDeclaration) psiMethod.getNativeNode();
            assert declaration != null;
            method = declaration.binding;
        } else if (owner instanceof EcjPsiBinaryMethod) {
            method = ((EcjPsiBinaryMethod) owner).getBinding();
        } else {
            assert false : owner.getClass();
            return PsiAnnotation.EMPTY_ARRAY;
        }

        List<PsiAnnotation> all = Lists.newArrayListWithExpectedSize(2);
        ExternalAnnotationRepository manager = mManager.getAnnotationRepository();

        while (method != null) {
            if (method.declaringClass == null) {
                // for example, for unresolved problem bindings
                break;
            }
            AnnotationBinding[] annotations = method.getAnnotations();
            int count = annotations.length;
            if (count > 0) {
                all = Lists.newArrayListWithExpectedSize(count);
                for (AnnotationBinding annotation : annotations) {
                    if (annotation != null) {
                        all.add(new EcjPsiBinaryAnnotation(mManager, modifierList, annotation));
                    }
                }
            }

            // Look for external annotations
            if (manager != null) {
                Collection<PsiAnnotation> external = manager.getAnnotations(method);
                if (external != null) {
                    all.addAll(external);
                }
            }

            method = EcjPsiManager.findSuperMethodBinding(method, false, false);
        }

        return EcjPsiManager.ensureUnique(all);
    } else if (owner instanceof PsiClass) {
        ReferenceBinding cls;
        if (owner instanceof EcjPsiClass) {
            EcjPsiClass psiClass = (EcjPsiClass) owner;
            TypeDeclaration declaration = (TypeDeclaration) psiClass.getNativeNode();
            assert declaration != null;
            cls = declaration.binding;
        } else if (owner instanceof EcjPsiBinaryClass) {
            cls = ((EcjPsiBinaryClass) owner).getTypeBinding();
        } else {
            assert false : owner.getClass();
            return PsiAnnotation.EMPTY_ARRAY;
        }

        List<PsiAnnotation> all = Lists.newArrayListWithExpectedSize(2);
        ExternalAnnotationRepository manager = mManager.getAnnotationRepository();

        while (cls != null) {
            AnnotationBinding[] annotations = cls.getAnnotations();
            int count = annotations.length;
            if (count > 0) {
                all = Lists.newArrayListWithExpectedSize(count);
                for (AnnotationBinding annotation : annotations) {
                    if (annotation != null) {
                        all.add(new EcjPsiBinaryAnnotation(mManager, modifierList, annotation));
                    }
                }
            }

            // Look for external annotations
            if (manager != null) {
                Collection<PsiAnnotation> external = manager.getAnnotations(cls);
                if (external != null) {
                    all.addAll(external);
                }
            }

            try {
                cls = cls.superclass();
            } catch (AbortCompilation ignore) {
                // Encountered symbol that couldn't be resolved (e.g. compiled class references
                // class not found on the classpath
                break;
            }
        }

        return EcjPsiManager.ensureUnique(all);
    } else if (owner instanceof PsiParameter) {
        MethodBinding method;
        int index;

        if (owner instanceof EcjPsiBinaryParameter) {
            EcjPsiBinaryParameter parameter = (EcjPsiBinaryParameter) owner;
            method = parameter.getOwnerMethod().getBinding();
            index = parameter.getIndex();
        } else if (owner instanceof EcjPsiParameter) {
            EcjPsiParameter parameter = (EcjPsiParameter) owner;
            if (parameter.getParent() instanceof PsiParameterList) {
                EcjPsiMethod psiMethod = (EcjPsiMethod) PsiTreeUtil.getParentOfType(parameter.getParent(),
                        PsiMethod.class, true);
                if (psiMethod == null) {
                    return getDirectAnnotations(owner);
                }
                index = ((PsiParameterList) parameter.getParent()).getParameterIndex(parameter);
                AbstractMethodDeclaration declaration = (AbstractMethodDeclaration) psiMethod.getNativeNode();
                assert declaration != null;
                method = declaration.binding;
            } else {
                // For each block, catch block
                return getDirectAnnotations(owner);
            }
        } else {
            // Unexpected method type
            assert false : owner.getClass();
            return getDirectAnnotations(owner);
        }

        List<PsiAnnotation> all = Lists.newArrayListWithExpectedSize(2);
        ExternalAnnotationRepository manager = mManager.getAnnotationRepository();

        while (method != null) {
            if (method.declaringClass == null) {
                // for example, for unresolved problem bindings
                break;
            }
            AnnotationBinding[][] parameterAnnotations = method.getParameterAnnotations();
            if (parameterAnnotations != null && index < parameterAnnotations.length) {
                AnnotationBinding[] annotations = parameterAnnotations[index];
                int count = annotations.length;
                if (count > 0) {
                    for (AnnotationBinding annotation : annotations) {
                        if (annotation != null) {
                            all.add(new EcjPsiBinaryAnnotation(mManager, modifierList, annotation));
                        }
                    }
                }
            }

            // Look for external annotations
            if (manager != null) {
                Collection<PsiAnnotation> external = manager.getParameterAnnotations(method, index);
                if (external != null) {
                    all.addAll(external);
                }
            }

            method = EcjPsiManager.findSuperMethodBinding(method, false, false);
        }

        return EcjPsiManager.ensureUnique(all);
    } else {
        // PsiField, PsiLocalVariable etc: no inheritance
        return getDirectAnnotations(owner);
    }
}