Java: use utility method

GitOrigin-RevId: d0d9531e2d33d6e1c03a92e9986baf28973df892
This commit is contained in:
Bas Leijdekkers
2025-04-09 16:34:52 +00:00
committed by intellij-monorepo-bot
parent b0fd95a45a
commit 9d68cc1b4a
2 changed files with 16 additions and 33 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.compiler.chainsSearch.context;
import com.intellij.compiler.CompilerReferenceService;
@@ -114,7 +114,7 @@ public final class ChainCompletionContext {
}
public @NotNull Set<PsiType> getContextTypes() {
return myContextElements.stream().map(ChainCompletionContext::getType).collect(Collectors.toSet());
return myContextElements.stream().map(PsiUtil::getTypeByPsiElement).collect(Collectors.toSet());
}
public @NotNull Set<CompilerRef> getContextClassReferences() {
@@ -135,7 +135,7 @@ public final class ChainCompletionContext {
public @Nullable PsiNamedElement getQualifierIfPresent(@NotNull PsiType targetType) {
return DumbService.getInstance(myProject).computeWithAlternativeResolveEnabled(() -> ContainerUtil.find(myContextElements, e -> {
PsiType elementType = getType(e);
PsiType elementType = PsiUtil.getTypeByPsiElement(e);
return elementType != null && targetType.isAssignableFrom(elementType);
}));
}
@@ -194,12 +194,12 @@ public final class ChainCompletionContext {
private static @NotNull Set<? extends PsiVariable> getEnclosingLocalVariables(@NotNull PsiElement place) {
Set<PsiLocalVariable> result = new HashSet<>();
if (place instanceof PsiLocalVariable) result.add((PsiLocalVariable)place);
if (place instanceof PsiLocalVariable variable) result.add(variable);
PsiElement parent = place.getParent();
while (parent != null) {
if (parent instanceof PsiFileSystemItem) break;
if (parent instanceof PsiLocalVariable && PsiTreeUtil.isAncestor(((PsiLocalVariable)parent).getInitializer(), place, false)) {
result.add((PsiLocalVariable)parent);
if (parent instanceof PsiLocalVariable variable && PsiTreeUtil.isAncestor(variable.getInitializer(), place, false)) {
result.add(variable);
}
parent = parent.getParent();
}
@@ -233,10 +233,10 @@ public final class ChainCompletionContext {
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
if ((!(element instanceof PsiMethod) || PropertyUtilBase.isSimplePropertyAccessor((PsiMethod)element)) &&
if ((!(element instanceof PsiMethod method) || PropertyUtilBase.isSimplePropertyAccessor(method)) &&
(!(element instanceof PsiVariable) || !myExcludedVariables.contains(element)) &&
(!(element instanceof PsiMember) || myResolveHelper.isAccessible((PsiMember)element, myPlace, null))) {
PsiType type = getType(element);
(!(element instanceof PsiMember member) || myResolveHelper.isAccessible(member, myPlace, null))) {
PsiType type = PsiUtil.getTypeByPsiElement(element);
if (type == null) {
return true;
}
@@ -250,7 +250,7 @@ public final class ChainCompletionContext {
@Override
public <T> T getHint(@NotNull Key<T> hintKey) {
if (hintKey == ElementClassHint.KEY) {
if (hintKey == KEY) {
//noinspection unchecked
return (T)this;
}
@@ -263,31 +263,21 @@ public final class ChainCompletionContext {
}
}
private static @Nullable PsiType getType(PsiElement element) {
if (element instanceof PsiVariable) {
return ((PsiVariable)element).getType();
}
if (element instanceof PsiMethod) {
return ((PsiMethod)element).getReturnType();
}
return null;
}
public static boolean isWidelyUsed(@NotNull PsiType type) {
type = type.getDeepComponentType();
if (type instanceof PsiPrimitiveType) {
return true;
}
if (!(type instanceof PsiClassType)) {
if (!(type instanceof PsiClassType classType)) {
return false;
}
String className = ((PsiClassType)type).getClassName();
String className = classType.getClassName();
if (className != null && WIDELY_USED_SHORT_NAMES.contains(className)) {
return false;
}
final PsiClass resolvedClass = ((PsiClassType)type).resolve();
final PsiClass resolvedClass = classType.resolve();
if (resolvedClass == null) return false;
final String qName = resolvedClass.getQualifiedName();
if (qName == null) return false;
@@ -22,6 +22,7 @@ import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiFormatUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.testFramework.LightPlatformTestCase;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.PsiTestUtil;
@@ -31,7 +32,6 @@ import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MostlySingularMultiMap;
import com.intellij.xml.util.XmlUtil;
import com.siyeh.ig.psiutils.ClassUtils;
import junit.framework.AssertionFailedError;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
@@ -41,6 +41,7 @@ import org.jetbrains.idea.eclipse.util.PathUtil;
import javax.xml.bind.annotation.XmlElement;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
public class ExternalAnnotationsManagerTest extends LightPlatformTestCase {
@@ -185,15 +186,7 @@ public class ExternalAnnotationsManagerTest extends LightPlatformTestCase {
}
private static @NotNull PsiType getType(@NotNull PsiModifierListOwner listOwner) {
if (listOwner instanceof PsiMethod m) {
return m.getReturnType();
}
else if (listOwner instanceof PsiVariable f) {
return f.getType();
}
else {
throw new AssertionFailedError("" + listOwner);
}
return Objects.requireNonNull(PsiUtil.getTypeByPsiElement(listOwner), () -> String.valueOf(listOwner));
}
private static String validatePath(String pathString, PsiType type) {