[java] since Java 9, underscore identifiers are completely prohibited (IDEA-132278)

This commit is contained in:
Roman Shevchenko
2015-07-22 18:23:28 +02:00
parent 0afda74389
commit cff7efd5d1
7 changed files with 35 additions and 19 deletions
@@ -725,13 +725,18 @@ public class HighlightUtil extends HighlightUtilBase {
}
@Nullable
static HighlightInfo checkUnderscore(@NotNull PsiIdentifier identifier,
@NotNull PsiVariable variable,
@NotNull LanguageLevel languageLevel) {
if ("_".equals(variable.getName()) && languageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
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();
static HighlightInfo checkUnderscore(@NotNull PsiIdentifier identifier, @NotNull LanguageLevel languageLevel) {
if ("_".equals(identifier.getText())) {
if (languageLevel.isAtLeast(LanguageLevel.JDK_1_9)) {
String text = JavaErrorMessages.message("underscore.identifier.error");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier).descriptionAndTooltip(text).create();
}
else if (languageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
PsiElement parent = identifier.getParent();
if (parent instanceof PsiParameter && ((PsiParameter)parent).getDeclarationScope() instanceof PsiLambdaExpression) {
String text = JavaErrorMessages.message("underscore.lambda.identifier");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier).descriptionAndTooltip(text).create();
}
}
}
@@ -621,8 +621,6 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
myHolder.add(HighlightNamesUtil.highlightVariableName(variable, identifier, colorsScheme));
}
}
myHolder.add(HighlightUtil.checkUnderscore(identifier, variable, myLanguageLevel));
}
else if (parent instanceof PsiClass) {
PsiClass aClass = (PsiClass)parent;
@@ -650,6 +648,8 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
}
}
myHolder.add(HighlightUtil.checkUnderscore(identifier, myLanguageLevel));
super.visitIdentifier(identifier);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,9 +34,13 @@ public class JavacQuirksInspectionVisitor extends JavaElementVisitor {
psiElement().withParent(PsiJavaCodeReferenceElement.class).withSuperParent(2, PsiJavaCodeReferenceElement.class);
private final ProblemsHolder myHolder;
private final LanguageLevel myLanguageLevel;
private final JavaSdkVersion mySdkVersion;
public JavacQuirksInspectionVisitor(ProblemsHolder holder) {
myHolder = holder;
mySdkVersion = JavaVersionService.getInstance().getJavaSdkVersion(myHolder.getFile());
myLanguageLevel = PsiUtil.getLanguageLevel(myHolder.getFile());
}
@Override
@@ -72,12 +76,11 @@ public class JavacQuirksInspectionVisitor extends JavaElementVisitor {
@Override
public void visitIdentifier(PsiIdentifier identifier) {
super.visitIdentifier(identifier);
final JavaSdkVersion version = JavaVersionService.getInstance().getJavaSdkVersion(identifier);
if (version != null && version.isAtLeast(JavaSdkVersion.JDK_1_8)) {
if ("_".equals(identifier.getText())) {
myHolder.registerProblem(identifier, JavaErrorMessages.message("underscore.identifier"),
version.isAtLeast(JavaSdkVersion.JDK_1_9) ? ProblemHighlightType.ERROR : ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
if ("_".equals(identifier.getText()) &&
mySdkVersion != null && mySdkVersion.isAtLeast(JavaSdkVersion.JDK_1_8) &&
myLanguageLevel.isLessThan(LanguageLevel.JDK_1_9)) {
final String message = JavaErrorMessages.message("underscore.identifier.warn");
myHolder.registerProblem(identifier, message, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
}
@@ -64,6 +64,10 @@ public enum LanguageLevel {
return compareTo(level) >= 0;
}
public boolean isLessThan(@NotNull LanguageLevel level) {
return compareTo(level) < 0;
}
@Nullable
public static LanguageLevel parse(@Nullable String value) {
if ("1.3".equals(value)) return JDK_1_3;
@@ -379,7 +379,8 @@ 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.identifier.warn=Use of '_' as an identifier might not be supported in releases after Java 8
underscore.identifier.error=As of Java 9, '_' is a keyword, and may not be used as an identifier
underscore.lambda.identifier=Use of '_' as a lambda parameter name is not allowed
feature.generics=Generics
@@ -0,0 +1,3 @@
class Undescore {
void <error descr="As of Java 9, '_' is a keyword, and may not be used as an identifier">_</error>() { }
}
@@ -15,7 +15,6 @@
*/
package com.intellij.codeInsight.daemon;
import com.intellij.codeInspection.compiler.JavacQuirksInspection;
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
import com.intellij.codeInspection.redundantCast.RedundantCastInspection;
import com.intellij.codeInspection.uncheckedWarnings.UncheckedWarningLocalInspection;
@@ -30,7 +29,7 @@ public class LightAdvHighlightingJdk9Test extends LightDaemonAnalyzerTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
enableInspectionTools(new UnusedDeclarationInspection(), new UncheckedWarningLocalInspection(), new JavacQuirksInspection(), new RedundantCastInspection());
enableInspectionTools(new UnusedDeclarationInspection(), new UncheckedWarningLocalInspection(), new RedundantCastInspection());
setLanguageLevel(LanguageLevel.JDK_1_9);
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_9, getModule(), getTestRootDisposable());
}
@@ -46,6 +45,7 @@ public class LightAdvHighlightingJdk9Test extends LightDaemonAnalyzerTestCase {
public void testSafeVarargsApplicability() { doTest(true, false); }
public void testPrivateInInterfaces() { doTest(false, false); }
public void testUnderscore() { doTest(false, false); }
public void testTryWithResources() { doTest(false, false); }
public void testValueTypes() { setLanguageLevel(LanguageLevel.JDK_X); doTest(false, false); }