Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pattern matching for instanceof #561

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 6 additions & 12 deletions core/src/main/java/org/kohsuke/stapler/RequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -639,15 +639,12 @@ public void bindJSON(Object bean, JSONObject src) {
@Override
public <T> List<T> bindJSONToList(Class<T> type, Object src) {
ArrayList<T> 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));
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/kohsuke/stapler/Stapler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/kohsuke/stapler/WebApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 6 additions & 12 deletions core/src/main/java/org/kohsuke/stapler/export/TypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -560,17 +558,15 @@ 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;
}
}

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();
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends AbstractTearOff<?, ? extends Script, ?>> ct : jcf.getClassTearOffTypes()) {
try {
Script s = owner.loadTearOff(ct).resolveScript(shortName);
Expand Down Expand Up @@ -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);
Expand Down