Skip to content

Commit e1ec9df

Browse files
dreab8sebersole
authored andcommitted
97 Add dynamic-class resolution support to MutableClassDetailsRegistry
1 parent 98e9a1f commit e1ec9df

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

hibernate-models-jandex/src/test/java/org/hibernate/models/dynamic/SimpleDynamicModelTests.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,64 @@ void testSimpleBasics() {
8787
checkPersistability( entityDetails.getFields().get( 1 ) );
8888
}
8989

90+
91+
@Test
92+
void testResolveClassDetails() {
93+
final SourceModelBuildingContext buildingContext = SourceModelTestHelper.createBuildingContext( (Index) null );
94+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
95+
96+
final ClassDetails integerClassDetails = classDetailsRegistry.getClassDetails( Integer.class.getName() );
97+
final ClassTypeDetailsImpl integerTypeDetails = new ClassTypeDetailsImpl( integerClassDetails, TypeDetails.Kind.CLASS );
98+
99+
final ClassDetails stringClassDetails = classDetailsRegistry.getClassDetails( String.class.getName() );
100+
final ClassTypeDetailsImpl stringTypeDetails = new ClassTypeDetailsImpl( stringClassDetails, TypeDetails.Kind.CLASS );
101+
102+
classDetailsRegistry.as( MutableClassDetailsRegistry.class )
103+
.resolveClassDetails( "TheEntity", (name) -> new DynamicClassDetails( name, buildingContext ) );
104+
105+
final DynamicClassDetails entityDetails = (DynamicClassDetails) classDetailsRegistry.resolveClassDetails( "TheEntity" );
106+
final Entity created = entityDetails.applyAnnotationUsage(
107+
JpaAnnotations.ENTITY,
108+
buildingContext
109+
);
110+
final Entity preExisting = entityDetails.applyAnnotationUsage(
111+
JpaAnnotations.ENTITY,
112+
buildingContext
113+
);
114+
assertThat( created ).isSameAs( preExisting );
115+
116+
final DynamicFieldDetails idMember = entityDetails.applyAttribute(
117+
"id",
118+
integerTypeDetails,
119+
false,
120+
false,
121+
buildingContext
122+
);
123+
final Id first = idMember.applyAnnotationUsage(
124+
JpaAnnotations.ID,
125+
buildingContext
126+
);
127+
final Id second = idMember.applyAnnotationUsage(
128+
JpaAnnotations.ID,
129+
buildingContext
130+
);
131+
assertThat( first ).isSameAs( second );
132+
133+
entityDetails.applyAttribute(
134+
"name",
135+
stringTypeDetails,
136+
false,
137+
false,
138+
buildingContext
139+
);
140+
141+
assertThat( entityDetails.getFields() ).hasSize( 2 );
142+
assertThat( entityDetails.getFields().get( 0 ).getName() ).isEqualTo( "id" );
143+
assertThat( entityDetails.getFields().get( 0 ).hasDirectAnnotationUsage( Id.class ) ).isTrue();
144+
checkPersistability( entityDetails.getFields().get( 0 ) );
145+
checkPersistability( entityDetails.getFields().get( 1 ) );
146+
}
147+
90148
private void checkPersistability(FieldDetails fieldDetails) {
91149
assertThat( fieldDetails.isPersistable() ).isTrue();
92150

hibernate-models/src/main/java/org/hibernate/models/internal/AbstractClassDetailsRegistry.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,40 @@ public void addClassDetails(String name, ClassDetails classDetails) {
123123
subTypes.add( classDetails );
124124
}
125125
}
126+
127+
@Override
128+
public ClassDetails resolveClassDetails(String name, ClassDetailsCreator creator) {
129+
if ( name == null ) {
130+
throw new IllegalArgumentException( "`name` cannot be null" );
131+
}
132+
133+
if ( "void".equals( name ) ) {
134+
return null;
135+
}
136+
137+
final ClassDetails existing = classDetailsMap.get( name );
138+
if ( existing != null ) {
139+
return existing;
140+
}
141+
142+
return createClassDetails( name, creator );
143+
}
144+
145+
protected ClassDetails createClassDetails(String name, ClassDetailsCreator creator) {
146+
try {
147+
final ClassDetails created = creator.createClassDetails( name );
148+
addClassDetails( name, created );
149+
return created;
150+
}
151+
catch (UnknownClassException e) {
152+
// see if it might be a package name...
153+
try {
154+
return creator.createClassDetails( name + ".package-info" );
155+
}
156+
catch (UnknownClassException noPackage) {
157+
throw e;
158+
}
159+
}
160+
}
161+
126162
}

hibernate-models/src/main/java/org/hibernate/models/internal/MutableClassDetailsRegistry.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.models.internal;
66

7+
import org.hibernate.models.UnknownClassException;
78
import org.hibernate.models.spi.ClassDetails;
89
import org.hibernate.models.spi.ClassDetailsRegistry;
910

@@ -21,4 +22,21 @@ public interface MutableClassDetailsRegistry extends ClassDetailsRegistry {
2122
* Adds a managed-class descriptor using the given {@code name} as the registration key
2223
*/
2324
void addClassDetails(String name, ClassDetails classDetails);
25+
26+
/**
27+
* Resolve (find or create) ClassDetails by name. If there is currently no
28+
* such registration, one is created using the specified {@code creator}.
29+
*/
30+
ClassDetails resolveClassDetails(String name, ClassDetailsCreator creator);
31+
32+
/**
33+
* Create a CLass Details
34+
*/
35+
@FunctionalInterface
36+
interface ClassDetailsCreator {
37+
/**
38+
* @throws UnknownClassException
39+
*/
40+
ClassDetails createClassDetails(String name) throws UnknownClassException;
41+
}
2442
}

0 commit comments

Comments
 (0)