java: single underscore variables highlighting for Java 8

This commit is contained in:
Roman Shevchenko
2013-08-07 15:07:21 +02:00
parent 5435f1c3bf
commit cfc55bcb41
5 changed files with 48 additions and 7 deletions
@@ -622,6 +622,22 @@ public class HighlightUtil extends HighlightUtilBase {
return null;
}
@Nullable
public static HighlightInfo checkUnderscore(@NotNull PsiIdentifier identifier, @NotNull PsiVariable variable) {
if ("_".equals(variable.getName()) && PsiUtil.isLanguageLevel8OrHigher(variable)) {
if (variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiLambdaExpression) {
String message = JavaErrorMessages.message("underscore.lambda.identifier");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier).descriptionAndTooltip(message).create();
}
else {
String message = JavaErrorMessages.message("underscore.identifier");
return HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING).range(identifier).descriptionAndTooltip(message).create();
}
}
return null;
}
@NotNull
public static String formatClass(@NotNull PsiClass aClass) {
return formatClass(aClass, true);
@@ -515,18 +515,19 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitForeachStatement(final PsiForeachStatement statement) {
myHolder.add(HighlightUtil.checkForEachFeature(statement, myLanguageLevel,myFile));
myHolder.add(HighlightUtil.checkForEachFeature(statement, myLanguageLevel, myFile));
}
@Override
public void visitImportStaticStatement(final PsiImportStaticStatement statement) {
myHolder.add(HighlightUtil.checkStaticImportFeature(statement, myLanguageLevel,myFile));
myHolder.add(HighlightUtil.checkStaticImportFeature(statement, myLanguageLevel, myFile));
}
@Override
public void visitIdentifier(final PsiIdentifier identifier) {
TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
PsiElement parent = identifier.getParent();
final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
if (parent instanceof PsiVariable) {
PsiVariable variable = (PsiVariable)parent;
myHolder.add(HighlightUtil.checkVariableAlreadyDefined(variable));
@@ -535,6 +536,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
final PsiElement child = variable.getLastChild();
if (child instanceof PsiErrorElement && child.getPrevSibling() == identifier) return;
}
boolean isMethodParameter = variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiMethod;
if (!isMethodParameter) { // method params are highlighted in visitMethod since we should make sure the method body was visited before
if (HighlightControlFlowUtil.isReassigned(variable, myFinalVarProblems)) {
@@ -547,6 +549,8 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
else {
myReassignedParameters.put((PsiParameter)variable, 1); // mark param as present in current file
}
myHolder.add(HighlightUtil.checkUnderscore(identifier, variable));
}
else if (parent instanceof PsiClass) {
PsiClass aClass = (PsiClass)parent;
@@ -569,6 +573,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
else {
visitParentReference(parent);
}
super.visitIdentifier(identifier);
}
@@ -363,6 +363,9 @@ override.not.allowed.in.interfaces=@Override is not allowed when implementing in
wildcard.not.expected=Unexpected wildcard
bound.not.expected=Unexpected bound
underscore.identifier=Use of '_' as an identifier might not be supported in releases after Java 8
underscore.lambda.identifier=Use of '_' as a lambda parameter name is not allowed
feature.generics=Generics
feature.annotations=Annotations
feature.static.imports=Static imports
@@ -0,0 +1,16 @@
class C {
void test() {
{
I <warning descr="Use of '_' as an identifier might not be supported in releases after Java 8">_</warning> = new I() { public void f(int i) { } };
accept(_);
}
{
accept(<error descr="Use of '_' as a lambda parameter name is not allowed">_</error> -> System.out.println(_));
accept((int <error descr="Use of '_' as a lambda parameter name is not allowed">_</error>) -> System.out.println(_));
}
}
interface I { void f(int i); }
void accept(I i) { i.f(42); }
}
@@ -86,10 +86,11 @@ public class LambdaHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testVoidCompatibility() { doTest(); }
public void testConditionalInferenceFromOppositePart() { doTest(); }
public void testDeclaredTypeParameterBoundsAndUnboundedWildcard() { doTest(); }
public void testConflictResolution() throws Exception {doTest();}
public void testIDEA108195() throws Exception {doTest();}
public void testDiamondInference() throws Exception { doTest();}
public void testFunctionalInterfaceCheck() throws Exception { doTest();}
public void testConflictResolution() { doTest(); }
public void testIDEA108195() { doTest(); }
public void testDiamondInference() { doTest();}
public void testFunctionalInterfaceCheck() { doTest();}
public void testUnderscores() { doTest(true);}
private void doTest() {
doTest(false);