From 5c58f82c0d1ddb5e5ddf6cb753748328f966b141 Mon Sep 17 00:00:00 2001 From: Daniel Beck Date: Sat, 25 Apr 2020 23:01:14 +0200 Subject: [PATCH] [JENKINS-62056] Fix ExtensionComponent#compareTo --- .../main/java/hudson/ExtensionComponent.java | 36 ++++++++++++++++--- .../test/java/hudson/ExtensionListTest.java | 5 +++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/hudson/ExtensionComponent.java b/core/src/main/java/hudson/ExtensionComponent.java index d274e11204..e4e35ebe1d 100644 --- a/core/src/main/java/hudson/ExtensionComponent.java +++ b/core/src/main/java/hudson/ExtensionComponent.java @@ -91,14 +91,40 @@ public class ExtensionComponent implements Comparable> if (Double.compare(a, b) > 0) return -1; if (Double.compare(a, b) < 0) return 1; - // make the order bit more deterministic among extensions of the same ordinal - if (this.instance instanceof Descriptor && that.instance instanceof Descriptor) { + boolean thisIsDescriptor = false; + String thisLabel = this.instance.getClass().getName(); + if (this.instance instanceof Descriptor) { try { - return Util.fixNull(((Descriptor)this.instance).getDisplayName()).compareTo(Util.fixNull(((Descriptor)that.instance).getDisplayName())); + thisLabel = Util.fixNull(((Descriptor) this.instance).getDisplayName()); + thisIsDescriptor = true; } catch (RuntimeException | LinkageError x) { - LOG.log(Level.WARNING, null, x); + LOG.log(Level.WARNING, "Failure during Descriptor#getDisplayName for " + this.instance.getClass().getName(), x); } } - return this.instance.getClass().getName().compareTo(that.instance.getClass().getName()); + + boolean thatIsDescriptor = false; + String thatLabel = that.instance.getClass().getName(); + if (that.instance instanceof Descriptor) { + try { + thatLabel = Util.fixNull(((Descriptor) that.instance).getDisplayName()); + thatIsDescriptor = true; + } catch (RuntimeException | LinkageError x) { + LOG.log(Level.WARNING, "Failure during Descriptor#getDisplayName for " + that.instance.getClass().getName(), x); + } + } + + if (thisIsDescriptor) { + if (thatIsDescriptor) { + return thisLabel.compareTo(thatLabel); + } else { + return 1; + } + } else { + if (thatIsDescriptor) { + return -1; + } + } + + return thisLabel.compareTo(thatLabel); } } diff --git a/test/src/test/java/hudson/ExtensionListTest.java b/test/src/test/java/hudson/ExtensionListTest.java index 3c3a6dddb5..de2ee11d53 100644 --- a/test/src/test/java/hudson/ExtensionListTest.java +++ b/test/src/test/java/hudson/ExtensionListTest.java @@ -217,4 +217,9 @@ public class ExtensionListTest { assertEquals(0, list.size()); } + @Issue("JENKINS-62056") + @Test + public void checkSort() { + ExtensionList.lookup(Object.class).get(0); // exceptions are a problem + } }