From 2c92eae462cd6963462fc983458dd91f72494049 Mon Sep 17 00:00:00 2001 From: Daniel Beck <1831569+daniel-beck@users.noreply.github.com> Date: Mon, 21 Oct 2024 20:35:37 +0200 Subject: [PATCH] [JENKINS-73768] Handle base=derived case in Util#isOverridden (#9728) Co-authored-by: Daniel Beck --- core/src/main/java/hudson/Util.java | 4 ++++ core/src/test/java/hudson/util/IsOverriddenTest.java | 3 +++ 2 files changed, 7 insertions(+) diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index b4529a0ea279..07d00a44d006 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -1525,6 +1525,10 @@ public static Number tryParseNumber(@CheckForNull String numberStr, @CheckForNul * does not contain the specified method. */ public static boolean isOverridden(@NonNull Class base, @NonNull Class derived, @NonNull String methodName, @NonNull Class... types) { + if (base == derived) { + // If base and derived are the same type, the method is not overridden by definition + return false; + } // If derived is not a subclass or implementor of base, it can't override any method // Technically this should also be triggered when base == derived, because it can't override its own method, but // the unit tests explicitly test for that as working. diff --git a/core/src/test/java/hudson/util/IsOverriddenTest.java b/core/src/test/java/hudson/util/IsOverriddenTest.java index c608e7453f9a..4711c5edf0a6 100644 --- a/core/src/test/java/hudson/util/IsOverriddenTest.java +++ b/core/src/test/java/hudson/util/IsOverriddenTest.java @@ -31,6 +31,7 @@ import static org.junit.Assert.assertTrue; import hudson.Util; +import java.io.PrintWriter; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ErrorCollector; @@ -52,6 +53,8 @@ public void isOverriddenTest() { assertTrue(Util.isOverridden(Base.class, Derived.class, "method")); assertTrue(Util.isOverridden(Base.class, Intermediate.class, "method")); assertFalse(Util.isOverridden(Base.class, Base.class, "method")); + assertFalse(Util.isOverridden(Throwable.class, Throwable.class, "printStackTrace", PrintWriter.class)); + assertFalse(Util.isOverridden(Throwable.class, Exception.class, "printStackTrace", PrintWriter.class)); assertTrue(Util.isOverridden(Base.class, Intermediate.class, "setX", Object.class)); assertTrue(Util.isOverridden(Base.class, Intermediate.class, "getX")); }