From ecee694ed942cf10188db5986bf9dd47dfd2a8c0 Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Thu, 17 Nov 2016 12:42:07 +0300 Subject: [PATCH] Java: Added test for the global inspection "Unused 'requires' statement in module-info" (IDEA-163139) --- .../reference/RefJavaManager.java | 1 + .../reference/RefJavaModule.java | 26 +++++++ .../reference/RefJavaManagerImpl.java | 3 + .../reference/RefJavaModuleImpl.java | 53 ++++++++++++++ .../Java9UnusedRequiresStatementTest.kt | 73 +++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 java/java-analysis-api/src/com/intellij/codeInspection/reference/RefJavaModule.java create mode 100644 java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefJavaModuleImpl.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInspection/Java9UnusedRequiresStatementTest.kt diff --git a/java/java-analysis-api/src/com/intellij/codeInspection/reference/RefJavaManager.java b/java/java-analysis-api/src/com/intellij/codeInspection/reference/RefJavaManager.java index 08a14174e63c..774346ba5f21 100644 --- a/java/java-analysis-api/src/com/intellij/codeInspection/reference/RefJavaManager.java +++ b/java/java-analysis-api/src/com/intellij/codeInspection/reference/RefJavaManager.java @@ -37,6 +37,7 @@ public abstract class RefJavaManager implements RefManagerExtension MANAGER = Key.create("RefJavaManager"); diff --git a/java/java-analysis-api/src/com/intellij/codeInspection/reference/RefJavaModule.java b/java/java-analysis-api/src/com/intellij/codeInspection/reference/RefJavaModule.java new file mode 100644 index 000000000000..848128b61d3e --- /dev/null +++ b/java/java-analysis-api/src/com/intellij/codeInspection/reference/RefJavaModule.java @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2016 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. + */ +package com.intellij.codeInspection.reference; + +import com.intellij.psi.PsiJavaModule; + +/** + * @author Pavel.Dolgov + */ +public interface RefJavaModule extends RefElement { + @Override + PsiJavaModule getElement(); +} diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefJavaManagerImpl.java b/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefJavaManagerImpl.java index 12f9dc818cd0..3bb528353324 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefJavaManagerImpl.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefJavaManagerImpl.java @@ -300,6 +300,9 @@ public class RefJavaManagerImpl extends RefJavaManager { else if (ref instanceof RefPackage) { return PACKAGE; } + else if (ref instanceof RefJavaModule) { + return JAVA_MODULE; + } return null; } diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefJavaModuleImpl.java b/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefJavaModuleImpl.java new file mode 100644 index 000000000000..579388d882fd --- /dev/null +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefJavaModuleImpl.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2016 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. + */ +package com.intellij.codeInspection.reference; + +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiJavaModule; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author Pavel.Dolgov + */ +public class RefJavaModuleImpl extends RefElementImpl implements RefJavaModule { + + protected RefJavaModuleImpl(@NotNull String name, @NotNull RefElement owner) { + super(name, owner); + } + + protected RefJavaModuleImpl(@NotNull PsiFile file, @NotNull RefManager manager) { + super(file, manager); + } + + protected RefJavaModuleImpl(@NotNull String name, + @NotNull PsiElement element, + @NotNull RefManager manager) { + super(name, element, manager); + } + + @Override + protected void initialize() { + + } + + @Nullable + @Override + public PsiJavaModule getElement() { + return (PsiJavaModule)super.getElement(); + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/Java9UnusedRequiresStatementTest.kt b/java/java-tests/testSrc/com/intellij/codeInspection/Java9UnusedRequiresStatementTest.kt new file mode 100644 index 000000000000..7e908300ddac --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInspection/Java9UnusedRequiresStatementTest.kt @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2016 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. + */ +package com.intellij.codeInspection + +import com.intellij.codeInspection.java19modules.Java9NonAccessibleTypeExposedInspection +import com.intellij.codeInspection.java19modules.Java9UnusedRequiresStatementInspection +import com.intellij.testFramework.fixtures.LightJava9ModulesCodeInsightFixtureTestCase +import com.intellij.testFramework.fixtures.MultiModuleJava9ProjectDescriptor +import com.intellij.testFramework.fixtures.MultiModuleJava9ProjectDescriptor.ModuleDescriptor +import org.intellij.lang.annotations.Language +import org.jetbrains.annotations.NonNls +import org.jetbrains.annotations.NotNull + +/** + * @author Pavel.Dolgov + */ +class Java9UnusedRequiresStatementTest : LightJava9ModulesCodeInsightFixtureTestCase() { + override fun setUp() { + super.setUp() + myFixture.enableInspections(Java9UnusedRequiresStatementInspection()) + + addFile("module-info.java", "module M2 { exports org.example.m2; }", ModuleDescriptor.M2) + addFile("module-info.java", "module M4 { exports org.example.m4; }", ModuleDescriptor.M4) + addFile("module-info.java", "module M6 { exports org.example.m6; requires M7; }", ModuleDescriptor.M6) + addFile("module-info.java", "module M7 { exports org.example.m7; }", ModuleDescriptor.M7) + + add("org.example.m2", "C2", ModuleDescriptor.M2) + add("org.example.m4", "C4", ModuleDescriptor.M4) + add("org.example.m6", "C6", ModuleDescriptor.M6) + add("org.example.m7", "C7", ModuleDescriptor.M7) + } + + fun test1() { + highlight("module MAIN { requires M2; }") + } + + fun test2() { + addMain("import org.example.m2.*; public class Main { C2 field; }") + highlight("module MAIN { requires M2; }") + } + + private fun highlight(@Language("JAVA") @NonNls text: String) { + val file = addFile("module-info.java", text, ModuleDescriptor.MAIN) + myFixture.configureFromExistingVirtualFile(file) + myFixture.checkHighlighting() + } + + private fun add(@NonNls packageName: String, @NonNls className: String, module: ModuleDescriptor) { + addFile("${packageName.replace('.', '/')}/${className}.java", + "package ${packageName}; public class @{className} { }", + module = module) + } + + private fun addMain(@Language("JAVA") @NonNls text: String) { + addFile("org.example.main/Main.java", + "package org.example.main; ${text}", + module = ModuleDescriptor.MAIN) + } + +} \ No newline at end of file