@Override public void visitAnonymousClass(@NotNull PsiAnonymousClass anonymousClass) { if (anonymousClass instanceof PsiEnumConstantInitializer) { return; } final PsiMember containingMember = PsiTreeUtil.getParentOfType(anonymousClass, PsiMember.class); if (containingMember == null || containingMember.hasModifierProperty(PsiModifier.STATIC)) { return; } final PsiJavaCodeReferenceElement reference = anonymousClass.getBaseClassReference(); if (reference.resolve() == null) { // don't warn on broken code return; } final PsiClass containingClass = PsiTreeUtil.getParentOfType(anonymousClass, PsiClass.class); if (containingClass == null) { return; } if (containingClass.getContainingClass() != null && !containingClass.hasModifierProperty(PsiModifier.STATIC)) { // strictly speaking can be named static inner class but not when part of the current // containing class return; } final InnerClassReferenceVisitor visitor = new InnerClassReferenceVisitor(anonymousClass); anonymousClass.accept(visitor); if (!visitor.canInnerClassBeStatic()) { return; } if (hasReferenceToLocalClass(anonymousClass)) { return; } registerClassError(anonymousClass); }
private static boolean hasReferenceToLocalClass(PsiAnonymousClass anonymousClass) { final LocalClassReferenceVisitor visitor = new LocalClassReferenceVisitor(); anonymousClass.accept(visitor); return visitor.hasReferenceToLocalClass(); }