Skip to content

Commit 1e88107

Browse files
committed
HHH-6173 Some initial refactorings to start processing @Embedded and @embeddable
1 parent 7700719 commit 1e88107

File tree

10 files changed

+221
-59
lines changed

10 files changed

+221
-59
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/domain/Attribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
package org.hibernate.metamodel.domain;
2525

2626
/**
27-
* Desribes an attribute.
27+
* Describes an attribute.
2828
*
2929
* @author Steve Ebersole
3030
*/

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ private ClassLoaderService classLoaderService(){
120120
return classLoaderService;
121121
}
122122

123-
124123
@Override
125124
public void bindIndependentMetadata(MetadataSources sources) {
126125
TypeDefBinder.bind( metadata, index );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.metamodel.source.annotations.entity;
25+
26+
27+
import org.jboss.jandex.Index;
28+
29+
import org.hibernate.service.ServiceRegistry;
30+
import org.hibernate.service.classloading.spi.ClassLoaderService;
31+
32+
/**
33+
* Helper class for keeping some context information needed during the processing of mapped classes.
34+
*
35+
* @author Hardy Ferentschik
36+
*/
37+
public class AnnotationBindingContext {
38+
private final ServiceRegistry serviceRegistry;
39+
private final Index index;
40+
41+
private ClassLoaderService classLoaderService;
42+
43+
public AnnotationBindingContext(Index index, ServiceRegistry serviceRegistry) {
44+
this.index = index;
45+
this.serviceRegistry = serviceRegistry;
46+
}
47+
48+
public ClassLoaderService classLoaderService() {
49+
if ( classLoaderService == null ) {
50+
classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
51+
}
52+
return classLoaderService;
53+
}
54+
}
55+
56+

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/AssociationAttribute.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@
4141
* @author Hardy Ferentschik
4242
*/
4343
public class AssociationAttribute extends SimpleAttribute {
44-
private final AssociationType associationType;
44+
private final AttributeType associationType;
4545
private final boolean ignoreNotFound;
4646
private final String referencedEntityType;
4747
private final Set<CascadeType> cascadeTypes;
4848

49-
public static AssociationAttribute createAssociationAttribute(String name, String type, AssociationType associationType, Map<DotName, List<AnnotationInstance>> annotations) {
49+
public static AssociationAttribute createAssociationAttribute(String name, String type, AttributeType associationType, Map<DotName, List<AnnotationInstance>> annotations) {
5050
return new AssociationAttribute( name, type, associationType, annotations );
5151
}
5252

53-
private AssociationAttribute(String name, String type, AssociationType associationType, Map<DotName, List<AnnotationInstance>> annotations) {
53+
private AssociationAttribute(String name, String type, AttributeType associationType, Map<DotName, List<AnnotationInstance>> annotations) {
5454
super( name, type, annotations, false );
5555
this.associationType = associationType;
5656
this.ignoreNotFound = ignoreNotFound();
@@ -72,7 +72,7 @@ public String getReferencedEntityType() {
7272
return referencedEntityType;
7373
}
7474

75-
public AssociationType getAssociationType() {
75+
public AttributeType getAssociationType() {
7676
return associationType;
7777
}
7878

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@
2828
import org.hibernate.metamodel.source.annotations.JPADotNames;
2929

3030
/**
31+
* An enum defining the type of mapped attribute.
32+
*
3133
* @author Hardy Ferentschik
3234
*/
33-
public enum AssociationType {
34-
NO_ASSOCIATION( null ),
35+
public enum AttributeType {
36+
BASIC( null ),
3537
ONE_TO_ONE( JPADotNames.ONE_TO_ONE ),
3638
ONE_TO_MANY( JPADotNames.ONE_TO_MANY ),
3739
MANY_TO_ONE( JPADotNames.MANY_TO_ONE ),
38-
MANY_TO_MANY( JPADotNames.MANY_TO_MANY );
40+
MANY_TO_MANY( JPADotNames.MANY_TO_MANY ),
41+
EMBEDDED( JPADotNames.EMBEDDED );
3942

4043
private final DotName annotationDotName;
4144

42-
AssociationType(DotName annotationDotName) {
45+
AttributeType(DotName annotationDotName) {
4346
this.annotationDotName = annotationDotName;
4447
}
4548

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.lang.reflect.Type;
3030
import java.util.ArrayList;
3131
import java.util.Collections;
32+
import java.util.EnumMap;
3233
import java.util.HashSet;
3334
import java.util.LinkedHashMap;
3435
import java.util.List;
@@ -55,8 +56,6 @@
5556
import org.hibernate.metamodel.source.annotations.JPADotNames;
5657
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
5758
import org.hibernate.metamodel.source.annotations.util.ReflectionHelper;
58-
import org.hibernate.service.ServiceRegistry;
59-
import org.hibernate.service.classloading.spi.ClassLoaderService;
6059

6160
/**
6261
* Represents an entity, mapped superclass or component configured via annotations/xml.
@@ -68,7 +67,15 @@ public class ConfiguredClass {
6867
* The parent of this configured class or {@code null} in case this configured class is the root of a hierarchy.
6968
*/
7069
private final ConfiguredClass parent;
70+
71+
/**
72+
* The Jandex class info for this configured class. Provides access to the annotation defined on this configured class.
73+
*/
7174
private final ClassInfo classInfo;
75+
76+
/**
77+
* The actual java type.
78+
*/
7279
private final Class<?> clazz;
7380

7481
private final boolean isRoot;
@@ -83,19 +90,24 @@ public class ConfiguredClass {
8390
private final IdType idType;
8491

8592
private final Map<String, MappedAttribute> mappedAttributes;
93+
private final Set<String> transientFieldNames = new HashSet<String>();
94+
private final Set<String> transientMethodNames = new HashSet<String>();
95+
96+
private final AnnotationBindingContext context;
8697

8798
public ConfiguredClass(ClassInfo info,
8899
ConfiguredClass parent,
89100
AccessType hierarchyAccessType,
90101
InheritanceType inheritanceType,
91-
ServiceRegistry serviceRegistry,
92-
ResolvedTypeWithMembers resolvedType) {
102+
ResolvedTypeWithMembers resolvedType,
103+
AnnotationBindingContext context) {
104+
this.context = context;
93105
this.classInfo = info;
94106
this.parent = parent;
95107
this.isRoot = parent == null;
96108
this.hierarchyAccessType = hierarchyAccessType;
97109
this.inheritanceType = inheritanceType;
98-
this.clazz = serviceRegistry.getService( ClassLoaderService.class ).classForName( info.toString() );
110+
this.clazz = context.classLoaderService().classForName( info.toString() );
99111

100112
this.configuredClassType = determineType();
101113
this.classAccessType = determineClassAccessType();
@@ -104,6 +116,9 @@ public ConfiguredClass(ClassInfo info,
104116
this.hasOwnTable = definesItsOwnTable();
105117
this.primaryTableName = determinePrimaryTableName();
106118

119+
// find transient field and method names
120+
findTransientFieldAndMethodNames();
121+
107122
List<MappedAttribute> simpleProps = collectAttributes( resolvedType );
108123
// make sure the properties are ordered by property name
109124
Collections.sort( simpleProps );
@@ -213,11 +228,6 @@ private AccessType determineClassAccessType() {
213228
* @return A list of the persistent properties of this configured class
214229
*/
215230
private List<MappedAttribute> collectAttributes(ResolvedTypeWithMembers resolvedTypes) {
216-
// create sets of transient field and method names
217-
Set<String> transientFieldNames = new HashSet<String>();
218-
Set<String> transientMethodNames = new HashSet<String>();
219-
populateTransientFieldAndMethodLists( transientFieldNames, transientMethodNames );
220-
221231
// use the class mate library to generic types
222232
ResolvedTypeWithMembers resolvedType = null;
223233
for ( HierarchicType hierarchicType : resolvedTypes.allTypesAndOverrides() ) {
@@ -381,42 +391,67 @@ private MappedAttribute createMappedProperty(Member member, ResolvedTypeWithMemb
381391
);
382392

383393
MappedAttribute attribute;
384-
AssociationType associationType = determineAssociationType( annotations );
385-
switch ( associationType ) {
386-
case NO_ASSOCIATION: {
394+
AttributeType attributeType = determineAttributeType( annotations );
395+
switch ( attributeType ) {
396+
case BASIC: {
387397
attribute = SimpleAttribute.createSimpleAttribute( name, ( (Class) type ).getName(), annotations );
388398
break;
389399
}
400+
case EMBEDDED: {
401+
throw new HibernateException( "foo" );
402+
}
403+
// TODO handle the different association types
390404
default: {
391405
attribute = AssociationAttribute.createAssociationAttribute(
392-
name, ( (Class) type ).getName(), associationType, annotations
406+
name, ( (Class) type ).getName(), attributeType, annotations
393407
);
394408
}
395409
}
396410

397411
return attribute;
398412
}
399413

400-
private AssociationType determineAssociationType(Map<DotName, List<AnnotationInstance>> annotations) {
414+
/**
415+
* Given the annotations defined on a persistent attribute this methods determines the attribute type.
416+
*
417+
* @param annotations the annotations defined on the persistent attribute
418+
*
419+
* @return an instance of the {@code AttributeType} enum
420+
*/
421+
private AttributeType determineAttributeType(Map<DotName, List<AnnotationInstance>> annotations) {
422+
EnumMap<AttributeType, AnnotationInstance> discoveredAttributeTypes =
423+
new EnumMap<AttributeType, AnnotationInstance>( AttributeType.class );
424+
401425
AnnotationInstance oneToOne = JandexHelper.getSingleAnnotation( annotations, JPADotNames.ONE_TO_ONE );
426+
if ( oneToOne != null ) {
427+
discoveredAttributeTypes.put( AttributeType.ONE_TO_ONE, oneToOne );
428+
}
429+
402430
AnnotationInstance oneToMany = JandexHelper.getSingleAnnotation( annotations, JPADotNames.ONE_TO_MANY );
403-
AnnotationInstance manyToOne = JandexHelper.getSingleAnnotation( annotations, JPADotNames.MANY_TO_ONE );
404-
AnnotationInstance manyToMany = JandexHelper.getSingleAnnotation( annotations, JPADotNames.MANY_TO_MANY );
431+
if ( oneToMany != null ) {
432+
discoveredAttributeTypes.put( AttributeType.ONE_TO_MANY, oneToMany );
433+
}
405434

406-
if ( oneToOne == null && oneToMany == null && manyToOne == null && manyToMany == null ) {
407-
return AssociationType.NO_ASSOCIATION;
435+
AnnotationInstance manyToOne = JandexHelper.getSingleAnnotation( annotations, JPADotNames.MANY_TO_ONE );
436+
if ( manyToOne != null ) {
437+
discoveredAttributeTypes.put( AttributeType.MANY_TO_ONE, manyToOne );
408438
}
409-
else if ( oneToOne != null && oneToMany == null && manyToOne == null && manyToMany == null ) {
410-
return AssociationType.ONE_TO_ONE;
439+
440+
AnnotationInstance manyToMany = JandexHelper.getSingleAnnotation( annotations, JPADotNames.MANY_TO_MANY );
441+
if ( manyToMany != null ) {
442+
discoveredAttributeTypes.put( AttributeType.MANY_TO_MANY, manyToMany );
411443
}
412-
else if ( oneToOne == null && oneToMany != null && manyToOne == null && manyToMany == null ) {
413-
return AssociationType.ONE_TO_MANY;
444+
445+
AnnotationInstance embedded = JandexHelper.getSingleAnnotation( annotations, JPADotNames.EMBEDDED );
446+
if ( embedded != null ) {
447+
discoveredAttributeTypes.put( AttributeType.EMBEDDED, embedded );
414448
}
415-
else if ( oneToOne == null && oneToMany == null && manyToOne != null && manyToMany == null ) {
416-
return AssociationType.MANY_TO_ONE;
449+
450+
if ( discoveredAttributeTypes.size() == 0 ) {
451+
return AttributeType.BASIC;
417452
}
418-
else if ( oneToOne == null && oneToMany == null && manyToOne == null && manyToMany != null ) {
419-
return AssociationType.MANY_TO_MANY;
453+
else if ( discoveredAttributeTypes.size() == 1 ) {
454+
return discoveredAttributeTypes.keySet().iterator().next();
420455
}
421456
else {
422457
throw new AnnotationException( "More than one association type configured for property " + getName() + " of class " + getName() );
@@ -435,11 +470,8 @@ private Type findResolvedType(String name, ResolvedMember[] resolvedMembers) {
435470

436471
/**
437472
* Populates the sets of transient field and method names.
438-
*
439-
* @param transientFieldNames Set to populate with the field names explicitly marked as @Transient
440-
* @param transientMethodNames set to populate with the method names explicitly marked as @Transient
441473
*/
442-
private void populateTransientFieldAndMethodLists(Set<String> transientFieldNames, Set<String> transientMethodNames) {
474+
private void findTransientFieldAndMethodNames() {
443475
List<AnnotationInstance> transientMembers = classInfo.annotations().get( JPADotNames.TRANSIENT );
444476
if ( transientMembers == null ) {
445477
return;
@@ -524,5 +556,3 @@ private IdType determineIdType() {
524556
return IdType.NONE;
525557
}
526558
}
527-
528-

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassHierarchy.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@
3939
import org.hibernate.metamodel.source.annotations.JPADotNames;
4040
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
4141
import org.hibernate.metamodel.source.annotations.util.ReflectionHelper;
42-
import org.hibernate.service.ServiceRegistry;
4342
import org.hibernate.service.classloading.spi.ClassLoaderService;
4443

4544
/**
46-
* Represents the inheritance structure of the configured classes within a class hierarchy.
45+
* Contains information about the access and inheritance type for all classes within a class hierarchy.
4746
*
4847
* @author Hardy Ferentschik
4948
*/
@@ -52,23 +51,23 @@ public class ConfiguredClassHierarchy implements Iterable<ConfiguredClass> {
5251
private final InheritanceType inheritanceType;
5352
private final List<ConfiguredClass> configuredClasses;
5453

55-
public static ConfiguredClassHierarchy create(List<ClassInfo> classes, ServiceRegistry serviceRegistry) {
56-
return new ConfiguredClassHierarchy( classes, serviceRegistry );
54+
public static ConfiguredClassHierarchy create(List<ClassInfo> classes, AnnotationBindingContext context) {
55+
return new ConfiguredClassHierarchy( classes, context );
5756
}
5857

59-
private ConfiguredClassHierarchy(List<ClassInfo> classes, ServiceRegistry serviceRegistry) {
58+
private ConfiguredClassHierarchy(List<ClassInfo> classes, AnnotationBindingContext context) {
6059
defaultAccessType = determineDefaultAccessType( classes );
6160
inheritanceType = determineInheritanceType( classes );
6261

63-
ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
64-
Class<?> clazz = classLoaderService.classForName( classes.get( classes.size() - 1 ).name().toString() );
65-
ResolvedTypeWithMembers resolvedMembers = ReflectionHelper.resolveMemberTypes( clazz );
62+
// the resolved type for the top level class in the hierarchy
63+
Class<?> clazz = context.classLoaderService().classForName( classes.get( classes.size() - 1 ).name().toString() );
64+
ResolvedTypeWithMembers resolvedType = ReflectionHelper.resolveMemberTypes( clazz );
6665

6766
configuredClasses = new ArrayList<ConfiguredClass>();
6867
ConfiguredClass parent = null;
6968
for ( ClassInfo info : classes ) {
7069
ConfiguredClass configuredClass = new ConfiguredClass(
71-
info, parent, defaultAccessType, inheritanceType, serviceRegistry, resolvedMembers
70+
info, parent, defaultAccessType, inheritanceType, resolvedType, context
7271
);
7372
configuredClasses.add( configuredClass );
7473
parent = configuredClass;
@@ -114,7 +113,7 @@ private AccessType determineDefaultAccessType(List<ClassInfo> classes) {
114113
if ( idAnnotations == null || idAnnotations.size() == 0 ) {
115114
continue;
116115
}
117-
accessType = processIdAnnotations( idAnnotations );
116+
accessType = determineAccessTypeByIdPlacement( idAnnotations );
118117
}
119118

120119
if ( accessType == null ) {
@@ -124,7 +123,7 @@ private AccessType determineDefaultAccessType(List<ClassInfo> classes) {
124123
return accessType;
125124
}
126125

127-
private AccessType processIdAnnotations(List<AnnotationInstance> idAnnotations) {
126+
private AccessType determineAccessTypeByIdPlacement(List<AnnotationInstance> idAnnotations) {
128127
AccessType accessType = null;
129128
for ( AnnotationInstance annotation : idAnnotations ) {
130129
AccessType tmpAccessType;
@@ -217,5 +216,3 @@ private String hierarchyListString(List<ClassInfo> classes) {
217216
return builder.toString();
218217
}
219218
}
220-
221-

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/MappedAttribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
*
4040
* @author Hardy Ferentschik
4141
*/
42-
public class MappedAttribute implements Comparable<MappedAttribute> {
42+
public abstract class MappedAttribute implements Comparable<MappedAttribute> {
4343
/**
4444
* Annotations defined on the attribute, keyed against the annotation dot name.
4545
*/

0 commit comments

Comments
 (0)