use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightMethodUtil method checkConstructorHandleSuperClassExceptions.
static HighlightInfo checkConstructorHandleSuperClassExceptions(PsiMethod method) {
if (!method.isConstructor()) {
return null;
}
PsiCodeBlock body = method.getBody();
PsiStatement[] statements = body == null ? null : body.getStatements();
if (statements == null)
return null;
// if we have unhandled exception inside method body, we could not have been called here,
// so the only problem it can catch here is with super ctr only
Collection<PsiClassType> unhandled = ExceptionUtil.collectUnhandledExceptions(method, method.getContainingClass());
if (unhandled.isEmpty())
return null;
String description = HighlightUtil.getUnhandledExceptionsDescriptor(unhandled);
TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
for (PsiClassType exception : unhandled) {
QuickFixAction.registerQuickFixAction(highlightInfo, new LocalQuickFixOnPsiElementAsIntentionAdapter(QUICK_FIX_FACTORY.createMethodThrowsFix(method, exception, true, false)));
}
return highlightInfo;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightMethodUtil method buildAccessProblem.
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();
if (result.isStaticsScopeCorrect()) {
HighlightUtil.registerAccessQuickFixAction(elementToFix, classReference, info, result.getCurrentFileResolveScope());
}
return info;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightMethodUtil method checkStaticMethodOverride.
private static HighlightInfo checkStaticMethodOverride(PsiClass aClass, PsiMethod method, boolean isMethodStatic, PsiClass superClass, PsiMethod superMethod, @NotNull PsiFile containingFile) {
if (superMethod == null)
return null;
PsiManager manager = containingFile.getManager();
PsiModifierList superModifierList = superMethod.getModifierList();
PsiModifierList modifierList = method.getModifierList();
if (superModifierList.hasModifierProperty(PsiModifier.PRIVATE))
return null;
if (superModifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && !JavaPsiFacade.getInstance(manager.getProject()).arePackagesTheSame(aClass, superClass)) {
return null;
}
boolean isSuperMethodStatic = superModifierList.hasModifierProperty(PsiModifier.STATIC);
if (isMethodStatic != isSuperMethodStatic) {
TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
@NonNls final String messageKey = isMethodStatic ? "static.method.cannot.override.instance.method" : "instance.method.cannot.override.static.method";
String description = JavaErrorMessages.message(messageKey, JavaHighlightUtil.formatMethod(method), HighlightUtil.formatClass(aClass), JavaHighlightUtil.formatMethod(superMethod), HighlightUtil.formatClass(superClass));
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
if (!isSuperMethodStatic || HighlightUtil.getIncompatibleModifier(PsiModifier.STATIC, modifierList) == null) {
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.STATIC, isSuperMethodStatic, false));
}
if (manager.isInProject(superMethod) && (!isMethodStatic || HighlightUtil.getIncompatibleModifier(PsiModifier.STATIC, superModifierList) == null)) {
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createModifierListFix(superMethod, PsiModifier.STATIC, isMethodStatic, true));
}
return info;
}
if (isMethodStatic) {
if (superClass.isInterface())
return null;
int accessLevel = PsiUtil.getAccessLevel(modifierList);
String accessModifier = PsiUtil.getAccessModifier(accessLevel);
HighlightInfo info = isWeaker(method, modifierList, accessModifier, accessLevel, superMethod, true);
if (info != null)
return info;
info = checkSuperMethodIsFinal(method, superMethod);
if (info != null)
return info;
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightMethodUtil method checkStaticMethodOverride.
/**
* @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) {
// constructors are not members and therefor don't override class methods
if (method.isConstructor()) {
return null;
}
PsiClass aClass = method.getContainingClass();
if (aClass == null)
return null;
final HierarchicalMethodSignature methodSignature = PsiSuperMethodImplUtil.getHierarchicalMethodSignature(method);
final List<HierarchicalMethodSignature> superSignatures = methodSignature.getSuperSignatures();
if (superSignatures.isEmpty()) {
return null;
}
boolean isStatic = method.hasModifierProperty(PsiModifier.STATIC);
for (HierarchicalMethodSignature signature : superSignatures) {
final PsiMethod superMethod = signature.getMethod();
final PsiClass superClass = superMethod.getContainingClass();
if (superClass == null)
continue;
final HighlightInfo highlightInfo = checkStaticMethodOverride(aClass, method, isStatic, superClass, superMethod, containingFile);
if (highlightInfo != null) {
return highlightInfo;
}
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightMethodUtil method checkMethodIncompatibleThrows.
static HighlightInfo checkMethodIncompatibleThrows(MethodSignatureBackedByPsiMethod methodSignature, List<HierarchicalMethodSignature> superMethodSignatures, boolean includeRealPositionInfo, PsiClass analyzedClass) {
PsiMethod method = methodSignature.getMethod();
PsiClass aClass = method.getContainingClass();
if (aClass == null)
return null;
PsiSubstitutor superSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(aClass, analyzedClass, PsiSubstitutor.EMPTY);
PsiClassType[] exceptions = method.getThrowsList().getReferencedTypes();
PsiJavaCodeReferenceElement[] referenceElements;
List<PsiElement> exceptionContexts;
if (includeRealPositionInfo) {
exceptionContexts = new ArrayList<>();
referenceElements = method.getThrowsList().getReferenceElements();
} else {
exceptionContexts = null;
referenceElements = null;
}
List<PsiClassType> checkedExceptions = new ArrayList<>();
for (int i = 0; i < exceptions.length; i++) {
PsiClassType exception = exceptions[i];
if (exception == null) {
LOG.error("throws: " + method.getThrowsList().getText() + "; method: " + method);
}
if (!ExceptionUtil.isUncheckedException(exception)) {
checkedExceptions.add(exception);
if (includeRealPositionInfo && i < referenceElements.length) {
PsiJavaCodeReferenceElement exceptionRef = referenceElements[i];
exceptionContexts.add(exceptionRef);
}
}
}
for (MethodSignatureBackedByPsiMethod superMethodSignature : superMethodSignatures) {
PsiMethod superMethod = superMethodSignature.getMethod();
int index = getExtraExceptionNum(methodSignature, superMethodSignature, checkedExceptions, superSubstitutor);
if (index != -1) {
if (aClass.isInterface()) {
final PsiClass superContainingClass = superMethod.getContainingClass();
if (superContainingClass != null && !superContainingClass.isInterface())
continue;
if (superContainingClass != null && !aClass.isInheritor(superContainingClass, true))
continue;
}
PsiClassType exception = checkedExceptions.get(index);
String description = JavaErrorMessages.message("overridden.method.does.not.throw", createClashMethodMessage(method, superMethod, true), JavaHighlightUtil.formatType(exception));
TextRange textRange;
if (includeRealPositionInfo) {
PsiElement exceptionContext = exceptionContexts.get(index);
textRange = exceptionContext.getTextRange();
} else {
textRange = TextRange.EMPTY_RANGE;
}
HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
QuickFixAction.registerQuickFixAction(errorResult, new LocalQuickFixOnPsiElementAsIntentionAdapter(QUICK_FIX_FACTORY.createMethodThrowsFix(method, exception, false, false)));
QuickFixAction.registerQuickFixAction(errorResult, new LocalQuickFixOnPsiElementAsIntentionAdapter(QUICK_FIX_FACTORY.createMethodThrowsFix(superMethod, exception, true, true)));
return errorResult;
}
}
return null;
}
Aggregations