Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.intellij.java.language.psi.util.InheritanceUtil;
import com.intellij.java.language.psi.util.PsiUtil;
import com.siyeh.ig.psiutils.MethodCallUtils;
import consulo.annotation.access.RequiredReadAction;
import consulo.java.language.module.util.JavaClassNames;
import consulo.language.psi.PsiElement;
import consulo.util.collection.ArrayUtil;
import consulo.util.lang.ObjectUtil;
Expand Down Expand Up @@ -220,7 +222,7 @@ class Simple implements CallMatcher {
static final Simple ENUM_VALUES =
new Simple("", Collections.singleton("values"), ArrayUtil.EMPTY_STRING_ARRAY, CallType.ENUM_STATIC);
static final Simple ENUM_VALUE_OF =
new Simple("", Collections.singleton("valueOf"), new String[]{CommonClassNames.JAVA_LANG_STRING}, CallType.ENUM_STATIC);
new Simple("", Collections.singleton("valueOf"), new String[]{JavaClassNames.JAVA_LANG_STRING}, CallType.ENUM_STATIC);
@Nonnull
private final String myClassName;
@Nonnull
Expand Down Expand Up @@ -283,6 +285,7 @@ private static boolean parameterTypeMatches(String type, PsiParameter parameter)

@Contract(pure = true)
@Override
@RequiredReadAction
public boolean methodReferenceMatches(PsiMethodReferenceExpression methodRef) {
if (methodRef == null) {
return false;
Expand Down Expand Up @@ -351,8 +354,8 @@ public boolean methodMatches(PsiMethod method) {
if (aClass == null) {
return false;
}
return myCallType.matches(aClass, myClassName, method.hasModifierProperty(PsiModifier.STATIC)) &&
parametersMatch(method.getParameterList());
return myCallType.matches(aClass, myClassName, method.isStatic())
&& parametersMatch(method.getParameterList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,24 @@
import com.intellij.java.language.psi.util.InheritanceUtil;
import com.siyeh.ig.callMatcher.CallMatcher;
import consulo.java.language.module.util.JavaClassNames;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NonNls;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import org.jetbrains.annotations.Contract;

import java.util.*;

public class CollectionUtils {

/**
* @noinspection StaticCollection
*/
@NonNls
private static final Set<String> s_allCollectionClassesAndInterfaces;
/**
* @noinspection StaticCollection
*/
@NonNls
private static final Map<String, String> s_interfaceForCollection = new HashMap<>();

static {
final Set<String> allCollectionClassesAndInterfaces = new HashSet<>();
Set<String> allCollectionClassesAndInterfaces = new HashSet<>();
allCollectionClassesAndInterfaces.add("java.util.AbstractCollection");
allCollectionClassesAndInterfaces.add("java.util.AbstractList");
allCollectionClassesAndInterfaces.add("java.util.AbstractMap");
Expand Down Expand Up @@ -123,7 +118,7 @@ public class CollectionUtils {
s_interfaceForCollection.put("WeakHashMap", "Map");
s_interfaceForCollection.put(JavaClassNames.JAVA_UTIL_ARRAY_LIST, JavaClassNames.JAVA_UTIL_LIST);
s_interfaceForCollection.put("java.util.EnumMap", JavaClassNames.JAVA_UTIL_MAP);
s_interfaceForCollection.put("java.util.EnumSet", JavaClassNames.JAVA_UTIL_SET);
s_interfaceForCollection.put(JavaClassNames.JAVA_UTIL_ENUM_SET, JavaClassNames.JAVA_UTIL_SET);
s_interfaceForCollection.put(JavaClassNames.JAVA_UTIL_HASH_MAP, JavaClassNames.JAVA_UTIL_MAP);
s_interfaceForCollection.put(JavaClassNames.JAVA_UTIL_HASH_SET, JavaClassNames.JAVA_UTIL_SET);
s_interfaceForCollection.put("java.util.Hashtable", JavaClassNames.JAVA_UTIL_MAP);
Expand All @@ -150,7 +145,7 @@ public class CollectionUtils {
* Matches a call which creates collection of the same size as the qualifier collection
*/
public static final CallMatcher DERIVED_COLLECTION = CallMatcher.anyOf(
CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_MAP, "keySet", "values", "entrySet").parameterCount(0),
CallMatcher.instanceCall(JavaClassNames.JAVA_UTIL_MAP, "keySet", "values", "entrySet").parameterCount(0),
CallMatcher.instanceCall("java.util.NavigableMap", "descendingKeySet", "descendingMap", "navigableKeySet")
.parameterCount(0),
CallMatcher.instanceCall("java.util.NavigableSet", "descendingSet").parameterCount(0)
Expand All @@ -167,11 +162,10 @@ public static Set<String> getAllCollectionNames() {

@Contract("null -> false")
public static boolean isConcreteCollectionClass(@Nullable PsiType type) {
if (!(type instanceof PsiClassType)) {
if (!(type instanceof PsiClassType classType)) {
return false;
}
final PsiClassType classType = (PsiClassType)type;
final PsiClass resolved = classType.resolve();
PsiClass resolved = classType.resolve();
if (resolved == null) {
return false;
}
Expand All @@ -180,30 +174,29 @@ public static boolean isConcreteCollectionClass(@Nullable PsiType type) {

@Contract("null -> false")
public static boolean isConcreteCollectionClass(PsiClass aClass) {
if (aClass == null || aClass.isEnum() || aClass.isInterface() || aClass.isAnnotationType() || aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
if (aClass == null || aClass.isEnum() || aClass.isInterface() || aClass.isAnnotationType() || aClass.isAbstract()) {
return false;
}
if (!InheritanceUtil.isInheritor(aClass, JavaClassNames.JAVA_UTIL_COLLECTION)
&& !InheritanceUtil.isInheritor(aClass, JavaClassNames.JAVA_UTIL_MAP)) {
return false;
}
@NonNls final String name = aClass.getQualifiedName();
String name = aClass.getQualifiedName();
return name != null && name.startsWith("java.util.");
}

public static boolean isCollectionClassOrInterface(@Nullable PsiType type) {
if (!(type instanceof PsiClassType)) {
if (!(type instanceof PsiClassType classType)) {
return false;
}
final PsiClassType classType = (PsiClassType)type;
final PsiClass resolved = classType.resolve();
PsiClass resolved = classType.resolve();
if (resolved == null) {
return false;
}
return InheritanceUtil.isInheritor(resolved, JavaClassNames.JAVA_UTIL_COLLECTION) ||
InheritanceUtil.isInheritor(resolved, JavaClassNames.JAVA_UTIL_MAP) ||
InheritanceUtil.isInheritor(resolved, "com.google.common.collect.Multimap") ||
InheritanceUtil.isInheritor(resolved, "com.google.common.collect.Table");
return InheritanceUtil.isInheritor(resolved, JavaClassNames.JAVA_UTIL_COLLECTION)
|| InheritanceUtil.isInheritor(resolved, JavaClassNames.JAVA_UTIL_MAP)
|| InheritanceUtil.isInheritor(resolved, "com.google.common.collect.Multimap")
|| InheritanceUtil.isInheritor(resolved, "com.google.common.collect.Table");
}

public static boolean isCollectionClassOrInterface(PsiClass aClass) {
Expand All @@ -218,11 +211,11 @@ private static boolean isCollectionClassOrInterface(PsiClass aClass, Set<PsiClas
if (!visitedClasses.add(aClass)) {
return false;
}
final String className = aClass.getQualifiedName();
String className = aClass.getQualifiedName();
if (s_allCollectionClassesAndInterfaces.contains(className)) {
return true;
}
final PsiClass[] supers = aClass.getSupers();
PsiClass[] supers = aClass.getSupers();
for (PsiClass aSuper : supers) {
if (isCollectionClassOrInterface(aSuper, visitedClasses)) {
return true;
Expand All @@ -235,29 +228,25 @@ public static boolean isWeakCollectionClass(@Nullable PsiType type) {
if (!(type instanceof PsiClassType)) {
return false;
}
final String typeText = type.getCanonicalText();
String typeText = type.getCanonicalText();
return "java.util.WeakHashMap".equals(typeText);
}

public static boolean isConstantEmptyArray(@Nonnull PsiField field) {
if (!field.hasModifierProperty(PsiModifier.STATIC) || !field.hasModifierProperty(PsiModifier.FINAL)) {
return false;
}
return isEmptyArray(field);
return field.isStatic() && field.isFinal() && isEmptyArray(field);
}

public static boolean isEmptyArray(PsiVariable variable) {
final PsiExpression initializer = variable.getInitializer();
if (initializer instanceof PsiArrayInitializerExpression) {
final PsiArrayInitializerExpression arrayInitializerExpression = (PsiArrayInitializerExpression)initializer;
final PsiExpression[] initializers = arrayInitializerExpression.getInitializers();
PsiExpression initializer = variable.getInitializer();
if (initializer instanceof PsiArrayInitializerExpression arrayInitializerExpr) {
PsiExpression[] initializers = arrayInitializerExpr.getInitializers();
return initializers.length == 0;
}
return ConstructionUtils.isEmptyArrayInitializer(initializer);
}

public static boolean isArrayOrCollectionField(@Nonnull PsiField field) {
final PsiType type = field.getType();
PsiType type = field.getType();
if (isCollectionClassOrInterface(type)) {
return true;
}
Expand All @@ -269,14 +258,8 @@ public static boolean isArrayOrCollectionField(@Nonnull PsiField field) {
}

public static String getInterfaceForClass(String name) {
final int parameterStart = name.indexOf((int)'<');
final String baseName;
if (parameterStart >= 0) {
baseName = name.substring(0, parameterStart).trim();
}
else {
baseName = name;
}
int parameterStart = name.indexOf((int)'<');
String baseName = parameterStart >= 0 ? name.substring(0, parameterStart).trim() : name;
return s_interfaceForCollection.get(baseName);
}
}
Loading
Loading