Java: don't consider constructors used when the class is used externally (IDEA-206602)

for global "Unused declaration" inspection

GitOrigin-RevId: e671cef5b81a2e148ec747e91e876959c0481d8d
This commit is contained in:
Bas Leijdekkers
2024-05-03 16:06:52 +00:00
committed by intellij-monorepo-bot
parent 02f130cf0e
commit 734fd63f37
6 changed files with 33 additions and 11 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection.ex;
import com.intellij.codeInsight.AnnotationUtil;
@@ -268,13 +268,8 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
return;
}
List<RefMethod> refConstructors = refClass.getConstructors();
if (refConstructors.size() == 1) {
addEntryPoint(refConstructors.get(0), isPersistent);
}
else if (refConstructors.size() > 1) {
// Many constructors here. Need to ask user which ones are used
for (RefMethod refConstructor : refConstructors) {
if (isPersistent) {
for (RefMethod refConstructor : refClass.getConstructors()) {
addEntryPoint(refConstructor, isPersistent);
}
}
@@ -1,3 +1,7 @@
class B extends A {
public static void main(String[] args) {
new B();
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>UsefulClass.java</file>
<line>4</line>
<entry_point TYPE="method" FQNAME="a.UsefulClass UsefulClass()" />
<description>Constructor is never used.</description>
</problem>
</problems>
@@ -0,0 +1,6 @@
package a;
public class UsefulClass {
public UsefulClass() {
}
}
@@ -0,0 +1,6 @@
package b;
import a.UsefulClass;
public class Another {
}
@@ -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-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.codeInspection;
import com.intellij.analysis.AnalysisScope;
@@ -29,6 +27,10 @@ public class UnusedDeclarationInSubScopeTest extends AbstractUnusedDeclarationTe
doTest();
}
public void testExternallyUsedClass() {
doTest();
}
@Override
protected void doTest() {
doTest("deadCode/" + getTestName(true), myToolWrapper);