[java] highlights default package references in module-info.java files (IDEA-181405)

This commit is contained in:
Roman Shevchenko
2017-11-02 11:39:42 +01:00
parent ee1ca768ff
commit 24ae1022e4
8 changed files with 79 additions and 55 deletions
@@ -2699,11 +2699,13 @@ public class HighlightUtil extends HighlightUtilBase {
if ((resolved instanceof PsiLocalVariable || resolved instanceof PsiParameter) && !(resolved instanceof ImplicitVariable)) {
return HighlightControlFlowUtil.checkVariableMustBeFinal((PsiVariable)resolved, ref, languageLevel);
}
if (resolved instanceof PsiClass &&
((PsiClass)resolved).getContainingClass() == null &&
PsiTreeUtil.getParentOfType(ref, PsiImportStatementBase.class) != null &&
PsiUtil.isFromDefaultPackage((PsiClass)resolved)) {
String description = JavaErrorMessages.message("cannot.resolve.symbol", refName.getText());
PsiUtil.isFromDefaultPackage(resolved) &&
(PsiTreeUtil.getParentOfType(ref, PsiImportStatementBase.class) != null ||
PsiUtil.isModuleFile(containingFile))) {
String description = JavaErrorMessages.message("class.in.default.package", ((PsiClass)resolved).getName());
return HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description).create();
}
@@ -286,6 +286,7 @@ class.is.ambiguous.in.single.static.import=Class ''{0}'' is ambiguous in a singl
field.is.already.defined.in.single.static.import=Field ''{0}'' is already defined in a single static import
field.is.ambiguous.in.single.static.import=Field ''{0}'' is ambiguous in a single static import
annotation.interface.members.may.not.have.parameters=@interface members may not have parameters
class.in.default.package=Class ''{0}'' is in the default package
local.variable.is.never.used=Variable ''{0}'' is never used
local.variable.is.not.used.for.reading=Variable ''{0}'' is assigned but never accessed
@@ -1,19 +0,0 @@
import static <error descr="Cannot resolve symbol 'MyConstants'">MyConstants</error>.*;
import <error descr="Cannot resolve symbol 'MyConstants'">MyConstants</error>;
import <error descr="Cannot resolve symbol 'MyConstants'">MyConstants</error>.Inner;
import static <error descr="Cannot resolve symbol 'MyConstants'">MyConstants</error>.Inner.*;
import static <error descr="Cannot resolve symbol 'MyConstants'">MyConstants</error>.Inner.INNER_CONSTANT;
class MyClient
{
private int field = MyConstants.CONSTANT;
}
class MyConstants
{
public static final int CONSTANT = 1;
public static class Inner {
public static final String INNER_CONSTANT = "const";
}
}
@@ -1,11 +1,9 @@
import <error descr="Cannot resolve symbol 'OuterClass'">OuterClass</error>.*;
import <error descr="Class 'OuterClass' is in the default package">OuterClass</error>.*;
class OuterClass{
class Inner{}
class OuterClass {
class Inner { }
}
class X{
class X {
Inner inner;
}
}
@@ -1,7 +1,5 @@
<warning descr="Unused import statement">import <error descr="Cannot resolve symbol 'OuterClass'">OuterClass</error>.*;</warning>
class OuterClass{
class Inner{}
}
<warning descr="Unused import statement">import <error descr="Class 'OuterClass' is in the default package">OuterClass</error>.*;</warning>
class OuterClass {
class Inner { }
}
@@ -0,0 +1,53 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.daemon
import com.intellij.idea.Bombed
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import java.util.*
class ClassInDefaultPackageHighlightingTest : LightCodeInsightFixtureTestCase() {
override fun setUp() {
super.setUp()
myFixture.addClass("""
public class MyConstants {
public static final int CONSTANT = 1;
public static class Inner {
public static final String INNER_CONSTANT = "const";
}""".trimIndent())
}
fun testAccessFromDefaultPackage() {
doTest("""
class C {
private int field = MyConstants.CONSTANT;
}""".trimIndent())
}
fun testImportsFromDefaultPackage() {
doTest("""
import <error descr="Class 'MyConstants' is in the default package">MyConstants</error>;
import <error descr="Class 'MyConstants' is in the default package">MyConstants</error>.Inner;
import static <error descr="Class 'MyConstants' is in the default package">MyConstants</error>.*;
import static <error descr="Class 'MyConstants' is in the default package">MyConstants</error>.Inner.*;
import static <error descr="Class 'MyConstants' is in the default package">MyConstants</error>.Inner.INNER_CONSTANT;
""".trimIndent())
}
@Bombed(user = "Roman Shevchenko", day = 1, month = Calendar.DECEMBER)
fun testAccessFromNormalCode() {
doTest("""
package pkg;
import <error descr="Cannot resolve symbol 'MyConstants'">MyConstants</error>;
import <error descr="Cannot resolve symbol 'MyConstants'">MyConstants</error>.Inner;
class C {
<error descr="Cannot resolve symbol 'MyConstants'">MyConstants</error> f = null;
Object o = new <error descr="Cannot resolve symbol 'MyConstants'">MyConstants</error>.Inner();
int i = <error descr="Cannot resolve symbol 'MyConstants'">MyConstants</error>.CONSTANT;
}""".trimIndent())
}
private fun doTest(text: String) {
myFixture.configureByText("test.java", text)
myFixture.checkHighlighting()
}
}
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.daemon;
import com.intellij.ToolExtensionPoints;
@@ -263,8 +249,6 @@ public class LightAdvHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testIDEADEV11919() { doTest(false); }
public void testIDEA67829() { doTest(false); }
public void testMethodCannotBeApplied() { doTest(false); }
public void testDefaultPackageClassInStaticImport() { doTest(false); }
public void testUnusedParamsOfPublicMethod() { doTest(true); }
public void testInnerClassesShadowing() { doTest(false); }
@@ -1,6 +1,4 @@
// Copyright 2000-2017 JetBrains s.r.o.
// Use of this source code is governed by the Apache 2.0 license that can be
// found in the LICENSE file.
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.daemon
import com.intellij.codeInsight.daemon.impl.JavaHighlightInfoTypes
@@ -383,6 +381,15 @@ class ModuleHighlightingTest : LightJava9ModulesCodeInsightFixtureTestCase() {
}""".trimIndent())
}
fun testAccessingDefaultPackage() {
addFile("X.java", "public class X {\n public static class XX extends X { }\n}")
highlight("""
module M {
uses <error descr="Class 'X' is in the default package">X</error>;
provides <error descr="Class 'X' is in the default package">X</error> with <error descr="Class 'X' is in the default package">X</error>.XX;
}""".trimIndent())
}
//<editor-fold desc="Helpers.">
private fun highlight(text: String) = highlight("module-info.java", text)