use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkNotAllowedModifier.
@Nullable
static HighlightInfo checkNotAllowedModifier(@NotNull PsiKeyword keyword, @NotNull PsiModifierList modifierList) {
PsiElement modifierOwner = modifierList.getParent();
Map<String, Set<String>> incompatibleModifierMap = getIncompatibleModifierMap(modifierOwner);
if (incompatibleModifierMap == null)
return null;
@PsiModifier.ModifierConstant String modifier = keyword.getText();
Set<String> incompatibles = incompatibleModifierMap.get(modifier);
PsiElement modifierOwnerParent = modifierOwner instanceof PsiMember ? ((PsiMember) modifierOwner).getContainingClass() : modifierOwner.getParent();
if (modifierOwnerParent == null)
modifierOwnerParent = modifierOwner.getParent();
boolean isAllowed = true;
if (modifierOwner instanceof PsiClass) {
PsiClass aClass = (PsiClass) modifierOwner;
if (aClass.isInterface()) {
if (PsiModifier.STATIC.equals(modifier) || PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.PACKAGE_LOCAL.equals(modifier)) {
isAllowed = modifierOwnerParent instanceof PsiClass;
}
} else {
if (PsiModifier.PUBLIC.equals(modifier)) {
isAllowed = modifierOwnerParent instanceof PsiJavaFile || modifierOwnerParent instanceof PsiClass && (modifierOwnerParent instanceof PsiSyntheticClass || ((PsiClass) modifierOwnerParent).getQualifiedName() != null);
} else if (PsiModifier.STATIC.equals(modifier) || PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.PACKAGE_LOCAL.equals(modifier)) {
isAllowed = modifierOwnerParent instanceof PsiClass && ((PsiClass) modifierOwnerParent).getQualifiedName() != null || FileTypeUtils.isInServerPageFile(modifierOwnerParent);
}
if (aClass.isEnum()) {
isAllowed &= !(PsiModifier.FINAL.equals(modifier) || PsiModifier.ABSTRACT.equals(modifier));
}
if (aClass.getContainingClass() instanceof PsiAnonymousClass) {
isAllowed &= !(PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier));
}
}
} else if (modifierOwner instanceof PsiMethod) {
PsiMethod method = (PsiMethod) modifierOwner;
isAllowed = !(method.isConstructor() && ourConstructorNotAllowedModifiers.contains(modifier));
PsiClass containingClass = method.getContainingClass();
if ((method.hasModifierProperty(PsiModifier.PUBLIC) || method.hasModifierProperty(PsiModifier.PROTECTED)) && method.isConstructor() && containingClass != null && containingClass.isEnum()) {
isAllowed = false;
}
if (PsiModifier.PRIVATE.equals(modifier)) {
isAllowed &= modifierOwnerParent instanceof PsiClass && (!((PsiClass) modifierOwnerParent).isInterface() || PsiUtil.isLanguageLevel9OrHigher(modifierOwner));
} else if (PsiModifier.STRICTFP.equals(modifier)) {
isAllowed &= modifierOwnerParent instanceof PsiClass && (!((PsiClass) modifierOwnerParent).isInterface() || PsiUtil.isLanguageLevel8OrHigher(modifierOwner));
} else if (PsiModifier.PROTECTED.equals(modifier) || PsiModifier.TRANSIENT.equals(modifier) || PsiModifier.SYNCHRONIZED.equals(modifier)) {
isAllowed &= modifierOwnerParent instanceof PsiClass && !((PsiClass) modifierOwnerParent).isInterface();
}
if (containingClass != null && containingClass.isInterface()) {
isAllowed &= !PsiModifier.NATIVE.equals(modifier);
}
if (containingClass != null && containingClass.isAnnotationType()) {
isAllowed &= !PsiModifier.STATIC.equals(modifier);
isAllowed &= !PsiModifier.DEFAULT.equals(modifier);
}
} else if (modifierOwner instanceof PsiField) {
if (PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.TRANSIENT.equals(modifier) || PsiModifier.STRICTFP.equals(modifier) || PsiModifier.SYNCHRONIZED.equals(modifier)) {
isAllowed = modifierOwnerParent instanceof PsiClass && !((PsiClass) modifierOwnerParent).isInterface();
}
} else if (modifierOwner instanceof PsiClassInitializer) {
isAllowed = PsiModifier.STATIC.equals(modifier);
} else if (modifierOwner instanceof PsiLocalVariable || modifierOwner instanceof PsiParameter) {
isAllowed = PsiModifier.FINAL.equals(modifier);
} else if (modifierOwner instanceof PsiReceiverParameter) {
isAllowed = false;
}
isAllowed &= incompatibles != null;
if (!isAllowed) {
String message = JavaErrorMessages.message("modifier.not.allowed", modifier);
HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(keyword).descriptionAndTooltip(message).create();
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createModifierListFix(modifierList, modifier, false, false));
return highlightInfo;
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkAssignability.
@Nullable
private static HighlightInfo checkAssignability(@Nullable PsiType lType, @Nullable PsiType rType, @Nullable PsiExpression expression, @NotNull TextRange textRange, int navigationShift) {
if (lType == rType)
return null;
if (expression == null) {
if (rType == null || lType == null || TypeConversionUtil.isAssignable(lType, rType))
return null;
} else if (TypeConversionUtil.areTypesAssignmentCompatible(lType, expression)) {
return null;
}
if (rType == null) {
rType = expression.getType();
}
HighlightInfo highlightInfo = createIncompatibleTypeHighlightInfo(lType, rType, textRange, navigationShift);
if (rType != null && expression != null && isCastIntentionApplicable(expression, lType)) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createAddTypeCastFix(lType, expression));
}
if (expression != null) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapLongWithMathToIntExactFix(lType, expression));
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapWithOptionalFix(lType, expression));
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapExpressionFix(lType, expression));
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapStringWithFileFix(lType, expression));
AddTypeArgumentsConditionalFix.register(highlightInfo, expression, lType);
registerCollectionToArrayFixAction(highlightInfo, rType, lType, expression);
}
ChangeNewOperatorTypeFix.register(highlightInfo, expression, lType);
return highlightInfo;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkExceptionThrownInTry.
@Nullable
static List<HighlightInfo> checkExceptionThrownInTry(@NotNull final PsiParameter parameter, @NotNull final Set<PsiClassType> thrownTypes) {
final PsiElement declarationScope = parameter.getDeclarationScope();
if (!(declarationScope instanceof PsiCatchSection))
return null;
final PsiType caughtType = parameter.getType();
if (caughtType instanceof PsiClassType) {
HighlightInfo info = checkSimpleCatchParameter(parameter, thrownTypes, (PsiClassType) caughtType);
return info == null ? null : Collections.singletonList(info);
}
if (caughtType instanceof PsiDisjunctionType) {
return checkMultiCatchParameter(parameter, thrownTypes);
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkIllegalModifierCombination.
@Nullable
static HighlightInfo checkIllegalModifierCombination(@NotNull PsiKeyword keyword, @NotNull PsiModifierList modifierList) {
@PsiModifier.ModifierConstant String modifier = keyword.getText();
String incompatible = getIncompatibleModifier(modifier, modifierList);
if (incompatible != null) {
String message = JavaErrorMessages.message("incompatible.modifiers", modifier, incompatible);
HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(keyword).descriptionAndTooltip(message).create();
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createModifierListFix(modifierList, modifier, false, false));
return highlightInfo;
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class ModuleHighlightUtil method checkPackageAccessTargets.
@NotNull
static List<HighlightInfo> checkPackageAccessTargets(@NotNull PsiPackageAccessibilityStatement statement) {
List<HighlightInfo> results = ContainerUtil.newSmartList();
Set<String> targets = ContainerUtil.newTroveSet();
for (PsiJavaModuleReferenceElement refElement : statement.getModuleReferences()) {
String refText = refElement.getReferenceText();
PsiPolyVariantReference ref = refElement.getReference();
assert ref != null : statement;
if (!targets.add(refText)) {
boolean exports = statement.getRole() == Role.EXPORTS;
String message = JavaErrorMessages.message(exports ? "module.duplicate.exports.target" : "module.duplicate.opens.target", refText);
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(refElement).descriptionAndTooltip(message).create();
QuickFixAction.registerQuickFixAction(info, factory().createDeleteFix(refElement, QuickFixBundle.message("delete.reference.fix.text")));
results.add(info);
} else if (ref.multiResolve(true).length == 0) {
String message = JavaErrorMessages.message("module.not.found", refElement.getReferenceText());
results.add(HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING).range(refElement).descriptionAndTooltip(message).create());
}
}
return results;
}
Aggregations