use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightControlFlowUtil method checkCannotWriteToFinal.
@Nullable
static HighlightInfo checkCannotWriteToFinal(@NotNull PsiExpression expression, @NotNull PsiFile containingFile) {
PsiReferenceExpression reference = null;
boolean readBeforeWrite = false;
if (expression instanceof PsiAssignmentExpression) {
final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) expression;
final PsiExpression left = PsiUtil.skipParenthesizedExprDown(assignmentExpression.getLExpression());
if (left instanceof PsiReferenceExpression) {
reference = (PsiReferenceExpression) left;
}
readBeforeWrite = assignmentExpression.getOperationTokenType() != JavaTokenType.EQ;
} else if (expression instanceof PsiPostfixExpression) {
final PsiExpression operand = PsiUtil.skipParenthesizedExprDown(((PsiPostfixExpression) expression).getOperand());
final IElementType sign = ((PsiPostfixExpression) expression).getOperationTokenType();
if (operand instanceof PsiReferenceExpression && (sign == JavaTokenType.PLUSPLUS || sign == JavaTokenType.MINUSMINUS)) {
reference = (PsiReferenceExpression) operand;
}
readBeforeWrite = true;
} else if (expression instanceof PsiPrefixExpression) {
final PsiExpression operand = PsiUtil.skipParenthesizedExprDown(((PsiPrefixExpression) expression).getOperand());
final IElementType sign = ((PsiPrefixExpression) expression).getOperationTokenType();
if (operand instanceof PsiReferenceExpression && (sign == JavaTokenType.PLUSPLUS || sign == JavaTokenType.MINUSMINUS)) {
reference = (PsiReferenceExpression) operand;
}
readBeforeWrite = true;
}
final PsiElement resolved = reference == null ? null : reference.resolve();
PsiVariable variable = resolved instanceof PsiVariable ? (PsiVariable) resolved : null;
if (variable == null || !variable.hasModifierProperty(PsiModifier.FINAL))
return null;
final boolean canWrite = canWriteToFinal(variable, expression, reference, containingFile) && checkWriteToFinalInsideLambda(variable, reference) == null;
if (readBeforeWrite || !canWrite) {
final String name = variable.getName();
String description = JavaErrorMessages.message(canWrite ? "variable.not.initialized" : "assignment.to.final.variable", name);
final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(reference.getTextRange()).descriptionAndTooltip(description).create();
final PsiElement innerClass = getInnerClassVariableReferencedFrom(variable, expression);
if (innerClass == null || variable instanceof PsiField) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createModifierListFix(variable, PsiModifier.FINAL, false, false));
} else {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createVariableAccessFromInnerClassFix(variable, innerClass));
}
return highlightInfo;
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class GenericsHighlightUtil method checkSameErasureNotSubSignatureInner.
@Nullable
private static HighlightInfo checkSameErasureNotSubSignatureInner(@NotNull HierarchicalMethodSignature signature, @NotNull PsiManager manager, @NotNull PsiClass aClass, @NotNull Map<MethodSignature, MethodSignatureBackedByPsiMethod> sameErasureMethods) {
PsiMethod method = signature.getMethod();
JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
if (!facade.getResolveHelper().isAccessible(method, aClass, null))
return null;
MethodSignature signatureToErase = method.getSignature(PsiSubstitutor.EMPTY);
MethodSignatureBackedByPsiMethod sameErasure = sameErasureMethods.get(signatureToErase);
HighlightInfo info;
if (sameErasure != null) {
if (aClass instanceof PsiTypeParameter || MethodSignatureUtil.findMethodBySuperMethod(aClass, sameErasure.getMethod(), false) != null || !(InheritanceUtil.isInheritorOrSelf(sameErasure.getMethod().getContainingClass(), method.getContainingClass(), true) || InheritanceUtil.isInheritorOrSelf(method.getContainingClass(), sameErasure.getMethod().getContainingClass(), true))) {
info = checkSameErasureNotSubSignatureOrSameClass(sameErasure, signature, aClass, method);
if (info != null)
return info;
}
} else {
sameErasureMethods.put(signatureToErase, signature);
}
List<HierarchicalMethodSignature> supers = signature.getSuperSignatures();
for (HierarchicalMethodSignature superSignature : supers) {
info = checkSameErasureNotSubSignatureInner(superSignature, manager, aClass, sameErasureMethods);
if (info != null)
return info;
if (superSignature.isRaw() && !signature.isRaw()) {
final PsiType[] parameterTypes = signature.getParameterTypes();
PsiType[] erasedTypes = superSignature.getErasedParameterTypes();
for (int i = 0; i < erasedTypes.length; i++) {
if (!Comparing.equal(parameterTypes[i], erasedTypes[i])) {
return getSameErasureMessage(false, method, superSignature.getMethod(), HighlightNamesUtil.getClassDeclarationTextRange(aClass));
}
}
}
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class GenericsHighlightUtil method checkForeachExpressionTypeIsIterable.
static HighlightInfo checkForeachExpressionTypeIsIterable(PsiExpression expression) {
if (expression == null || expression.getType() == null)
return null;
final PsiType itemType = JavaGenericsUtil.getCollectionItemType(expression);
if (itemType == null) {
String description = JavaErrorMessages.message("foreach.not.applicable", JavaHighlightUtil.formatType(expression.getType()));
final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createNotIterableForEachLoopFix(expression));
return highlightInfo;
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class GenericsHighlightUtil method checkOverrideEquivalentMethods.
static Collection<HighlightInfo> checkOverrideEquivalentMethods(@NotNull PsiClass aClass) {
List<HighlightInfo> result = new ArrayList<>();
final Collection<HierarchicalMethodSignature> signaturesWithSupers = aClass.getVisibleSignatures();
PsiManager manager = aClass.getManager();
Map<MethodSignature, MethodSignatureBackedByPsiMethod> sameErasureMethods = new THashMap<>(MethodSignatureUtil.METHOD_PARAMETERS_ERASURE_EQUALITY);
final Set<MethodSignature> foundProblems = new THashSet<>(MethodSignatureUtil.METHOD_PARAMETERS_ERASURE_EQUALITY);
for (HierarchicalMethodSignature signature : signaturesWithSupers) {
HighlightInfo info = checkSameErasureNotSubSignatureInner(signature, manager, aClass, sameErasureMethods);
if (info != null && foundProblems.add(signature)) {
result.add(info);
}
if (aClass instanceof PsiTypeParameter) {
info = HighlightMethodUtil.checkMethodIncompatibleReturnType(signature, signature.getSuperSignatures(), true, HighlightNamesUtil.getClassDeclarationTextRange(aClass));
if (info != null) {
result.add(info);
}
}
}
return result.isEmpty() ? null : result;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class GenericsHighlightUtil method checkVarArgParameterIsLast.
@Nullable
static HighlightInfo checkVarArgParameterIsLast(@NotNull PsiParameter parameter) {
PsiElement declarationScope = parameter.getDeclarationScope();
if (declarationScope instanceof PsiMethod) {
PsiParameter[] params = ((PsiMethod) declarationScope).getParameterList().getParameters();
if (params[params.length - 1] != parameter) {
String description = JavaErrorMessages.message("vararg.not.last.parameter");
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(parameter).descriptionAndTooltip(description).create();
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createMakeVarargParameterLastFix(parameter));
return info;
}
}
return null;
}
Aggregations