private static boolean isAcceptedByPattern( @NotNull PsiClass element, String qualifiedName, ClassPattern pattern, Set<PsiClass> visited) { if (qualifiedName == null) { return false; } if (qualifiedName.equals(pattern.pattern)) { return true; } if (pattern.pattern.endsWith(PATTERN_SUFFIX) && qualifiedName.startsWith(StringUtil.trimEnd(pattern.pattern, PATTERN_SUFFIX))) { return true; } if (pattern.hierarchically) { for (PsiClass superClass : element.getSupers()) { final String superClassQualifiedName = superClass.getQualifiedName(); if (visited.add(superClass) && isAcceptedByPattern(superClass, superClassQualifiedName, pattern, visited)) { return true; } } } return false; }
@NotNull public static PsiElement[] findSuperElements(@NotNull PsiElement element) { if (element instanceof PsiClass) { PsiClass aClass = (PsiClass) element; List<PsiClass> allSupers = new ArrayList<>(Arrays.asList(aClass.getSupers())); for (Iterator<PsiClass> iterator = allSupers.iterator(); iterator.hasNext(); ) { PsiClass superClass = iterator.next(); if (CommonClassNames.JAVA_LANG_OBJECT.equals(superClass.getQualifiedName())) iterator.remove(); } return allSupers.toArray(new PsiClass[allSupers.size()]); } if (element instanceof PsiMethod) { PsiMethod method = (PsiMethod) element; if (method.isConstructor()) { PsiMethod constructorInSuper = PsiSuperMethodUtil.findConstructorInSuper(method); if (constructorInSuper != null) { return new PsiMethod[] {constructorInSuper}; } } else { PsiMethod[] superMethods = method.findSuperMethods(false); if (superMethods.length == 0) { PsiMethod superMethod = getSiblingInheritedViaSubClass(method); if (superMethod != null) { superMethods = new PsiMethod[] {superMethod}; } } return superMethods; } } return PsiElement.EMPTY_ARRAY; }
private static boolean isSelfInheritor(PsiClass psiClass, ArrayList<PsiClass> visited) { if (visited.contains(psiClass)) return true; visited.add(psiClass); for (PsiClass aSuper : psiClass.getSupers()) { if (isSelfInheritor(aSuper, visited)) return true; } visited.remove(psiClass); return false; }
private static void getAllMethodsInner( PsiClass clazz, List<PsiMethod> allMethods, HashSet<PsiClass> visited) { if (visited.contains(clazz)) return; visited.add(clazz); ContainerUtil.addAll(allMethods, clazz.getMethods()); final PsiClass[] supers = clazz.getSupers(); for (PsiClass aSuper : supers) { getAllMethodsInner(aSuper, allMethods, visited); } }
@Override public void onInitialize(RefElement refElement) { ((RefElementImpl) refElement).setFlag(true, CAN_BE_FINAL_MASK); if (refElement instanceof RefClass) { final RefClass refClass = (RefClass) refElement; final PsiClass psiClass = refClass.getElement(); if (refClass.isEntry()) { ((RefClassImpl) refClass).setFlag(false, CAN_BE_FINAL_MASK); return; } if (refClass.isAbstract() || refClass.isAnonymous() || refClass.isInterface()) { ((RefClassImpl) refClass).setFlag(false, CAN_BE_FINAL_MASK); return; } if (!refClass.isSelfInheritor(psiClass)) { for (PsiClass psiSuperClass : psiClass.getSupers()) { if (myManager.belongsToScope(psiSuperClass)) { RefClass refSuperClass = (RefClass) myManager.getReference(psiSuperClass); if (refSuperClass != null) { ((RefClassImpl) refSuperClass).setFlag(false, CAN_BE_FINAL_MASK); } } } } } else if (refElement instanceof RefMethod) { final RefMethod refMethod = (RefMethod) refElement; final PsiElement element = refMethod.getElement(); if (element instanceof PsiMethod) { PsiMethod psiMethod = (PsiMethod) element; if (refMethod.isConstructor() || refMethod.isAbstract() || refMethod.isStatic() || PsiModifier.PRIVATE.equals(refMethod.getAccessModifier()) || refMethod.getOwnerClass().isAnonymous() || refMethod.getOwnerClass().isInterface()) { ((RefMethodImpl) refMethod).setFlag(false, CAN_BE_FINAL_MASK); } if (PsiModifier.PRIVATE.equals(refMethod.getAccessModifier()) && refMethod.getOwner() != null && !(refMethod.getOwnerClass().getOwner() instanceof RefElement)) { ((RefMethodImpl) refMethod).setFlag(false, CAN_BE_FINAL_MASK); } for (PsiMethod psiSuperMethod : psiMethod.findSuperMethods()) { if (myManager.belongsToScope(psiSuperMethod)) { RefMethod refSuperMethod = (RefMethod) myManager.getReference(psiSuperMethod); if (refSuperMethod != null) { ((RefMethodImpl) refSuperMethod).setFlag(false, CAN_BE_FINAL_MASK); } } } } } }
private void initializeSuperReferences(PsiClass psiClass) { if (!isSelfInheritor(psiClass)) { for (PsiClass psiSuperClass : psiClass.getSupers()) { if (getRefManager().belongsToScope(psiSuperClass)) { RefClassImpl refClass = (RefClassImpl) getRefManager().getReference(psiSuperClass); if (refClass != null) { addBaseClass(refClass); refClass.addSubClass(this); } } } } }
private static boolean isSuperEventListenerAllowed( PsiClass eventClass, PsiMethod method, MinecraftModule module) { final PsiClass[] supers = eventClass.getSupers(); for (PsiClass aSuper : supers) { if (module.isEventClassValid(aSuper, method)) { return true; } if (isSuperEventListenerAllowed(aSuper, method, module)) { return true; } } return false; }
public static boolean inheritsJUnitTestCase(PsiClass psiClass) { PsiClass current = psiClass; while (current != null) { PsiClass[] supers = current.getSupers(); if (supers.length > 0) { PsiClass parent = supers[0]; if ("junit.framework.TestCase".equals(parent.getQualifiedName())) return true; current = parent; // handle typo where class extends itself if (current == psiClass) return false; } else { current = null; } } return false; }
private static void addIdAndSuperClasses(PsiFile file, @NotNull TIntHashSet forward) { if (file instanceof PsiJavaFile && file.getName().equals("Object.class") && ((PsiJavaFile) file).getPackageName().equals("java.lang")) { return; } VirtualFile virtualFile = PsiUtilCore.getVirtualFile(file); if (virtualFile instanceof VirtualFileWithId && forward.add(getAbsId(virtualFile)) && file instanceof PsiClassOwner) { for (PsiClass aClass : ((PsiClassOwner) file).getClasses()) { for (PsiClass superClass : aClass.getSupers()) { addIdAndSuperClasses(superClass.getContainingFile(), forward); } } } }
private List<PsiClass> calculateInterfacesSupported() { final List<PsiClass> out = new ArrayList<PsiClass>(); final PsiClass[] supers = sourceClass.getSupers(); for (PsiClass superClass : supers) { if (!superClass.isInterface()) { continue; } final PsiMethod[] superclassMethods = superClass.getMethods(); if (superclassMethods.length == 0) { continue; } boolean allMethodsCovered = true; for (PsiMethod method : superclassMethods) { boolean isCovered = false; for (PsiMethod movedMethod : methods) { if (isSuperMethod(method, movedMethod)) { isCovered = true; break; } } if (!isCovered) { allMethodsCovered = false; break; } } if (allMethodsCovered) { out.add(superClass); } } final Project project = sourceClass.getProject(); final PsiManager manager = sourceClass.getManager(); final GlobalSearchScope scope = GlobalSearchScope.allScope(project); if (usesDefaultSerialization(sourceClass)) { final PsiClass serializable = JavaPsiFacade.getInstance(manager.getProject()).findClass("java.io.Serializable", scope); out.add(serializable); } if (usesDefaultClone(sourceClass)) { final PsiClass cloneable = JavaPsiFacade.getInstance(manager.getProject()).findClass("java.lang.Cloneable", scope); out.add(cloneable); } return out; }