-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
AttributeFactory.java
executable file
·997 lines (911 loc) · 36 KB
/
AttributeFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
* third-party contributors as indicated by either @author tags or express
* copyright attribution statements applied by the authors. All
* third-party contributions are distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.jpa.internal.metamodel;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;
import java.util.Iterator;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.IdentifiableType;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.Type;
import org.jboss.logging.Logger;
import org.hibernate.annotations.common.AssertionFailure;
import org.hibernate.jpa.internal.EntityManagerMessageLogger;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Map;
import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Value;
import org.hibernate.tuple.entity.EntityMetamodel;
import org.hibernate.type.ComponentType;
import org.hibernate.type.EmbeddedComponentType;
import org.hibernate.type.EntityType;
/**
* A factory for building {@link Attribute} instances. Exposes 3 main services for building<ol>
* <li>{@link #buildAttribute normal attributes}</li>
* <li>{@link #buildIdAttribute id attributes}</li>
* <li>{@link #buildVersionAttribute version attributes}</li>
* <ol>
*
* @author Steve Ebersole
* @author Emmanuel Bernard
*/
public class AttributeFactory {
private static final EntityManagerMessageLogger LOG = Logger.getMessageLogger(EntityManagerMessageLogger.class,
AttributeFactory.class.getName());
private final MetadataContext context;
public AttributeFactory(MetadataContext context) {
this.context = context;
}
/**
* Build a normal attribute.
*
* @param ownerType The descriptor of the attribute owner (aka declarer).
* @param property The Hibernate property descriptor for the attribute
* @param <X> The type of the owner
* @param <Y> The attribute type
* @return The built attribute descriptor or null if the attribute is not part of the JPA 2 model (eg backrefs)
*/
@SuppressWarnings({ "unchecked" })
public <X, Y> AttributeImplementor<X, Y> buildAttribute(AbstractManagedType<X> ownerType, Property property) {
if ( property.isSynthetic() ) {
// hide synthetic/virtual properties (fabricated by Hibernate) from the JPA metamodel.
LOG.tracef(
"Skipping synthetic property %s(%s)",
ownerType.getTypeName(),
property.getName()
);
return null;
}
LOG.trace("Building attribute [" + ownerType.getTypeName() + "." + property.getName() + "]");
final AttributeContext<X> attributeContext = wrap( ownerType, property );
final AttributeMetadata<X,Y> attributeMetadata =
determineAttributeMetadata( attributeContext, NORMAL_MEMBER_RESOLVER );
if (attributeMetadata == null) {
return null;
}
if (attributeMetadata.isPlural()) {
return buildPluralAttribute((PluralAttributeMetadata)attributeMetadata);
}
final SingularAttributeMetadata<X, Y> singularAttributeMetadata = (SingularAttributeMetadata<X, Y>)attributeMetadata;
final Type<Y> metaModelType = getMetaModelType(singularAttributeMetadata.getValueContext());
return new SingularAttributeImpl<X, Y>(
attributeMetadata.getName(),
attributeMetadata.getJavaType(),
ownerType,
attributeMetadata.getMember(),
false,
false,
property.isOptional(),
metaModelType,
attributeMetadata.getPersistentAttributeType()
);
}
private <X> AttributeContext<X> wrap(final AbstractManagedType<X> ownerType, final Property property) {
return new AttributeContext<X>() {
public AbstractManagedType<X> getOwnerType() {
return ownerType;
}
public Property getPropertyMapping() {
return property;
}
};
}
/**
* Build the identifier attribute descriptor
*
* @param ownerType The descriptor of the attribute owner (aka declarer).
* @param property The Hibernate property descriptor for the identifier attribute
* @param <X> The type of the owner
* @param <Y> The attribute type
* @return The built attribute descriptor
*/
@SuppressWarnings({ "unchecked" })
public <X, Y> SingularAttributeImpl<X, Y> buildIdAttribute(AbstractIdentifiableType<X> ownerType, Property property) {
LOG.trace("Building identifier attribute [" + ownerType.getTypeName() + "." + property.getName() + "]");
final AttributeContext<X> attributeContext = wrap( ownerType, property );
final SingularAttributeMetadata<X,Y> attributeMetadata =
(SingularAttributeMetadata<X, Y>) determineAttributeMetadata( attributeContext, IDENTIFIER_MEMBER_RESOLVER );
final Type<Y> metaModelType = getMetaModelType( attributeMetadata.getValueContext() );
return new SingularAttributeImpl.Identifier(
property.getName(),
attributeMetadata.getJavaType(),
ownerType,
attributeMetadata.getMember(),
metaModelType,
attributeMetadata.getPersistentAttributeType()
);
}
/**
* Build the version attribute descriptor
*
* @param ownerType The descriptor of the attribute owner (aka declarer).
* @param property The Hibernate property descriptor for the version attribute
* @param <X> The type of the owner
* @param <Y> The attribute type
* @return The built attribute descriptor
*/
@SuppressWarnings({ "unchecked" })
public <X, Y> SingularAttributeImpl<X, Y> buildVersionAttribute(AbstractIdentifiableType<X> ownerType, Property property) {
LOG.trace("Building version attribute [ownerType.getTypeName()" + "." + "property.getName()]");
final AttributeContext<X> attributeContext = wrap( ownerType, property );
final SingularAttributeMetadata<X,Y> attributeMetadata =
(SingularAttributeMetadata<X, Y>) determineAttributeMetadata( attributeContext, VERSION_MEMBER_RESOLVER );
final Type<Y> metaModelType = getMetaModelType( attributeMetadata.getValueContext() );
return new SingularAttributeImpl.Version(
property.getName(),
attributeMetadata.getJavaType(),
ownerType,
attributeMetadata.getMember(),
metaModelType,
attributeMetadata.getPersistentAttributeType()
);
}
@SuppressWarnings( "unchecked" )
private <X, Y, E, K> AttributeImplementor<X, Y> buildPluralAttribute(PluralAttributeMetadata<X,Y,E> attributeMetadata) {
final Type<E> elementType = getMetaModelType( attributeMetadata.getElementValueContext() );
if ( java.util.Map.class.isAssignableFrom( attributeMetadata.getJavaType() ) ) {
final Type<K> keyType = getMetaModelType( attributeMetadata.getMapKeyValueContext() );
return PluralAttributeImpl.create( attributeMetadata.getOwnerType(), elementType, attributeMetadata.getJavaType(), keyType )
.member( attributeMetadata.getMember() )
.property( attributeMetadata.getPropertyMapping() )
.persistentAttributeType( attributeMetadata.getPersistentAttributeType() )
.build();
}
return PluralAttributeImpl.create(attributeMetadata.getOwnerType(), elementType, attributeMetadata.getJavaType(), null).member(attributeMetadata.getMember()).property(attributeMetadata.getPropertyMapping()).persistentAttributeType(attributeMetadata.getPersistentAttributeType()).build();
}
@SuppressWarnings( "unchecked" )
private <Y> Type<Y> getMetaModelType(ValueContext typeContext) {
switch ( typeContext.getValueClassification() ) {
case BASIC: {
return new BasicTypeImpl<Y>(
typeContext.getBindableType(),
Type.PersistenceType.BASIC
);
}
case ENTITY: {
final org.hibernate.type.EntityType type = (EntityType) typeContext.getValue().getType();
return (Type<Y>) context.locateEntityType( type.getAssociatedEntityName() );
}
case EMBEDDABLE: {
final Component component = (Component) typeContext.getValue();
final EmbeddableTypeImpl<Y> embeddableType = new EmbeddableTypeImpl<Y>(
typeContext.getBindableType(),
typeContext.getAttributeMetadata().getOwnerType(),
(ComponentType) typeContext.getValue().getType()
);
context.registerEmbeddedableType( embeddableType );
final Iterator<Property> subProperties = component.getPropertyIterator();
while ( subProperties.hasNext() ) {
final Property property = subProperties.next();
final AttributeImplementor<Y, Object> attribute = buildAttribute( embeddableType, property );
if ( attribute != null ) {
embeddableType.getBuilder().addAttribute( attribute );
}
}
embeddableType.lock();
return embeddableType;
}
default: {
throw new AssertionFailure( "Unknown type : " + typeContext.getValueClassification() );
}
}
}
private EntityMetamodel getDeclarerEntityMetamodel(AbstractIdentifiableType<?> ownerType) {
final Type.PersistenceType persistenceType = ownerType.getPersistenceType();
if ( persistenceType == Type.PersistenceType.ENTITY) {
return context.getSessionFactory()
.getEntityPersister( ownerType.getTypeName() )
.getEntityMetamodel();
}
else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS) {
PersistentClass persistentClass =
context.getPersistentClassHostingProperties( (MappedSuperclassTypeImpl<?>) ownerType );
return context.getSessionFactory()
.getEntityPersister( persistentClass.getClassName() )
.getEntityMetamodel();
}
else {
throw new AssertionFailure( "Cannot get the metamodel for PersistenceType: " + persistenceType );
}
}
/**
* A contract for defining the meta information about a {@link Value}
*/
private interface ValueContext {
/**
* Enum of the simplified types a value might be. These relate more to the Hibernate classification
* then the JPA classification
*/
enum ValueClassification {
EMBEDDABLE,
ENTITY,
BASIC
}
/**
* Retrieve the value itself
*
* @return The value
*/
public Value getValue();
public Class getBindableType();
/**
* Retrieve the simplified value classification
*
* @return The value type
*/
public ValueClassification getValueClassification();
/**
* Retrieve the metadata about the attribute from which this value comes
*
* @return The "containing" attribute metadata.
*/
public AttributeMetadata getAttributeMetadata();
}
/**
* Basic contract for describing an attribute. The "description" is partially in terms
* of JPA ({@link #getPersistentAttributeType} and {@link #getOwnerType}), partially in
* terms of Hibernate ({@link #getPropertyMapping}) and partially just in terms of the java
* model itself ({@link #getName}, {@link #getMember} and {@link #getJavaType}).
*
* @param <X> The attribute owner type
* @param <Y> The attribute type.
*/
private interface AttributeMetadata<X,Y> {
/**
* Retrieve the name of the attribute
*
* @return The attribute name
*/
public String getName();
/**
* Retrieve the member defining the attribute
*
* @return The attribute member
*/
public Member getMember();
/**
* Retrieve the attribute java type.
*
* @return The java type of the attribute.
*/
public Class<Y> getJavaType();
/**
* Get the JPA attribute type classification for this attribute.
*
* @return The JPA attribute type classification
*/
public Attribute.PersistentAttributeType getPersistentAttributeType();
/**
* Retrieve the attribute owner's metamodel information
*
* @return The metamodel information for the attribute owner
*/
public AbstractManagedType<X> getOwnerType();
/**
* Retrieve the Hibernate property mapping related to this attribute.
*
* @return The Hibernate property mapping
*/
public Property getPropertyMapping();
/**
* Is the attribute plural (a collection)?
*
* @return True if it is plural, false otherwise.
*/
public boolean isPlural();
}
/**
* Attribute metadata contract for a non-plural attribute.
* @param <X> The owner type
* @param <Y> The attribute type
*/
private interface SingularAttributeMetadata<X,Y> extends AttributeMetadata<X,Y> {
/**
* Retrieve the value context for this attribute
*
* @return The attributes value context
*/
public ValueContext getValueContext();
}
/**
* Attribute metadata contract for a plural attribute.
* @param <X> The owner type
* @param <Y> The attribute type (the collection type)
* @param <E> The collection element type
*/
private interface PluralAttributeMetadata<X,Y,E> extends AttributeMetadata<X,Y> {
/**
* Retrieve the JPA collection type classification for this attribute
*
* @return The JPA collection type classification
*/
public PluralAttribute.CollectionType getAttributeCollectionType();
/**
* Retrieve the value context for the collection's elements.
*
* @return The value context for the collection's elements.
*/
public ValueContext getElementValueContext();
/**
* Retrieve the value context for the collection's keys (if a map, null otherwise).
*
* @return The value context for the collection's keys (if a map, null otherwise).
*/
public ValueContext getMapKeyValueContext();
}
/**
* Bundle's a Hibernate property mapping together with the JPA metamodel information
* of the attribute owner.
*
* @param <X> The owner type.
*/
private interface AttributeContext<X> {
/**
* Retrieve the attribute owner.
*
* @return The owner.
*/
public AbstractManagedType<X> getOwnerType();
/**
* Retrieve the Hibernate property mapping.
*
* @return The Hibvernate property mapping.
*/
public Property getPropertyMapping();
}
/**
* Contract for how we resolve the {@link Member} for a give attribute context.
*/
private interface MemberResolver {
public Member resolveMember(AttributeContext attributeContext);
}
/**
* Here is most of the nuts and bolts of this factory, where we interpret the known JPA metadata
* against the known Hibernate metadata and build a descriptor for the attribute.
*
* @param attributeContext The attribute to be described
* @param memberResolver Strategy for how to resolve the member defining the attribute.
* @param <X> The owner type
* @param <Y> The attribute type
*
* @return The attribute description
*/
@SuppressWarnings({ "unchecked" })
private <X,Y> AttributeMetadata<X,Y> determineAttributeMetadata(
AttributeContext<X> attributeContext,
MemberResolver memberResolver) {
LOG.trace("Starting attribute metadata determination [" + attributeContext.getPropertyMapping().getName() + "]");
final Member member = memberResolver.resolveMember( attributeContext );
LOG.trace(" Determined member [" + member + "]");
final Value value = attributeContext.getPropertyMapping().getValue();
final org.hibernate.type.Type type = value.getType();
LOG.trace(" Determined type [name=" + type.getName() + ", class=" + type.getClass().getName() + "]");
if ( type.isAnyType() ) {
// ANY mappings are currently not supported in the JPA metamodel; see HHH-6589
if ( context.isIgnoreUnsupported() ) {
return null;
}
else {
throw new UnsupportedOperationException( "ANY not supported" );
}
}
else if ( type.isAssociationType() ) {
// collection or entity
if ( type.isEntityType() ) {
// entity
return new SingularAttributeMetadataImpl<X,Y>(
attributeContext.getPropertyMapping(),
attributeContext.getOwnerType(),
member,
determineSingularAssociationAttributeType( member )
);
}
// collection
if (value instanceof Collection) {
final Collection collValue = (Collection)value;
final Value elementValue = collValue.getElement();
final org.hibernate.type.Type elementType = elementValue.getType();
// First, determine the type of the elements and use that to help determine the
// collection type)
final Attribute.PersistentAttributeType elementPersistentAttributeType;
final Attribute.PersistentAttributeType persistentAttributeType;
if (elementType.isAnyType()) {
throw new UnsupportedOperationException("collection of any not supported yet");
}
final boolean isManyToMany = isManyToMany(member);
if (elementValue instanceof Component) {
elementPersistentAttributeType = Attribute.PersistentAttributeType.EMBEDDED;
persistentAttributeType = Attribute.PersistentAttributeType.ELEMENT_COLLECTION;
} else if (elementType.isAssociationType()) {
elementPersistentAttributeType = isManyToMany ? Attribute.PersistentAttributeType.MANY_TO_MANY : Attribute.PersistentAttributeType.ONE_TO_MANY;
persistentAttributeType = elementPersistentAttributeType;
} else {
elementPersistentAttributeType = Attribute.PersistentAttributeType.BASIC;
persistentAttributeType = Attribute.PersistentAttributeType.ELEMENT_COLLECTION;
}
final Attribute.PersistentAttributeType keyPersistentAttributeType;
// Finally, we determine the type of the map key (if needed)
if (value instanceof Map) {
final Value keyValue = ((Map)value).getIndex();
final org.hibernate.type.Type keyType = keyValue.getType();
if (keyType.isAnyType()) throw new UnsupportedOperationException("collection of any not supported yet");
if (keyValue instanceof Component) keyPersistentAttributeType = Attribute.PersistentAttributeType.EMBEDDED;
else if (keyType.isAssociationType()) keyPersistentAttributeType = Attribute.PersistentAttributeType.MANY_TO_ONE;
else keyPersistentAttributeType = Attribute.PersistentAttributeType.BASIC;
} else keyPersistentAttributeType = null;
return new PluralAttributeMetadataImpl(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(),
member, persistentAttributeType, elementPersistentAttributeType,
keyPersistentAttributeType);
} else if (value instanceof OneToMany) {
// TODO : is this even possible??? Really OneToMany should be describing the
// element value within a o.h.mapping.Collection (see logic branch above)
throw new IllegalArgumentException("HUH???");
// final boolean isManyToMany = isManyToMany( member );
// //one to many with FK => entity
// return new PluralAttributeMetadataImpl(
// attributeContext.getPropertyMapping(),
// attributeContext.getOwnerType(),
// member,
// isManyToMany
// ? Attribute.PersistentAttributeType.MANY_TO_MANY
// : Attribute.PersistentAttributeType.ONE_TO_MANY
// value,
// AttributeContext.TypeStatus.ENTITY,
// Attribute.PersistentAttributeType.ONE_TO_MANY,
// null, null, null
// );
}
}
else if ( attributeContext.getPropertyMapping().isComposite() ) {
// component
return new SingularAttributeMetadataImpl<X,Y>(
attributeContext.getPropertyMapping(),
attributeContext.getOwnerType(),
member,
Attribute.PersistentAttributeType.EMBEDDED
);
}
else {
// basic type
return new SingularAttributeMetadataImpl<X,Y>(
attributeContext.getPropertyMapping(),
attributeContext.getOwnerType(),
member,
Attribute.PersistentAttributeType.BASIC
);
}
throw new UnsupportedOperationException( "oops, we are missing something: " + attributeContext.getPropertyMapping() );
}
public static Attribute.PersistentAttributeType determineSingularAssociationAttributeType(Member member) {
if ( Field.class.isInstance( member ) ) {
return ( (Field) member ).getAnnotation( OneToOne.class ) != null
? Attribute.PersistentAttributeType.ONE_TO_ONE
: Attribute.PersistentAttributeType.MANY_TO_ONE;
}
else if (MapMember.class.isInstance( member )) {
return Attribute.PersistentAttributeType.MANY_TO_ONE; // curious to see how this works for non-annotated methods
}
else {
return ( (Method) member ).getAnnotation( OneToOne.class ) != null
? Attribute.PersistentAttributeType.ONE_TO_ONE
: Attribute.PersistentAttributeType.MANY_TO_ONE;
}
}
private abstract class BaseAttributeMetadata<X,Y> implements AttributeMetadata<X,Y> {
private final Property propertyMapping;
private final AbstractManagedType<X> ownerType;
private final Member member;
private final Class<Y> javaType;
private final Attribute.PersistentAttributeType persistentAttributeType;
@SuppressWarnings({ "unchecked" })
protected BaseAttributeMetadata(
Property propertyMapping,
AbstractManagedType<X> ownerType,
Member member,
Attribute.PersistentAttributeType persistentAttributeType) {
this.propertyMapping = propertyMapping;
this.ownerType = ownerType;
this.member = member;
this.persistentAttributeType = persistentAttributeType;
final Class declaredType;
// we can support method or field members here. Is there really any other valid type?
if ( Field.class.isInstance( member ) ) {
declaredType = ( (Field) member ).getType();
}
else if ( Method.class.isInstance( member ) ) {
declaredType = ( (Method) member ).getReturnType();
}
else if ( MapMember.class.isInstance( member ) ) {
declaredType = ((MapMember) member).getType();
}
else {
throw new IllegalArgumentException( "Cannot determine java-type from given member [" + member + "]" );
}
this.javaType = accountForPrimitiveTypes( declaredType );
}
public String getName() {
return propertyMapping.getName();
}
public Member getMember() {
return member;
}
public String getMemberDescription() {
return determineMemberDescription( getMember() );
}
public String determineMemberDescription(Member member) {
return member.getDeclaringClass().getName() + '#' + member.getName();
}
public Class<Y> getJavaType() {
return javaType;
}
public Attribute.PersistentAttributeType getPersistentAttributeType() {
return persistentAttributeType;
}
public AbstractManagedType<X> getOwnerType() {
return ownerType;
}
public boolean isPlural() {
return propertyMapping.getType().isCollectionType();
}
public Property getPropertyMapping() {
return propertyMapping;
}
}
@SuppressWarnings({ "unchecked" })
protected <Y> Class<Y> accountForPrimitiveTypes(Class<Y> declaredType) {
// if ( !declaredType.isPrimitive() ) {
// return declaredType;
// }
//
// if ( Boolean.TYPE.equals( declaredType ) ) {
// return (Class<Y>) Boolean.class;
// }
// if ( Character.TYPE.equals( declaredType ) ) {
// return (Class<Y>) Character.class;
// }
// if( Byte.TYPE.equals( declaredType ) ) {
// return (Class<Y>) Byte.class;
// }
// if ( Short.TYPE.equals( declaredType ) ) {
// return (Class<Y>) Short.class;
// }
// if ( Integer.TYPE.equals( declaredType ) ) {
// return (Class<Y>) Integer.class;
// }
// if ( Long.TYPE.equals( declaredType ) ) {
// return (Class<Y>) Long.class;
// }
// if ( Float.TYPE.equals( declaredType ) ) {
// return (Class<Y>) Float.class;
// }
// if ( Double.TYPE.equals( declaredType ) ) {
// return (Class<Y>) Double.class;
// }
//
// throw new IllegalArgumentException( "Unexpected type [" + declaredType + "]" );
// if the field is defined as int, return int not Integer...
return declaredType;
}
private class SingularAttributeMetadataImpl<X,Y>
extends BaseAttributeMetadata<X,Y>
implements SingularAttributeMetadata<X,Y> {
private final ValueContext valueContext;
private SingularAttributeMetadataImpl(
Property propertyMapping,
AbstractManagedType<X> ownerType,
Member member,
Attribute.PersistentAttributeType persistentAttributeType) {
super( propertyMapping, ownerType, member, persistentAttributeType );
valueContext = new ValueContext() {
public Value getValue() {
return getPropertyMapping().getValue();
}
public Class getBindableType() {
return getAttributeMetadata().getJavaType();
}
public ValueClassification getValueClassification() {
switch ( getPersistentAttributeType() ) {
case EMBEDDED: {
return ValueClassification.EMBEDDABLE;
}
case BASIC: {
return ValueClassification.BASIC;
}
default: {
return ValueClassification.ENTITY;
}
}
}
public AttributeMetadata getAttributeMetadata() {
return SingularAttributeMetadataImpl.this;
}
};
}
public ValueContext getValueContext() {
return valueContext;
}
}
private class PluralAttributeMetadataImpl<X,Y,E>
extends BaseAttributeMetadata<X,Y>
implements PluralAttributeMetadata<X,Y,E> {
private final PluralAttribute.CollectionType attributeCollectionType;
private final Attribute.PersistentAttributeType elementPersistentAttributeType;
private final Attribute.PersistentAttributeType keyPersistentAttributeType;
private final Class elementJavaType;
private final Class keyJavaType;
private final ValueContext elementValueContext;
private final ValueContext keyValueContext;
private PluralAttributeMetadataImpl(
Property propertyMapping,
AbstractManagedType<X> ownerType,
Member member,
Attribute.PersistentAttributeType persistentAttributeType,
Attribute.PersistentAttributeType elementPersistentAttributeType,
Attribute.PersistentAttributeType keyPersistentAttributeType) {
super( propertyMapping, ownerType, member, persistentAttributeType );
this.attributeCollectionType = determineCollectionType( getJavaType() );
this.elementPersistentAttributeType = elementPersistentAttributeType;
this.keyPersistentAttributeType = keyPersistentAttributeType;
ParameterizedType signatureType = getSignatureType( member );
if ( keyPersistentAttributeType == null ) {
elementJavaType = signatureType != null ?
getClassFromGenericArgument( signatureType.getActualTypeArguments()[0] ) :
Object.class; //FIXME and honor targetEntity?
keyJavaType = null;
}
else {
keyJavaType = signatureType != null ?
getClassFromGenericArgument( signatureType.getActualTypeArguments()[0] ) :
Object.class; //FIXME and honor targetEntity?
elementJavaType = signatureType != null ?
getClassFromGenericArgument( signatureType.getActualTypeArguments()[1] ) :
Object.class; //FIXME and honor targetEntity?
}
this.elementValueContext = new ValueContext() {
public Value getValue() {
return ( (Collection) getPropertyMapping().getValue() ).getElement();
}
public Class getBindableType() {
return elementJavaType;
}
public ValueClassification getValueClassification() {
switch ( PluralAttributeMetadataImpl.this.elementPersistentAttributeType ) {
case EMBEDDED: {
return ValueClassification.EMBEDDABLE;
}
case BASIC: {
return ValueClassification.BASIC;
}
default: {
return ValueClassification.ENTITY;
}
}
}
public AttributeMetadata getAttributeMetadata() {
return PluralAttributeMetadataImpl.this;
}
};
// interpret the key, if one
if ( keyPersistentAttributeType != null ) {
this.keyValueContext = new ValueContext() {
public Value getValue() {
return ( (Map) getPropertyMapping().getValue() ).getIndex();
}
public Class getBindableType() {
return keyJavaType;
}
public ValueClassification getValueClassification() {
switch ( PluralAttributeMetadataImpl.this.keyPersistentAttributeType ) {
case EMBEDDED: {
return ValueClassification.EMBEDDABLE;
}
case BASIC: {
return ValueClassification.BASIC;
}
default: {
return ValueClassification.ENTITY;
}
}
}
public AttributeMetadata getAttributeMetadata() {
return PluralAttributeMetadataImpl.this;
}
};
}
else {
keyValueContext = null;
}
}
private Class<?> getClassFromGenericArgument(java.lang.reflect.Type type) {
if ( type instanceof Class ) {
return (Class) type;
}
else if ( type instanceof TypeVariable ) {
final java.lang.reflect.Type upperBound = ( ( TypeVariable ) type ).getBounds()[0];
return getClassFromGenericArgument( upperBound );
}
else if ( type instanceof ParameterizedType ) {
final java.lang.reflect.Type rawType = ( (ParameterizedType) type ).getRawType();
return getClassFromGenericArgument( rawType );
}
else {
throw new AssertionFailure(
"Fail to process type argument in a generic declaration. Member : " + getMemberDescription()
+ " Type: " + type.getClass()
);
}
}
public ValueContext getElementValueContext() {
return elementValueContext;
}
public PluralAttribute.CollectionType getAttributeCollectionType() {
return attributeCollectionType;
}
public ValueContext getMapKeyValueContext() {
return keyValueContext;
}
}
public static ParameterizedType getSignatureType(Member member) {
final java.lang.reflect.Type type;
if (Field.class.isInstance( member )) {
type = ( ( Field ) member ).getGenericType();
}
else if ( Method.class.isInstance( member ) ) {
type = ( ( Method ) member ).getGenericReturnType();
}
else {
type = ( (MapMember) member ).getType();
}
//this is a raw type
if ( type instanceof Class ) return null;
return (ParameterizedType) type;
}
public static PluralAttribute.CollectionType determineCollectionType(Class javaType) {
if ( java.util.List.class.isAssignableFrom( javaType ) ) {
return PluralAttribute.CollectionType.LIST;
}
else if ( java.util.Set.class.isAssignableFrom( javaType ) ) {
return PluralAttribute.CollectionType.SET;
}
else if ( java.util.Map.class.isAssignableFrom( javaType ) ) {
return PluralAttribute.CollectionType.MAP;
}
else if ( java.util.Collection.class.isAssignableFrom( javaType ) ) {
return PluralAttribute.CollectionType.COLLECTION;
}
else {
throw new IllegalArgumentException( "Expecting collection type [" + javaType.getName() + "]" );
}
}
public static boolean isManyToMany(Member member) {
if ( Field.class.isInstance( member ) ) {
return ( (Field) member ).getAnnotation( ManyToMany.class ) != null;
}
else if ( Method.class.isInstance( member ) ) {
return ( (Method) member ).getAnnotation( ManyToMany.class ) != null;
}
return false;
}
private final MemberResolver EMBEDDED_MEMBER_RESOLVER = new MemberResolver() {
/**
* {@inheritDoc}
*/
public Member resolveMember(AttributeContext attributeContext) {
final EmbeddableTypeImpl embeddableType = ( EmbeddableTypeImpl<?> ) attributeContext.getOwnerType();
final String attributeName = attributeContext.getPropertyMapping().getName();
return embeddableType.getHibernateType()
.getComponentTuplizer()
.getGetter( embeddableType.getHibernateType().getPropertyIndex( attributeName ) )
.getMember();
}
};
private final MemberResolver VIRTUAL_IDENTIFIER_MEMBER_RESOLVER = new MemberResolver() {
/**
* {@inheritDoc}
*/
public Member resolveMember(AttributeContext attributeContext) {
final AbstractIdentifiableType identifiableType = (AbstractIdentifiableType) attributeContext.getOwnerType();
final EntityMetamodel entityMetamodel = getDeclarerEntityMetamodel( identifiableType );
if ( ! entityMetamodel.getIdentifierProperty().isVirtual() ) {
throw new IllegalArgumentException( "expecting IdClass mapping" );
}
org.hibernate.type.Type type = entityMetamodel.getIdentifierProperty().getType();
if ( ! EmbeddedComponentType.class.isInstance( type ) ) {
throw new IllegalArgumentException( "expecting IdClass mapping" );
}
final EmbeddedComponentType componentType = (EmbeddedComponentType) type;
final String attributeName = attributeContext.getPropertyMapping().getName();
return componentType.getComponentTuplizer()
.getGetter( componentType.getPropertyIndex( attributeName ) )
.getMember();
}
};
/**
* A {@link Member} resolver for normal attributes.
*/
private final MemberResolver NORMAL_MEMBER_RESOLVER = new MemberResolver() {
/**
* {@inheritDoc}
*/
public Member resolveMember(AttributeContext attributeContext) {
final AbstractManagedType ownerType = attributeContext.getOwnerType();
final Property property = attributeContext.getPropertyMapping();
final Type.PersistenceType persistenceType = ownerType.getPersistenceType();
if ( Type.PersistenceType.EMBEDDABLE == persistenceType ) {
return EMBEDDED_MEMBER_RESOLVER.resolveMember( attributeContext );
}
else if ( Type.PersistenceType.ENTITY == persistenceType
|| Type.PersistenceType.MAPPED_SUPERCLASS == persistenceType ) {
final AbstractIdentifiableType identifiableType = (AbstractIdentifiableType) ownerType;
final EntityMetamodel entityMetamodel = getDeclarerEntityMetamodel( identifiableType );
final String propertyName = property.getName();
final Integer index = entityMetamodel.getPropertyIndexOrNull( propertyName );
if ( index == null ) {
// just like in #determineIdentifierJavaMember , this *should* indicate we have an IdClass mapping
return VIRTUAL_IDENTIFIER_MEMBER_RESOLVER.resolveMember( attributeContext );
}
else {
return entityMetamodel.getTuplizer()
.getGetter( index )
.getMember();
}
}
else {
throw new IllegalArgumentException( "Unexpected owner type : " + persistenceType );
}
}
};
private final MemberResolver IDENTIFIER_MEMBER_RESOLVER = new MemberResolver() {
public Member resolveMember(AttributeContext attributeContext) {
final AbstractIdentifiableType identifiableType = (AbstractIdentifiableType) attributeContext.getOwnerType();
final EntityMetamodel entityMetamodel = getDeclarerEntityMetamodel( identifiableType );
if ( ! attributeContext.getPropertyMapping().getName()
.equals( entityMetamodel.getIdentifierProperty().getName() ) ) {
// this *should* indicate processing part of an IdClass...
return VIRTUAL_IDENTIFIER_MEMBER_RESOLVER.resolveMember( attributeContext );
}
return entityMetamodel.getTuplizer().getIdentifierGetter().getMember();
}
};
private final MemberResolver VERSION_MEMBER_RESOLVER = new MemberResolver() {
public Member resolveMember(AttributeContext attributeContext) {
final AbstractIdentifiableType identifiableType = (AbstractIdentifiableType) attributeContext.getOwnerType();
final EntityMetamodel entityMetamodel = getDeclarerEntityMetamodel( identifiableType );
final String versionPropertyName = attributeContext.getPropertyMapping().getName();
if ( ! versionPropertyName.equals( entityMetamodel.getVersionProperty().getName() ) ) {
// this should never happen, but to be safe...
throw new IllegalArgumentException( "Given property did not match declared version property" );
}
return entityMetamodel.getTuplizer().getVersionGetter().getMember();
}
};
}