Skip to content

Use instanceof instead of Class#isInstance where feasible #25446

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

Closed
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 @@ -137,7 +137,7 @@ private static List<TypeElement> staticTypesIn(Iterable<? extends Element> eleme
List<TypeElement> list = new ArrayList<>();
for (Element element : elements) {
if (TYPE_KINDS.contains(element.getKind()) && element.getModifiers().contains(Modifier.STATIC)) {
list.add(TypeElement.class.cast(element));
list.add((TypeElement) element);
}
}
return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Object getTarget() {
@Override
public Object generate(Object target, Method method, Object... params) {
JCacheOperation<?> operation = this.cacheOperationSource.getCacheOperation(method, target.getClass());
if (!(AbstractJCacheKeyOperation.class.isInstance(operation))) {
if (!(operation instanceof AbstractJCacheKeyOperation)) {
throw new IllegalStateException("Invalid operation, should be a key-based operation " + operation);
}
CacheKeyInvocationContext<?> invocationContext = createCacheKeyInvocationContext(target, operation, params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1994,7 +1994,7 @@ public void testSetCustomMessageCodesResolverBeforeInitializeBindingResultForBea
mpv.add("age", "invalid");
binder.bind(mpv);
assertThat(binder.getBindingResult().getFieldError("age").getCode()).isEqualTo("errors.typeMismatch");
assertThat(BeanWrapper.class.cast(binder.getInternalBindingResult().getPropertyAccessor()).getAutoGrowCollectionLimit()).isEqualTo(512);
assertThat(((BeanWrapper) binder.getInternalBindingResult().getPropertyAccessor()).getAutoGrowCollectionLimit()).isEqualTo(512);
}

@Test // SPR-15009
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ protected TextMessage createTextMessage(MessageCreator creator) throws JMSExcept
new StubTextMessage((String) invocation.getArguments()[0]));
javax.jms.Message message = creator.createMessage(mock);
verify(mock).createTextMessage(any());
return TextMessage.class.cast(message);
return (TextMessage) message;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class SessionHolder extends EntityManagerHolder {

public SessionHolder(Session session) {
// Check below is always true against Hibernate >= 5.2 but not against 5.0/5.1 at runtime
super(EntityManager.class.isInstance(session) ? session : null);
super(session instanceof EntityManager ? session : null);
this.session = session;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ResultMatcher handlerType(Class<?> type) {
assertNotNull("No handler", handler);
if (handler != null) {
Class<?> actual = handler.getClass();
if (HandlerMethod.class.isInstance(handler)) {
if (handler instanceof HandlerMethod) {
actual = ((HandlerMethod) handler).getBeanType();
}
assertEquals("Handler type", type, ClassUtils.getUserClass(actual));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static <T> T objectResult(@Nullable Collection<?> results, @Nullable Clas
if (String.class == requiredType) {
result = result.toString();
}
else if (Number.class.isAssignableFrom(requiredType) && Number.class.isInstance(result)) {
else if (Number.class.isAssignableFrom(requiredType) && result instanceof Number) {
try {
result = NumberUtils.convertNumberToTargetClass(((Number) result), (Class<? extends Number>) requiredType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
*/
protected HttpURLConnection openConnection(URL url, @Nullable Proxy proxy) throws IOException {
URLConnection urlConnection = (proxy != null ? url.openConnection(proxy) : url.openConnection());
if (!HttpURLConnection.class.isInstance(urlConnection)) {
if (!(urlConnection instanceof HttpURLConnection)) {
throw new IllegalStateException("HttpURLConnection required for [" + url + "] but got: " + urlConnection);
}
return (HttpURLConnection) urlConnection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public InputStream getBody() throws IOException {
@Override
public ServerHttpAsyncRequestControl getAsyncRequestControl(ServerHttpResponse response) {
if (this.asyncRequestControl == null) {
if (!ServletServerHttpResponse.class.isInstance(response)) {
if (!(response instanceof ServletServerHttpResponse)) {
throw new IllegalArgumentException(
"Response must be a ServletServerHttpResponse: " + response.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void assertCustomConfig() throws Exception {

Object config = request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG);
assertThat(config).as("Request config should be set").isNotNull();
assertThat(RequestConfig.class.isInstance(config)).as("Wrong request config type" + config.getClass().getName()).isTrue();
assertThat(config instanceof RequestConfig).as("Wrong request config type" + config.getClass().getName()).isTrue();
RequestConfig requestConfig = (RequestConfig) config;
assertThat(requestConfig.getConnectTimeout()).as("Wrong custom connection timeout").isEqualTo(1234);
assertThat(requestConfig.getConnectionRequestTimeout()).as("Wrong custom connection request timeout").isEqualTo(4321);
Expand Down