Skip to content

Commit 6612868

Browse files
committed
some code and warning cleanups in org.hibernate.boot
1 parent 04b8d80 commit 6612868

File tree

8 files changed

+148
-168
lines changed

8 files changed

+148
-168
lines changed

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.hibernate.event.spi.PreUpsertEvent;
2525
import org.hibernate.event.spi.PreUpsertEventListener;
2626
import org.hibernate.internal.CoreMessageLogger;
27-
import org.hibernate.internal.util.collections.CollectionHelper;
2827
import org.hibernate.metamodel.RepresentationMode;
2928
import org.hibernate.persister.entity.EntityPersister;
3029

@@ -33,10 +32,12 @@
3332
import jakarta.validation.ConstraintViolation;
3433
import jakarta.validation.ConstraintViolationException;
3534
import jakarta.validation.TraversableResolver;
36-
import jakarta.validation.Validation;
3735
import jakarta.validation.Validator;
3836
import jakarta.validation.ValidatorFactory;
3937

38+
import static jakarta.validation.Validation.buildDefaultValidatorFactory;
39+
import static org.hibernate.internal.util.collections.CollectionHelper.setOfSize;
40+
4041
/**
4142
* Event listener used to enable Bean Validation for insert/update/delete events.
4243
*
@@ -71,8 +72,7 @@ public BeanValidationEventListener(
7172

7273
public void initialize(Map<String,Object> settings, ClassLoaderService classLoaderService) {
7374
if ( !initialized ) {
74-
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
75-
init( factory, settings, classLoaderService );
75+
init( buildDefaultValidatorFactory(), settings, classLoaderService );
7676
}
7777
}
7878

@@ -138,36 +138,34 @@ private <T> void validate(
138138
final Class<?>[] groups = groupsPerOperation.get( operation );
139139
if ( groups.length > 0 ) {
140140
final Set<ConstraintViolation<T>> constraintViolations = validator.validate( object, groups );
141-
if ( constraintViolations.size() > 0 ) {
142-
Set<ConstraintViolation<?>> propagatedViolations = CollectionHelper.setOfSize( constraintViolations.size() );
143-
Set<String> classNames = new HashSet<>();
141+
if ( !constraintViolations.isEmpty() ) {
142+
final Set<ConstraintViolation<?>> propagatedViolations = setOfSize( constraintViolations.size() );
143+
final Set<String> classNames = new HashSet<>();
144144
for ( ConstraintViolation<?> violation : constraintViolations ) {
145145
LOG.trace( violation );
146146
propagatedViolations.add( violation );
147147
classNames.add( violation.getLeafBean().getClass().getName() );
148148
}
149-
StringBuilder builder = new StringBuilder();
149+
final StringBuilder builder = new StringBuilder();
150150
builder.append( "Validation failed for classes " );
151151
builder.append( classNames );
152152
builder.append( " during " );
153153
builder.append( operation.getName() );
154154
builder.append( " time for groups " );
155155
builder.append( toString( groups ) );
156156
builder.append( "\nList of constraint violations:[\n" );
157-
for (ConstraintViolation<?> violation : constraintViolations) {
157+
for ( ConstraintViolation<?> violation : constraintViolations ) {
158158
builder.append( "\t" ).append( violation.toString() ).append("\n");
159159
}
160160
builder.append( "]" );
161161

162-
throw new ConstraintViolationException(
163-
builder.toString(), propagatedViolations
164-
);
162+
throw new ConstraintViolationException( builder.toString(), propagatedViolations );
165163
}
166164
}
167165
}
168166

169167
private String toString(Class<?>[] groups) {
170-
StringBuilder toString = new StringBuilder( "[" );
168+
final StringBuilder toString = new StringBuilder( "[" );
171169
for ( Class<?> group : groups ) {
172170
toString.append( group.getName() ).append( ", " );
173171
}

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
1515
import org.hibernate.boot.spi.ClassLoaderAccess;
1616
import org.hibernate.internal.util.StringHelper;
17-
import org.hibernate.internal.util.collections.CollectionHelper;
1817

1918
import jakarta.validation.groups.Default;
2019

20+
import static org.hibernate.internal.util.collections.CollectionHelper.mapOfSize;
21+
2122
/**
2223
* @author Emmanuel Bernard
2324
*/
@@ -29,35 +30,34 @@ public class GroupsPerOperation {
2930
private static final Class<?>[] DEFAULT_GROUPS = new Class<?>[] { Default.class };
3031
private static final Class<?>[] EMPTY_GROUPS = new Class<?>[] { };
3132

32-
private final Map<Operation, Class<?>[]> groupsPerOperation = CollectionHelper.mapOfSize( 4 );
33+
private final Map<Operation, Class<?>[]> groupsPerOperation = mapOfSize( 4 );
3334

3435
private GroupsPerOperation() {
3536
}
3637

37-
public static GroupsPerOperation from(Map settings, ClassLoaderAccess classLoaderAccess) {
38-
GroupsPerOperation groupsPerOperation = new GroupsPerOperation();
39-
38+
public static GroupsPerOperation from(Map<String,Object> settings, ClassLoaderAccess classLoaderAccess) {
39+
final GroupsPerOperation groupsPerOperation = new GroupsPerOperation();
4040
applyOperationGrouping( groupsPerOperation, Operation.INSERT, settings, classLoaderAccess );
4141
applyOperationGrouping( groupsPerOperation, Operation.UPDATE, settings, classLoaderAccess );
4242
applyOperationGrouping( groupsPerOperation, Operation.DELETE, settings, classLoaderAccess );
4343
applyOperationGrouping( groupsPerOperation, Operation.UPSERT, settings, classLoaderAccess );
4444
applyOperationGrouping( groupsPerOperation, Operation.DDL, settings, classLoaderAccess );
45-
4645
return groupsPerOperation;
4746
}
4847

4948
private static void applyOperationGrouping(
5049
GroupsPerOperation groupsPerOperation,
5150
Operation operation,
52-
Map settings,
51+
Map<String,Object> settings,
5352
ClassLoaderAccess classLoaderAccess) {
5453
groupsPerOperation.groupsPerOperation.put(
5554
operation,
5655
buildGroupsForOperation( operation, settings, classLoaderAccess )
5756
);
5857
}
5958

60-
public static Class<?>[] buildGroupsForOperation(Operation operation, Map settings, ClassLoaderAccess classLoaderAccess) {
59+
public static Class<?>[] buildGroupsForOperation(
60+
Operation operation, Map<String,Object> settings, ClassLoaderAccess classLoaderAccess) {
6161
Object property = settings.get( operation.getJakartaGroupPropertyName() );
6262
if ( property == null ) {
6363
property = settings.get( operation.getGroupPropertyName() );
@@ -67,21 +67,20 @@ public static Class<?>[] buildGroupsForOperation(Operation operation, Map settin
6767
return operation == Operation.DELETE ? EMPTY_GROUPS : DEFAULT_GROUPS;
6868
}
6969

70-
if ( property instanceof Class<?>[] ) {
71-
return (Class<?>[]) property;
70+
if ( property instanceof Class<?>[] classes ) {
71+
return classes;
7272
}
7373

74-
if ( property instanceof String ) {
75-
String stringProperty = (String) property;
76-
String[] groupNames = StringHelper.split( ",", stringProperty );
74+
if ( property instanceof String string ) {
75+
final String[] groupNames = StringHelper.split( ",", string );
7776
if ( groupNames.length == 1 && groupNames[0].isEmpty() ) {
7877
return EMPTY_GROUPS;
7978
}
8079

81-
List<Class<?>> groupsList = new ArrayList<>(groupNames.length);
82-
for (String groupName : groupNames) {
83-
String cleanedGroupName = groupName.trim();
84-
if ( cleanedGroupName.length() > 0) {
80+
final List<Class<?>> groupsList = new ArrayList<>( groupNames.length );
81+
for ( String groupName : groupNames ) {
82+
final String cleanedGroupName = groupName.trim();
83+
if ( !cleanedGroupName.isEmpty() ) {
8584
try {
8685
groupsList.add( classLoaderAccess.classForName( cleanedGroupName ) );
8786
}
@@ -90,11 +89,13 @@ public static Class<?>[] buildGroupsForOperation(Operation operation, Map settin
9089
}
9190
}
9291
}
93-
return groupsList.toArray( new Class<?>[groupsList.size()] );
92+
return groupsList.toArray( new Class<?>[0] );
9493
}
9594

9695
//null is bad and excluded by instanceof => exception is raised
97-
throw new HibernateException( JAKARTA_JPA_GROUP_PREFIX + operation.getJakartaGroupPropertyName() + " is of unknown type: String or Class<?>[] only");
96+
throw new HibernateException( JAKARTA_JPA_GROUP_PREFIX
97+
+ operation.getJakartaGroupPropertyName()
98+
+ " is of unknown type: String or Class<?>[] only");
9899
}
99100

100101
public Class<?>[] get(Operation operation) {

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

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.hibernate.type.AnyType;
1919
import org.hibernate.type.CollectionType;
2020
import org.hibernate.type.ComponentType;
21-
import org.hibernate.type.CompositeType;
2221
import org.hibernate.type.EntityType;
2322
import org.hibernate.type.Type;
2423

@@ -27,9 +26,8 @@
2726

2827
/**
2928
* Use Hibernate metadata to ignore cascade on entities.
30-
* cascade on embeddable objects or collection of embeddable objects are accepted
31-
*
32-
* Also use Hibernate's native isInitialized method call.
29+
* Cascade on embeddable objects or collection of embeddable objects are accepted
30+
* Also use Hibernate's native {@link Hibernate#isInitialized} method call.
3331
*
3432
* @author Emmanuel Bernard
3533
*/
@@ -40,44 +38,43 @@ public HibernateTraversableResolver(
4038
EntityPersister persister,
4139
ConcurrentHashMap<EntityPersister, Set<String>> associationsPerEntityPersister,
4240
SessionFactoryImplementor factory) {
43-
this.associations = associationsPerEntityPersister.get( persister );
44-
if (this.associations == null) {
45-
this.associations = new HashSet<>();
41+
associations = associationsPerEntityPersister.get( persister );
42+
if ( associations == null ) {
43+
associations = new HashSet<>();
4644
addAssociationsToTheSetForAllProperties( persister.getPropertyNames(), persister.getPropertyTypes(), "", factory );
4745
associationsPerEntityPersister.put( persister, associations );
4846
}
4947
}
5048

51-
private void addAssociationsToTheSetForAllProperties(String[] names, Type[] types, String prefix, SessionFactoryImplementor factory) {
49+
private void addAssociationsToTheSetForAllProperties(
50+
String[] names, Type[] types, String prefix, SessionFactoryImplementor factory) {
5251
final int length = names.length;
5352
for( int index = 0 ; index < length; index++ ) {
5453
addAssociationsToTheSetForOneProperty( names[index], types[index], prefix, factory );
5554
}
5655
}
5756

58-
private void addAssociationsToTheSetForOneProperty(String name, Type type, String prefix, SessionFactoryImplementor factory) {
59-
60-
if ( type instanceof CollectionType ) {
61-
CollectionType collType = (CollectionType) type;
62-
Type assocType = collType.getElementType( factory );
63-
addAssociationsToTheSetForOneProperty(name, assocType, prefix, factory);
57+
private void addAssociationsToTheSetForOneProperty(
58+
String name, Type type, String prefix, SessionFactoryImplementor factory) {
59+
if ( type instanceof CollectionType collectionType ) {
60+
addAssociationsToTheSetForOneProperty( name, collectionType.getElementType( factory ), prefix, factory );
6461
}
6562
//ToOne association
6663
else if ( type instanceof EntityType || type instanceof AnyType ) {
6764
associations.add( prefix + name );
6865
}
69-
else if ( type instanceof ComponentType ) {
70-
ComponentType componentType = (ComponentType) type;
66+
else if ( type instanceof ComponentType componentType ) {
7167
addAssociationsToTheSetForAllProperties(
7268
componentType.getPropertyNames(),
7369
componentType.getSubtypes(),
7470
( prefix.isEmpty() ? name : prefix + name ) + '.',
75-
factory);
71+
factory
72+
);
7673
}
7774
}
7875

7976
private String getStringBasedPath(Path.Node traversableProperty, Path pathToTraversableObject) {
80-
StringBuilder path = new StringBuilder( );
77+
final StringBuilder path = new StringBuilder( );
8178
for ( Path.Node node : pathToTraversableObject ) {
8279
if (node.getName() != null) {
8380
path.append( node.getName() ).append( '.' );
@@ -89,7 +86,6 @@ private String getStringBasedPath(Path.Node traversableProperty, Path pathToTrav
8986
+ path );
9087
}
9188
path.append( traversableProperty.getName() );
92-
9389
return path.toString();
9490
}
9591

@@ -100,15 +96,14 @@ public boolean isReachable(Object traversableObject,
10096
ElementType elementType) {
10197
//lazy, don't load
10298
return Hibernate.isInitialized( traversableObject )
103-
&& Hibernate.isPropertyInitialized( traversableObject, traversableProperty.getName() );
99+
&& Hibernate.isPropertyInitialized( traversableObject, traversableProperty.getName() );
104100
}
105101

106102
public boolean isCascadable(Object traversableObject,
107103
Path.Node traversableProperty,
108104
Class<?> rootBeanType,
109105
Path pathToTraversableObject,
110106
ElementType elementType) {
111-
String path = getStringBasedPath( traversableProperty, pathToTraversableObject );
112-
return ! associations.contains(path);
107+
return !associations.contains( getStringBasedPath( traversableProperty, pathToTraversableObject ) );
113108
}
114109
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,9 @@ private static void applyMin(Property property, ConstraintDescriptor<?> descript
279279
final long min = minConstraint.getAnnotation().value();
280280

281281
for ( Selectable selectable : property.getSelectables() ) {
282-
if ( selectable instanceof Column ) {
283-
Column col = (Column) selectable;
284-
String checkConstraint = col.getQuotedName(dialect) + ">=" + min;
285-
applySQLCheck( col, checkConstraint );
282+
if ( selectable instanceof Column column ) {
283+
final String checkConstraint = column.getQuotedName( dialect ) + ">=" + min;
284+
applySQLCheck( column, checkConstraint );
286285
}
287286
}
288287
}
@@ -295,10 +294,9 @@ private static void applyMax(Property property, ConstraintDescriptor<?> descript
295294
final long max = maxConstraint.getAnnotation().value();
296295

297296
for ( Selectable selectable : property.getSelectables() ) {
298-
if ( selectable instanceof Column ) {
299-
Column col = (Column) selectable;
300-
String checkConstraint = col.getQuotedName( dialect ) + "<=" + max;
301-
applySQLCheck( col, checkConstraint );
297+
if ( selectable instanceof Column column ) {
298+
final String checkConstraint = column.getQuotedName( dialect ) + "<=" + max;
299+
applySQLCheck( column, checkConstraint );
302300
}
303301
}
304302
}
@@ -354,10 +352,9 @@ private static void applyDigits(Property property, ConstraintDescriptor<?> descr
354352
int fractionalDigits = digitsConstraint.getAnnotation().fraction();
355353

356354
for ( Selectable selectable : property.getSelectables() ) {
357-
if ( selectable instanceof Column ) {
358-
Column col = (Column) selectable;
359-
col.setPrecision( integerDigits + fractionalDigits );
360-
col.setScale( fractionalDigits );
355+
if ( selectable instanceof Column column ) {
356+
column.setPrecision( integerDigits + fractionalDigits );
357+
column.setScale( fractionalDigits );
361358
}
362359
}
363360

@@ -387,10 +384,9 @@ private static void applyLength(Property property, ConstraintDescriptor<?> descr
387384
int max = (Integer) descriptor.getAttributes().get( "max" );
388385

389386
for ( Selectable selectable : property.getSelectables() ) {
390-
if ( selectable instanceof Column ) {
391-
Column col = (Column) selectable;
387+
if ( selectable instanceof Column column ) {
392388
if ( max < Integer.MAX_VALUE ) {
393-
col.setLength( max );
389+
column.setLength( max );
394390
}
395391
}
396392
}

0 commit comments

Comments
 (0)