use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkValidArrayAccessExpression.
@Nullable
static HighlightInfo checkValidArrayAccessExpression(@NotNull PsiArrayAccessExpression arrayAccessExpression) {
final PsiExpression arrayExpression = arrayAccessExpression.getArrayExpression();
final PsiType arrayExpressionType = arrayExpression.getType();
if (arrayExpressionType != null && !(arrayExpressionType instanceof PsiArrayType)) {
final String description = JavaErrorMessages.message("array.type.expected", JavaHighlightUtil.formatType(arrayExpressionType));
final HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(arrayExpression).descriptionAndTooltip(description).create();
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createReplaceWithListAccessFix(arrayAccessExpression));
return info;
}
final PsiExpression indexExpression = arrayAccessExpression.getIndexExpression();
return indexExpression != null ? checkAssignability(PsiType.INT, indexExpression.getType(), indexExpression, indexExpression) : null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkUnaryOperatorApplicable.
@Nullable
static HighlightInfo checkUnaryOperatorApplicable(@Nullable PsiJavaToken token, @Nullable PsiExpression expression) {
if (token != null && expression != null && !TypeConversionUtil.isUnaryOperatorApplicable(token, expression)) {
PsiType type = expression.getType();
if (type == null)
return null;
String message = JavaErrorMessages.message("unary.operator.not.applicable", token.getText(), JavaHighlightUtil.formatType(type));
PsiElement parentExpr = token.getParent();
HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parentExpr).descriptionAndTooltip(message).create();
if (parentExpr instanceof PsiPrefixExpression && token.getTokenType() == JavaTokenType.EXCL) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createNegationBroadScopeFix((PsiPrefixExpression) parentExpr));
}
return highlightInfo;
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkCaseStatement.
@Nullable
static HighlightInfo checkCaseStatement(@NotNull PsiSwitchLabelStatement statement) {
PsiSwitchStatement switchStatement = statement.getEnclosingSwitchStatement();
if (switchStatement == null) {
String description = JavaErrorMessages.message("case.statement.outside.switch");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(description).create();
}
if (switchStatement.getBody() == null)
return null;
PsiExpression switchExpression = switchStatement.getExpression();
PsiType switchType = switchExpression == null ? PsiType.INT : switchExpression.getType();
// check constant expression
PsiExpression caseValue = statement.getCaseValue();
// Every case constant expression associated with a switch statement must be assignable ($5.2) to the type of the switch Expression.
if (caseValue != null && switchExpression != null) {
HighlightInfo highlightInfo = checkAssignability(switchType, caseValue.getType(), caseValue, caseValue);
if (highlightInfo != null)
return highlightInfo;
}
Object value = null;
boolean isEnumSwitch = false;
if (!statement.isDefaultCase() && caseValue != null) {
if (caseValue instanceof PsiReferenceExpression) {
PsiElement element = ((PsiReferenceExpression) caseValue).resolve();
if (element instanceof PsiEnumConstant) {
isEnumSwitch = true;
value = ((PsiEnumConstant) element).getName();
if (((PsiReferenceExpression) caseValue).getQualifier() != null) {
String message = JavaErrorMessages.message("qualified.enum.constant.in.switch");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(caseValue).descriptionAndTooltip(message).create();
}
}
}
if (!isEnumSwitch) {
value = ConstantExpressionUtil.computeCastTo(caseValue, switchType);
}
if (value == null) {
String description = JavaErrorMessages.message("constant.expression.required");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(caseValue).descriptionAndTooltip(description).create();
}
}
// check duplicate
PsiStatement[] statements = switchStatement.getBody().getStatements();
for (PsiStatement st : statements) {
if (st == statement)
continue;
if (!(st instanceof PsiSwitchLabelStatement))
continue;
PsiSwitchLabelStatement labelStatement = (PsiSwitchLabelStatement) st;
if (labelStatement.isDefaultCase() != statement.isDefaultCase())
continue;
PsiExpression caseExpr = labelStatement.getCaseValue();
if (isEnumSwitch && caseExpr instanceof PsiReferenceExpression) {
PsiElement element = ((PsiReferenceExpression) caseExpr).resolve();
if (!(element instanceof PsiEnumConstant && Comparing.equal(((PsiEnumConstant) element).getName(), value)))
continue;
} else {
// not assignable error already caught
if (!TypeConversionUtil.areTypesAssignmentCompatible(switchType, caseExpr))
continue;
if (!Comparing.equal(ConstantExpressionUtil.computeCastTo(caseExpr, switchType), value))
continue;
}
String description = statement.isDefaultCase() ? JavaErrorMessages.message("duplicate.default.switch.label") : JavaErrorMessages.message("duplicate.switch.label", value);
PsiElement element = value == null ? statement : caseValue;
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description).create();
}
// must be followed with colon
PsiElement lastChild = statement.getLastChild();
while (lastChild instanceof PsiComment || lastChild instanceof PsiWhiteSpace) {
lastChild = lastChild.getPrevSibling();
}
if (!(lastChild instanceof PsiJavaToken && ((PsiJavaToken) lastChild).getTokenType() == JavaTokenType.COLON)) {
int start = statement.getTextRange().getEndOffset();
int end = statement.getTextRange().getEndOffset() + 1;
String description = JavaErrorMessages.message("switch.colon.expected.after.case.label");
CharSequence chars = statement.getContainingFile().getViewProvider().getContents();
boolean isAfterEndOfLine = end >= chars.length() || chars.charAt(start) == '\n' || chars.charAt(start) == '\r';
HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(start, end).descriptionAndTooltip(description);
if (isAfterEndOfLine) {
builder.endOfLine();
}
return builder.create();
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkWithImprovedCatchAnalysis.
@Nullable
static Collection<HighlightInfo> checkWithImprovedCatchAnalysis(@NotNull PsiParameter parameter, @NotNull Collection<PsiClassType> thrownInTryStatement, @NotNull PsiFile containingFile) {
final PsiElement scope = parameter.getDeclarationScope();
if (!(scope instanceof PsiCatchSection))
return null;
final PsiCatchSection catchSection = (PsiCatchSection) scope;
final PsiCatchSection[] allCatchSections = catchSection.getTryStatement().getCatchSections();
final int idx = ArrayUtilRt.find(allCatchSections, catchSection);
if (idx <= 0)
return null;
final Collection<PsiClassType> thrownTypes = ContainerUtil.newHashSet(thrownInTryStatement);
final PsiManager manager = containingFile.getManager();
final GlobalSearchScope parameterResolveScope = parameter.getResolveScope();
thrownTypes.add(PsiType.getJavaLangError(manager, parameterResolveScope));
thrownTypes.add(PsiType.getJavaLangRuntimeException(manager, parameterResolveScope));
final Collection<HighlightInfo> result = ContainerUtil.newArrayList();
final List<PsiTypeElement> parameterTypeElements = PsiUtil.getParameterTypeElements(parameter);
final boolean isMultiCatch = parameterTypeElements.size() > 1;
for (PsiTypeElement catchTypeElement : parameterTypeElements) {
final PsiType catchType = catchTypeElement.getType();
if (ExceptionUtil.isGeneralExceptionType(catchType))
continue;
// collect exceptions which are caught by this type
Collection<PsiClassType> caught = ContainerUtil.findAll(thrownTypes, catchType::isAssignableFrom);
if (caught.isEmpty())
continue;
final Collection<PsiClassType> caughtCopy = ContainerUtil.newHashSet(caught);
// exclude all which are caught by previous catch sections
for (int i = 0; i < idx; i++) {
final PsiParameter prevCatchParameter = allCatchSections[i].getParameter();
if (prevCatchParameter == null)
continue;
for (PsiTypeElement prevCatchTypeElement : PsiUtil.getParameterTypeElements(prevCatchParameter)) {
final PsiType prevCatchType = prevCatchTypeElement.getType();
caught.removeIf(prevCatchType::isAssignableFrom);
if (caught.isEmpty())
break;
}
}
// check & warn
if (caught.isEmpty()) {
final String message = JavaErrorMessages.message("exception.already.caught.warn", formatTypes(caughtCopy), caughtCopy.size());
final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING).range(catchSection).descriptionAndTooltip(message).create();
if (isMultiCatch) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createDeleteMultiCatchFix(catchTypeElement));
} else {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createDeleteCatchFix(parameter));
}
result.add(highlightInfo);
}
}
return result;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkFeature.
@Nullable
static HighlightInfo checkFeature(@NotNull PsiElement element, @NotNull Feature feature, @NotNull LanguageLevel level, @NotNull PsiFile file) {
if (file.getManager().isInProject(file) && !level.isAtLeast(feature.level)) {
String message = getUnsupportedFeatureMessage(element, feature, level, file);
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(message).create();
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createIncreaseLanguageLevelFix(feature.level));
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createShowModulePropertiesFix(element));
return info;
}
return null;
}
Aggregations