removed nullable collection anti-pattern

This commit is contained in:
Alexey Kudravtsev
2016-11-21 16:29:52 +03:00
parent ae05e9e1b1
commit 81f334d3a1
16 changed files with 56 additions and 132 deletions
@@ -230,12 +230,10 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
else {
List<RefEntity> children = refClass.getChildren();
if (children != null) {
for (RefEntity entity : children) {
if (entity instanceof RefMethodImpl && entity.getName().startsWith(pattern.method + "(")) {
((RefMethodImpl)entity).setEntry(true);
((RefMethodImpl)entity).setPermanentEntry(true);
}
for (RefEntity entity : children) {
if (entity instanceof RefMethodImpl && entity.getName().startsWith(pattern.method + "(")) {
((RefMethodImpl)entity).setEntry(true);
((RefMethodImpl)entity).setPermanentEntry(true);
}
}
}
@@ -83,11 +83,12 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod {
super.add(child);
}
@NotNull
@Override
public List<RefEntity> getChildren() {
List<RefEntity> superChildren = super.getChildren();
if (myParameters == null) return superChildren;
if (superChildren == null || superChildren.isEmpty()) return Arrays.<RefEntity>asList(myParameters);
if (superChildren.isEmpty()) return Arrays.<RefEntity>asList(myParameters);
List<RefEntity> allChildren = new ArrayList<>(superChildren.size() + myParameters.length);
allChildren.addAll(superChildren);
@@ -38,7 +38,6 @@ import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiNonJavaFileReferenceProcessor;
import com.intellij.psi.search.PsiSearchHelper;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
@@ -50,8 +49,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.List;
@@ -82,24 +79,16 @@ public class VisibilityInspection extends GlobalJavaBatchInspectionTool {
myPackageLocalForMembersCheckbox = new JCheckBox(InspectionsBundle.message("inspection.visibility.option"));
myPackageLocalForMembersCheckbox.setSelected(SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS);
myPackageLocalForMembersCheckbox.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS = myPackageLocalForMembersCheckbox.isSelected();
}
});
myPackageLocalForMembersCheckbox.getModel().addChangeListener(
e -> SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS = myPackageLocalForMembersCheckbox.isSelected());
gc.gridy = 0;
add(myPackageLocalForMembersCheckbox, gc);
myPackageLocalForTopClassesCheckbox = new JCheckBox(InspectionsBundle.message("inspection.visibility.option1"));
myPackageLocalForTopClassesCheckbox.setSelected(SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES);
myPackageLocalForTopClassesCheckbox.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES = myPackageLocalForTopClassesCheckbox.isSelected();
}
});
myPackageLocalForTopClassesCheckbox.getModel().addChangeListener(
e -> SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES = myPackageLocalForTopClassesCheckbox.isSelected());
gc.gridy = 1;
add(myPackageLocalForTopClassesCheckbox, gc);
@@ -107,12 +96,7 @@ public class VisibilityInspection extends GlobalJavaBatchInspectionTool {
myPrivateForInnersCheckbox = new JCheckBox(InspectionsBundle.message("inspection.visibility.option2"));
myPrivateForInnersCheckbox.setSelected(SUGGEST_PRIVATE_FOR_INNERS);
myPrivateForInnersCheckbox.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
SUGGEST_PRIVATE_FOR_INNERS = myPrivateForInnersCheckbox.isSelected();
}
});
myPrivateForInnersCheckbox.getModel().addChangeListener(e -> SUGGEST_PRIVATE_FOR_INNERS = myPrivateForInnersCheckbox.isSelected());
gc.gridy = 2;
gc.weighty = 1;
@@ -311,10 +295,8 @@ public class VisibilityInspection extends GlobalJavaBatchInspectionTool {
}
List children = refClass.getChildren();
if (children != null) {
for (Object refElement : children) {
if (!isAccessible((RefJavaElement)refElement, accessModifier)) return false;
}
for (Object refElement : children) {
if (!isAccessible((RefJavaElement)refElement, accessModifier)) return false;
}
for (final RefElement refElement : refClass.getInTypeReferences()) {
@@ -395,7 +377,7 @@ public class VisibilityInspection extends GlobalJavaBatchInspectionTool {
if (modifierList == null) return false;
final PsiElement toElement = to.getElement();
final boolean [] resolved = new boolean[] {false};
final boolean [] resolved = {false};
modifierList.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitReferenceExpression(PsiReferenceExpression expression) {
@@ -447,12 +429,9 @@ public class VisibilityInspection extends GlobalJavaBatchInspectionTool {
refEntity.accept(new RefJavaVisitor() {
@Override public void visitField(@NotNull final RefField refField) {
if (refField.getAccessModifier() != PsiModifier.PRIVATE) {
globalContext.enqueueFieldUsagesProcessor(refField, new GlobalJavaInspectionContext.UsagesProcessor() {
@Override
public boolean process(PsiReference psiReference) {
ignoreElement(processor, refField);
return false;
}
globalContext.enqueueFieldUsagesProcessor(refField, psiReference -> {
ignoreElement(processor, refField);
return false;
});
}
}
@@ -460,40 +439,28 @@ public class VisibilityInspection extends GlobalJavaBatchInspectionTool {
@Override public void visitMethod(@NotNull final RefMethod refMethod) {
if (!refMethod.isExternalOverride() && refMethod.getAccessModifier() != PsiModifier.PRIVATE &&
!(refMethod instanceof RefImplicitConstructor)) {
globalContext.enqueueDerivedMethodsProcessor(refMethod, new GlobalJavaInspectionContext.DerivedMethodsProcessor() {
@Override
public boolean process(PsiMethod derivedMethod) {
ignoreElement(processor, refMethod);
return false;
}
globalContext.enqueueDerivedMethodsProcessor(refMethod, derivedMethod -> {
ignoreElement(processor, refMethod);
return false;
});
globalContext.enqueueMethodUsagesProcessor(refMethod, new GlobalJavaInspectionContext.UsagesProcessor() {
@Override
public boolean process(PsiReference psiReference) {
ignoreElement(processor, refMethod);
return false;
}
globalContext.enqueueMethodUsagesProcessor(refMethod, psiReference -> {
ignoreElement(processor, refMethod);
return false;
});
}
}
@Override public void visitClass(@NotNull final RefClass refClass) {
if (!refClass.isAnonymous()) {
globalContext.enqueueDerivedClassesProcessor(refClass, new GlobalJavaInspectionContext.DerivedClassesProcessor() {
@Override
public boolean process(PsiClass inheritor) {
ignoreElement(processor, refClass);
return false;
}
globalContext.enqueueDerivedClassesProcessor(refClass, inheritor -> {
ignoreElement(processor, refClass);
return false;
});
globalContext.enqueueClassUsagesProcessor(refClass, new GlobalJavaInspectionContext.UsagesProcessor() {
@Override
public boolean process(PsiReference psiReference) {
ignoreElement(processor, refClass);
return false;
}
globalContext.enqueueClassUsagesProcessor(refClass, psiReference -> {
ignoreElement(processor, refClass);
return false;
});
final RefMethod defaultConstructor = refClass.getDefaultConstructor();
@@ -503,13 +470,10 @@ public class VisibilityInspection extends GlobalJavaBatchInspectionTool {
if (qualifiedName != null) {
final Project project = manager.getProject();
PsiSearchHelper.SERVICE.getInstance(project)
.processUsagesInNonJavaFiles(qualifiedName, new PsiNonJavaFileReferenceProcessor() {
@Override
public boolean process(PsiFile file, int startOffset, int endOffset) {
entryPointsManager.addEntryPoint(defaultConstructor, false);
ignoreElement(processor, defaultConstructor);
return false;
}
.processUsagesInNonJavaFiles(qualifiedName, (file, startOffset, endOffset) -> {
entryPointsManager.addEntryPoint(defaultConstructor, false);
ignoreElement(processor, defaultConstructor);
return false;
}, GlobalSearchScope.projectScope(project));
}
}
@@ -48,7 +48,7 @@ public interface RefEntity extends UserDataHolder {
*
* @return the list of children.
*/
@Nullable
@NotNull
List<RefEntity> getChildren();
/**
@@ -27,6 +27,8 @@ package com.intellij.codeInspection.reference;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.Key;
import com.intellij.util.BitUtil;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -60,9 +62,10 @@ abstract class RefEntityImpl implements RefEntity {
return myName;
}
@NotNull
@Override
public synchronized List<RefEntity> getChildren() {
return myChildren;
return ObjectUtils.notNull(myChildren, ContainerUtil.emptyList());
}
@Override
@@ -659,11 +659,9 @@ public class RefManagerImpl extends RefManager {
@Override
public void removeRefElement(@NotNull RefElement refElement, @NotNull List<RefElement> deletedRefs) {
List<RefEntity> children = refElement.getChildren();
if (children != null) {
RefElement[] refElements = children.toArray(new RefElement[children.size()]);
for (RefElement refChild : refElements) {
removeRefElement(refChild, deletedRefs);
}
RefElement[] refElements = children.toArray(new RefElement[children.size()]);
for (RefElement refChild : refElements) {
removeRefElement(refChild, deletedRefs);
}
((RefManagerImpl)refElement.getRefManager()).removeReference(refElement);
@@ -305,10 +305,8 @@ public class GlobalInspectionContextImpl extends GlobalInspectionContextBase imp
InspectionToolPresentation presentation = getPresentation(toolWrapper);
presentation.ignoreCurrentElement(refElement);
final List<RefEntity> children = refElement.getChildren();
if (children != null) {
for (RefEntity child : children) {
ignoreElementRecursively(toolWrapper, child);
}
for (RefEntity child : children) {
ignoreElementRecursively(toolWrapper, child);
}
}
}
@@ -150,10 +150,8 @@ public class SuppressActionSequentialTask implements SequentialTask {
suppressedNodes.add(entity);
}
final List<RefEntity> children = entity.getChildren();
if (children != null) {
for (RefEntity child : children) {
toIgnoreInView.addLast(child);
}
for (RefEntity child : children) {
toIgnoreInView.addLast(child);
}
}
}
@@ -59,9 +59,6 @@ public class DependencyUtils {
addOwnerClassesToSet(element.getOutReferences(), dependencies);
addOwnerClassesToSet(element.getOutTypeReferences(), dependencies);
final List<RefEntity> children = element.getChildren();
if (children == null) {
return;
}
for (RefEntity child : children) {
if (child instanceof RefJavaElement && !(child instanceof RefClass)) {
tabulateDependencyClasses((RefJavaElement)child, dependencies);
@@ -72,14 +69,14 @@ public class DependencyUtils {
private static void addOwnerClassesToSet(Collection<? extends RefElement> references, Set<RefClass> set) {
final RefJavaUtil refUtil = RefJavaUtil.getInstance();
for (RefElement reference : references) {
final RefClass refClass = (reference instanceof RefClass) ? (RefClass)reference : refUtil.getOwnerClass(reference);
final RefClass refClass = reference instanceof RefClass ? (RefClass)reference : refUtil.getOwnerClass(reference);
if (refClass != null && !refClass.isAnonymous() && !refClass.isLocalClass()) {
set.add(refClass);
}
}
}
public static Set<RefClass> calculateTransitiveDependenciesForClass(RefClass refClass) {
static Set<RefClass> calculateTransitiveDependenciesForClass(RefClass refClass) {
final Set<RefClass> dependencies = refClass.getUserData(TRANSITIVE_DEPENDENCY_CLASSES_KEY);
if (dependencies != null) {
return dependencies;
@@ -93,8 +90,8 @@ public class DependencyUtils {
private static void tabulateTransitiveDependencyClasses(
RefClass refClass, Set<RefClass> newDependencies) {
final LinkedList<RefClass> pendingClasses = new LinkedList<>();
final Set<RefClass> processedClasses = new HashSet<>();
pendingClasses.addLast(refClass);
final Set<RefClass> processedClasses = new HashSet<>();
while (!pendingClasses.isEmpty()) {
final RefClass classToProcess = pendingClasses.removeFirst();
newDependencies.add(classToProcess);
@@ -130,9 +127,6 @@ public class DependencyUtils {
addOwnerClassesToSet(refClass.getInTypeReferences(), dependents);
}
final List<RefEntity> children = element.getChildren();
if (children == null) {
return;
}
for (RefEntity child : children) {
if (child instanceof RefElement && !(child instanceof RefClass)) {
tabulateDependentClasses((RefElement)child, dependents);
@@ -140,7 +134,7 @@ public class DependencyUtils {
}
}
public static Set<RefClass> calculateTransitiveDependentsForClass(RefClass refClass) {
static Set<RefClass> calculateTransitiveDependentsForClass(RefClass refClass) {
final Set<RefClass> dependents = refClass.getUserData(TRANSITIVE_DEPENDENT_CLASSES_KEY);
if (dependents != null) {
return dependents;
@@ -154,8 +148,8 @@ public class DependencyUtils {
private static void tabulateTransitiveDependentClasses(
RefClass refClass, Set<RefClass> newDependents) {
final LinkedList<RefClass> pendingClasses = new LinkedList<>();
final Set<RefClass> processedClasses = new HashSet<>();
pendingClasses.addLast(refClass);
final Set<RefClass> processedClasses = new HashSet<>();
while (!pendingClasses.isEmpty()) {
final RefClass classToProcess = pendingClasses.removeFirst();
newDependents.add(classToProcess);
@@ -172,7 +166,7 @@ public class DependencyUtils {
newDependents.remove(refClass);
}
public static Set<RefPackage> calculateDependenciesForPackage(
private static Set<RefPackage> calculateDependenciesForPackage(
RefPackage refPackage) {
final Set<RefPackage> dependencies =
refPackage.getUserData(DEPENDENCY_PACKAGES_KEY);
@@ -198,9 +192,6 @@ public class DependencyUtils {
}
}
final List<RefEntity> children = entity.getChildren();
if (children == null) {
return;
}
for (RefEntity child : children) {
if (!(child instanceof RefPackage)) {
tabulateDependencyPackages(child, dependencies);
@@ -208,7 +199,7 @@ public class DependencyUtils {
}
}
public static Set<RefPackage> calculateDependentsForPackage(
private static Set<RefPackage> calculateDependentsForPackage(
RefPackage refPackage) {
final Set<RefPackage> dependents =
refPackage.getUserData(DEPENDENT_PACKAGES_KEY);
@@ -234,9 +225,6 @@ public class DependencyUtils {
}
}
final List<RefEntity> children = entity.getChildren();
if (children == null) {
return;
}
for (RefEntity child : children) {
if (!(child instanceof RefPackage)) {
tabulateDependentPackages(child, dependents);
@@ -244,7 +232,7 @@ public class DependencyUtils {
}
}
public static Set<RefPackage> calculateTransitiveDependentsForPackage(
static Set<RefPackage> calculateTransitiveDependentsForPackage(
RefPackage refPackage) {
final Set<RefPackage> dependents =
refPackage.getUserData(TRANSITIVE_DEPENDENT_PACKAGES_KEY);
@@ -261,8 +249,8 @@ public class DependencyUtils {
RefPackage refPackage, Set<RefPackage> newDependents) {
final LinkedList<RefPackage> pendingPackages =
new LinkedList<>();
final Set<RefPackage> processedPackages = new HashSet<>();
pendingPackages.addLast(refPackage);
final Set<RefPackage> processedPackages = new HashSet<>();
while (!pendingPackages.isEmpty()) {
final RefPackage packageToProcess = pendingPackages.removeFirst();
newDependents.add(packageToProcess);
@@ -279,7 +267,7 @@ public class DependencyUtils {
newDependents.remove(refPackage);
}
public static Set<RefPackage> calculateTransitiveDependenciesForPackage(
static Set<RefPackage> calculateTransitiveDependenciesForPackage(
RefPackage refPackage) {
final Set<RefPackage> dependencies =
refPackage.getUserData(TRANSITIVE_DEPENDENCY_PACKAGES_KEY);
@@ -297,8 +285,8 @@ public class DependencyUtils {
RefPackage refPackage, Set<RefPackage> newDependencies) {
final LinkedList<RefPackage> pendingPackages =
new LinkedList<>();
final Set<RefPackage> processedPackages = new HashSet<>();
pendingPackages.addLast(refPackage);
final Set<RefPackage> processedPackages = new HashSet<>();
while (!pendingPackages.isEmpty()) {
final RefPackage packageToProcess = pendingPackages.removeFirst();
newDependencies.add(packageToProcess);
@@ -64,9 +64,6 @@ class InitializationDependencyUtils {
}
}
final List<RefEntity> children = element.getChildren();
if (children == null) {
return;
}
for (RefEntity child : children) {
if (child instanceof RefElement) {
tabulateInitializationDependencyClasses((RefElement)child,
@@ -138,9 +135,6 @@ class InitializationDependencyUtils {
}
}
final List<RefEntity> children = element.getChildren();
if (children == null) {
return;
}
for (RefEntity child : children) {
if (child instanceof RefElement) {
tabulateInitializationDependentClasses((RefElement)child,
@@ -54,9 +54,6 @@ public class ModuleWithTooFewClassesInspection extends BaseGlobalInspection {
}
final RefModule refModule = (RefModule)refEntity;
final List<RefEntity> children = refModule.getChildren();
if (children == null) {
return null;
}
int numClasses = 0;
for (RefEntity child : children) {
if (child instanceof RefClass) {
@@ -54,9 +54,6 @@ public class ModuleWithTooManyClassesInspection extends BaseGlobalInspection {
return null;
}
final List<RefEntity> children = refEntity.getChildren();
if (children == null) {
return null;
}
int numClasses = 0;
for (RefEntity child : children) {
if (child instanceof RefClass) {
@@ -55,9 +55,6 @@ public class DisjointPackageInspection extends BaseGlobalInspection {
}
final RefPackage refPackage = (RefPackage)refEntity;
final List<RefEntity> children = refPackage.getChildren();
if (children == null) {
return null;
}
final Set<RefClass> childClasses = new HashSet<>();
for (RefEntity child : children) {
if (!(child instanceof RefClass)) {
@@ -51,9 +51,6 @@ public class PackageInMultipleModulesInspection extends BaseGlobalInspection {
return null;
}
final List<RefEntity> children = refEntity.getChildren();
if (children == null) {
return null;
}
final Set<RefModule> modules = new HashSet<>();
for (RefEntity child : children) {
if (!(child instanceof RefClass)) {
@@ -54,9 +54,6 @@ public class PackageWithTooFewClassesInspection extends BaseGlobalInspection {
return null;
}
final List<RefEntity> children = refEntity.getChildren();
if (children == null) {
return null;
}
int numClasses = 0;
boolean subpackage = false;
for (RefEntity child : children) {
@@ -54,9 +54,6 @@ public class PackageWithTooManyClassesInspection extends BaseGlobalInspection {
return null;
}
final List<RefEntity> children = refEntity.getChildren();
if (children == null) {
return null;
}
int numClasses = 0;
for (RefEntity child : children) {
if (child instanceof RefClass) {