From dbb8a2bac3f2d3069615e73867bff2185ec3d4a5 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 11 Oct 2019 16:22:17 +0200 Subject: [PATCH] unused declaration: a class with non-static fields is not a utility class GitOrigin-RevId: 4c3f04dce18ee56fb0a0fe3beeee405fe91f750e --- .../reference/RefClassImpl.java | 10 ++++-- .../deadCode/nonUtility/expected.xml | 32 +++++++++++++++++++ .../deadCode/nonUtility/src/NonUtility.java | 6 ++++ .../codeInspection/UnusedDeclarationTest.java | 20 +++--------- 4 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 java/java-tests/testData/inspection/deadCode/nonUtility/expected.xml create mode 100644 java/java-tests/testData/inspection/deadCode/nonUtility/src/NonUtility.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefClassImpl.java b/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefClassImpl.java index c883b8ec3bb9..daa4e5fdf179 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefClassImpl.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/reference/RefClassImpl.java @@ -167,13 +167,17 @@ public class RefClassImpl extends RefJavaElementImpl implements RefClass { } } } + if (!isInterface()) { + for (int i = 0; i < uFields.length && utilityClass; i++) { + if (!uFields[i].isStatic()) { + utilityClass = false; + } + } + } if (!utilityClass) { utilityClass = ClassUtils.isSingleton(uClass.getJavaPsi()); } - if (utilityClass && !isInterface() && uMethods.length == 0 && ContainerUtil.find(uFields, UField::isStatic) == null) { - utilityClass = false; - } if (varargConstructor != null && getDefaultConstructor() == null) { setDefaultConstructor((RefMethodImpl)varargConstructor); diff --git a/java/java-tests/testData/inspection/deadCode/nonUtility/expected.xml b/java/java-tests/testData/inspection/deadCode/nonUtility/expected.xml new file mode 100644 index 000000000000..c8fbfbb3a2da --- /dev/null +++ b/java/java-tests/testData/inspection/deadCode/nonUtility/expected.xml @@ -0,0 +1,32 @@ + + + + + NonUtility.java + 1 + unused declaration + Class is not instantiated. + + + + NonUtility.java + 2 + unused declaration + Field has no usages. + + + + NonUtility.java + 3 + unused declaration + Field has no usages. + + + + NonUtility.java + 5 + unused declaration + Method is never used. + + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/deadCode/nonUtility/src/NonUtility.java b/java/java-tests/testData/inspection/deadCode/nonUtility/src/NonUtility.java new file mode 100644 index 000000000000..72e686990b20 --- /dev/null +++ b/java/java-tests/testData/inspection/deadCode/nonUtility/src/NonUtility.java @@ -0,0 +1,6 @@ +public class NonUtility { + private int i; + private int j; + + public static void utility() {} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java index c1cf4a92e7a7..04cc5e6b110b 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java @@ -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-2019 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.codeInspection; import com.intellij.codeInspection.ex.EntryPointsManagerBase; @@ -142,6 +128,10 @@ public class UnusedDeclarationTest extends AbstractUnusedDeclarationTest { doTest5(); } + public void testNonUtility() { + doTest(); + } + public void testJunitEntryPoint() { doTest(); }