Skip to content

Commit

Permalink
Polish code using instanceof pattern matching.
Browse files Browse the repository at this point in the history
See #3134
  • Loading branch information
shin-mallang authored and gregturn committed Sep 5, 2023
1 parent 34a9c04 commit 358e4b5
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public Page<Revision<N, T>> findRevisions(ID id, Pageable pageable) {

AuditQuery baseQuery = createBaseQuery(id);

List<AuditOrder> orderMapped = (pageable.getSort() instanceof RevisionSort) ?
Collections.singletonList(mapRevisionSort((RevisionSort) pageable.getSort())) :
List<AuditOrder> orderMapped = (pageable.getSort() instanceof RevisionSort revisionSort) ?
Collections.singletonList(mapRevisionSort(revisionSort)) :
mapPropertySort(pageable.getSort());

orderMapped.forEach(baseQuery::addOrder);
Expand Down Expand Up @@ -232,8 +232,8 @@ static class QueryResult<T> {

RevisionMetadata<?> createRevisionMetadata() {

return metadata instanceof DefaultRevisionEntity //
? new DefaultRevisionMetadata((DefaultRevisionEntity) metadata, revisionType) //
return metadata instanceof DefaultRevisionEntity defaultRevisionEntity //
? new DefaultRevisionMetadata(defaultRevisionEntity, revisionType) //
: new AnnotationRevisionMetadata<>(Hibernate.unproxy(metadata), RevisionNumber.class, RevisionTimestamp.class,
revisionType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static String getHibernateQuery(Object query) {
try {

// Try the new Hibernate implementation first
if (query instanceof SqmQuery) {
return ((SqmQuery) query).getSqmStatement().toHqlString();
if (query instanceof SqmQuery sqmQuery) {
return sqmQuery.getSqmStatement().toHqlString();
}

// Couple of cases in which this still breaks, see HHH-15389
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public static boolean isEntityManagerOfType(EntityManager em, String type) {
EntityManager entityManagerToUse = em;
Object delegate = em.getDelegate();

if (delegate instanceof EntityManager) {
entityManagerToUse = (EntityManager) delegate;
if (delegate instanceof EntityManager delegateEntityManager) {
entityManagerToUse = delegateEntityManager;
}

return isOfType(entityManagerToUse, type, entityManagerToUse.getClass().getClassLoader());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <X> void processBean(@Observes ProcessBean<X> processBean) {
Bean<X> bean = processBean.getBean();
for (Type type : bean.getTypes()) {
// Check if the bean is an EntityManager.
if (type instanceof Class<?> && EntityManager.class.isAssignableFrom((Class<?>) type)) {
if (type instanceof Class<?> classType && EntityManager.class.isAssignableFrom(classType)) {
Set<Annotation> qualifiers = new HashSet<>(bean.getQualifiers());
if (bean.isAlternative() || !entityManagers.containsKey(qualifiers)) {
LOGGER.debug(String.format("Discovered '%s' with qualifiers %s", EntityManager.class.getName(), qualifiers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ List<JpaQueryParsingToken> generateOrderByArguments(String primaryFromAlias, Sor
*/
private void checkSortExpression(Sort.Order order) {

if (order instanceof JpaSort.JpaOrder && ((JpaSort.JpaOrder) order).isUnsafe()) {
if (order instanceof JpaSort.JpaOrder jpaOrder && jpaOrder.isUnsafe()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void setValue(String key, @Nullable Object value) {
values = new LinkedHashMap<>(2);
}

if (value == null || (value instanceof String && !StringUtils.hasText((String) value))) {
if (value == null || (value instanceof String stringValue && !StringUtils.hasText(stringValue))) {
this.values.remove(key);
}
this.values.put(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,8 @@ private static Collection<?> toCollection(@Nullable Object value) {
return null;
}

if (value instanceof Collection) {
if (value instanceof Collection<?> collection) {

Collection<?> collection = (Collection<?>) value;
return collection.isEmpty() ? null : collection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,12 +846,10 @@ private static boolean requiresOuterJoin(From<?, ?> from, PropertyPath property,
return true;
}

if (!(propertyPathModel instanceof Attribute)) {
if (!(propertyPathModel instanceof Attribute<?, ?> attribute)) {
return false;
}

Attribute<?, ?> attribute = (Attribute<?, ?>) propertyPathModel;

// not a persistent attribute type association (@OneToOne, @ManyToOne)
if (!ASSOCIATION_TYPES.containsKey(attribute.getPersistentAttributeType())) {
return false;
Expand Down Expand Up @@ -882,11 +880,11 @@ private static <T> T getAnnotationProperty(Attribute<?, ?> attribute, String pro

Member member = attribute.getJavaMember();

if (!(member instanceof AnnotatedElement)) {
if (!(member instanceof AnnotatedElement annotatedMember)) {
return defaultValue;
}

Annotation annotation = AnnotationUtils.getAnnotation((AnnotatedElement) member, associationAnnotation);
Annotation annotation = AnnotationUtils.getAnnotation(annotatedMember, associationAnnotation);
return annotation == null ? defaultValue : (T) AnnotationUtils.getValue(annotation, propertyName);
}

Expand Down Expand Up @@ -945,7 +943,7 @@ private static boolean isAlreadyInnerJoined(From<?, ?> from, String attribute) {
*/
static void checkSortExpression(Order order) {

if (order instanceof JpaOrder && ((JpaOrder) order).isUnsafe()) {
if (order instanceof JpaOrder jpaOrder && jpaOrder.isUnsafe()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

BeanFactory definitionFactory = definition.getBeanFactory();

if (!(definitionFactory instanceof BeanDefinitionRegistry)) {
if (!(definitionFactory instanceof BeanDefinitionRegistry definitionRegistry)) {
continue;
}

String entityManagerBeanName = "jpaSharedEM_AWC_" + definition.getBeanName();
BeanDefinitionRegistry definitionRegistry = (BeanDefinitionRegistry) definitionFactory;

if (!beanFactory.containsBeanDefinition(entityManagerBeanName)
&& !definitionRegistry.containsBeanDefinition(entityManagerBeanName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ public <T> JPQLQuery<T> applySorting(Sort sort, JPQLQuery<T> query) {
return query;
}

if (sort instanceof QSort) {
return addOrderByFrom((QSort) sort, query);
if (sort instanceof QSort qsort) {
return addOrderByFrom(qsort, query);
}

return addOrderByFrom(sort, query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public static Collection<EntityManagerFactoryBeanDefinition> getEntityManagerFac

BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();

if (parentBeanFactory instanceof ConfigurableListableBeanFactory) {
definitions.addAll(getEntityManagerFactoryBeanDefinitions((ConfigurableListableBeanFactory) parentBeanFactory));
if (parentBeanFactory instanceof ConfigurableListableBeanFactory parentConfigurableListableBeanFactory) {
definitions.addAll(getEntityManagerFactoryBeanDefinitions(parentConfigurableListableBeanFactory));
}

return definitions;
Expand Down Expand Up @@ -157,8 +157,8 @@ public static BeanDefinition getBeanDefinition(String name, ConfigurableListable

BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();

if (parentBeanFactory instanceof ConfigurableListableBeanFactory) {
return getBeanDefinition(name, (ConfigurableListableBeanFactory) parentBeanFactory);
if (parentBeanFactory instanceof ConfigurableListableBeanFactory parentConfigurableListableBeanFactory) {
return getBeanDefinition(name, parentConfigurableListableBeanFactory);
}

throw o_O;
Expand Down

0 comments on commit 358e4b5

Please sign in to comment.