Skip to content

Commit 407dce4

Browse files
committed
[Misc] Use pattern matching for instanceof in search-solr (SonarCloud java:S6201)
Replace instanceof-check-and-cast patterns with instanceof pattern matching in the xwiki-platform-search-solr module, as flagged by SonarCloud rule java:S6201. Co-Authored-By: Vincent Massol <vincent@massol.net>
1 parent a0dc143 commit 407dce4

6 files changed

Lines changed: 36 additions & 37 deletions

File tree

xwiki-platform-core/xwiki-platform-search/xwiki-platform-search-solr/xwiki-platform-search-solr-api/src/main/java/org/xwiki/search/solr/internal/DefaultSolrUtils.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ public static String getTypeName(Object value)
194194
return typeName;
195195
}
196196

197-
if (value instanceof Collection) {
198-
typeName = getTypeName((Collection) value);
197+
if (value instanceof Collection collection) {
198+
typeName = getTypeName(collection);
199199
} else if (value.getClass().isArray()) {
200200
typeName = getTypeName(value.getClass().getComponentType());
201201
if (typeName != null) {
@@ -270,12 +270,12 @@ private static boolean isList(Class<?> clazz)
270270
private static String getTypeName(Type type)
271271
{
272272
if (type != null) {
273-
if (type instanceof Class) {
274-
return getTypeName((Class) type);
275-
} else if (type instanceof ParameterizedType) {
276-
Type rawType = ((ParameterizedType) type).getRawType();
273+
if (type instanceof Class clazz) {
274+
return getTypeName(clazz);
275+
} else if (type instanceof ParameterizedType parameterizedType) {
276+
Type rawType = parameterizedType.getRawType();
277277

278-
if (rawType instanceof Class && Iterable.class.isAssignableFrom((Class) rawType)) {
278+
if (rawType instanceof Class rawClass && Iterable.class.isAssignableFrom(rawClass)) {
279279
Map<TypeVariable<?>, Type> variables = TypeUtils.getTypeArguments(type, Iterable.class);
280280

281281
return getIterableTypeName(variables.get(ITERABLE_PARAMETER));
@@ -365,11 +365,11 @@ public void setMap(String mapFieldName, Map<String, ?> fieldValue, SolrInputDocu
365365

366366
if (typeName != null) {
367367
document.setField(getMapFieldName(key, mapFieldName, typeName), value);
368-
} else if (value instanceof Iterable) {
369-
typeName = getTypeName((Iterable) value);
368+
} else if (value instanceof Iterable iterable) {
369+
typeName = getTypeName(iterable);
370370

371371
if (typeName != null) {
372-
for (Object element : (Iterable) value) {
372+
for (Object element : iterable) {
373373
document.addField(getMapFieldName(key, mapFieldName, typeName), element);
374374
}
375375
}
@@ -488,8 +488,8 @@ private String toString(Object fieldValue, Type valueType)
488488
}
489489

490490
String str;
491-
if (value instanceof String) {
492-
str = (String) value;
491+
if (value instanceof String string) {
492+
str = string;
493493
} else if (CLASS_SUFFIX_MAPPING.containsKey(fieldValue.getClass())) {
494494
str = value.toString();
495495
} else {
@@ -506,8 +506,8 @@ private String toString(Object fieldValue)
506506
}
507507

508508
String str;
509-
if (fieldValue instanceof String) {
510-
str = (String) fieldValue;
509+
if (fieldValue instanceof String string) {
510+
str = string;
511511
} else if (CLASS_SUFFIX_MAPPING.containsKey(fieldValue.getClass())) {
512512
str = fieldValue.toString();
513513
} else {
@@ -535,8 +535,8 @@ public String toFilterQueryString(Object fieldValue)
535535
}
536536

537537
String str;
538-
if (fieldValue instanceof Date) {
539-
str = ((Date) fieldValue).toInstant().toString();
538+
if (fieldValue instanceof Date date) {
539+
str = date.toInstant().toString();
540540
} else {
541541
str = toFilterQueryString(toString(fieldValue));
542542
}
@@ -557,8 +557,8 @@ public String toFilterQueryString(Object fieldValue, Type valueType)
557557
}
558558

559559
String str;
560-
if (value instanceof Date) {
561-
str = ((Date) value).toInstant().toString();
560+
if (value instanceof Date date) {
561+
str = date.toInstant().toString();
562562
} else {
563563
str = toFilterQueryString(toString(value, valueType));
564564
}
@@ -598,7 +598,7 @@ public <T> T get(String fieldName, SolrDocument document, Type targetType)
598598

599599
private <T> T toValue(Object storedValue, Type targetType)
600600
{
601-
if (storedValue == null && (!(targetType instanceof Class) || !((Class) targetType).isPrimitive())) {
601+
if (storedValue == null && (!(targetType instanceof Class clazz) || !clazz.isPrimitive())) {
602602
return null;
603603
}
604604

xwiki-platform-core/xwiki-platform-search/xwiki-platform-search-solr/xwiki-platform-search-solr-api/src/main/java/org/xwiki/search/solr/internal/SolrIndexEventListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ public void onEvent(Event event, Object source, Object data)
169169
} else if (event instanceof GeneralMailConfigurationUpdatedEvent) {
170170
// Refresh the index when the mail configuration is changed because the mail configuration is used to
171171
// decide if emails shall be indexed or not.
172-
if (source instanceof String) {
173-
this.solrIndexer.get().index(new WikiReference((String) source), true);
172+
if (source instanceof String sourceString) {
173+
this.solrIndexer.get().index(new WikiReference(sourceString), true);
174174
} else {
175175
this.solrIndexer.get().index(null, true);
176176
}

xwiki-platform-core/xwiki-platform-search/xwiki-platform-search-solr/xwiki-platform-search-solr-api/src/main/java/org/xwiki/search/solr/internal/SolrIndexInitializeListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ public void onEvent(Event event, Object source, Object data)
119119
request.setId(requestId);
120120
}
121121
} else if (startupMode == SolrConfiguration.SynchronizeAtStartupMode.WIKI
122-
&& event instanceof WikiReadyEvent) {
123-
WikiReadyEvent wikiReadyEvent = (WikiReadyEvent) event;
122+
&& event instanceof WikiReadyEvent wikiReadyEvent) {
124123
WikiReference wikiReference = new WikiReference(wikiReadyEvent.getWikiId());
125124
request = new IndexerRequest();
126125
request.setRootReference(wikiReference);

xwiki-platform-core/xwiki-platform-search/xwiki-platform-search-solr/xwiki-platform-search-solr-api/src/main/java/org/xwiki/search/solr/internal/metadata/AbstractSolrMetadataExtractor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ protected void setPropertyValue(XWikiSolrInputDocument solrDocument, BasePropert
393393
PropertyClass propertyClass, Locale locale)
394394
{
395395
Object propertyValue = property.getValue();
396-
if (propertyClass instanceof StaticListClass) {
397-
setStaticListPropertyValue(solrDocument, property, (StaticListClass) propertyClass, locale);
396+
if (propertyClass instanceof StaticListClass staticListClass) {
397+
setStaticListPropertyValue(solrDocument, property, staticListClass, locale);
398398
} else if (propertyClass instanceof TextAreaClass
399399
|| (propertyClass != null && "String".equals(propertyClass.getClassType()))
400400
|| (propertyValue instanceof CharSequence
@@ -429,9 +429,9 @@ protected void setPropertyValue(XWikiSolrInputDocument solrDocument, BasePropert
429429
setPropertyValue(solrDocument, property, new TypedValue(value), locale);
430430
}
431431
}
432-
} else if (propertyValue instanceof Integer && propertyClass instanceof BooleanClass) {
432+
} else if (propertyValue instanceof Integer integerValue && propertyClass instanceof BooleanClass) {
433433
// Boolean properties are stored as integers (0 is false and 1 is true).
434-
Boolean booleanValue = ((Integer) propertyValue) != 0;
434+
Boolean booleanValue = integerValue != 0;
435435
setPropertyValue(solrDocument, property, new TypedValue(booleanValue), locale);
436436
} else if (!property.isSensitive(xcontextProvider.get())) {
437437
// Avoid indexing passwords and, when obfuscation is enabled, emails.

xwiki-platform-core/xwiki-platform-search/xwiki-platform-search-solr/xwiki-platform-search-solr-api/src/main/java/org/xwiki/search/solr/internal/metadata/DefaultLinkStore.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ public Set<EntityReference> resolveLinkedEntities(EntityReference target) throws
126126
Collection<Object> links = solrDocument.getFieldValues(FieldUtils.LINKS);
127127
Set<EntityReference> entities = new HashSet<>(links.size());
128128
for (Object link : links) {
129-
if (link instanceof String) {
130-
EntityReference entityLink = this.linkSerializer.unserialize((String) link);
129+
if (link instanceof String linkString) {
130+
EntityReference entityLink = this.linkSerializer.unserialize(linkString);
131131

132132
if (entityLink != null) {
133133
// Make sure to resolve the reference as a DOCUMENT based references and not a PAGE one
@@ -213,8 +213,8 @@ EntityReference toDocumentBasedReference(EntityReference entityReference)
213213
}
214214

215215
if (entityReference.getType() == EntityType.PAGE) {
216-
return this.currentDocumentResolver.resolve(pageReference instanceof PageReference
217-
? (PageReference) pageReference : new PageReference(pageReference));
216+
return this.currentDocumentResolver.resolve(pageReference instanceof PageReference pageRef
217+
? pageRef : new PageReference(pageReference));
218218
}
219219

220220
EntityType documentBasedType =
@@ -225,7 +225,7 @@ EntityReference toDocumentBasedReference(EntityReference entityReference)
225225

226226
// Find the right DOCUMENT reference
227227
DocumentReference documentReference = this.currentDocumentResolver.resolve(
228-
pageReference instanceof PageReference ? (PageReference) pageReference : new PageReference(pageReference));
228+
pageReference instanceof PageReference pageRef ? pageRef : new PageReference(pageReference));
229229

230230
// Switch the parent
231231
return documentBasedReference.replaceParent(documentBasedReference.extractReference(EntityType.DOCUMENT),

xwiki-platform-core/xwiki-platform-search/xwiki-platform-search-solr/xwiki-platform-search-solr-query/src/main/java/org/xwiki/query/solr/internal/SolrQueryExecutor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ public <T> List<T> execute(Query query) throws QueryException
129129
// A better way would be using a PostFilter as described in this article:
130130
// http://java.dzone.com/articles/custom-security-filtering-solr
131131
List<DocumentReference> usersToCheck = new ArrayList<>(2);
132-
if (query instanceof SecureQuery) {
133-
if (((SecureQuery) query).isCurrentUserChecked()) {
132+
if (query instanceof SecureQuery secureQuery) {
133+
if (secureQuery.isCurrentUserChecked()) {
134134
usersToCheck.add(xcontextProvider.get().getUserReference());
135135
}
136-
if (((SecureQuery) query).isCurrentAuthorChecked()) {
136+
if (secureQuery.isCurrentAuthorChecked()) {
137137
usersToCheck.add(xcontextProvider.get().getAuthorReference());
138138
}
139139
} else {
@@ -171,8 +171,8 @@ private SolrQuery createSolrQuery(Query query)
171171
for (Entry<String, Object> entry : query.getNamedParameters().entrySet()) {
172172
Object value = entry.getValue();
173173

174-
if (value instanceof Iterable) {
175-
solrQuery.set(entry.getKey(), toStringArray((Iterable) value));
174+
if (value instanceof Iterable iterable) {
175+
solrQuery.set(entry.getKey(), toStringArray(iterable));
176176
} else if (value != null && value.getClass().isArray()) {
177177
solrQuery.set(entry.getKey(), toStringArray(value));
178178
} else {

0 commit comments

Comments
 (0)