unused library: don't check runtime dependencies (IDEA-178133ˆ)

This commit is contained in:
Anna Kozlova
2017-08-29 16:21:03 +03:00
parent 7f29026b10
commit d01a145417
5 changed files with 20 additions and 1 deletions
@@ -100,7 +100,9 @@ public class UnusedLibrariesInspection extends GlobalInspectionTool {
final List<CommonProblemDescriptor> result = new ArrayList<>();
for (OrderEntry entry : moduleRootManager.getOrderEntries()) {
if (entry instanceof LibraryOrderEntry && !((LibraryOrderEntry)entry).isExported()) {
if (entry instanceof LibraryOrderEntry &&
!((LibraryOrderEntry)entry).isExported() &&
((LibraryOrderEntry)entry).getScope() != DependencyScope.RUNTIME) {
final Set<VirtualFile> files = new HashSet<>(Arrays.asList(((LibraryOrderEntry)entry).getRootFiles(OrderRootType.CLASSES)));
boolean allRootsUnused = usedRoots == null || !files.removeAll(usedRoots);
if (allRootsUnused) {
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,2 @@
class Test {
}
@@ -19,6 +19,10 @@ package com.intellij.java.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.analysis.AnalysisScope;
import com.intellij.codeInspection.unusedLibraries.UnusedLibrariesInspection;
import com.intellij.openapi.roots.DependencyScope;
import com.intellij.openapi.roots.LibraryOrderEntry;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.OrderEntry;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.project.IntelliJProjectConfiguration;
import com.intellij.testFramework.InspectionTestCase;
@@ -35,6 +39,13 @@ public class UnusedLibraryInspectionTest extends InspectionTestCase {
protected void setupRootModel(@NotNull String testDir, @NotNull VirtualFile[] sourceDir, String sdkName) {
super.setupRootModel(testDir, sourceDir, sdkName);
PsiTestUtil.addProjectLibrary(getModule(), "JUnit", IntelliJProjectConfiguration.getProjectLibraryClassesRootPaths("JUnit4"));
if (getTestName(true).endsWith("Runtime")) {
for (OrderEntry entry : ModuleRootManager.getInstance(getModule()).getOrderEntries()) {
if (entry instanceof LibraryOrderEntry && "JUnit".equals(((LibraryOrderEntry)entry).getLibraryName())) {
((LibraryOrderEntry)entry).setScope(DependencyScope.RUNTIME);
}
}
}
}
private void doTest() {
@@ -49,6 +60,7 @@ public class UnusedLibraryInspectionTest extends InspectionTestCase {
public void testSimple() { doTest(); }
public void testUsedJunit() { doTest(); }
public void testJunitAsRuntime() { doTest(); }
public void testUsedJunitFromField() { doTest(); }
public void testUsedInParameterAnnotation() { doTest(); }
}