Skip to content

Commit 7d9c170

Browse files
committed
uniform reporting of null argument to EM, Session, and StatelessSession methods
1 parent 4ad08ed commit 7d9c170

File tree

15 files changed

+223
-30
lines changed

15 files changed

+223
-30
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,7 @@ public interface Session extends SharedSessionContract, EntityManager {
488488
*
489489
* @param object the managed entity to evict
490490
*
491-
* @throws NullPointerException if the passed object is {@code null}
492-
* @throws IllegalArgumentException if the passed object is not mapped as an entity
491+
* @throws IllegalArgumentException if the given object is not an entity
493492
*/
494493
void evict(Object object);
495494

@@ -1026,7 +1025,10 @@ public interface Session extends SharedSessionContract, EntityManager {
10261025
void refresh(Object object, LockOptions lockOptions);
10271026

10281027
/**
1029-
* Return the entity name for a persistent entity.
1028+
* Return the entity name for the given persistent entity.
1029+
* <p>
1030+
* If the given entity is an uninitialized proxy, the proxy is initialized by
1031+
* side effect.
10301032
*
10311033
* @param object a persistent entity associated with this session
10321034
*

hibernate-core/src/main/java/org/hibernate/event/spi/DeleteEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class DeleteEvent extends AbstractEvent {
2525
public DeleteEvent(Object object, EventSource source) {
2626
super(source);
2727
if (object == null) {
28-
throw new IllegalArgumentException( "attempt to create delete event with null entity" );
28+
throw new IllegalArgumentException( "Entity may not be null" );
2929
}
3030
this.object = object;
3131
}

hibernate-core/src/main/java/org/hibernate/event/spi/EvictEvent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class EvictEvent extends AbstractEvent {
1919

2020
public EvictEvent(Object object, EventSource source) {
2121
super(source);
22+
if (object == null) {
23+
throw new IllegalArgumentException( "Entity may not be null" );
24+
}
2225
this.object = object;
2326
}
2427

hibernate-core/src/main/java/org/hibernate/event/spi/LockEvent.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,24 @@ public LockEvent(String entityName, Object original, LockOptions lockOptions, Ev
3333

3434
public LockEvent(Object object, LockMode lockMode, EventSource source) {
3535
super(source);
36+
if (object == null) {
37+
throw new IllegalArgumentException( "Entity may not be null" );
38+
}
39+
if (lockMode == null) {
40+
throw new IllegalArgumentException( "LockMode may not be null" );
41+
}
3642
this.object = object;
3743
this.lockOptions = lockMode.toLockOptions();
3844
}
3945

4046
public LockEvent(Object object, LockOptions lockOptions, EventSource source) {
4147
super(source);
48+
if (object == null) {
49+
throw new IllegalArgumentException( "Entity may not be null" );
50+
}
51+
if (lockOptions == null) {
52+
throw new IllegalArgumentException( "LockOptions may not be null" );
53+
}
4254
this.object = object;
4355
this.lockOptions = lockOptions;
4456
}

hibernate-core/src/main/java/org/hibernate/event/spi/MergeEvent.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,14 @@ public MergeEvent(String entityName, Object original, Object id, EventSource sou
2828
this(entityName, original, source);
2929
this.requestedId = id;
3030
if ( requestedId == null ) {
31-
throw new IllegalArgumentException(
32-
"attempt to create merge event with null identifier"
33-
);
31+
throw new IllegalArgumentException( "Identifier may not be null" );
3432
}
3533
}
3634

3735
public MergeEvent(Object object, EventSource source) {
3836
super(source);
3937
if ( object == null ) {
40-
throw new IllegalArgumentException(
41-
"attempt to create merge event with null entity"
42-
);
38+
throw new IllegalArgumentException( "Entity may not be null" );
4339
}
4440
this.original = object;
4541
}

hibernate-core/src/main/java/org/hibernate/event/spi/PersistEvent.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ public PersistEvent(String entityName, Object original, EventSource source) {
2424
public PersistEvent(Object object, EventSource source) {
2525
super(source);
2626
if ( object == null ) {
27-
throw new IllegalArgumentException(
28-
"attempt to create event with null entity"
29-
);
27+
throw new IllegalArgumentException( "Entity may not be null" );
3028
}
3129
this.object = object;
3230
}

hibernate-core/src/main/java/org/hibernate/event/spi/RefreshEvent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class RefreshEvent extends AbstractEvent {
2525
public RefreshEvent(Object object, EventSource source) {
2626
super(source);
2727
if (object == null) {
28-
throw new IllegalArgumentException("Attempt to generate refresh event with null object");
28+
throw new IllegalArgumentException("Entity may not be null");
2929
}
3030
this.object = object;
3131
}
@@ -38,15 +38,15 @@ public RefreshEvent(String entityName, Object object, EventSource source){
3838
public RefreshEvent(Object object, LockMode lockMode, EventSource source) {
3939
this(object, source);
4040
if (lockMode == null) {
41-
throw new IllegalArgumentException("Attempt to generate refresh event with null lock mode");
41+
throw new IllegalArgumentException("LockMode may not be null");
4242
}
4343
this.lockOptions.setLockMode(lockMode);
4444
}
4545

4646
public RefreshEvent(Object object, LockOptions lockOptions, EventSource source) {
4747
this(object, source);
4848
if (lockOptions == null) {
49-
throw new IllegalArgumentException("Attempt to generate refresh event with null lock request");
49+
throw new IllegalArgumentException("LockMode may not be null");
5050
}
5151
this.lockOptions = lockOptions;
5252
}

hibernate-core/src/main/java/org/hibernate/event/spi/ReplicateEvent.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@ public ReplicateEvent(String entityName, Object object, ReplicationMode replicat
2727
this.entityName = entityName;
2828

2929
if ( object == null ) {
30-
throw new IllegalArgumentException(
31-
"attempt to create replication strategy with null entity"
32-
);
30+
throw new IllegalArgumentException( "Entity may not be null" );
3331
}
3432
if ( replicationMode == null ) {
35-
throw new IllegalArgumentException(
36-
"attempt to create replication strategy with null replication mode"
37-
);
33+
throw new IllegalArgumentException( "ReplicationMode may not be null" );
3834
}
3935

4036
this.object = object;

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,9 @@ public EntityPersister getEntityPersister(final String entityName, final Object
15501550
public Object getIdentifier(Object object) {
15511551
checkOpen();
15521552
checkTransactionSynchStatus();
1553+
if ( object == null ) {
1554+
throw new IllegalArgumentException( "Entity may not be null" );
1555+
}
15531556
final LazyInitializer lazyInitializer = extractLazyInitializer( object );
15541557
if ( lazyInitializer != null ) {
15551558
if ( lazyInitializer.getSession() != this ) {
@@ -1809,6 +1812,11 @@ public String bestGuessEntityName(Object object, EntityEntry entry) {
18091812
public String getEntityName(Object object) {
18101813
checkOpen();
18111814
// checkTransactionSynchStatus();
1815+
1816+
if ( object == null ) {
1817+
throw new IllegalArgumentException( "Entity may not be null" );
1818+
}
1819+
18121820
final LazyInitializer lazyInitializer = extractLazyInitializer( object );
18131821
if ( lazyInitializer != null ) {
18141822
if ( !persistenceContext.containsProxy( object ) ) {
@@ -2774,8 +2782,13 @@ public LockModeType getLockMode(Object entity) {
27742782
throw new TransactionRequiredException( "No active transaction" );
27752783
}
27762784

2785+
if ( entity == null ) {
2786+
throw new IllegalArgumentException( "Entity may not be null" );
2787+
}
27772788
if ( !contains( entity ) ) {
2778-
throw getExceptionConverter().convert( new IllegalArgumentException( "Entity not associated with the persistence context" ) );
2789+
// convert() calls markForRollbackOnly()
2790+
throw getExceptionConverter()
2791+
.convert( new IllegalArgumentException( "Entity not associated with the persistence context" ) );
27792792
}
27802793

27812794
return LockModeTypeHelper.getLockModeType( getCurrentLockMode( entity ) );

hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,15 +1207,21 @@ public Object getContextEntityIdentifier(Object object) {
12071207
@Override
12081208
public String guessEntityName(Object entity) {
12091209
checkOpen();
1210+
if ( entity == null ) {
1211+
throw new IllegalArgumentException( "Entity may not be null" );
1212+
}
12101213
return entity.getClass().getName();
12111214
}
12121215

12131216
@Override
1214-
public EntityPersister getEntityPersister(String entityName, Object object) {
1217+
public EntityPersister getEntityPersister(String entityName, Object entity) {
12151218
checkOpen();
1219+
if ( entity == null ) {
1220+
throw new IllegalArgumentException( "Entity may not be null" );
1221+
}
12161222
return entityName == null
1217-
? requireEntityPersister( guessEntityName( object ) )
1218-
: requireEntityPersister( entityName ).getSubclassEntityPersister( object, getFactory() );
1223+
? requireEntityPersister( guessEntityName( entity ) )
1224+
: requireEntityPersister( entityName ).getSubclassEntityPersister( entity, getFactory() );
12191225
}
12201226

12211227
@Override

0 commit comments

Comments
 (0)