Java: fix "Overloaded varargs method" inspection false positive (IDEA-377692)

GitOrigin-RevId: 2a1de9daa11d6f3444db6daea3619f9b29c51c04
This commit is contained in:
Bas Leijdekkers
2025-08-28 19:42:36 +00:00
committed by intellij-monorepo-bot
parent 67ce471725
commit 8cd9b74820
2 changed files with 76 additions and 64 deletions
@@ -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;
}
}
@@ -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<T> {" +
" void makeItSo(T command, int... values);" +
" }" +
" class Bar implements Foo<String> {" +
" public void makeItSo(final String command, final int... values) {" +
" }" +
" }");
doTest("""
interface Foo<T> {
void makeItSo(T command, int... values);
}
class Bar implements Foo<String> {
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<? extends RequestDataValueProcessor> processors;
public CompositeRequestDataValueProcessor(final RequestDataValueProcessor... processors) {
this(Arrays.asList(processors));
}
public CompositeRequestDataValueProcessor(final List<? extends RequestDataValueProcessor> 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) {}
}""");
}
}