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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,48 @@
import com.intellij.java.language.psi.*;
import com.intellij.java.language.psi.util.PsiFormatUtil;
import com.intellij.java.language.psi.util.PsiFormatUtilBase;
import consulo.language.LangBundle;
import consulo.annotation.access.RequiredReadAction;
import consulo.language.localize.LanguageLocalize;
import consulo.language.psi.PsiDirectory;
import consulo.language.psi.PsiElement;
import consulo.language.psi.PsiFile;

import consulo.localize.LocalizeValue;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;

public class HighlightMessageUtil {
private HighlightMessageUtil() {
}
private static final LocalizeValue QUESTION_MARK = LocalizeValue.of("?");

@Nullable
public static String getSymbolName(@Nonnull PsiElement symbol, PsiSubstitutor substitutor) {
String symbolName = null;

if (symbol instanceof PsiClass) {
if (symbol instanceof PsiAnonymousClass) {
symbolName = LangBundle.message("java.terms.anonymous.class");
} else {
symbolName = ((PsiClass) symbol).getQualifiedName();
if (symbolName == null) {
symbolName = ((PsiClass) symbol).getName();
}
}
} else if (symbol instanceof PsiMethod) {
symbolName = PsiFormatUtil.formatMethod((PsiMethod) symbol,
substitutor, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS,
PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_FQ_CLASS_NAMES);
} else if (symbol instanceof PsiVariable) {
symbolName = ((PsiVariable) symbol).getName();
} else if (symbol instanceof PsiJavaPackage) {
symbolName = ((PsiJavaPackage) symbol).getQualifiedName();
} else if (symbol instanceof PsiFile) {
PsiDirectory directory = ((PsiFile) symbol).getContainingDirectory();
PsiJavaPackage aPackage = directory == null ? null : JavaDirectoryService.getInstance().getPackage(directory);
symbolName = aPackage == null ? null : aPackage.getQualifiedName();
} else if (symbol instanceof PsiDirectory) {
symbolName = ((PsiDirectory) symbol).getName();
private HighlightMessageUtil() {
}

return symbolName;
}
@Nonnull
@RequiredReadAction
public static LocalizeValue getSymbolName(@Nonnull PsiElement symbol, PsiSubstitutor substitutor) {
return switch (symbol) {
case PsiAnonymousClass ac -> LanguageLocalize.javaTermsAnonymousClass();
case PsiClass c -> {
String n = c.getQualifiedName();
if (n == null) {
n = c.getName();
}
yield LocalizeValue.of(n);
}
case PsiMethod m -> LocalizeValue.of(PsiFormatUtil.formatMethod(
m,
substitutor,
PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS,
PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_FQ_CLASS_NAMES
));
case PsiVariable v -> LocalizeValue.of(v.getName());
case PsiJavaPackage jp -> LocalizeValue.of(jp.getQualifiedName());
case PsiFile f -> {
PsiDirectory directory = f.getContainingDirectory();
PsiJavaPackage aPackage = directory == null ? null : JavaDirectoryService.getInstance().getPackage(directory);
yield aPackage == null ? QUESTION_MARK : LocalizeValue.of(aPackage.getQualifiedName());
}
case PsiDirectory d -> LocalizeValue.of(d.getName());
default -> QUESTION_MARK;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,10 @@ public static HighlightInfo checkMethodCall(
}
else if (candidateInfo != null && !candidateInfo.isApplicable()) {
if (candidateInfo.isTypeArgumentsApplicable()) {
String methodName = HighlightMessageUtil.getSymbolName(resolved, substitutor);
LocalizeValue methodName = HighlightMessageUtil.getSymbolName(resolved, substitutor);
PsiElement parent = resolved.getParent();
String containerName = parent == null ? "" : HighlightMessageUtil.getSymbolName(parent, substitutor);
LocalizeValue containerName =
parent == null ? LocalizeValue.empty() : HighlightMessageUtil.getSymbolName(parent, substitutor);
String argTypes = buildArgTypesList(list);
String description = JavaErrorLocalize.wrongMethodArguments(methodName, containerName, argTypes).get();
SimpleReference<PsiElement> elementToHighlight = SimpleReference.create(list);
Expand Down Expand Up @@ -2192,8 +2193,8 @@ else if (classReference != null && (!result.isAccessible()
holder.add(buildAccessProblem(classReference, result, constructor));
}
else if (!applicable) {
String constructorName = HighlightMessageUtil.getSymbolName(constructor, result.getSubstitutor());
String containerName = HighlightMessageUtil.getSymbolName(constructor.getContainingClass(), result.getSubstitutor());
LocalizeValue constructorName = HighlightMessageUtil.getSymbolName(constructor, result.getSubstitutor());
LocalizeValue containerName = HighlightMessageUtil.getSymbolName(constructor.getContainingClass(), result.getSubstitutor());
String argTypes = buildArgTypesList(list);
String toolTip = createMismatchedArgumentsHtmlTooltip(result, list);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2193,9 +2193,10 @@ private static boolean resolvesToImmediateSuperInterface(
}

@Nonnull
@RequiredReadAction
public static LocalizeValue buildProblemWithStaticDescription(@Nonnull PsiElement refElement) {
String type = FindUsagesProvider.forLanguage(JavaLanguage.INSTANCE).getType(refElement);
String name = HighlightMessageUtil.getSymbolName(refElement, PsiSubstitutor.EMPTY);
LocalizeValue name = HighlightMessageUtil.getSymbolName(refElement, PsiSubstitutor.EMPTY);
return JavaErrorLocalize.nonStaticSymbolReferencedFromStaticContext(type, name);
}

Expand Down Expand Up @@ -2268,14 +2269,14 @@ private static LocalizeValue buildProblemWithAccessDescription(
) {
assert resolved instanceof PsiModifierListOwner : resolved;
PsiModifierListOwner refElement = (PsiModifierListOwner)resolved;
String symbolName = HighlightMessageUtil.getSymbolName(refElement, result.getSubstitutor());
LocalizeValue symbolName = HighlightMessageUtil.getSymbolName(refElement, result.getSubstitutor());

if (refElement.hasModifierProperty(PsiModifier.PRIVATE)) {
String containerName = getContainerName(refElement, result.getSubstitutor());
LocalizeValue containerName = getContainerName(refElement, result.getSubstitutor());
return JavaErrorLocalize.privateSymbol(symbolName, containerName);
}
else if (refElement.hasModifierProperty(PsiModifier.PROTECTED)) {
String containerName = getContainerName(refElement, result.getSubstitutor());
LocalizeValue containerName = getContainerName(refElement, result.getSubstitutor());
return JavaErrorLocalize.protectedSymbol(symbolName, containerName);
}
else {
Expand All @@ -2285,11 +2286,11 @@ else if (refElement.hasModifierProperty(PsiModifier.PROTECTED)) {
symbolName = HighlightMessageUtil.getSymbolName(refElement, result.getSubstitutor());
}
if (refElement.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) || packageLocalClass != null) {
String containerName = getContainerName(refElement, result.getSubstitutor());
LocalizeValue containerName = getContainerName(refElement, result.getSubstitutor());
return JavaErrorLocalize.packageLocalSymbol(symbolName, containerName);
}
else {
String containerName = getContainerName(refElement, result.getSubstitutor());
LocalizeValue containerName = getContainerName(refElement, result.getSubstitutor());
return JavaErrorLocalize.visibilityAccessProblem(symbolName, containerName);
}
}
Expand All @@ -2305,9 +2306,9 @@ private static PsiElement getContainer(PsiModifierListOwner refElement) {
return refElement.getParent();
}

private static String getContainerName(PsiModifierListOwner refElement, PsiSubstitutor substitutor) {
PsiElement container = getContainer(refElement);
return container == null ? "?" : HighlightMessageUtil.getSymbolName(container, substitutor);
@RequiredReadAction
private static LocalizeValue getContainerName(PsiModifierListOwner refElement, PsiSubstitutor substitutor) {
return HighlightMessageUtil.getSymbolName(getContainer(refElement), substitutor);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,7 @@ public void visitResourceExpression(@Nonnull PsiResourceExpression resource) {
}

@Override
@RequiredReadAction
public void visitTypeElement(@Nonnull PsiTypeElement type) {
if (!myHolder.hasErrorResults()) {
myHolder.add(HighlightUtil.checkIllegalType(type));
Expand All @@ -2054,7 +2055,8 @@ public void visitTypeElement(@Nonnull PsiTypeElement type) {
}

@Override
public void visitTypeCastExpression(PsiTypeCastExpression typeCast) {
@RequiredReadAction
public void visitTypeCastExpression(@Nonnull PsiTypeCastExpression typeCast) {
super.visitTypeCastExpression(typeCast);
try {
if (!myHolder.hasErrorResults()) {
Expand All @@ -2069,6 +2071,7 @@ public void visitTypeCastExpression(PsiTypeCastExpression typeCast) {
}

@Override
@RequiredReadAction
public void visitTypeParameterList(PsiTypeParameterList list) {
PsiTypeParameter[] typeParameters = list.getTypeParameters();
if (typeParameters.length > 0) {
Expand All @@ -2080,7 +2083,8 @@ public void visitTypeParameterList(PsiTypeParameterList list) {
}

@Override
public void visitVariable(PsiVariable variable) {
@RequiredReadAction
public void visitVariable(@Nonnull PsiVariable variable) {
super.visitVariable(variable);
try {
if (!myHolder.hasErrorResults()) {
Expand Down Expand Up @@ -2109,20 +2113,21 @@ private boolean isReassigned(@Nonnull PsiVariable variable) {
}

@Override
public void visitConditionalExpression(PsiConditionalExpression expression) {
@RequiredReadAction
public void visitConditionalExpression(@Nonnull PsiConditionalExpression expression) {
super.visitConditionalExpression(expression);
if (myLanguageLevel.isAtLeast(LanguageLevel.JDK_1_8) && PsiPolyExpressionUtil.isPolyExpression(expression)) {
final PsiExpression thenExpression = expression.getThenExpression();
final PsiExpression elseExpression = expression.getElseExpression();
PsiExpression thenExpression = expression.getThenExpression();
PsiExpression elseExpression = expression.getElseExpression();
if (thenExpression != null && elseExpression != null) {
final PsiType conditionalType = expression.getType();
PsiType conditionalType = expression.getType();
if (conditionalType != null) {
final PsiExpression[] sides = {
PsiExpression[] sides = {
thenExpression,
elseExpression
};
for (PsiExpression side : sides) {
final PsiType sideType = side.getType();
PsiType sideType = side.getType();
if (sideType != null && !TypeConversionUtil.isAssignable(conditionalType, sideType)) {
myHolder.add(HighlightUtil.checkAssignability(conditionalType, sideType, side, side));
}
Expand Down
Loading
Loading