java: report unused utility classes with unused methods as unused (IDEA-217378)

for "Unused declaration" inspection in batch mode

GitOrigin-RevId: f19b13dd262f0059adc4c08237e620ec8256e013
This commit is contained in:
Bas Leijdekkers
2024-04-22 23:27:18 +00:00
committed by intellij-monorepo-bot
parent 4440657a8d
commit 34d1d3a30f
4 changed files with 28 additions and 11 deletions
@@ -140,7 +140,7 @@ public final class RefClassImpl extends RefJavaElementImpl implements RefClass {
if (uMethod.isConstructor()) {
constructorSeen = true;
final List<UParameter> parameters = uMethod.getUastParameters();
if (!parameters.isEmpty()|| uMethod.getVisibility() != UastVisibility.PRIVATE) {
if (!parameters.isEmpty() || uMethod.getVisibility() != UastVisibility.PRIVATE) {
utilityClass = false;
}
@@ -439,11 +439,6 @@ public final class RefClassImpl extends RefJavaElementImpl implements RefClass {
return checkFlag(IS_ANNOTATION_MASK);
}
@Override
public boolean isSuspicious() {
return !(isUtilityClass() && getOutReferences().isEmpty()) && super.isSuspicious();
}
@Override
public boolean isUtilityClass() {
return checkFlag(IS_UTILITY_MASK);
@@ -527,10 +522,10 @@ public final class RefClassImpl extends RefJavaElementImpl implements RefClass {
if (super.isReferenced()) return true;
if (isInterface()) {
if (!getDerivedReferences().isEmpty()) return true;
return !getDerivedReferences().isEmpty();
}
else if (isAbstract()) {
if (!getSubClasses().isEmpty()) return true;
return !getSubClasses().isEmpty();
}
return false;
@@ -541,10 +536,10 @@ public final class RefClassImpl extends RefJavaElementImpl implements RefClass {
if (super.hasSuspiciousCallers()) return true;
if (isInterface()) {
if (!getDerivedReferences().isEmpty()) return true;
return !getDerivedReferences().isEmpty();
}
else if (isAbstract()) {
if (!getSubClasses().isEmpty()) return true;
return !getSubClasses().isEmpty();
}
return false;
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Example.java</file>
<line>4</line>
<description>Method is never used.</description>
</problem>
<problem>
<file>Example.java</file>
<line>3</line>
<description>No class references have been found. Class static initializer is not reachable.</description>
</problem>
</problems>
@@ -0,0 +1,5 @@
package deadCode.utilityClass.src;
public class Example {
public static void test() {}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2022 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.java.codeInspection;
import com.intellij.codeInspection.ex.EntryPointsManagerBase;
@@ -267,6 +267,10 @@ public class UnusedDeclarationInspectionTest extends AbstractUnusedDeclarationTe
doTest();
}
public void testUtilityClass() {
doTest();
}
public void testJunitMethodSource() {doTest();}
public void testImplicitClass() {