Skip to content

Commit 56e2d4b

Browse files
committed
lots of safe type casts through whole code base
1 parent 8f654f0 commit 56e2d4b

File tree

277 files changed

+1642
-1834
lines changed

Some content is hidden

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

277 files changed

+1642
-1834
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/AltibaseSqlAstTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ protected void renderComparison(Expression lhs, ComparisonOperator operator, Exp
6464
@Override
6565
public void visitOver(Over<?> over) {
6666
final Expression expression = over.getExpression();
67-
if ( expression instanceof FunctionExpression && "row_number".equals( ( (FunctionExpression) expression ).getFunctionName() ) ) {
67+
if ( expression instanceof FunctionExpression functionExpression
68+
&& "row_number".equals( functionExpression.getFunctionName() ) ) {
6869
if ( over.getPartitions().isEmpty() && over.getOrderList().isEmpty()
6970
&& over.getStartKind() == FrameKind.UNBOUNDED_PRECEDING
7071
&& over.getEndKind() == FrameKind.CURRENT_ROW

hibernate-core/src/main/java/org/hibernate/ConnectionAcquisitionMode.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,13 @@ public static ConnectionAcquisitionMode interpret(Object setting) {
4040
if ( setting == null ) {
4141
return null;
4242
}
43-
44-
if ( setting instanceof ConnectionAcquisitionMode mode ) {
43+
else if ( setting instanceof ConnectionAcquisitionMode mode ) {
4544
return mode;
4645
}
47-
48-
final String value = setting.toString();
49-
if ( isEmpty( value ) ) {
50-
return null;
46+
else {
47+
final String value = setting.toString();
48+
return isEmpty( value ) ? null : interpret( value );
5149
}
5250

53-
return interpret( value );
5451
}
5552
}

hibernate-core/src/main/java/org/hibernate/ConnectionReleaseMode.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,21 @@ public static ConnectionReleaseMode interpret(Object setting) {
7171
if ( setting == null ) {
7272
return null;
7373
}
74-
75-
if ( setting instanceof ConnectionReleaseMode mode ) {
74+
else if ( setting instanceof ConnectionReleaseMode mode ) {
7675
return mode;
7776
}
78-
79-
final String value = setting.toString();
80-
if ( isEmpty( value ) ) {
81-
return null;
77+
else {
78+
final String value = setting.toString();
79+
if ( isEmpty( value ) ) {
80+
return null;
81+
}
82+
// here we disregard "auto"
83+
else if ( value.equalsIgnoreCase( "auto" ) ) {
84+
return null;
85+
}
86+
else {
87+
return parse( value );
88+
}
8289
}
83-
84-
// here we disregard "auto"
85-
if ( value.equalsIgnoreCase( "auto" ) ) {
86-
return null;
87-
}
88-
89-
return parse( value );
9090
}
9191
}

hibernate-core/src/main/java/org/hibernate/Hibernate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
* delegate.
6666
* <li>The proxy does not have the same concrete type as the proxied delegate, and so
6767
* {@link #getClass(Object)} must be used in place of {@link Object#getClass()},
68-
* and this method fetches the entity by side-effect.
68+
* and this method fetches the entity by side effect.
6969
* <li>For a polymorphic association, the concrete type of the associated entity is
7070
* not known until the delegate is fetched from the database, and so
7171
* {@link #unproxy(Object, Class)}} must be used to perform typecasts, and

hibernate-core/src/main/java/org/hibernate/PropertySetterAccessException.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ public PropertySetterAccessException(
5151
}
5252

5353
public static String loggablePropertyValueString(Object value) {
54-
if ( value instanceof Collection || value instanceof HibernateProxy ) {
55-
return value.getClass().getSimpleName();
56-
}
57-
return value.toString();
54+
return value instanceof Collection || value instanceof HibernateProxy
55+
? value.getClass().getSimpleName()
56+
: value.toString();
5857
}
5958

6059
@Override

hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ else if ( attribute.isEmbeddedAttributeMapping() ) {
211211

212212
private void addCollectionKey(
213213
PluralAttributeMapping pluralAttributeMapping,
214-
Object o,
214+
Object object,
215215
PersistenceContext persistenceContext) {
216-
if ( o instanceof PersistentCollection ) {
216+
if ( object instanceof PersistentCollection ) {
217217
final CollectionPersister collectionPersister = pluralAttributeMapping.getCollectionDescriptor();
218218
final Object key = AbstractEntityPersister.getCollectionKey(
219219
collectionPersister,
@@ -223,7 +223,7 @@ private void addCollectionKey(
223223
);
224224
if ( key != null ) {
225225
final CollectionKey collectionKey = new CollectionKey( collectionPersister, key );
226-
persistenceContext.addCollectionByKey( collectionKey, (PersistentCollection<?>) o );
226+
persistenceContext.addCollectionByKey( collectionKey, (PersistentCollection<?>) object );
227227
}
228228
}
229229
}

hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ protected void postCommitDelete(boolean success) {
234234
}
235235

236236
private static void postCommitDeleteOnUnsuccessful(PostDeleteEventListener listener, PostDeleteEvent event) {
237-
if ( listener instanceof PostCommitDeleteEventListener ) {
238-
( (PostCommitDeleteEventListener) listener ).onPostDeleteCommitFailed( event );
237+
if ( listener instanceof PostCommitDeleteEventListener postCommitDeleteEventListener ) {
238+
postCommitDeleteEventListener.onPostDeleteCommitFailed( event );
239239
}
240240
else {
241241
//default to the legacy implementation that always fires the event

hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ protected void postCommitUpdate(boolean success) {
383383
}
384384

385385
private void onPostCommitFailure(PostUpdateEventListener listener, PostUpdateEvent event) {
386-
if ( listener instanceof PostCommitUpdateEventListener ) {
387-
((PostCommitUpdateEventListener) listener).onPostUpdateCommitFailed( event );
386+
if ( listener instanceof PostCommitUpdateEventListener postCommitUpdateEventListener ) {
387+
postCommitUpdateEventListener.onPostUpdateCommitFailed( event );
388388
}
389389
else {
390390
//default to the legacy implementation that always fires the event

hibernate-core/src/main/java/org/hibernate/annotations/OnDeleteAction.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,25 @@ public static OnDeleteAction fromExternalForm(Object value) {
6363
if ( value == null ) {
6464
return null;
6565
}
66-
67-
if ( value instanceof OnDeleteAction onDeleteAction ) {
66+
else if ( value instanceof OnDeleteAction onDeleteAction ) {
6867
return onDeleteAction;
6968
}
69+
else {
70+
final String valueString = value.toString();
71+
try {
72+
return valueOf( valueString );
73+
}
74+
catch (IllegalArgumentException e) {
75+
// the name did not match the enum value name...
76+
}
7077

71-
final String valueString = value.toString();
72-
try {
73-
return valueOf( valueString );
74-
}
75-
catch (IllegalArgumentException e) {
76-
// the name did not match the enum value name...
77-
}
78-
79-
for ( OnDeleteAction checkAction : values() ) {
80-
if ( checkAction.getAlternativeName().equalsIgnoreCase( valueString ) ) {
81-
return checkAction;
78+
for ( OnDeleteAction checkAction : values() ) {
79+
if ( checkAction.getAlternativeName().equalsIgnoreCase( valueString ) ) {
80+
return checkAction;
81+
}
8282
}
83-
}
8483

85-
return null;
84+
return null;
85+
}
8686
}
8787
}

hibernate-core/src/main/java/org/hibernate/binder/internal/CommentBinder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void bind(Comment comment, MetadataBuildingContext context, PersistentCla
3535
+ "' was annotated '@Comment'");
3636
}
3737
else if ( value instanceof Collection collection ) {
38-
Table table = collection.getCollectionTable();
38+
final Table table = collection.getCollectionTable();
3939
// by default, the comment goes on the table
4040
if ( on.isEmpty() || table.getName().equalsIgnoreCase( on ) ) {
4141
table.setComment( text );

hibernate-core/src/main/java/org/hibernate/boot/beanvalidation/BeanValidationIntegrator.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public static void validateFactory(Object object) {
6060
validateMethod.invoke( null, object );
6161
}
6262
catch (InvocationTargetException e) {
63-
if ( e.getTargetException() instanceof HibernateException ) {
64-
throw (HibernateException) e.getTargetException();
63+
if ( e.getTargetException() instanceof HibernateException exception ) {
64+
throw exception;
6565
}
6666
throw new HibernateException( "Unable to check validity of passed ValidatorFactory", e );
6767
}
@@ -139,8 +139,8 @@ public SessionFactoryServiceRegistry getServiceRegistry() {
139139
activateMethod.invoke( null, activationContext );
140140
}
141141
catch (InvocationTargetException e) {
142-
if ( e.getTargetException() instanceof HibernateException ) {
143-
throw (HibernateException) e.getTargetException();
142+
if ( e.getTargetException() instanceof HibernateException exception ) {
143+
throw exception;
144144
}
145145
throw new IntegrationException( "Error activating Bean Validation integration", e.getTargetException() );
146146
}

hibernate-core/src/main/java/org/hibernate/boot/beanvalidation/GroupsPerOperation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import org.hibernate.HibernateException;
1212
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
1313
import org.hibernate.boot.spi.ClassLoaderAccess;
14-
import org.hibernate.internal.util.StringHelper;
1514

1615
import jakarta.validation.groups.Default;
1716

17+
import static org.hibernate.internal.util.StringHelper.split;
1818
import static org.hibernate.internal.util.collections.CollectionHelper.mapOfSize;
1919

2020
/**
@@ -70,7 +70,7 @@ public static Class<?>[] buildGroupsForOperation(
7070
}
7171

7272
if ( property instanceof String string ) {
73-
final String[] groupNames = StringHelper.split( ",", string );
73+
final String[] groupNames = split( ",", string );
7474
if ( groupNames.length == 1 && groupNames[0].isEmpty() ) {
7575
return EMPTY_GROUPS;
7676
}

hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -1200,12 +1200,10 @@ public String getPhysicalColumnName(Table table, Identifier logicalName) throws
12001200
}
12011201
}
12021202

1203-
if ( currentTable instanceof DenormalizedTable ) {
1204-
currentTable = ( (DenormalizedTable) currentTable ).getIncludedTable();
1205-
}
1206-
else {
1207-
currentTable = null;
1208-
}
1203+
currentTable =
1204+
currentTable instanceof DenormalizedTable denormalizedTable
1205+
? denormalizedTable.getIncludedTable()
1206+
: null;
12091207
}
12101208

12111209
assert table != null;
@@ -1235,12 +1233,10 @@ public String getLogicalColumnName(Table table, Identifier physicalName) throws
12351233
}
12361234
}
12371235

1238-
if ( currentTable instanceof DenormalizedTable ) {
1239-
currentTable = ( (DenormalizedTable) currentTable ).getIncludedTable();
1240-
}
1241-
else {
1242-
currentTable = null;
1243-
}
1236+
currentTable =
1237+
currentTable instanceof DenormalizedTable denormalizedTable
1238+
? denormalizedTable.getIncludedTable()
1239+
: null;
12441240
}
12451241

12461242
if ( logicalName == null ) {

hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataImpl.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,12 @@ public void orderColumns(boolean forceOrdering) {
428428
}
429429
}
430430
for ( UserDefinedType userDefinedType : namespace.getUserDefinedTypes() ) {
431-
if ( userDefinedType instanceof UserDefinedObjectType objectType ) {
432-
if ( objectType.getColumns().size() > 1 ) {
433-
final List<Column> objectTypeColumns = columnOrderingStrategy.orderUserDefinedTypeColumns(
434-
objectType,
435-
this
436-
);
437-
if ( objectTypeColumns != null ) {
438-
objectType.reorderColumns( objectTypeColumns );
439-
}
431+
if ( userDefinedType instanceof UserDefinedObjectType objectType
432+
&& objectType.getColumns().size() > 1 ) {
433+
final List<Column> objectTypeColumns =
434+
columnOrderingStrategy.orderUserDefinedTypeColumns( objectType, this );
435+
if ( objectTypeColumns != null ) {
436+
objectType.reorderColumns( objectTypeColumns );
440437
}
441438
}
442439
}

hibernate-core/src/main/java/org/hibernate/boot/jaxb/hbm/transform/BootModelPreprocessor.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.hibernate.mapping.RootClass;
1919
import org.hibernate.mapping.Table;
2020
import org.hibernate.mapping.ToOne;
21+
import org.hibernate.mapping.Value;
2122

2223
/**
2324
* @author Steve Ebersole
@@ -91,11 +92,12 @@ private static void processProperty(
9192
TransformationState transformationState) {
9293
entityTypeInfo.put( property.getName(), new PropertyInfo( property ) );
9394

94-
if ( property.getValue() instanceof Component component ) {
95+
final Value value = property.getValue();
96+
if ( value instanceof Component component ) {
9597
final String componentRole = entityName + "." + property.getName();
9698
buildComponentEntries( componentRole, component, transformationState );
9799
}
98-
else if ( property.getValue() instanceof IndexedCollection indexedCollection ) {
100+
else if ( value instanceof IndexedCollection indexedCollection ) {
99101
if ( indexedCollection.getIndex() instanceof Component index ) {
100102
final String componentRole = entityName + "." + property.getName() + ".key";
101103
buildComponentEntries( componentRole, index, transformationState );
@@ -105,13 +107,13 @@ else if ( property.getValue() instanceof IndexedCollection indexedCollection ) {
105107
buildComponentEntries( componentRole, element, transformationState );
106108
}
107109
}
108-
else if ( property.getValue() instanceof Collection collection ) {
110+
else if ( value instanceof Collection collection ) {
109111
if ( collection.getElement() instanceof Component element ) {
110112
final String componentRole = entityName + "." + property.getName() + ".value";
111113
buildComponentEntries( componentRole, element, transformationState );
112114
}
113115
}
114-
else if ( property.getValue() instanceof ToOne toOne ) {
116+
else if ( value instanceof ToOne toOne ) {
115117
// could be the target of an inverse mapping, and we will need this information for transforming to mapped-by
116118
transformationState.registerMappableAttributesByColumns( entityName, property.getName(), toOne.getSelectables() );
117119
}

0 commit comments

Comments
 (0)