Skip to content

Commit 8e8ddbf

Browse files
claude[bot]vmassol
andauthored
[Misc] Use instanceof pattern matching instead of explicit casts (SonarQube java:S6201) (#1858)
* Convert 53 instanceof-and-cast sites in xwiki-commons-component-default, xwiki-commons-filter-api, xwiki-commons-job-api, xwiki-commons-logging-api and xwiki-commons-properties to Java 16+ instanceof pattern matching. Co-authored-by: Vincent Massol <vincent@massol.net>
1 parent 04ca230 commit 8e8ddbf

25 files changed

Lines changed: 95 additions & 106 deletions

File tree

xwiki-commons-core/xwiki-commons-component/xwiki-commons-component-default/src/main/java/org/xwiki/component/annotation/AbstractComponentDependencyFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ protected Class<?> getGenericRole(Field field)
5656
{
5757
Type type = field.getGenericType();
5858

59-
if (type instanceof ParameterizedType) {
60-
ParameterizedType pType = (ParameterizedType) type;
59+
if (type instanceof ParameterizedType pType) {
6160
Type[] types = pType.getActualTypeArguments();
62-
if (types.length > 0 && types[types.length - 1] instanceof Class) {
63-
return (Class) types[types.length - 1];
61+
if (types.length > 0 && types[types.length - 1] instanceof Class clazz) {
62+
return clazz;
6463
}
6564
}
6665

xwiki-commons-core/xwiki-commons-component/xwiki-commons-component-default/src/main/java/org/xwiki/component/annotation/ComponentAnnotationLoader.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,7 @@ private void addInterfaceRoleTypes(Set<Type> types, Type interfaceType, Class<?>
370370
Class<?> interfaceClass;
371371
Type resolvedInterfaceType = interfaceType;
372372

373-
if (interfaceType instanceof ParameterizedType) {
374-
ParameterizedType interfaceParameterizedType = (ParameterizedType) interfaceType;
375-
373+
if (interfaceType instanceof ParameterizedType interfaceParameterizedType) {
376374
interfaceClass = ReflectionUtils.getTypeClass(interfaceType);
377375
Type[] variableParameters = interfaceParameterizedType.getActualTypeArguments();
378376

@@ -420,13 +418,12 @@ private void addSuperclassRoleTypes(Set<Type> types, Class<?> componentClass, Ty
420418
{
421419
Type superType = componentClass.getGenericSuperclass();
422420
if (superType != null && superType != Object.class) {
423-
if (superType instanceof ParameterizedType) {
424-
ParameterizedType superParameterizedType = (ParameterizedType) superType;
421+
if (superType instanceof ParameterizedType superParameterizedType) {
425422
types.addAll(findComponentRoleTypes((Class) superParameterizedType.getRawType(), ReflectionUtils
426423
.resolveSuperArguments(superParameterizedType.getActualTypeArguments(), componentClass,
427424
parameters)));
428-
} else if (superType instanceof Class) {
429-
types.addAll(findComponentRoleTypes((Class) superType, null));
425+
} else if (superType instanceof Class superClass) {
426+
types.addAll(findComponentRoleTypes(superClass, null));
430427
}
431428
}
432429
}

xwiki-commons-core/xwiki-commons-component/xwiki-commons-component-default/src/main/java/org/xwiki/component/embed/EmbeddableComponentManager.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,7 @@ private ComponentEntry<?> tryOtherProvider(Type roleType, String roleHint)
452452
{
453453
ComponentEntry<?> entry = null;
454454

455-
if (roleType instanceof ParameterizedType) {
456-
ParameterizedType parameterizedRoleType = (ParameterizedType) roleType;
457-
455+
if (roleType instanceof ParameterizedType parameterizedRoleType) {
458456
if (parameterizedRoleType.getRawType() == Provider.class) {
459457
// Try to get the javax version of the provider
460458
ParameterizedType javaxProviderType = new DefaultParameterizedType(null, javax.inject.Provider.class,
@@ -834,8 +832,8 @@ private void releaseInstance(ComponentEntry<?> componentEntry) throws ComponentL
834832
Object instance = componentEntry.instance.get();
835833

836834
// Give a chance to the component to clean up
837-
if (instance instanceof Disposable) {
838-
((Disposable) instance).dispose();
835+
if (instance instanceof Disposable disposable) {
836+
disposable.dispose();
839837
}
840838

841839
componentEntry.instance.set(null);
@@ -967,10 +965,10 @@ private int getPriority(RoleHint<?> rh)
967965

968966
// Protection to prevent infinite recursion in case a component implementation points to this
969967
// instance.
970-
if (instance instanceof Disposable && componentEntry.instance.get() != this) {
968+
if (instance instanceof Disposable disposable && componentEntry.instance.get() != this) {
971969
try {
972970
SHUTDOWN_LOGGER.debug("Disposing component [{}]...", instance.getClass().getName());
973-
((Disposable) instance).dispose();
971+
disposable.dispose();
974972
SHUTDOWN_LOGGER.debug("Component [{}] has been disposed", instance.getClass().getName());
975973
} catch (ComponentLifecycleException e) {
976974
this.logger.error("Failed to dispose component with role type [{}] and role hint [{}]",

xwiki-commons-core/xwiki-commons-filter/xwiki-commons-filter-api/src/main/java/org/xwiki/filter/internal/FilterUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public static boolean sendBeginEvent(Object filter, FilterDescriptor descriptor,
9797

9898
if (elementDescriptor != null && elementDescriptor.getBeginMethod() != null) {
9999
sendEvent(elementDescriptor.getBeginMethod(), elementDescriptor, filter, parameters);
100-
} else if (filter instanceof UnknownFilter) {
101-
((UnknownFilter) filter).beginUnknwon(id, parameters);
100+
} else if (filter instanceof UnknownFilter unknownFilter) {
101+
unknownFilter.beginUnknwon(id, parameters);
102102
} else {
103103
return false;
104104
}
@@ -123,8 +123,8 @@ public static boolean sendEndEvent(Object filter, FilterDescriptor descriptor, S
123123

124124
if (elementDescriptor != null && elementDescriptor.getEndMethod() != null) {
125125
sendEvent(elementDescriptor.getEndMethod(), elementDescriptor, filter, parameters);
126-
} else if (filter instanceof UnknownFilter) {
127-
((UnknownFilter) filter).endUnknwon(id, parameters);
126+
} else if (filter instanceof UnknownFilter unknownFilter) {
127+
unknownFilter.endUnknwon(id, parameters);
128128
} else {
129129
return false;
130130
}
@@ -149,8 +149,8 @@ public static boolean sendOnEvent(Object filter, FilterDescriptor descriptor, St
149149

150150
if (elementDescriptor != null && elementDescriptor.getOnMethod() != null) {
151151
sendEvent(elementDescriptor.getOnMethod(), elementDescriptor, filter, parameters);
152-
} else if (filter instanceof UnknownFilter) {
153-
((UnknownFilter) filter).onUnknwon(id, parameters);
152+
} else if (filter instanceof UnknownFilter unknownFilter) {
153+
unknownFilter.onUnknwon(id, parameters);
154154
} else {
155155
return false;
156156
}

xwiki-commons-core/xwiki-commons-filter/xwiki-commons-filter-api/src/main/java/org/xwiki/filter/internal/converter/FilterEventParametersConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ protected FilterEventParameters convertToType(Type type, Object value)
5050

5151
FilterEventParameters parameters;
5252

53-
if (value instanceof FilterEventParameters) {
54-
parameters = (FilterEventParameters) value;
55-
} else if (value instanceof Map) {
53+
if (value instanceof FilterEventParameters filterEventParameters) {
54+
parameters = filterEventParameters;
55+
} else if (value instanceof Map map) {
5656
parameters = new FilterEventParameters();
57-
parameters.putAll((Map) value);
57+
parameters.putAll(map);
5858
} else {
5959
String parametersString = value.toString().trim();
6060

xwiki-commons-core/xwiki-commons-filter/xwiki-commons-filter-api/src/main/java/org/xwiki/filter/internal/input/InputSourceConverter.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ protected <G extends InputSource> G convertToType(Type targetType, Object value)
7676

7777
if (value instanceof String) {
7878
inputSource = fromString(value.toString());
79-
} else if (value instanceof InputStream) {
80-
inputSource = new DefaultInputStreamInputSource((InputStream) value);
81-
} else if (value instanceof byte[]) {
82-
inputSource = new DefaultByteArrayInputSource((byte[]) value);
83-
} else if (value instanceof File) {
84-
inputSource = new DefaultFileInputSource((File) value);
85-
} else if (value instanceof Reader) {
86-
inputSource = new DefaultReaderInputSource((Reader) value);
87-
} else if (value instanceof URL) {
88-
inputSource = new DefaultURLInputSource((URL) value);
79+
} else if (value instanceof InputStream inputStream) {
80+
inputSource = new DefaultInputStreamInputSource(inputStream);
81+
} else if (value instanceof byte[] bytes) {
82+
inputSource = new DefaultByteArrayInputSource(bytes);
83+
} else if (value instanceof File file) {
84+
inputSource = new DefaultFileInputSource(file);
85+
} else if (value instanceof Reader reader) {
86+
inputSource = new DefaultReaderInputSource(reader);
87+
} else if (value instanceof URL url) {
88+
inputSource = new DefaultURLInputSource(url);
8989
} else {
9090
inputSource = fromObject(value);
9191
}

xwiki-commons-core/xwiki-commons-filter/xwiki-commons-filter-api/src/main/java/org/xwiki/filter/internal/output/OutputTargetConverter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ protected <G extends OutputTarget> G convertToType(Type targetType, Object value
7373

7474
if (value instanceof String) {
7575
outputTarget = fromString(value.toString());
76-
} else if (value instanceof OutputStream) {
77-
outputTarget = new DefaultOutputStreamOutputTarget((OutputStream) value);
78-
} else if (value instanceof File) {
79-
outputTarget = new DefaultFileOutputTarget((File) value);
80-
} else if (value instanceof Writer) {
81-
outputTarget = new DefaultWriterOutputTarget((Writer) value);
76+
} else if (value instanceof OutputStream outputStream) {
77+
outputTarget = new DefaultOutputStreamOutputTarget(outputStream);
78+
} else if (value instanceof File file) {
79+
outputTarget = new DefaultFileOutputTarget(file);
80+
} else if (value instanceof Writer writer) {
81+
outputTarget = new DefaultWriterOutputTarget(writer);
8282
} else {
8383
ParameterizedType componentRole =
8484
TypeUtils.parameterize(org.xwiki.filter.output.OutputTargetConverter.class, value.getClass());

xwiki-commons-core/xwiki-commons-filter/xwiki-commons-filter-api/src/main/java/org/xwiki/filter/type/FilterStreamType.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,10 @@ public boolean equals(Object object)
288288
if (this == object) {
289289
result = true;
290290
} else {
291-
if (object instanceof FilterStreamType) {
292-
result = Objects.equals(getType(), ((FilterStreamType) object).getType())
293-
&& Objects.equals(getDataFormat(), ((FilterStreamType) object).getDataFormat())
294-
&& Objects.equals(getVersion(), ((FilterStreamType) object).getVersion());
291+
if (object instanceof FilterStreamType filterStreamType) {
292+
result = Objects.equals(getType(), filterStreamType.getType())
293+
&& Objects.equals(getDataFormat(), filterStreamType.getDataFormat())
294+
&& Objects.equals(getVersion(), filterStreamType.getVersion());
295295
} else {
296296
result = false;
297297
}

xwiki-commons-core/xwiki-commons-filter/xwiki-commons-filter-api/src/main/java/org/xwiki/filter/type/SystemType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public boolean equals(Object object)
116116
if (this == object) {
117117
result = true;
118118
} else {
119-
if (object instanceof SystemType) {
120-
result = Objects.equals(getId(), ((SystemType) object).getId());
119+
if (object instanceof SystemType systemType) {
120+
result = Objects.equals(getId(), systemType.getId());
121121
} else {
122122
result = false;
123123
}

xwiki-commons-core/xwiki-commons-job/xwiki-commons-job-api/src/main/java/org/xwiki/job/AbstractJob.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ public void initialize(Request request)
177177
this.status = createNewStatus(this.request);
178178

179179
// Create a filesystem log tail if the status is supposed to be serialized (to avoid creating too much files)
180-
if (this.status instanceof AbstractJobStatus && JobUtils.isSerializable(this.status)) {
181-
((AbstractJobStatus) this.status).setLoggerTail(this.store.createLoggerTail(request.getId(), false));
180+
if (this.status instanceof AbstractJobStatus abstractJobStatus && JobUtils.isSerializable(this.status)) {
181+
abstractJobStatus.setLoggerTail(this.store.createLoggerTail(request.getId(), false));
182182
}
183183
}
184184

@@ -265,11 +265,11 @@ protected void jobStarting()
265265

266266
this.observationManager.notify(new JobStartedEvent(getRequest().getId(), getType(), this.request), this);
267267

268-
if (this.status instanceof AbstractJobStatus) {
268+
if (this.status instanceof AbstractJobStatus abstractJobStatus) {
269269
((AbstractJobStatus<R>) this.status).setStartDate(new Date());
270270
((AbstractJobStatus<R>) this.status).setState(JobStatus.State.RUNNING);
271271

272-
((AbstractJobStatus) this.status).startListening();
272+
abstractJobStatus.startListening();
273273
}
274274

275275
if (getRequest().isVerbose()) {
@@ -292,9 +292,9 @@ protected void jobFinished(Throwable error)
292292
this.lock.lock();
293293

294294
try {
295-
if (this.status instanceof AbstractJobStatus) {
295+
if (this.status instanceof AbstractJobStatus abstractJobStatus) {
296296
// Store error
297-
((AbstractJobStatus) this.status).setError(error);
297+
abstractJobStatus.setError(error);
298298
}
299299

300300
// Give a chance to any listener to do custom action associated to the job
@@ -310,15 +310,15 @@ protected void jobFinished(Throwable error)
310310
}
311311
}
312312

313-
if (this.status instanceof AbstractJobStatus) {
313+
if (this.status instanceof AbstractJobStatus abstractJobStatus) {
314314
// Indicate when the job ended
315-
((AbstractJobStatus) this.status).setEndDate(new Date());
315+
abstractJobStatus.setEndDate(new Date());
316316

317317
// Stop updating job status (progress, log, etc.)
318-
((AbstractJobStatus) this.status).stopListening();
318+
abstractJobStatus.stopListening();
319319

320320
// Update job state
321-
((AbstractJobStatus) this.status).setState(JobStatus.State.FINISHED);
321+
abstractJobStatus.setState(JobStatus.State.FINISHED);
322322
}
323323

324324
// Release threads waiting for job being done

0 commit comments

Comments
 (0)