Skip to content

Commit 88c2312

Browse files
committed
HHH-18714 add an explicit type check instead of the cast to raw
also: - get rid another use of a raw type and warning suppression - change Boolean -> boolean
1 parent 5b7673d commit 88c2312

File tree

1 file changed

+37
-60
lines changed

1 file changed

+37
-60
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java

+37-60
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.checkerframework.checker.nullness.qual.Nullable;
2323

24+
import org.hibernate.AnnotationException;
2425
import org.hibernate.boot.model.NamedEntityGraphDefinition;
2526
import org.hibernate.boot.query.NamedQueryDefinition;
2627
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
@@ -501,7 +502,7 @@ private void applyNamedEntityGraphs(Collection<NamedEntityGraphDefinition> named
501502
}
502503

503504
final NamedEntityGraph namedEntityGraph = definition.getAnnotation();
504-
final RootGraphImpl<?> entityGraph =
505+
final RootGraphImplementor<?> entityGraph =
505506
createEntityGraph(
506507
namedEntityGraph,
507508
definition.getRegisteredName(),
@@ -513,24 +514,26 @@ private void applyNamedEntityGraphs(Collection<NamedEntityGraphDefinition> named
513514
}
514515
}
515516

516-
private <T> RootGraphImpl<T> createEntityGraph(
517+
private <T> RootGraphImplementor<T> createEntityGraph(
517518
NamedEntityGraph namedEntityGraph,
518519
String registeredName,
519520
EntityDomainType<T> entityType,
520521
boolean includeAllAttributes) {
521-
final RootGraphImpl<T> entityGraph =
522+
final RootGraphImplementor<T> entityGraph =
522523
createRootGraph( registeredName, entityType, includeAllAttributes );
523524

524525
if ( namedEntityGraph.subclassSubgraphs() != null ) {
525-
for ( var subclassSubgraph : namedEntityGraph.subclassSubgraphs() ) {
526-
GraphImplementor<?> subgraph = (GraphImplementor<?>) entityGraph.addTreatedSubgraph(
527-
(Class) subclassSubgraph.type() );
528-
529-
applyNamedAttributeNodes(
530-
subclassSubgraph.attributeNodes(),
531-
namedEntityGraph,
532-
subgraph
533-
);
526+
for ( NamedSubgraph subclassSubgraph : namedEntityGraph.subclassSubgraphs() ) {
527+
final Class<?> subgraphType = subclassSubgraph.type();
528+
final Class<T> graphJavaType = entityGraph.getGraphedType().getJavaType();
529+
if ( !graphJavaType.isAssignableFrom( subgraphType ) ) {
530+
throw new AnnotationException( "Named subgraph type '" + subgraphType.getName()
531+
+ "' is not a subtype of the graph type '" + graphJavaType.getName() + "'" );
532+
}
533+
@SuppressWarnings("unchecked") // Safe, because we just checked
534+
final Class<? extends T> subtype = (Class<? extends T>) subgraphType;
535+
final GraphImplementor<? extends T> subgraph = entityGraph.addTreatedSubgraph( subtype );
536+
applyNamedAttributeNodes( subclassSubgraph.attributeNodes(), namedEntityGraph, subgraph );
534537
}
535538
}
536539

@@ -541,7 +544,7 @@ private <T> RootGraphImpl<T> createEntityGraph(
541544
return entityGraph;
542545
}
543546

544-
private static <T> RootGraphImpl<T> createRootGraph(
547+
private static <T> RootGraphImplementor<T> createRootGraph(
545548
String name, EntityDomainType<T> entityType, boolean includeAllAttributes) {
546549
final RootGraphImpl<T> entityGraph = new RootGraphImpl<>( name, entityType );
547550
if ( includeAllAttributes ) {
@@ -558,8 +561,8 @@ private void applyNamedAttributeNodes(
558561
GraphImplementor<?> graphNode) {
559562
for ( NamedAttributeNode namedAttributeNode : namedAttributeNodes ) {
560563
final String value = namedAttributeNode.value();
561-
final AttributeNodeImplementor<?> attributeNode = (AttributeNodeImplementor<?>) graphNode.addAttributeNode(
562-
value );
564+
final AttributeNodeImplementor<?> attributeNode =
565+
(AttributeNodeImplementor<?>) graphNode.addAttributeNode( value );
563566

564567
if ( isNotEmpty( namedAttributeNode.subgraph() ) ) {
565568
applyNamedSubgraphs(
@@ -580,43 +583,36 @@ private void applyNamedAttributeNodes(
580583
}
581584
}
582585

583-
@SuppressWarnings({ "unchecked", "rawtypes" })
584586
private <T> void applyNamedSubgraphs(
585587
NamedEntityGraph namedEntityGraph,
586588
String subgraphName,
587-
AttributeNodeImplementor<T> attributeNode, Boolean isKeySubGraph) {
589+
AttributeNodeImplementor<T> attributeNode,
590+
boolean isKeySubGraph) {
588591
for ( NamedSubgraph namedSubgraph : namedEntityGraph.subgraphs() ) {
589592
if ( subgraphName.equals( namedSubgraph.name() ) ) {
590-
final var isDefaultSubgraphType = namedSubgraph.type().equals( void.class );
591-
final Class subGraphType = isDefaultSubgraphType ? null : namedSubgraph.type();
592-
593-
final SubGraphImplementor<?> subgraph = makeAttributeNodeSubgraph(
594-
attributeNode, isKeySubGraph,
595-
subGraphType
596-
);
597-
598-
applyNamedAttributeNodes(
599-
namedSubgraph.attributeNodes(),
600-
namedEntityGraph,
601-
subgraph
602-
);
593+
final boolean isDefaultSubgraphType = namedSubgraph.type().equals( void.class );
594+
final Class<?> subGraphType = isDefaultSubgraphType ? null : namedSubgraph.type();
595+
final SubGraphImplementor<?> subgraph =
596+
makeAttributeNodeSubgraph( attributeNode, isKeySubGraph, subGraphType );
597+
applyNamedAttributeNodes( namedSubgraph.attributeNodes(), namedEntityGraph, subgraph );
603598
}
604599
}
605600
}
606601

607602
private static <T> SubGraphImplementor<?> makeAttributeNodeSubgraph(
608603
AttributeNodeImplementor<T> attributeNode,
609-
Boolean isKeySubGraph,
610-
Class<T> subGraphType) {
611-
604+
boolean isKeySubGraph,
605+
Class<?> subGraphType) {
612606
if ( isKeySubGraph ) {
613-
return subGraphType != null ? attributeNode.makeKeySubGraph( subGraphType )
607+
return subGraphType != null
608+
? attributeNode.makeKeySubGraph( subGraphType )
614609
: attributeNode.makeKeySubGraph();
615610
}
616-
617-
return subGraphType != null ?
618-
attributeNode.makeSubGraph( subGraphType ) :
619-
attributeNode.makeSubGraph();
611+
else {
612+
return subGraphType != null
613+
? attributeNode.makeSubGraph( subGraphType )
614+
: attributeNode.makeSubGraph();
615+
}
620616
}
621617

622618
private <X> Class<X> resolveRequestedClass(String entityName) {
@@ -783,30 +779,11 @@ public void processJpa(
783779

784780
private void populateStaticMetamodel(MetadataImplementor bootMetamodel, MetadataContext context) {
785781
bootMetamodel.visitNamedHqlQueryDefinitions( definition
786-
-> injectTypedQueryReference(
787-
definition,
788-
namedQueryMetamodelClass(
789-
definition,
790-
context
791-
)
792-
) );
782+
-> injectTypedQueryReference( definition, namedQueryMetamodelClass( definition, context ) ) );
793783
bootMetamodel.visitNamedNativeQueryDefinitions( definition
794-
-> injectTypedQueryReference(
795-
definition,
796-
namedQueryMetamodelClass(
797-
definition,
798-
context
799-
)
800-
) );
784+
-> injectTypedQueryReference( definition, namedQueryMetamodelClass( definition, context ) ) );
801785
bootMetamodel.getNamedEntityGraphs().values().forEach( definition
802-
-> injectEntityGraph(
803-
definition,
804-
graphMetamodelClass(
805-
definition,
806-
context
807-
),
808-
this
809-
) );
786+
-> injectEntityGraph( definition, graphMetamodelClass( definition, context ), this ) );
810787
}
811788

812789
private Class<?> namedQueryMetamodelClass(NamedQueryDefinition<?> definition, MetadataContext context) {

0 commit comments

Comments
 (0)