diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java index 7fe0f099fa53..a8a9d44df735 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java @@ -28,6 +28,7 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.openapi.projectRoots.JavaSdkVersion; import com.intellij.openapi.util.Comparing; +import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; @@ -46,18 +47,17 @@ import com.intellij.util.containers.MostlySingularMultiMap; import com.intellij.util.ui.UIUtil; import com.intellij.xml.util.XmlStringUtil; import org.intellij.lang.annotations.Language; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.text.MessageFormat; import java.util.*; +import static com.intellij.openapi.util.Pair.pair; + /** - * Highlight method problems - * * @author cdr - * Date: Aug 14, 2002 + * @since Aug 14, 2002 */ public class HighlightMethodUtil { private static final QuickFixFactory QUICK_FIX_FACTORY = QuickFixFactory.getInstance(); @@ -67,12 +67,21 @@ public class HighlightMethodUtil { private HighlightMethodUtil() { } static String createClashMethodMessage(PsiMethod method1, PsiMethod method2, boolean showContainingClasses) { - @NonNls String pattern = showContainingClasses ? "clash.methods.message.show.classes" : "clash.methods.message"; - return JavaErrorMessages.message(pattern, + if (showContainingClasses) { + PsiClass class1 = method1.getContainingClass(); + PsiClass class2 = method2.getContainingClass(); + if (class1 != null && class2 != null) { + return JavaErrorMessages.message("clash.methods.message.show.classes", + JavaHighlightUtil.formatMethod(method1), + JavaHighlightUtil.formatMethod(method2), + HighlightUtil.formatClass(class1), + HighlightUtil.formatClass(class2)); + } + } + + return JavaErrorMessages.message("clash.methods.message", JavaHighlightUtil.formatMethod(method1), - JavaHighlightUtil.formatMethod(method2), - HighlightUtil.formatClass(method1.getContainingClass()), - HighlightUtil.formatClass(method2.getContainingClass())); + JavaHighlightUtil.formatMethod(method2)); } static HighlightInfo checkMethodWeakerPrivileges(@NotNull MethodSignatureBackedByPsiMethod methodSignature, @@ -95,34 +104,37 @@ public class HighlightMethodUtil { return null; } - private static HighlightInfo isWeaker(final PsiMethod method, final PsiModifierList modifierList, final String accessModifier, final int accessLevel, - final PsiMethod superMethod, - final boolean includeRealPositionInfo) { + private static HighlightInfo isWeaker(PsiMethod method, + PsiModifierList modifierList, + String accessModifier, + int accessLevel, + PsiMethod superMethod, + boolean includeRealPositionInfo) { int superAccessLevel = PsiUtil.getAccessLevel(superMethod.getModifierList()); if (accessLevel < superAccessLevel) { String description = JavaErrorMessages.message("weaker.privileges", createClashMethodMessage(method, superMethod, true), VisibilityUtil.toPresentableText(accessModifier), PsiUtil.getAccessModifier(superAccessLevel)); - TextRange textRange; + TextRange textRange = TextRange.EMPTY_RANGE; if (includeRealPositionInfo) { PsiElement keyword = PsiUtil.findModifierInList(modifierList, accessModifier); - if (keyword == null) { - // in case of package-private or some crazy third-party plugin where some access modifier implied even if it's absent - textRange = method.getNameIdentifier().getTextRange(); - } - else { + if (keyword != null) { textRange = keyword.getTextRange(); } + else { + // in case of package-private or some crazy third-party plugin where some access modifier implied even if it's absent + PsiIdentifier identifier = method.getNameIdentifier(); + if (identifier != null) { + textRange = identifier.getTextRange(); + } + } } - else { - textRange = TextRange.EMPTY_RANGE; - } - HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create(); - QuickFixAction.registerQuickFixAction(highlightInfo, - QUICK_FIX_FACTORY.createModifierListFix(method, PsiUtil.getAccessModifier(superAccessLevel), true, false)); - return highlightInfo; + HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create(); + QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createModifierListFix(method, PsiUtil.getAccessModifier(superAccessLevel), true, false)); + return info; } + return null; } @@ -149,12 +161,19 @@ public class HighlightMethodUtil { if (returnType == null || superReturnType == null || method == superMethod) continue; PsiClass superClass = superMethod.getContainingClass(); if (superClass == null) continue; - TextRange toHighlight = textRange != null ? textRange - : includeRealPositionInfo ? method.getReturnTypeElement().getTextRange() : TextRange.EMPTY_RANGE; - HighlightInfo highlightInfo = checkSuperMethodSignature(superMethod, superMethodSignature, superReturnType, method, methodSignature, - returnType, JavaErrorMessages.message("incompatible.return.type"), - toHighlight, PsiUtil.getLanguageLevel(aClass)); - if (highlightInfo != null) return highlightInfo; + if (textRange == null && includeRealPositionInfo) { + PsiTypeElement typeElement = method.getReturnTypeElement(); + if (typeElement != null) { + textRange = typeElement.getTextRange(); + } + } + if (textRange == null) { + textRange = TextRange.EMPTY_RANGE; + } + HighlightInfo info = checkSuperMethodSignature( + superMethod, superMethodSignature, superReturnType, method, methodSignature, returnType, + JavaErrorMessages.message("incompatible.return.type"), textRange, PsiUtil.getLanguageLevel(aClass)); + if (info != null) return info; } return null; @@ -239,10 +258,11 @@ public class HighlightMethodUtil { private static HighlightInfo checkSuperMethodIsFinal(PsiMethod method, PsiMethod superMethod) { // strange things happen when super method is from Object and method from interface if (superMethod.hasModifierProperty(PsiModifier.FINAL)) { + PsiClass superClass = superMethod.getContainingClass(); String description = JavaErrorMessages.message("final.method.override", - JavaHighlightUtil.formatMethod(method), - JavaHighlightUtil.formatMethod(superMethod), - HighlightUtil.formatClass(superMethod.getContainingClass())); + JavaHighlightUtil.formatMethod(method), + JavaHighlightUtil.formatMethod(superMethod), + superClass != null ? HighlightUtil.formatClass(superClass) : ""); TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method); HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create(); QuickFixAction.registerQuickFixAction(errorResult, @@ -253,8 +273,9 @@ public class HighlightMethodUtil { } static HighlightInfo checkMethodIncompatibleThrows(MethodSignatureBackedByPsiMethod methodSignature, - List superMethodSignatures, - boolean includeRealPositionInfo, PsiClass analyzedClass) { + List superMethodSignatures, + boolean includeRealPositionInfo, + PsiClass analyzedClass) { PsiMethod method = methodSignature.getMethod(); PsiClass aClass = method.getContainingClass(); if (aClass == null) return null; @@ -276,7 +297,7 @@ public class HighlightMethodUtil { if (exception == null) { LOG.error("throws: " + method.getThrowsList().getText() + "; method: " + method); } - if (!ExceptionUtil.isUncheckedException(exception)) { + else if (!ExceptionUtil.isUncheckedException(exception)) { checkedExceptions.add(exception); if (includeRealPositionInfo && i < referenceElements.length) { PsiJavaCodeReferenceElement exceptionRef = referenceElements[i]; @@ -410,6 +431,7 @@ public class HighlightMethodUtil { } else if (candidateInfo != null && !candidateInfo.isApplicable()) { if (candidateInfo.isTypeArgumentsApplicable()) { + assert resolved != null; String methodName = HighlightMessageUtil.getSymbolName(resolved, substitutor); PsiElement parent = resolved.getParent(); String containerName = parent == null ? "" : HighlightMessageUtil.getSymbolName(parent, substitutor); @@ -639,27 +661,11 @@ public class HighlightMethodUtil { @NotNull PsiResolveHelper resolveHelper, @NotNull LanguageLevel languageLevel, @NotNull PsiFile file) { - MethodCandidateInfo methodCandidate1 = null; - MethodCandidateInfo methodCandidate2 = null; - for (JavaResolveResult result : resolveResults) { - if (!(result instanceof MethodCandidateInfo)) continue; - MethodCandidateInfo candidate = (MethodCandidateInfo)result; - if (candidate.isApplicable() && !candidate.getElement().isConstructor()) { - if (methodCandidate1 == null) { - methodCandidate1 = candidate; - } - else { - methodCandidate2 = candidate; - break; - } - } - } + MethodCandidateInfo methodCandidate2 = findCandidates(resolveResults).second; + if (methodCandidate2 != null) return null; MethodCandidateInfo[] candidates = toMethodCandidates(resolveResults); HighlightInfoType highlightInfoType = HighlightInfoType.ERROR; - if (methodCandidate2 != null) { - return null; - } String description; PsiElement elementToHighlight = ObjectUtils.notNull(referenceToMethod.getReferenceNameElement(), referenceToMethod); if (element != null && !resolveResult.isAccessible()) { @@ -723,21 +729,9 @@ public class HighlightMethodUtil { @NotNull PsiMethodCallExpression methodCall, @NotNull PsiResolveHelper resolveHelper, @NotNull PsiElement elementToHighlight) { - MethodCandidateInfo methodCandidate1 = null; - MethodCandidateInfo methodCandidate2 = null; - for (JavaResolveResult result : resolveResults) { - if (!(result instanceof MethodCandidateInfo)) continue; - MethodCandidateInfo candidate = (MethodCandidateInfo)result; - if (candidate.isApplicable() && !candidate.getElement().isConstructor()) { - if (methodCandidate1 == null) { - methodCandidate1 = candidate; - } - else { - methodCandidate2 = candidate; - break; - } - } - } + Pair pair = findCandidates(resolveResults); + MethodCandidateInfo methodCandidate1 = pair.first; + MethodCandidateInfo methodCandidate2 = pair.second; MethodCandidateInfo[] candidates = toMethodCandidates(resolveResults); String description; @@ -803,10 +797,27 @@ public class HighlightMethodUtil { return info; } - @NotNull - private static MethodCandidateInfo[] toMethodCandidates(@NotNull JavaResolveResult[] resolveResults) { - List candidateList = new ArrayList<>(resolveResults.length); + private static Pair findCandidates(JavaResolveResult[] resolveResults) { + MethodCandidateInfo methodCandidate1 = null; + MethodCandidateInfo methodCandidate2 = null; + for (JavaResolveResult result : resolveResults) { + if (!(result instanceof MethodCandidateInfo)) continue; + MethodCandidateInfo candidate = (MethodCandidateInfo)result; + if (candidate.isApplicable() && !candidate.getElement().isConstructor()) { + if (methodCandidate1 == null) { + methodCandidate1 = candidate; + } + else { + methodCandidate2 = candidate; + break; + } + } + } + return pair(methodCandidate1, methodCandidate2); + } + private static MethodCandidateInfo[] toMethodCandidates(JavaResolveResult[] resolveResults) { + List candidateList = new ArrayList<>(resolveResults.length); for (JavaResolveResult result : resolveResults) { if (!(result instanceof MethodCandidateInfo)) continue; MethodCandidateInfo candidate = (MethodCandidateInfo)result; @@ -903,19 +914,17 @@ public class HighlightMethodUtil { PsiMethod method = methodCandidate.getElement(); PsiParameter[] parameters = method.getParameterList().getParameters(); PsiSubstitutor substitutor = methodCandidate.getSubstitutor(); - @NonNls @Language("HTML") String ms = "" + method.getName() + ""; - + StringBuilder ms = new StringBuilder("" + method.getName() + ""); for (int j = 0; j < parameters.length; j++) { PsiParameter parameter = parameters[j]; PsiType type = substitutor.substitute(parameter.getType()); - ms += "" + (j == 0 ? "(" : "") + - XmlStringUtil.escapeString(type.getPresentableText()) - + (j == parameters.length - 1 ? ")" : ",") + ""; + ms.append("").append(j == 0 ? "(" : "").append(XmlStringUtil.escapeString(type.getPresentableText())) + .append(j == parameters.length - 1 ? ")" : ",").append(""); } if (parameters.length == 0) { - ms += "()"; + ms.append("()"); } - return ms; + return ms.toString(); } private static String createMismatchedArgumentsHtmlTooltip(MethodCandidateInfo info, PsiExpressionList list) { @@ -937,12 +946,12 @@ public class HighlightMethodUtil { int cols = Math.max(parameters.length, expressions.length); @Language("HTML") - @NonNls String parensizedName = methodName + (parameters.length == 0 ? "( ) " : ""); + String parenthesizedName = methodName + (parameters.length == 0 ? "( ) " : ""); String errorMessage = info != null ? info.getInferenceErrorMessage() : null; return JavaErrorMessages.message( "argument.mismatch.html.tooltip", cols - parameters.length + 1, - parensizedName, + parenthesizedName, HighlightUtil.formatClass(aClass, false), createMismatchedArgsHtmlTooltipParamsRow(parameters, substitutor, expressions), createMismatchedArgsHtmlTooltipArgumentsRow(expressions, parameters, substitutor, cols), @@ -950,7 +959,7 @@ public class HighlightMethodUtil { ); } - private static String esctrim(@NotNull String s) { + private static String escTrim(@NotNull String s) { return XmlStringUtil.escapeString(trimNicely(s)); } @@ -988,7 +997,6 @@ public class HighlightMethodUtil { : createLongMismatchedArgumentsHtmlTooltip(list, info, parameters, methodName, substitutor, aClass); } - @SuppressWarnings("StringContatenationInLoop") @Language("HTML") private static String createLongMismatchedArgumentsHtmlTooltip(PsiExpressionList list, @Nullable MethodCandidateInfo info, @@ -998,110 +1006,119 @@ public class HighlightMethodUtil { PsiClass aClass) { PsiExpression[] expressions = list.getExpressions(); - @SuppressWarnings("NonConstantStringShouldBeStringBuffer") @NonNls - String s = "" + - ""+ - ""+ - "" - ; - - for (int i = 0; i < Math.max(parameters.length,expressions.length); i++) { + StringBuilder s = new StringBuilder() + .append("
" + - "" + methodName + "() in " + HighlightUtil.formatClass(aClass, false) +" cannot be applied to:" + - "
Expected
Parameters:
Actual
Arguments:

") + .append("") + .append("") + .append(""); + for (int i = 0; i < Math.max(parameters.length, expressions.length); i++) { PsiParameter parameter = i < parameters.length ? parameters[i] : null; PsiExpression expression = i < expressions.length ? expressions[i] : null; boolean showShort = showShortType(i, parameters, expressions, substitutor); - @NonNls String mismatchColor = showShort ? null : UIUtil.isUnderDarcula() ? "FF6B68" : "red"; + String mismatchColor = showShort ? null : UIUtil.isUnderDarcula() ? "FF6B68" : "red"; - s += ""; - s += ""; + s.append(""); - s += ""; + s.append(""); - s += ""; - - s += ""; + s.append(""); + s.append(""); } + s.append("
").append("").append(methodName) + .append("() in ").append(HighlightUtil.formatClass(aClass, false)).append(" cannot be applied to:") + .append("
Expected
Parameters:
Actual
Arguments:

"; + s.append(""); + + s.append(""); if (parameter != null) { String name = parameter.getName(); if (name != null) { - s += esctrim(name) +":"; + s.append(escTrim(name)).append(":"); } } - s += ""; + s.append(""); if (parameter != null) { PsiType type = substitutor.substitute(parameter.getType()); - s += "" + - esctrim(showShort ? type.getPresentableText() : JavaHighlightUtil.formatType(type)) - + "" - ; + s.append(""); + s.append(escTrim(showShort ? type.getPresentableText() : JavaHighlightUtil.formatType(type))); + s.append(""); } - s += ""; + s.append(""); if (expression != null) { PsiType type = expression.getType(); - s += "" + - esctrim(expression.getText()) + "  "+ - (mismatchColor == null || type == null || type == PsiType.NULL ? "" : "("+esctrim(JavaHighlightUtil.formatType(type))+")") - + "" - ; - + s.append(""); + s.append(escTrim(expression.getText())); + s.append("  "); + if (mismatchColor != null && type != null && type != PsiType.NULL) { + s.append("(").append(escTrim(JavaHighlightUtil.formatType(type))).append(")"); + } + s.append(""); } - s += "
"); - s+= ""; - final String errorMessage = info != null ? info.getInferenceErrorMessage() : null; + String errorMessage = info != null ? info.getInferenceErrorMessage() : null; if (errorMessage != null) { - s+= "reason: "; - s += XmlStringUtil.escapeString(errorMessage).replaceAll("\n", "
"); + s.append("reason: ").append(XmlStringUtil.escapeString(errorMessage).replaceAll("\n", "
")); } - s+= ""; - return s; + s.append(""); + + return s.toString(); } - @SuppressWarnings("StringContatenationInLoop") @Language("HTML") - private static String createMismatchedArgsHtmlTooltipArgumentsRow(final PsiExpression[] expressions, final PsiParameter[] parameters, - final PsiSubstitutor substitutor, final int cols) { - @Language("HTML") - - @NonNls String ms = ""; + private static String createMismatchedArgsHtmlTooltipArgumentsRow(PsiExpression[] expressions, + PsiParameter[] parameters, + PsiSubstitutor substitutor, + int cols) { + StringBuilder ms = new StringBuilder(); for (int i = 0; i < expressions.length; i++) { PsiExpression expression = expressions[i]; PsiType type = expression.getType(); - boolean showShort = showShortType(i, parameters, expressions, substitutor); - @NonNls String mismatchColor = showShort ? null : MISMATCH_COLOR; - ms += " " + "" + (i == 0 ? "(" : "") - + "" + - XmlStringUtil.escapeString(showShort ? type.getPresentableText() : JavaHighlightUtil.formatType(type)) - + "" - + (i == expressions.length - 1 ? ")" : ",") + ""; + String mismatchColor = showShort ? null : MISMATCH_COLOR; + ms.append(" " + ""); + ms.append(i == 0 ? "(" : ""); + ms.append(""); + ms.append(XmlStringUtil.escapeString(type != null && showShort ? type.getPresentableText() : JavaHighlightUtil.formatType(type))); + ms.append(""); + ms.append(i == expressions.length - 1 ? ")" : ","); + ms.append(""); } for (int i = expressions.length; i < cols + 1; i++) { - ms += "" + (i == 0 ? "()" : "") + - " "; + ms.append(""); + if (i == 0) ms.append("()"); + ms.append(" "); } - return ms; + return ms.toString(); } - @SuppressWarnings("StringContatenationInLoop") @Language("HTML") - private static String createMismatchedArgsHtmlTooltipParamsRow(final PsiParameter[] parameters, - final PsiSubstitutor substitutor, - final PsiExpression[] expressions) { - @NonNls String ms = ""; + private static String createMismatchedArgsHtmlTooltipParamsRow(PsiParameter[] parameters, + PsiSubstitutor substitutor, + PsiExpression[] expressions) { + StringBuilder ms = new StringBuilder(); for (int i = 0; i < parameters.length; i++) { PsiParameter parameter = parameters[i]; PsiType type = substitutor.substitute(parameter.getType()); - ms += "" + (i == 0 ? "(" : "") + - XmlStringUtil.escapeString(showShortType(i, parameters, expressions, substitutor) - ? type.getPresentableText() - : JavaHighlightUtil.formatType(type)) - + (i == parameters.length - 1 ? ")" : ",") + ""; + boolean showShort = showShortType(i, parameters, expressions, substitutor); + ms.append(""); + ms.append(i == 0 ? "(" : ""); + ms.append(XmlStringUtil.escapeString(showShort ? type.getPresentableText() : JavaHighlightUtil.formatType(type))); + ms.append(i == parameters.length - 1 ? ")" : ","); + ms.append(""); } - return ms; + return ms.toString(); } private static boolean showShortType(int i, @@ -1132,8 +1149,7 @@ public class HighlightMethodUtil { String description = JavaErrorMessages.message("missing.method.body"); errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(start, end).descriptionAndTooltip(description).create(); if (HighlightUtil.getIncompatibleModifier(PsiModifier.ABSTRACT, method.getModifierList()) == null && !(aClass instanceof PsiAnonymousClass)) { - QuickFixAction.registerQuickFixAction(errorResult, - QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.ABSTRACT, true, false)); + QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.ABSTRACT, true, false)); } QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createAddMethodBodyFix(method)); } @@ -1162,24 +1178,22 @@ public class HighlightMethodUtil { return errorResult; } - - static HighlightInfo checkConstructorName(PsiMethod method) { - String methodName = method.getName(); + static HighlightInfo checkConstructorName(@NotNull PsiMethod method) { PsiClass aClass = method.getContainingClass(); - HighlightInfo errorResult = null; - if (aClass != null) { String className = aClass instanceof PsiAnonymousClass ? null : aClass.getName(); - if (className == null || !Comparing.strEqual(methodName, className)) { - PsiElement element = method.getNameIdentifier(); + if (className == null || !Comparing.strEqual(method.getName(), className)) { + PsiElement element = ObjectUtils.notNull(method.getNameIdentifier(), method); String description = JavaErrorMessages.message("missing.return.type"); - errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create(); + HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create(); if (className != null) { - QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createRenameElementFix(method, className)); + QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createRenameElementFix(method, className)); } + return info; } } - return errorResult; + + return null; } @Nullable @@ -1325,7 +1339,10 @@ public class HighlightMethodUtil { if (info != null) { QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createInsertSuperFix(constructor)); QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createInsertThisFix(constructor)); - QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createAddDefaultConstructorFix(aClass.getSuperClass())); + PsiClass superClass = aClass.getSuperClass(); + if (superClass != null) { + QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createAddDefaultConstructorFix(superClass)); + } } return info; } @@ -1335,7 +1352,7 @@ public class HighlightMethodUtil { * @return error if static method overrides instance method or * instance method overrides static. see JLS 8.4.6.1, 8.4.6.2 */ - static HighlightInfo checkStaticMethodOverride(@NotNull PsiMethod method,@NotNull PsiFile containingFile) { + static HighlightInfo checkStaticMethodOverride(@NotNull PsiMethod method, @NotNull PsiFile containingFile) { // constructors are not members and therefor don't override class methods if (method.isConstructor()) { return null; @@ -1354,15 +1371,21 @@ public class HighlightMethodUtil { final PsiMethod superMethod = signature.getMethod(); final PsiClass superClass = superMethod.getContainingClass(); if (superClass == null) continue; - final HighlightInfo highlightInfo = checkStaticMethodOverride(aClass, method, isStatic, superClass, superMethod,containingFile); + final HighlightInfo highlightInfo = checkStaticMethodOverride(aClass, method, isStatic, superClass, superMethod, containingFile); if (highlightInfo != null) { return highlightInfo; } } + return null; } - private static HighlightInfo checkStaticMethodOverride(PsiClass aClass, PsiMethod method, boolean isMethodStatic, PsiClass superClass, PsiMethod superMethod,@NotNull PsiFile containingFile) { + private static HighlightInfo checkStaticMethodOverride(PsiClass aClass, + PsiMethod method, + boolean isMethodStatic, + PsiClass superClass, + PsiMethod superMethod, + PsiFile containingFile) { if (superMethod == null) return null; PsiManager manager = containingFile.getManager(); PsiModifierList superModifierList = superMethod.getModifierList(); @@ -1375,7 +1398,7 @@ public class HighlightMethodUtil { boolean isSuperMethodStatic = superModifierList.hasModifierProperty(PsiModifier.STATIC); if (isMethodStatic != isSuperMethodStatic) { TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method); - @NonNls final String messageKey = isMethodStatic + final String messageKey = isMethodStatic ? "static.method.cannot.override.instance.method" : "instance.method.cannot.override.static.method"; @@ -1410,8 +1433,8 @@ public class HighlightMethodUtil { return null; } - private static HighlightInfo checkInterfaceInheritedMethodsReturnTypes(@NotNull List superMethodSignatures, - @NotNull LanguageLevel languageLevel) { + private static HighlightInfo checkInterfaceInheritedMethodsReturnTypes(List superMethodSignatures, + LanguageLevel languageLevel) { if (superMethodSignatures.size() < 2) return null; final MethodSignatureBackedByPsiMethod[] returnTypeSubstitutable = {superMethodSignatures.get(0)}; for (int i = 1; i < superMethodSignatures.size(); i++) { @@ -1458,6 +1481,7 @@ public class HighlightMethodUtil { boolean appendImplementMethodFix = true; final Collection visibleSignatures = aClass.getVisibleSignatures(); PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper(); + Ultimate: for (HierarchicalMethodSignature signature : visibleSignatures) { PsiMethod method = signature.getMethod(); @@ -1465,8 +1489,8 @@ public class HighlightMethodUtil { List superSignatures = signature.getSuperSignatures(); boolean allAbstracts = method.hasModifierProperty(PsiModifier.ABSTRACT); - final PsiClass containingClass = method.getContainingClass(); - if (aClass.equals(containingClass)) continue; //to be checked at method level + PsiClass containingClass = method.getContainingClass(); + if (containingClass == null || aClass.equals(containingClass)) continue; //to be checked at method level if (aClass.isInterface() && !containingClass.isInterface()) continue; HighlightInfo highlightInfo; @@ -1484,11 +1508,12 @@ public class HighlightMethodUtil { for (HierarchicalMethodSignature superSignature : superSignatures) { PsiMethod superMethod = superSignature.getMethod(); if (!superMethod.hasModifierProperty(PsiModifier.STATIC)) { + PsiClass superClass = superMethod.getContainingClass(); description = JavaErrorMessages.message("static.method.cannot.override.instance.method", - JavaHighlightUtil.formatMethod(method), - HighlightUtil.formatClass(containingClass), - JavaHighlightUtil.formatMethod(superMethod), - HighlightUtil.formatClass(superMethod.getContainingClass())); + JavaHighlightUtil.formatMethod(method), + HighlightUtil.formatClass(containingClass), + JavaHighlightUtil.formatMethod(superMethod), + superClass != null ? HighlightUtil.formatClass(superClass) : ""); appendImplementMethodFix = false; break Ultimate; } @@ -1676,37 +1701,35 @@ public class HighlightMethodUtil { holder.add(info); } } - else { - if (classReference != null && (!result.isAccessible() || - constructor.hasModifierProperty(PsiModifier.PROTECTED) && callingProtectedConstructorFromDerivedClass(constructorCall, aClass))) { - holder.add(buildAccessProblem(classReference, result, constructor)); - } - else if (!applicable) { - String constructorName = HighlightMessageUtil.getSymbolName(constructor, result.getSubstitutor()); - String containerName = HighlightMessageUtil.getSymbolName(constructor.getContainingClass(), result.getSubstitutor()); - String argTypes = buildArgTypesList(list); - String description = JavaErrorMessages.message("wrong.method.arguments", constructorName, containerName, argTypes); - String toolTip = createMismatchedArgumentsHtmlTooltip(result, list); + else if (classReference != null && + (!result.isAccessible() || + constructor.hasModifierProperty(PsiModifier.PROTECTED) && callingProtectedConstructorFromDerivedClass(constructorCall, aClass))) { + holder.add(buildAccessProblem(classReference, result, constructor)); + } + else if (!applicable) { + String constructorName = HighlightMessageUtil.getSymbolName(constructor, result.getSubstitutor()); + PsiClass constructorClass = constructor.getContainingClass(); + String containerName = constructorClass != null ? HighlightMessageUtil.getSymbolName(constructorClass, result.getSubstitutor()) : ""; + String argTypes = buildArgTypesList(list); + String description = JavaErrorMessages.message("wrong.method.arguments", constructorName, containerName, argTypes); + String toolTip = createMismatchedArgumentsHtmlTooltip(result, list); - HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(infoElement).description(description).escapedToolTip(toolTip).navigationShift(+1).create(); - if (info != null) { - JavaResolveResult[] methodCandidates = results; - if (constructorCall instanceof PsiNewExpression) { - methodCandidates = resolveHelper.getReferencedMethodCandidates((PsiCallExpression)constructorCall, true); - } - registerFixesOnInvalidConstructorCall(constructorCall, classReference, list, aClass, constructors, methodCandidates, infoElement, info); - registerMethodReturnFixAction(info, result, constructorCall); - holder.add(info); - } - } - else { + HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(infoElement).description(description).escapedToolTip(toolTip).navigationShift(+1).create(); + if (info != null) { + JavaResolveResult[] methodCandidates = results; if (constructorCall instanceof PsiNewExpression) { - PsiReferenceParameterList typeArgumentList = ((PsiNewExpression)constructorCall).getTypeArgumentList(); - HighlightInfo info = GenericsHighlightUtil.checkReferenceTypeArgumentList(constructor, typeArgumentList, result.getSubstitutor(), false, javaSdkVersion); - if (info != null) { - holder.add(info); - } + methodCandidates = resolveHelper.getReferencedMethodCandidates((PsiCallExpression)constructorCall, true); } + registerFixesOnInvalidConstructorCall(constructorCall, classReference, list, aClass, constructors, methodCandidates, infoElement, info); + registerMethodReturnFixAction(info, result, constructorCall); + holder.add(info); + } + } + else if (constructorCall instanceof PsiNewExpression) { + PsiReferenceParameterList typeArgumentList = ((PsiNewExpression)constructorCall).getTypeArgumentList(); + HighlightInfo info = GenericsHighlightUtil.checkReferenceTypeArgumentList(constructor, typeArgumentList, result.getSubstitutor(), false, javaSdkVersion); + if (info != null) { + holder.add(info); } } @@ -1764,8 +1787,7 @@ public class HighlightMethodUtil { private static HighlightInfo buildAccessProblem(@NotNull PsiJavaCodeReferenceElement classReference, JavaResolveResult result, PsiMember elementToFix) { String description = HighlightUtil.buildProblemWithAccessDescription(classReference, result); - HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(classReference).descriptionAndTooltip( - description).navigationShift(+1).create(); + HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(classReference).descriptionAndTooltip(description).navigationShift(+1).create(); if (result.isStaticsScopeCorrect()) { HighlightUtil.registerAccessQuickFixAction(elementToFix, classReference, info, result.getCurrentFileResolveScope()); } @@ -1794,9 +1816,7 @@ public class HighlightMethodUtil { builder.append("("); PsiExpression[] args = list.getExpressions(); for (int i = 0; i < args.length; i++) { - if (i > 0) { - builder.append(", "); - } + if (i > 0) builder.append(", "); PsiType argType = args[i].getType(); builder.append(argType != null ? JavaHighlightUtil.formatType(argType) : "?"); } @@ -1853,9 +1873,8 @@ public class HighlightMethodUtil { if (method != null && context.getManager().isInProject(method)) { IntentionAction fix = QUICK_FIX_FACTORY.createChangeMethodSignatureFromUsageFix(method, expressions, substitutor, context, false, 2); QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, fix); - IntentionAction f2 = QUICK_FIX_FACTORY.createChangeMethodSignatureFromUsageReverseOrderFix(method, expressions, substitutor, context, - false, 2); + IntentionAction f2 = QUICK_FIX_FACTORY.createChangeMethodSignatureFromUsageReverseOrderFix(method, expressions, substitutor, context, false, 2); QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, f2); } } -} +} \ No newline at end of file