Skip to content

Commit 6dab68d

Browse files
committed
#45 - Allow full construction of DynamicClassDetails, DynamicFieldDetails and DynamicMethodDetails
1 parent 2eda68f commit 6dab68d

File tree

6 files changed

+211
-19
lines changed

6 files changed

+211
-19
lines changed

src/main/java/org/hibernate/models/internal/dynamic/DynamicClassDetails.java

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.hibernate.models.spi.TypeVariableDetails;
2121

2222
/**
23+
* ClassDetails which does not necessarily map to a physical Class (dynamic models)
24+
*
2325
* @author Steve Ebersole
2426
*/
2527
public class DynamicClassDetails extends AbstractAnnotationTarget implements ClassDetailsSupport {
@@ -53,12 +55,24 @@ public DynamicClassDetails(
5355
ClassDetails superClass,
5456
TypeDetails genericSuperType,
5557
SourceModelBuildingContext buildingContext) {
58+
this( name, className, null, isAbstract, superClass, genericSuperType, buildingContext );
59+
}
60+
61+
public DynamicClassDetails(
62+
String name,
63+
String className,
64+
Class<?> javaType,
65+
boolean isAbstract,
66+
ClassDetails superClass,
67+
TypeDetails genericSuperType,
68+
SourceModelBuildingContext buildingContext) {
5669
super( buildingContext );
5770
this.name = name;
5871
this.className = className;
5972
this.isAbstract = isAbstract;
6073
this.superClass = superClass;
6174
this.genericSuperType = genericSuperType;
75+
this.javaType = javaType;
6276
}
6377

6478
@Override

src/main/java/org/hibernate/models/internal/dynamic/DynamicFieldDetails.java

+23-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.hibernate.models.spi.TypeVariableScope;
1919

2020
/**
21+
* FieldDetails which does not necessarily map to a physical Field (dynamic models)
22+
*
2123
* @author Steve Ebersole
2224
*/
2325
public class DynamicFieldDetails extends AbstractAnnotationTarget implements FieldDetails, MutableMemberDetails {
@@ -35,20 +37,32 @@ public DynamicFieldDetails(
3537
ClassDetails declaringType,
3638
int modifierFlags,
3739
SourceModelBuildingContext buildingContext) {
40+
this(
41+
name,
42+
type,
43+
declaringType,
44+
modifierFlags,
45+
type != null && type.getName().startsWith( "[" ),
46+
type != null && ( type.isImplementor( Collection.class ) || type.isImplementor( Map.class ) ),
47+
buildingContext
48+
);
49+
}
50+
51+
public DynamicFieldDetails(
52+
String name,
53+
TypeDetails type,
54+
ClassDetails declaringType,
55+
int modifierFlags,
56+
boolean isArray,
57+
boolean isPlural,
58+
SourceModelBuildingContext buildingContext) {
3859
super( buildingContext );
3960
this.name = name;
4061
this.type = type;
4162
this.declaringType = declaringType;
4263
this.modifierFlags = modifierFlags;
43-
44-
if ( type != null ) {
45-
this.isArray = type.getName().startsWith( "[" );
46-
this.isPlural = isArray || type.isImplementor( Collection.class ) || type.isImplementor( Map.class );
47-
}
48-
else {
49-
this.isArray = false;
50-
this.isPlural = false;
51-
}
64+
this.isArray = isArray;
65+
this.isPlural = isPlural;
5266
}
5367

5468
@Override

src/main/java/org/hibernate/models/internal/dynamic/DynamicMethodDetails.java

+29-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.hibernate.models.spi.TypeVariableScope;
2020

2121
/**
22+
* MethodDetails which does not necessarily map to a physical Method (dynamic models)
23+
*
2224
* @author Steve Ebersole
2325
*/
2426
public class DynamicMethodDetails extends AbstractAnnotationTarget implements MethodDetails, MutableMemberDetails {
@@ -43,23 +45,41 @@ public DynamicMethodDetails(
4345
ClassDetails returnType,
4446
List<ClassDetails> argumentTypes,
4547
SourceModelBuildingContext buildingContext) {
48+
this(
49+
name,
50+
type,
51+
declaringType,
52+
methodKind,
53+
modifierFlags,
54+
type != null && type.getName().startsWith( "[" ),
55+
type != null && ( type.isImplementor( Collection.class ) || type.isImplementor( Map.class ) ),
56+
returnType,
57+
argumentTypes,
58+
buildingContext
59+
);
60+
}
61+
62+
public DynamicMethodDetails(
63+
String name,
64+
TypeDetails type,
65+
ClassDetails declaringType,
66+
MethodKind methodKind,
67+
int modifierFlags,
68+
boolean isArray,
69+
boolean isPlural,
70+
ClassDetails returnType,
71+
List<ClassDetails> argumentTypes,
72+
SourceModelBuildingContext buildingContext) {
4673
super( buildingContext );
4774
this.name = name;
4875
this.type = type;
4976
this.declaringType = declaringType;
5077
this.methodKind = methodKind;
5178
this.modifierFlags = modifierFlags;
79+
this.isArray = isArray;
80+
this.isPlural = isPlural;
5281
this.returnType = returnType;
5382
this.argumentTypes = argumentTypes;
54-
55-
if ( type != null ) {
56-
this.isArray = type.getName().startsWith( "[" );
57-
this.isPlural = isArray || type.isImplementor( Collection.class ) || type.isImplementor( Map.class );
58-
}
59-
else {
60-
this.isArray = false;
61-
this.isPlural = false;
62-
}
6383
}
6484

6585
@Override

src/test/java/org/hibernate/models/annotations/DynamicAnnotationTests.java renamed to src/test/java/org/hibernate/models/dynamic/DynamicAnnotationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Copyright: Red Hat Inc. and Hibernate Authors
66
*/
77

8-
package org.hibernate.models.annotations;
8+
package org.hibernate.models.dynamic;
99

1010
import java.util.List;
1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
* Copyright: Red Hat Inc. and Hibernate Authors
6+
*/
7+
8+
package org.hibernate.models.dynamic;
9+
10+
import java.lang.reflect.Modifier;
11+
12+
import org.hibernate.models.SourceModelTestHelper;
13+
import org.hibernate.models.internal.ClassTypeDetailsImpl;
14+
import org.hibernate.models.internal.SourceModelBuildingContextImpl;
15+
import org.hibernate.models.internal.dynamic.DynamicClassDetails;
16+
import org.hibernate.models.internal.dynamic.DynamicFieldDetails;
17+
import org.hibernate.models.orm.JpaAnnotations;
18+
import org.hibernate.models.spi.ClassDetails;
19+
import org.hibernate.models.spi.ClassDetailsRegistry;
20+
import org.hibernate.models.spi.MutableClassDetails;
21+
import org.hibernate.models.spi.TypeDetails;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.jboss.jandex.Index;
26+
27+
/**
28+
* @author Steve Ebersole
29+
*/
30+
public class SimpleDynamicModelTests {
31+
@Test
32+
void testSimpleBasics() {
33+
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper.createBuildingContext( (Index) null );
34+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
35+
36+
final ClassDetails integerClassDetails = classDetailsRegistry.getClassDetails( Integer.class.getName() );
37+
final ClassTypeDetailsImpl integerTypeDetails = new ClassTypeDetailsImpl( integerClassDetails, TypeDetails.Kind.CLASS );
38+
39+
final ClassDetails stringClassDetails = classDetailsRegistry.getClassDetails( String.class.getName() );
40+
final ClassTypeDetailsImpl stringTypeDetails = new ClassTypeDetailsImpl( stringClassDetails, TypeDetails.Kind.CLASS );
41+
42+
final MutableClassDetails entityDetails = (MutableClassDetails) classDetailsRegistry.resolveClassDetails(
43+
"TheEntity",
44+
name -> new DynamicClassDetails( name, buildingContext )
45+
);
46+
entityDetails.addAnnotationUsage( JpaAnnotations.ENTITY.createUsage( entityDetails, buildingContext ) );
47+
48+
49+
final DynamicFieldDetails idFieldDetails = new DynamicFieldDetails(
50+
"id",
51+
integerTypeDetails,
52+
entityDetails,
53+
Modifier.fieldModifiers(),
54+
false,
55+
false,
56+
buildingContext
57+
);
58+
entityDetails.addField( idFieldDetails );
59+
60+
final DynamicFieldDetails nameFieldDetails = new DynamicFieldDetails(
61+
"name",
62+
stringTypeDetails,
63+
entityDetails,
64+
Modifier.fieldModifiers(),
65+
false,
66+
false,
67+
buildingContext
68+
);
69+
entityDetails.addField( nameFieldDetails );
70+
}
71+
72+
@Test
73+
void testSimpleEmbedded() {
74+
final SourceModelBuildingContextImpl buildingContext = SourceModelTestHelper.createBuildingContext( (Index) null );
75+
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
76+
77+
final ClassDetails integerClassDetails = classDetailsRegistry.getClassDetails( Integer.class.getName() );
78+
final ClassTypeDetailsImpl integerTypeDetails = new ClassTypeDetailsImpl( integerClassDetails, TypeDetails.Kind.CLASS );
79+
80+
final ClassDetails stringClassDetails = classDetailsRegistry.getClassDetails( String.class.getName() );
81+
final ClassTypeDetailsImpl stringTypeDetails = new ClassTypeDetailsImpl( stringClassDetails, TypeDetails.Kind.CLASS );
82+
83+
final MutableClassDetails entityDetails = (MutableClassDetails) classDetailsRegistry.resolveClassDetails(
84+
"TheEntity",
85+
name -> new DynamicClassDetails( name, buildingContext )
86+
);
87+
entityDetails.addAnnotationUsage( JpaAnnotations.ENTITY.createUsage( entityDetails, buildingContext ) );
88+
89+
final MutableClassDetails nameEmbeddableDetails = (MutableClassDetails) classDetailsRegistry.resolveClassDetails(
90+
"TheName",
91+
name -> new DynamicClassDetails( name, buildingContext )
92+
);
93+
nameEmbeddableDetails.addAnnotationUsage( JpaAnnotations.EMBEDDABLE.createUsage( entityDetails, buildingContext ) );
94+
final ClassTypeDetailsImpl nameEmbeddableTypeDetails = new ClassTypeDetailsImpl( nameEmbeddableDetails, TypeDetails.Kind.CLASS );
95+
96+
final DynamicFieldDetails firstFieldDetails = new DynamicFieldDetails(
97+
"first",
98+
stringTypeDetails,
99+
nameEmbeddableDetails,
100+
Modifier.fieldModifiers(),
101+
false,
102+
false,
103+
buildingContext
104+
);
105+
nameEmbeddableDetails.addField( firstFieldDetails );
106+
107+
final DynamicFieldDetails lastFieldDetails = new DynamicFieldDetails(
108+
"last",
109+
stringTypeDetails,
110+
nameEmbeddableDetails,
111+
Modifier.fieldModifiers(),
112+
false,
113+
false,
114+
buildingContext
115+
);
116+
nameEmbeddableDetails.addField( lastFieldDetails );
117+
118+
119+
final DynamicFieldDetails nameFieldDetails = new DynamicFieldDetails(
120+
"name",
121+
nameEmbeddableTypeDetails,
122+
entityDetails,
123+
Modifier.fieldModifiers(),
124+
false,
125+
false,
126+
buildingContext
127+
);
128+
entityDetails.addField( nameFieldDetails );
129+
}
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
* Copyright: Red Hat Inc. and Hibernate Authors
6+
*/
7+
8+
/**
9+
* Tests verifying expected usage patterns of {@linkplain org.hibernate.models.dynamic dynamic models}
10+
* as expected by Hibernate ORM
11+
*
12+
* @author Steve Ebersole
13+
*/
14+
package org.hibernate.models.dynamic;

0 commit comments

Comments
 (0)