Skip to content

Commit

Permalink
squash more warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Sep 22, 2024
1 parent 8b83a53 commit 6da6276
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.hibernate.type.OneToOneType;
import org.hibernate.type.Type;

import static java.util.Collections.EMPTY_LIST;
import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy;
import static org.hibernate.engine.spi.CascadingActions.CHECK_ON_FLUSH;
import static org.hibernate.pretty.MessageHelper.infoString;
Expand Down Expand Up @@ -89,7 +90,8 @@ public static <T> void cascade(
LOG.tracev( "Processing cascade {0} for: {1}", action, persister.getEntityName() );
}
final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal();
final boolean enhancedForLazyLoading = persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading();
final boolean enhancedForLazyLoading =
persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading();
final EntityEntry entry;
if ( enhancedForLazyLoading ) {
entry = persistenceContext.getEntry( parent );
Expand All @@ -112,8 +114,9 @@ public static <T> void cascade(
final String propertyName = propertyNames[ i ];
final Type type = types[i];
final boolean isUninitializedProperty =
hasUninitializedLazyProperties &&
!persister.getBytecodeEnhancementMetadata().isAttributeLoaded( parent, propertyName );
hasUninitializedLazyProperties
&& !persister.getBytecodeEnhancementMetadata()
.isAttributeLoaded( parent, propertyName );

if ( style.doCascade( action ) ) {
final Object child;
Expand All @@ -129,14 +132,13 @@ public static <T> void cascade(
// parent was not in the PersistenceContext
continue;
}
if ( type instanceof CollectionType ) {
if ( type instanceof CollectionType collectionType ) {
// CollectionType#getCollection gets the PersistentCollection
// that corresponds to the uninitialized collection from the
// PersistenceContext. If not present, an uninitialized
// PersistentCollection will be added to the PersistenceContext.
// The action may initialize it later, if necessary.
// This needs to be done even when action.performOnLazyProperty() returns false.
final CollectionType collectionType = (CollectionType) type;
child = collectionType.getCollection(
collectionType.getKeyOfOwner( parent, eventSource ),
eventSource,
Expand All @@ -146,15 +148,14 @@ public static <T> void cascade(
}
else if ( type instanceof AnyType || type instanceof ComponentType ) {
// Hibernate does not support lazy embeddables, so this shouldn't happen.
throw new UnsupportedOperationException(
"Lazy components are not supported."
);
throw new UnsupportedOperationException( "Lazy embeddables are not supported" );
}
else if ( action.performOnLazyProperty() && type instanceof EntityType ) {
// Only need to initialize a lazy entity attribute when action.performOnLazyProperty()
// returns true.
LazyAttributeLoadingInterceptor interceptor = persister.getBytecodeEnhancementMetadata()
.extractInterceptor( parent );
final LazyAttributeLoadingInterceptor interceptor =
persister.getBytecodeEnhancementMetadata()
.extractInterceptor( parent );
child = interceptor.fetchAttribute( parent, propertyName );

}
Expand Down Expand Up @@ -238,10 +239,10 @@ private static <T> void cascadeProperty(
style,
anything,
isCascadeDeleteEnabled
);
);
}
}
else if ( type instanceof ComponentType ) {
else if ( type instanceof ComponentType componentType ) {
if ( componentPath == null && propertyName != null ) {
componentPath = new ArrayList<>();
}
Expand All @@ -255,7 +256,7 @@ else if ( type instanceof ComponentType ) {
componentPath,
parent,
child,
(CompositeType) type,
componentType,
anything
);
if ( componentPath != null ) {
Expand All @@ -273,7 +274,8 @@ else if ( type instanceof ComponentType ) {
type,
style,
propertyName,
isCascadeDeleteEnabled );
isCascadeDeleteEnabled
);
}
}

Expand Down Expand Up @@ -376,8 +378,8 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(

private static boolean isForeignKeyToParent(Type type) {
return type instanceof CollectionType
|| type instanceof OneToOneType
&& ((OneToOneType) type).getForeignKeyDirection() == ForeignKeyDirection.TO_PARENT;
|| type instanceof OneToOneType oneToOneType
&& oneToOneType.getForeignKeyDirection() == ForeignKeyDirection.TO_PARENT;
}

/**
Expand All @@ -389,7 +391,7 @@ private static boolean isForeignKeyToParent(Type type) {
* @return True if the attribute represents a logical one to one association
*/
private static boolean isLogicalOneToOne(Type type) {
return type instanceof EntityType && ( (EntityType) type ).isLogicalOneToOne();
return type instanceof EntityType entityType && entityType.isLogicalOneToOne();
}

private static boolean cascadeAssociationNow(
Expand All @@ -399,24 +401,22 @@ private static boolean cascadeAssociationNow(
SessionFactoryImplementor factory,
boolean unownedTransient) {
return associationType.getForeignKeyDirection().cascadeNow( cascadePoint )
// For check on flush, we should only check unowned associations when strictness is enforced
&& ( action != CHECK_ON_FLUSH || unownedTransient || !isUnownedAssociation( associationType, factory ) );
// For check on flush, we should only check unowned associations when strictness is enforced
&& ( action != CHECK_ON_FLUSH || unownedTransient || !isUnownedAssociation( associationType, factory ) );
}

private static boolean isUnownedAssociation(AssociationType associationType, SessionFactoryImplementor factory) {
if ( associationType instanceof ManyToOneType ) {
final ManyToOneType manyToOne = (ManyToOneType) associationType;
if ( associationType instanceof ManyToOneType manyToOne ) {
// logical one-to-one + non-null unique key property name indicates unowned
return manyToOne.isLogicalOneToOne() && manyToOne.getRHSUniqueKeyPropertyName() != null;
}
else if ( associationType instanceof OneToOneType ) {
final OneToOneType oneToOne = (OneToOneType) associationType;
else if ( associationType instanceof OneToOneType oneToOne ) {
// constrained false + non-null unique key property name indicates unowned
return oneToOne.isNullable() && oneToOne.getRHSUniqueKeyPropertyName() != null;
}
else if ( associationType instanceof CollectionType ) {
else if ( associationType instanceof CollectionType collectionType ) {
// for collections, we can ask the persister if we're on the inverse side
return ( (CollectionType) associationType ).isInverse( factory );
return collectionType.isInverse( factory );
}
return false;
}
Expand Down Expand Up @@ -454,7 +454,7 @@ private static <T> void cascadeComponent(
subPropertyName,
anything,
false
);
);
}
}
}
Expand All @@ -473,7 +473,7 @@ private static <T> void cascadeAssociation(
if ( type instanceof EntityType || type instanceof AnyType ) {
cascadeToOne( action, eventSource, parent, child, type, style, anything, isCascadeDeleteEnabled );
}
else if ( type instanceof CollectionType ) {
else if ( type instanceof CollectionType collectionType ) {
cascadeCollection(
action,
cascadePoint,
Expand All @@ -483,7 +483,7 @@ else if ( type instanceof CollectionType ) {
child,
style,
anything,
(CollectionType) type
collectionType
);
}
}
Expand All @@ -505,17 +505,13 @@ private static <T> void cascadeCollection(
eventSource.getFactory().getMappingMetamodel()
.getCollectionDescriptor( type.getRole() );
final Type elemType = persister.getElementType();

CascadePoint elementsCascadePoint = cascadePoint;
if ( cascadePoint == CascadePoint.AFTER_INSERT_BEFORE_DELETE ) {
elementsCascadePoint = CascadePoint.AFTER_INSERT_BEFORE_DELETE_VIA_COLLECTION;
}

//cascade to current collection elements
if ( elemType instanceof EntityType || elemType instanceof AnyType || elemType instanceof ComponentType ) {
cascadeCollectionElements(
action,
elementsCascadePoint,
cascadePoint == CascadePoint.AFTER_INSERT_BEFORE_DELETE
? CascadePoint.AFTER_INSERT_BEFORE_DELETE_VIA_COLLECTION
: cascadePoint,
eventSource,
componentPath,
parent,
Expand All @@ -541,9 +537,10 @@ private static <T> void cascadeToOne(
final CascadeStyle style,
final T anything,
final boolean isCascadeDeleteEnabled) {
final String entityName = type instanceof EntityType
? ( (EntityType) type ).getAssociatedEntityName()
: null;
final String entityName =
type instanceof EntityType entityType
? entityType.getAssociatedEntityName()
: null;
if ( style.reallyDoCascade( action ) ) {
//not really necessary, but good for consistency...
final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal();
Expand Down Expand Up @@ -572,6 +569,7 @@ private static <T> void cascadeCollectionElements(
final Type elemType,
final T anything,
final boolean isCascadeDeleteEnabled) throws HibernateException {

final boolean reallyDoCascade = style.reallyDoCascade( action )
&& child != CollectionType.UNFETCHED_COLLECTION;

Expand All @@ -581,15 +579,15 @@ private static <T> void cascadeCollectionElements(
LOG.tracev( "Cascade {0} for collection: {1}", action, collectionType.getRole() );
}

final Iterator<?> itr = action.getCascadableChildrenIterator( eventSource, collectionType, child );
while ( itr.hasNext() ) {
final Iterator<?> iterator = action.getCascadableChildrenIterator( eventSource, collectionType, child );
while ( iterator.hasNext() ) {
cascadeProperty(
action,
cascadePoint,
eventSource,
componentPath,
parent,
itr.next(),
iterator.next(),
elemType,
style,
collectionType.getRole().substring( collectionType.getRole().lastIndexOf('.') + 1 ),
Expand All @@ -606,9 +604,9 @@ private static <T> void cascadeCollectionElements(
final boolean deleteOrphans = style.hasOrphanDelete()
&& action.deleteOrphans()
&& elemType instanceof EntityType
&& child instanceof PersistentCollection
&& child instanceof PersistentCollection<?> persistentCollection
// a newly instantiated collection can't have orphans
&& ! ( (PersistentCollection<?>) child ).isNewlyInstantiated();
&& !persistentCollection.isNewlyInstantiated();

if ( deleteOrphans ) {
final boolean traceEnabled = LOG.isTraceEnabled();
Expand All @@ -634,10 +632,8 @@ private static void deleteOrphans(EventSource eventSource, String entityName, Pe
//TODO: suck this logic into the collection!
final Collection<?> orphans;
if ( pc.wasInitialized() ) {
final CollectionEntry ce = eventSource.getPersistenceContextInternal().getCollectionEntry( pc );
orphans = ce==null
? java.util.Collections.EMPTY_LIST
: ce.getOrphans( entityName, pc );
final CollectionEntry entry = eventSource.getPersistenceContextInternal().getCollectionEntry( pc );
orphans = entry == null ? EMPTY_LIST : entry.getOrphans( entityName, pc );
}
else {
orphans = pc.getQueuedOrphans( entityName );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,8 +1167,7 @@ private void addDirectDependency(Type type, @Nullable Object value, IdentityHash
if ( value == null ) {
return;
}
if ( type instanceof EntityType ) {
final EntityType entityType = (EntityType) type;
if ( type instanceof EntityType entityType ) {
final InsertInfo insertInfo = insertInfosByEntity.get( value );
if ( insertInfo != null ) {
if ( entityType.isOneToOne()
Expand All @@ -1188,8 +1187,7 @@ private void addDirectDependency(Type type, @Nullable Object value, IdentityHash
}
}
}
else if ( type instanceof CollectionType ) {
CollectionType collectionType = (CollectionType) type;
else if ( type instanceof CollectionType collectionType ) {
final PluralAttributeMapping pluralAttributeMapping = insertAction.getSession()
.getFactory()
.getMappingMetamodel()
Expand All @@ -1212,9 +1210,8 @@ else if ( type instanceof CollectionType ) {
}
}
}
else if ( type instanceof ComponentType ) {
else if ( type instanceof ComponentType compositeType ) {
// Support recursive checks of composite type properties for associations and collections.
ComponentType compositeType = (ComponentType) type;
final SharedSessionContractImplementor session = insertAction.getSession();
final Object[] componentValues = compositeType.getPropertyValues( value, session );
for ( int j = 0; j < componentValues.length; ++j ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface CascadeStyle extends Serializable {
*
* @return True if the action should be cascaded under this style; false otherwise.
*/
boolean doCascade(CascadingAction action);
boolean doCascade(CascadingAction<?> action);

/**
* Probably more aptly named something like doCascadeToCollectionElements(); it is
Expand All @@ -38,7 +38,7 @@ public interface CascadeStyle extends Serializable {
* @return True if the action should be really cascaded under this style;
* false otherwise.
*/
boolean reallyDoCascade(CascadingAction action);
boolean reallyDoCascade(CascadingAction<?> action);

/**
* Do we need to delete orphaned collection elements?
Expand Down
Loading

0 comments on commit 6da6276

Please sign in to comment.