Skip to content

Commit 33e2d12

Browse files
committed
Replace explicit type conversion with instanceof pattern variable in plugins dir and libs dir
Signed-off-by: Binlong Gao <gbinlong@amazon.com>
1 parent a2e1fa2 commit 33e2d12

File tree

48 files changed

+300
-330
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+300
-330
lines changed

libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
9595
// Skip all not-public elements
9696
checkPublicVisibility(null, element);
9797

98-
if (element instanceof TypeElement) {
99-
process((TypeElement) element);
98+
if (element instanceof TypeElement typeElement) {
99+
process(typeElement);
100100
}
101101
}
102102

@@ -185,31 +185,31 @@ private void process(ExecutableElement executable, Element enclosing) {
185185

186186
// Process method return types
187187
final TypeMirror returnType = executable.getReturnType();
188-
if (returnType instanceof ReferenceType) {
189-
process(executable, (ReferenceType) returnType);
188+
if (returnType instanceof ReferenceType refType) {
189+
process(executable, refType);
190190
}
191191

192192
// Process method thrown types
193193
for (final TypeMirror thrownType : executable.getThrownTypes()) {
194-
if (thrownType instanceof ReferenceType) {
195-
process(executable, (ReferenceType) thrownType);
194+
if (thrownType instanceof ReferenceType refType) {
195+
process(executable, refType);
196196
}
197197
}
198198

199199
// Process method type parameters
200200
for (final TypeParameterElement typeParameter : executable.getTypeParameters()) {
201201
for (final TypeMirror boundType : typeParameter.getBounds()) {
202-
if (boundType instanceof ReferenceType) {
203-
process(executable, (ReferenceType) boundType);
202+
if (boundType instanceof ReferenceType refType) {
203+
process(executable, refType);
204204
}
205205
}
206206
}
207207

208208
// Process method arguments
209209
for (final VariableElement parameter : executable.getParameters()) {
210210
final TypeMirror parameterType = parameter.asType();
211-
if (parameterType instanceof ReferenceType) {
212-
process(executable, (ReferenceType) parameterType);
211+
if (parameterType instanceof ReferenceType refType) {
212+
process(executable, refType);
213213
}
214214
}
215215
}
@@ -220,12 +220,12 @@ private void process(ExecutableElement executable, Element enclosing) {
220220
* @param type wildcard type
221221
*/
222222
private void process(ExecutableElement executable, WildcardType type) {
223-
if (type.getExtendsBound() instanceof ReferenceType) {
224-
process(executable, (ReferenceType) type.getExtendsBound());
223+
if (type.getExtendsBound() instanceof ReferenceType refType) {
224+
process(executable, refType);
225225
}
226226

227-
if (type.getSuperBound() instanceof ReferenceType) {
228-
process(executable, (ReferenceType) type.getSuperBound());
227+
if (type.getSuperBound() instanceof ReferenceType refType) {
228+
process(executable, refType);
229229
}
230230
}
231231

@@ -240,8 +240,7 @@ private void process(ExecutableElement executable, ReferenceType ref) {
240240
return;
241241
}
242242

243-
if (ref instanceof DeclaredType) {
244-
final DeclaredType declaredType = (DeclaredType) ref;
243+
if (ref instanceof DeclaredType declaredType) {
245244

246245
final Element element = declaredType.asElement();
247246
if (inspectable(element)) {
@@ -250,24 +249,23 @@ private void process(ExecutableElement executable, ReferenceType ref) {
250249
}
251250

252251
for (final TypeMirror type : declaredType.getTypeArguments()) {
253-
if (type instanceof ReferenceType) {
254-
process(executable, (ReferenceType) type);
255-
} else if (type instanceof WildcardType) {
256-
process(executable, (WildcardType) type);
252+
if (type instanceof ReferenceType refType) {
253+
process(executable, refType);
254+
} else if (type instanceof WildcardType wildcardType) {
255+
process(executable, wildcardType);
257256
}
258257
}
259-
} else if (ref instanceof ArrayType) {
260-
final TypeMirror componentType = ((ArrayType) ref).getComponentType();
261-
if (componentType instanceof ReferenceType) {
262-
process(executable, (ReferenceType) componentType);
258+
} else if (ref instanceof ArrayType arrayType) {
259+
final TypeMirror componentType = arrayType.getComponentType();
260+
if (componentType instanceof ReferenceType refType) {
261+
process(executable, refType);
263262
}
264-
} else if (ref instanceof TypeVariable) {
265-
final TypeVariable typeVariable = (TypeVariable) ref;
266-
if (typeVariable.getUpperBound() instanceof ReferenceType) {
267-
process(executable, (ReferenceType) typeVariable.getUpperBound());
263+
} else if (ref instanceof TypeVariable typeVariable) {
264+
if (typeVariable.getUpperBound() instanceof ReferenceType refType) {
265+
process(executable, refType);
268266
}
269-
if (typeVariable.getLowerBound() instanceof ReferenceType) {
270-
process(executable, (ReferenceType) typeVariable.getLowerBound());
267+
if (typeVariable.getLowerBound() instanceof ReferenceType refType) {
268+
process(executable, refType);
271269
}
272270
}
273271

@@ -348,8 +346,8 @@ private void process(Element element) {
348346
continue;
349347
}
350348

351-
if (enclosed instanceof ExecutableElement) {
352-
process((ExecutableElement) enclosed, element);
349+
if (enclosed instanceof ExecutableElement executableElement) {
350+
process(executableElement, element);
353351
}
354352
}
355353
}

libs/common/src/main/java/org/opensearch/common/bootstrap/JarHell.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public static void checkJarHell(Consumer<String> output) throws IOException, URI
9696
ClassLoader loader = JarHell.class.getClassLoader();
9797
output.accept("java.class.path: " + System.getProperty("java.class.path"));
9898
output.accept("sun.boot.class.path: " + System.getProperty("sun.boot.class.path"));
99-
if (loader instanceof URLClassLoader) {
100-
output.accept("classloader urls: " + Arrays.toString(((URLClassLoader) loader).getURLs()));
99+
if (loader instanceof URLClassLoader urlClassLoader) {
100+
output.accept("classloader urls: " + Arrays.toString(urlClassLoader.getURLs()));
101101
}
102102
checkJarHell(parseClassPath(), output);
103103
}

libs/common/src/main/java/org/opensearch/common/transport/NetworkExceptionHelper.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@
4343
public class NetworkExceptionHelper {
4444

4545
public static boolean isConnectException(Throwable e) {
46-
if (e instanceof ConnectException) {
47-
return true;
48-
}
49-
return false;
46+
return e instanceof ConnectException;
5047
}
5148

5249
public static boolean isCloseConnectionException(Throwable e) {

libs/common/src/main/java/org/opensearch/common/util/io/IOUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public static void close(final Exception ex, final Iterable<? extends Closeable>
139139
}
140140

141141
if (firstException != null) {
142-
if (firstException instanceof IOException) {
143-
throw (IOException) firstException;
142+
if (firstException instanceof IOException ioe) {
143+
throw ioe;
144144
} else {
145145
// since we only assigned an IOException or a RuntimeException to ex above, in this case ex must be a RuntimeException
146146
throw (RuntimeException) firstException;

libs/common/src/main/java/org/opensearch/common/util/set/Sets.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static <T> HashSet<T> newHashSet(Iterator<T> iterator) {
6868

6969
public static <T> HashSet<T> newHashSet(Iterable<T> iterable) {
7070
Objects.requireNonNull(iterable);
71-
return iterable instanceof Collection ? new HashSet<>((Collection) iterable) : newHashSet(iterable.iterator());
71+
return iterable instanceof Collection<T> collection ? new HashSet<>(collection) : newHashSet(iterable.iterator());
7272
}
7373

7474
public static <T> HashSet<T> newHashSet(T... elements) {

libs/core/src/main/java/org/opensearch/ExceptionsHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ public static Throwable unwrap(Throwable t, Class<?>... clazzes) {
318318
*/
319319
public static boolean reThrowIfNotNull(@Nullable Throwable e) {
320320
if (e != null) {
321-
if (e instanceof RuntimeException) {
322-
throw (RuntimeException) e;
321+
if (e instanceof RuntimeException re) {
322+
throw re;
323323
} else {
324324
throw new RuntimeException(e);
325325
}
@@ -442,8 +442,8 @@ private static class GroupBy {
442442
// which does not include the cluster alias.
443443
String indexName = failure.index();
444444
if (indexName == null) {
445-
if (cause instanceof OpenSearchException) {
446-
final Index index = ((OpenSearchException) cause).getIndex();
445+
if (cause instanceof OpenSearchException ose) {
446+
final Index index = ose.getIndex();
447447
if (index != null) {
448448
indexName = index.getName();
449449
}

libs/core/src/main/java/org/opensearch/OpenSearchException.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ public String getDetailedMessage() {
328328
if (getCause() != null) {
329329
StringBuilder sb = new StringBuilder();
330330
sb.append(toString()).append("; ");
331-
if (getCause() instanceof OpenSearchException) {
332-
sb.append(((OpenSearchException) getCause()).getDetailedMessage());
331+
if (getCause() instanceof OpenSearchException ose) {
332+
sb.append(ose.getDetailedMessage());
333333
} else {
334334
sb.append(getCause());
335335
}
@@ -409,8 +409,7 @@ protected static void innerToXContent(
409409
headerToXContent(builder, entry.getKey().substring(OPENSEARCH_PREFIX_KEY.length()), entry.getValue());
410410
}
411411

412-
if (throwable instanceof OpenSearchException) {
413-
OpenSearchException exception = (OpenSearchException) throwable;
412+
if (throwable instanceof OpenSearchException exception) {
414413
exception.metadataToXContent(builder, params);
415414
}
416415

@@ -602,8 +601,8 @@ public static OpenSearchException innerFromXContent(XContentParser parser, boole
602601
public static void generateThrowableXContent(XContentBuilder builder, ToXContent.Params params, Throwable t) throws IOException {
603602
t = ExceptionsHelper.unwrapCause(t);
604603

605-
if (t instanceof OpenSearchException) {
606-
((OpenSearchException) t).toXContent(builder, params);
604+
if (t instanceof OpenSearchException ose) {
605+
ose.toXContent(builder, params);
607606
} else {
608607
innerToXContent(builder, params, t, getExceptionName(t), t.getMessage(), emptyMap(), emptyMap(), t.getCause());
609608
}
@@ -674,8 +673,8 @@ public static OpenSearchException failureFromXContent(XContentParser parser) thr
674673
*/
675674
public OpenSearchException[] guessRootCauses() {
676675
final Throwable cause = getCause();
677-
if (cause != null && cause instanceof OpenSearchException) {
678-
return ((OpenSearchException) cause).guessRootCauses();
676+
if (cause instanceof OpenSearchException ose) {
677+
return ose.guessRootCauses();
679678
}
680679
return new OpenSearchException[] { this };
681680
}
@@ -687,9 +686,9 @@ public OpenSearchException[] guessRootCauses() {
687686
*/
688687
public static OpenSearchException[] guessRootCauses(Throwable t) {
689688
Throwable ex = ExceptionsHelper.unwrapCause(t);
690-
if (ex instanceof OpenSearchException) {
689+
if (ex instanceof OpenSearchException ose) {
691690
// OpenSearchException knows how to guess its own root cause
692-
return ((OpenSearchException) ex).guessRootCauses();
691+
return ose.guessRootCauses();
693692
}
694693
if (ex instanceof XContentParseException) {
695694
/*

libs/core/src/main/java/org/opensearch/core/common/bytes/AbstractBytesReference.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ public boolean equals(Object other) {
112112
if (this == other) {
113113
return true;
114114
}
115-
if (other instanceof BytesReference) {
116-
final BytesReference otherRef = (BytesReference) other;
115+
if (other instanceof BytesReference otherRef) {
117116
if (length() != otherRef.length()) {
118117
return false;
119118
}

libs/core/src/main/java/org/opensearch/core/common/bytes/BytesArray.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ public boolean equals(Object other) {
105105
if (this == other) {
106106
return true;
107107
}
108-
if (other instanceof BytesArray) {
109-
final BytesArray that = (BytesArray) other;
108+
if (other instanceof BytesArray that) {
110109
return Arrays.equals(bytes, offset, offset + length, that.bytes, that.offset, that.offset + that.length);
111110
}
112111
return super.equals(other);

libs/core/src/main/java/org/opensearch/core/common/io/stream/DataOutputStreamOutput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public void reset() throws IOException {
7171

7272
@Override
7373
public void close() throws IOException {
74-
if (out instanceof Closeable) {
75-
((Closeable) out).close();
74+
if (out instanceof Closeable closeable) {
75+
closeable.close();
7676
}
7777
}
7878
}

0 commit comments

Comments
 (0)