diff --git a/java/java-analysis-impl/src/com/siyeh/ig/naming/OverloadedVarargsMethodInspection.java b/java/java-analysis-impl/src/com/siyeh/ig/naming/OverloadedVarargsMethodInspection.java index 44953827c60f..b4d7af197d07 100644 --- a/java/java-analysis-impl/src/com/siyeh/ig/naming/OverloadedVarargsMethodInspection.java +++ b/java/java-analysis-impl/src/com/siyeh/ig/naming/OverloadedVarargsMethodInspection.java @@ -1,4 +1,4 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.siyeh.ig.naming; import com.intellij.codeInspection.options.OptPane; @@ -58,12 +58,11 @@ public final class OverloadedVarargsMethodInspection extends BaseInspection { final PsiMethod[] sameNameMethods = aClass.findMethodsByName(methodName, true); for (PsiMethod sameNameMethod : sameNameMethods) { PsiClass superClass = sameNameMethod.getContainingClass(); - PsiSubstitutor substitutor = superClass != null ? TypeConversionUtil.getSuperClassSubstitutor(superClass, aClass, PsiSubstitutor.EMPTY) - : PsiSubstitutor.EMPTY; - if (!MethodSignatureUtil.areSignaturesEqual(sameNameMethod.getSignature(substitutor), - method.getSignature(PsiSubstitutor.EMPTY))) { - if (ignoreInconvertibleTypes && !areConvertibleTypesWithVarArgs(method.getParameterList(), - sameNameMethod.getParameterList())) { + PsiSubstitutor substitutor = superClass != null + ? TypeConversionUtil.getSuperClassSubstitutor(superClass, aClass, PsiSubstitutor.EMPTY) + : PsiSubstitutor.EMPTY; + if (!MethodSignatureUtil.areSignaturesEqual(sameNameMethod.getSignature(substitutor), method.getSignature(PsiSubstitutor.EMPTY))) { + if (ignoreInconvertibleTypes && !areConvertibleTypesWithVarArgs(method.getParameterList(), sameNameMethod.getParameterList())) { continue; } registerMethodError(method, method); @@ -93,7 +92,7 @@ public final class OverloadedVarargsMethodInspection extends BaseInspection { PsiType otherType = getTypeForComparison(otherParameters[i]); - if (!type.isConvertibleFrom(otherType) && !otherType.isConvertibleFrom(type)) { + if (!type.isAssignableFrom(otherType) && !otherType.isAssignableFrom(type)) { return false; } } diff --git a/java/java-tests/testSrc/com/siyeh/ig/naming/OverloadedVarargsMethodInspectionTest.java b/java/java-tests/testSrc/com/siyeh/ig/naming/OverloadedVarargsMethodInspectionTest.java index 2e9e48927281..541fba544c58 100644 --- a/java/java-tests/testSrc/com/siyeh/ig/naming/OverloadedVarargsMethodInspectionTest.java +++ b/java/java-tests/testSrc/com/siyeh/ig/naming/OverloadedVarargsMethodInspectionTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2014 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-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.siyeh.ig.naming; import com.intellij.codeInspection.InspectionProfileEntry; @@ -29,64 +15,91 @@ public class OverloadedVarargsMethodInspectionTest extends LightJavaInspectionTe } public void testOneWarning() { - doTest("class Overload {" + - " public void overload() {}" + - " public void overload(int p1) {}" + - " public void overload(int p1, String p2) {}" + - " public void /*Overloaded varargs method 'overload()'*/overload/**/(int p1, String p2, String... p3) {}" + - "}"); + doTest(""" + class Overload { + public void overload() {} + public void overload(int p1) {} + public void overload(int p1, String p2) {} + public void /*Overloaded varargs method 'overload()'*/overload/**/(int p1, String p2, String... p3) {} + }"""); } public void testWarnWhenSuperMethod() { - doTest("class Super {" + - " public void method() {}" + - "}" + - "class Overload extends Super {" + - " public void /*Overloaded varargs method 'method()'*/method/**/(String... ss) {}" + - "}"); + doTest(""" + class Super { + public void method() {} + } + class Overload extends Super { + public void /*Overloaded varargs method 'method()'*/method/**/(String... ss) {} + }"""); } public void testOverridingMethod() { - doTest("interface Base {" + - " void test(String... ss);" + - "}" + - "class Impl implements Base {" + - " public void test(String... ss) {}" + - "}"); + doTest(""" + interface Base { + void test(String... ss); + } + class Impl implements Base { + public void test(String... ss) {} + }"""); } public void testGenericMethods() { - doTest("interface Foo {" + - " void makeItSo(T command, int... values);" + - " }" + - " class Bar implements Foo {" + - " public void makeItSo(final String command, final int... values) {" + - " }" + - " }"); + doTest(""" + interface Foo { + void makeItSo(T command, int... values); + } + class Bar implements Foo { + public void makeItSo(final String command, final int... values) { + } + }"""); } public void testNoWarningBecauseOfTypes() { - doTest("class Overload {" + - " public void method() {}" + - " public void method(int p1) {}" + - " public void method(int p1, int p2) {}" + - " public void method(int p1, String p2, int p3) {}" + - " public void method(int p1, String p2, String p3, int p4) {}" + - " public void method(int p1, String p2, String... p3) {}" + - "}"); + doTest(""" + class Overload { + public void method() {} + public void method(int p1) {} + public void method(int p1, int p2) {} + public void method(int p1, String p2, int p3) {} + public void method(int p1, String p2, String p3, int p4) {} + public void method(int p1, String p2, String... p3) {} + }"""); + } + + public void testNoWarningBecauseOfTypes2() { + doTest(""" + import java.util.*; + final class CompositeRequestDataValueProcessor { + + private final List processors; + + public CompositeRequestDataValueProcessor(final RequestDataValueProcessor... processors) { + this(Arrays.asList(processors)); + } + + public CompositeRequestDataValueProcessor(final List processors) { + this.processors = processors; + } + } + + interface RequestDataValueProcessor {} + """); } public void testWarningForConvertibleArgumentTypes() { - doTest("class Overload {" + - " public void method(Number p1, String p2) {}" + - " public void /*Overloaded varargs method 'method()'*/method/**/(Integer p1, String p2, String... p3) {}" + - "}"); + doTest(""" + class Overload { + public void method(Number p1, String p2) {} + public void /*Overloaded varargs method 'method()'*/method/**/(Integer p1, String p2, String... p3) {} + }"""); } public void testWarningWithOneArgument() { - doTest("class Overload {" + - " public void method() {}" + - " public void /*Overloaded varargs method 'method()'*/method/**/(String... p1) {}" + - "}"); + doTest(""" + class Overload { + public void method() {} + public void /*Overloaded varargs method 'method()'*/method/**/(String... p1) {} + }"""); } }