From 3cbe9e2a4df99ad2cb78e526f633b448dda409cd Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 13 Jun 2024 09:39:37 -0700 Subject: [PATCH] Use pattern matching for `instanceof` --- .../kohsuke/stapler/HttpResponseRenderer.java | 3 +-- .../java/org/kohsuke/stapler/RequestImpl.java | 18 ++++++------------ .../main/java/org/kohsuke/stapler/Stapler.java | 3 +-- .../main/java/org/kohsuke/stapler/WebApp.java | 3 +-- .../kohsuke/stapler/bind/BoundObjectTable.java | 3 +-- .../org/kohsuke/stapler/export/TypeUtil.java | 18 ++++++------------ .../stapler/jelly/groovy/JellyBuilder.java | 3 +-- .../stapler/jelly/CustomJellyContext.java | 3 +-- .../stapler/jelly/DefaultScriptInvoker.java | 3 +-- .../org/kohsuke/stapler/jelly/IncludeTag.java | 3 +-- .../stapler/jelly/JellyClassTearOff.java | 6 ++---- 11 files changed, 22 insertions(+), 44 deletions(-) diff --git a/core/src/main/java/org/kohsuke/stapler/HttpResponseRenderer.java b/core/src/main/java/org/kohsuke/stapler/HttpResponseRenderer.java index f0e2f269a..b01fd35e6 100644 --- a/core/src/main/java/org/kohsuke/stapler/HttpResponseRenderer.java +++ b/core/src/main/java/org/kohsuke/stapler/HttpResponseRenderer.java @@ -123,9 +123,8 @@ protected boolean handlePrimitive(StaplerResponse rsp, Object response) throws I protected boolean handleHttpResponse(StaplerRequest req, StaplerResponse rsp, Object node, Object response) throws IOException, ServletException { - if (response instanceof HttpResponse) { + if (response instanceof HttpResponse r) { // let the result render the response - HttpResponse r = (HttpResponse) response; try { r.generateResponse(req, rsp, node); } catch (IOException | ServletException | RuntimeException e) { diff --git a/core/src/main/java/org/kohsuke/stapler/RequestImpl.java b/core/src/main/java/org/kohsuke/stapler/RequestImpl.java index f8fdd4dab..4219707b0 100644 --- a/core/src/main/java/org/kohsuke/stapler/RequestImpl.java +++ b/core/src/main/java/org/kohsuke/stapler/RequestImpl.java @@ -639,15 +639,12 @@ public void bindJSON(Object bean, JSONObject src) { @Override public List bindJSONToList(Class type, Object src) { ArrayList r = new ArrayList<>(); - if (src instanceof JSONObject) { - JSONObject j = (JSONObject) src; + if (src instanceof JSONObject j) { r.add(bindJSON(type, j)); } - if (src instanceof JSONArray) { - JSONArray a = (JSONArray) src; + if (src instanceof JSONArray a) { for (Object o : a) { - if (o instanceof JSONObject) { - JSONObject j = (JSONObject) o; + if (o instanceof JSONObject j) { r.add(bindJSON(type, j)); } } @@ -786,8 +783,7 @@ public Object convertJSON(Object o) { Lister l = Lister.create(type, genericType); - if (o instanceof JSONObject) { - JSONObject j = (JSONObject) o; + if (o instanceof JSONObject j) { if (j.isNullObject()) { // another flavor of null. json-lib sucks. return ReflectionUtils.getVmDefaultValueFor(type); @@ -890,12 +886,11 @@ public Object convertJSON(Object o) { return l.toCollection(); } } - if (o instanceof JSONArray) { + if (o instanceof JSONArray a) { if (l == null) { throw new WrongTypeException( String.format("Got type array but no lister class found for type %s", type)); } - JSONArray a = (JSONArray) o; TypePair itemType = new TypePair(l.itemGenericType, l.itemType); for (Object item : a) { l.add(itemType.convertJSON(item)); @@ -982,8 +977,7 @@ private Object instantiate(Class actualType, JSONObject j) { * Calls {@link DataBoundResolvable#bindResolve(StaplerRequest, JSONObject)} if the object has it. */ private Object bindResolve(Object o, JSONObject src) { - if (o instanceof DataBoundResolvable) { - DataBoundResolvable dbr = (DataBoundResolvable) o; + if (o instanceof DataBoundResolvable dbr) { o = dbr.bindResolve(this, src); } return o; diff --git a/core/src/main/java/org/kohsuke/stapler/Stapler.java b/core/src/main/java/org/kohsuke/stapler/Stapler.java index dd74985d6..9c639657f 100644 --- a/core/src/main/java/org/kohsuke/stapler/Stapler.java +++ b/core/src/main/java/org/kohsuke/stapler/Stapler.java @@ -799,8 +799,7 @@ boolean tryInvoke(RequestImpl req, ResponseImpl rsp, Object node) throws IOExcep ancestor.addToOwner(); // try overrides - if (node instanceof StaplerOverridable) { - StaplerOverridable o = (StaplerOverridable) node; + if (node instanceof StaplerOverridable o) { Collection list = o.getOverrides(); if (list != null) { int count = 0; diff --git a/core/src/main/java/org/kohsuke/stapler/WebApp.java b/core/src/main/java/org/kohsuke/stapler/WebApp.java index 76381a30b..3525bada8 100644 --- a/core/src/main/java/org/kohsuke/stapler/WebApp.java +++ b/core/src/main/java/org/kohsuke/stapler/WebApp.java @@ -270,8 +270,7 @@ public MetaClass getMetaClass(Object o) { } public Klass getKlass(Object o) { - if (o instanceof KInstance) { - KInstance ki = (KInstance) o; + if (o instanceof KInstance ki) { Klass k = ki.getKlass(); if (k != null) { return k; diff --git a/core/src/main/java/org/kohsuke/stapler/bind/BoundObjectTable.java b/core/src/main/java/org/kohsuke/stapler/bind/BoundObjectTable.java index 8be6b2453..7683e6dfa 100644 --- a/core/src/main/java/org/kohsuke/stapler/bind/BoundObjectTable.java +++ b/core/src/main/java/org/kohsuke/stapler/bind/BoundObjectTable.java @@ -217,8 +217,7 @@ public static class Table implements Serializable { private synchronized Bound add(Ref ref) { final Object target = ref.get(); - if (target instanceof WithWellKnownURL) { - WithWellKnownURL w = (WithWellKnownURL) target; + if (target instanceof WithWellKnownURL w) { String url = w.getWellKnownUrl(); if (!url.startsWith("/")) { LOGGER.warning("WithWellKnownURL.getWellKnownUrl must start with a slash. But we got " + url diff --git a/core/src/main/java/org/kohsuke/stapler/export/TypeUtil.java b/core/src/main/java/org/kohsuke/stapler/export/TypeUtil.java index 0fcdd1901..c6f8e26e3 100644 --- a/core/src/main/java/org/kohsuke/stapler/export/TypeUtil.java +++ b/core/src/main/java/org/kohsuke/stapler/export/TypeUtil.java @@ -391,9 +391,8 @@ public Type getOwnerType() { */ @Override public boolean equals(Object o) { - if (o instanceof ParameterizedType) { + if (o instanceof ParameterizedType that) { // Check that information is equivalent - ParameterizedType that = (ParameterizedType) o; if (this == that) { return true; @@ -517,8 +516,7 @@ public String toString() { @Override public boolean equals(Object o) { - if (o instanceof GenericArrayType) { - GenericArrayType that = (GenericArrayType) o; + if (o instanceof GenericArrayType that) { Type thatComponentType = that.getGenericComponentType(); return genericComponentType.equals(thatComponentType); @@ -560,8 +558,7 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (obj instanceof WildcardType) { - WildcardType that = (WildcardType) obj; + if (obj instanceof WildcardType that) { return Arrays.equals(that.getLowerBounds(), lb) && Arrays.equals(that.getUpperBounds(), ub); } return false; @@ -569,8 +566,7 @@ public boolean equals(Object obj) { } public static Type getTypeArgument(Type type, int i) { - if (type instanceof ParameterizedType) { - ParameterizedType p = (ParameterizedType) type; + if (type instanceof ParameterizedType p) { return fix(p.getActualTypeArguments()[i]); } else { throw new IllegalArgumentException(); @@ -584,13 +580,11 @@ public static Type getTypeArgument(Type type, int i) { * See bug 6202725. */ private static Type fix(Type t) { - if (!(t instanceof GenericArrayType)) { + if (!(t instanceof GenericArrayType gat)) { return t; } - GenericArrayType gat = (GenericArrayType) t; - if (gat.getGenericComponentType() instanceof Class) { - Class c = (Class) gat.getGenericComponentType(); + if (gat.getGenericComponentType() instanceof Class c) { return Array.newInstance(c, 0).getClass(); } diff --git a/groovy/src/main/java/org/kohsuke/stapler/jelly/groovy/JellyBuilder.java b/groovy/src/main/java/org/kohsuke/stapler/jelly/groovy/JellyBuilder.java index c9780762f..9056715f0 100644 --- a/groovy/src/main/java/org/kohsuke/stapler/jelly/groovy/JellyBuilder.java +++ b/groovy/src/main/java/org/kohsuke/stapler/jelly/groovy/JellyBuilder.java @@ -377,8 +377,7 @@ private TagScript createTagScript(QName n, Map attributes) throws JellyExc * limitations under the License. */ private void configureTag(Tag tag, Map attributes) throws JellyException { - if (tag instanceof DynaTag) { - DynaTag dynaTag = (DynaTag) tag; + if (tag instanceof DynaTag dynaTag) { for (Object o : attributes.entrySet()) { Entry entry = (Entry) o; diff --git a/jelly/src/main/java/org/kohsuke/stapler/jelly/CustomJellyContext.java b/jelly/src/main/java/org/kohsuke/stapler/jelly/CustomJellyContext.java index 2baf6c149..4199e3b95 100644 --- a/jelly/src/main/java/org/kohsuke/stapler/jelly/CustomJellyContext.java +++ b/jelly/src/main/java/org/kohsuke/stapler/jelly/CustomJellyContext.java @@ -175,8 +175,7 @@ private InternationalizedStringExpression createI18nExp(String text) throws Jell @Override protected Expression createEscapingExpression(Expression exp) { - if (exp instanceof InternationalizedStringExpression) { - InternationalizedStringExpression i18nexp = (InternationalizedStringExpression) exp; + if (exp instanceof InternationalizedStringExpression i18nexp) { return i18nexp.makeEscapingExpression(); } return super.createEscapingExpression(exp); diff --git a/jelly/src/main/java/org/kohsuke/stapler/jelly/DefaultScriptInvoker.java b/jelly/src/main/java/org/kohsuke/stapler/jelly/DefaultScriptInvoker.java index 01ad29854..59d1bb864 100644 --- a/jelly/src/main/java/org/kohsuke/stapler/jelly/DefaultScriptInvoker.java +++ b/jelly/src/main/java/org/kohsuke/stapler/jelly/DefaultScriptInvoker.java @@ -84,8 +84,7 @@ private boolean doCompression(Script script) { if (COMPRESS_BY_DEFAULT) { return true; } - if (script instanceof TagScript) { - TagScript ts = (TagScript) script; + if (script instanceof TagScript ts) { if (ts.getLocalName().equals("compress")) { return true; } diff --git a/jelly/src/main/java/org/kohsuke/stapler/jelly/IncludeTag.java b/jelly/src/main/java/org/kohsuke/stapler/jelly/IncludeTag.java index 6e6403ca5..063359c1a 100644 --- a/jelly/src/main/java/org/kohsuke/stapler/jelly/IncludeTag.java +++ b/jelly/src/main/java/org/kohsuke/stapler/jelly/IncludeTag.java @@ -162,8 +162,7 @@ public void doTag(XMLOutput output) throws JellyTagException { try { String source = null; if (JellyFacet.TRACE) { - if (script instanceof TagScript) { - TagScript ts = (TagScript) script; + if (script instanceof TagScript ts) { source = ts.getFileName(); } else { source = page + " (exact source location unknown)"; diff --git a/jelly/src/main/java/org/kohsuke/stapler/jelly/JellyClassTearOff.java b/jelly/src/main/java/org/kohsuke/stapler/jelly/JellyClassTearOff.java index de19ebfab..5708db991 100644 --- a/jelly/src/main/java/org/kohsuke/stapler/jelly/JellyClassTearOff.java +++ b/jelly/src/main/java/org/kohsuke/stapler/jelly/JellyClassTearOff.java @@ -78,8 +78,7 @@ public Script resolveScript(String name) throws JellyException { } for (Facet f : owner.webApp.facets) { - if (f instanceof JellyCompatibleFacet && !(f instanceof JellyFacet)) { - JellyCompatibleFacet jcf = (JellyCompatibleFacet) f; + if (f instanceof JellyCompatibleFacet jcf && !(f instanceof JellyFacet)) { for (Class> ct : jcf.getClassTearOffTypes()) { try { Script s = owner.loadTearOff(ct).resolveScript(shortName); @@ -108,8 +107,7 @@ public boolean serveIndexJelly(StaplerRequest req, StaplerResponse rsp, Object n Script script = findScript("index.jelly"); if (script != null) { String src = "index.jelly"; - if (script instanceof JellyViewScript) { - JellyViewScript jvs = (JellyViewScript) script; + if (script instanceof JellyViewScript jvs) { src = jvs.getName(); } Dispatcher.anonymizedTraceEval(req, rsp, node, "%s: Jelly index: %s", src);