-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
JpaJandexScavenger.java
591 lines (524 loc) · 27.5 KB
/
JpaJandexScavenger.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
package io.quarkus.hibernate.orm.deployment;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmCompositeAttributeType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmDiscriminatorSubclassEntityType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmEntityBaseDefinition;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmJoinedSubclassEntityType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmRootEntityType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmUnionSubclassEntityType;
import org.hibernate.boot.jaxb.mapping.EntityOrMappedSuperclass;
import org.hibernate.boot.jaxb.mapping.JaxbConverter;
import org.hibernate.boot.jaxb.mapping.JaxbEmbeddable;
import org.hibernate.boot.jaxb.mapping.JaxbEntity;
import org.hibernate.boot.jaxb.mapping.JaxbEntityListener;
import org.hibernate.boot.jaxb.mapping.JaxbEntityListeners;
import org.hibernate.boot.jaxb.mapping.JaxbEntityMappings;
import org.hibernate.boot.jaxb.mapping.JaxbMappedSuperclass;
import org.hibernate.boot.jaxb.mapping.JaxbPersistenceUnitDefaults;
import org.hibernate.boot.jaxb.mapping.JaxbPersistenceUnitMetadata;
import org.hibernate.boot.jaxb.mapping.ManagedType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.Declaration;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Type;
import io.quarkus.builder.BuildException;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.hibernate.orm.deployment.xml.QuarkusMappingFileParser;
import io.quarkus.hibernate.orm.runtime.boot.xml.RecordableXmlMapping;
import io.quarkus.runtime.configuration.ConfigurationException;
/**
* Scan the Jandex index to find JPA entities (and embeddables supporting entity models).
* <p>
* The output is then both consumed as plain list to use as a filter for which classes
* need to be enhanced, collect them for storage in the JPADeploymentTemplate and registered
* for reflective access.
* TODO some of these are going to be redundant?
*
* @author Emmanuel Bernard emmanuel@hibernate.org
* @author Sanne Grinovero <sanne@hibernate.org>
*/
public final class JpaJandexScavenger {
public static final List<DotName> EMBEDDED_ANNOTATIONS = Arrays.asList(ClassNames.EMBEDDED_ID, ClassNames.EMBEDDED);
private static final String XML_MAPPING_DEFAULT_ORM_XML = "META-INF/orm.xml";
private static final String XML_MAPPING_NO_FILE = "no-file";
private final BuildProducer<ReflectiveClassBuildItem> reflectiveClass;
private final BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles;
private final List<JpaModelPersistenceUnitContributionBuildItem> persistenceUnitContributions;
private final IndexView index;
private final Set<String> ignorableNonIndexedClasses;
JpaJandexScavenger(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles,
List<JpaModelPersistenceUnitContributionBuildItem> persistenceUnitContributions,
IndexView index,
Set<String> ignorableNonIndexedClasses) {
this.reflectiveClass = reflectiveClass;
this.hotDeploymentWatchedFiles = hotDeploymentWatchedFiles;
this.persistenceUnitContributions = persistenceUnitContributions;
this.index = index;
this.ignorableNonIndexedClasses = ignorableNonIndexedClasses;
}
public JpaModelBuildItem discoverModelAndRegisterForReflection() throws BuildException {
Collector collector = new Collector();
for (DotName packageAnnotation : HibernateOrmTypes.PACKAGE_ANNOTATIONS) {
enlistJPAModelAnnotatedPackages(collector, packageAnnotation);
}
enlistJPAModelClasses(collector, ClassNames.JPA_ENTITY);
enlistJPAModelClasses(collector, ClassNames.EMBEDDABLE);
enlistJPAModelClasses(collector, ClassNames.MAPPED_SUPERCLASS);
enlistJPAModelIdClasses(collector, ClassNames.ID_CLASS);
enlistEmbeddedsAndElementCollections(collector);
enlistPotentialCdiBeanClasses(collector, ClassNames.CONVERTER);
for (DotName annotation : HibernateOrmTypes.JPA_LISTENER_ANNOTATIONS) {
enlistPotentialCdiBeanClasses(collector, annotation);
}
for (JpaModelPersistenceUnitContributionBuildItem persistenceUnitContribution : persistenceUnitContributions) {
enlistExplicitMappings(collector, persistenceUnitContribution);
}
Set<String> managedClassNames = new HashSet<>(collector.entityTypes);
managedClassNames.addAll(collector.modelTypes);
for (String className : managedClassNames) {
reflectiveClass.produce(ReflectiveClassBuildItem.builder(className).methods().fields().build());
}
if (!collector.enumTypes.isEmpty()) {
reflectiveClass.produce(ReflectiveClassBuildItem.builder("java.lang.Enum").methods().build());
for (String className : collector.enumTypes) {
reflectiveClass.produce(ReflectiveClassBuildItem.builder(className).methods().build());
}
}
// for the java types we collected (usually from java.time but it could be from other types),
// we just register them for reflection
for (String javaType : collector.javaTypes) {
reflectiveClass.produce(ReflectiveClassBuildItem.builder(javaType).methods().build());
}
if (!collector.unindexedClasses.isEmpty()) {
Set<String> unIgnorableIndexedClasses = collector.unindexedClasses.stream().map(DotName::toString)
.collect(Collectors.toSet());
unIgnorableIndexedClasses.removeAll(ignorableNonIndexedClasses);
if (!unIgnorableIndexedClasses.isEmpty()) {
final String unindexedClassesErrorMessage = unIgnorableIndexedClasses.stream().map(d -> "\t- " + d + "\n")
.collect(Collectors.joining());
throw new ConfigurationException(
"Unable to properly register the hierarchy of the following JPA classes as they are not in the Jandex index:\n"
+ unindexedClassesErrorMessage
+ "Consider adding them to the index either by creating a Jandex index " +
"for your dependency via the Maven plugin, an empty META-INF/beans.xml or quarkus.index-dependency properties.");
}
}
return new JpaModelBuildItem(collector.packages, collector.entityTypes, managedClassNames,
collector.potentialCdiBeanTypes, collector.xmlMappingsByPU);
}
private void enlistExplicitMappings(Collector collector,
JpaModelPersistenceUnitContributionBuildItem persistenceUnitContribution) {
// Classes explicitly mentioned in persistence.xml
for (String className : persistenceUnitContribution.explicitlyListedClassNames) {
enlistExplicitClass(collector, className);
}
// Classes explicitly mentioned in a mapping file
Set<String> mappingFileNames = new LinkedHashSet<>(persistenceUnitContribution.explicitlyListedMappingFiles);
if (!mappingFileNames.remove(XML_MAPPING_NO_FILE)) {
mappingFileNames.add(XML_MAPPING_DEFAULT_ORM_XML);
}
try (QuarkusMappingFileParser parser = QuarkusMappingFileParser.create()) {
for (String mappingFileName : mappingFileNames) {
hotDeploymentWatchedFiles.produce(new HotDeploymentWatchedFileBuildItem(mappingFileName));
Optional<RecordableXmlMapping> mappingOptional = parser.parse(persistenceUnitContribution.persistenceUnitName,
persistenceUnitContribution.persistenceUnitRootURL, mappingFileName);
if (!mappingOptional.isPresent()) {
if (persistenceUnitContribution.explicitlyListedMappingFiles.contains(mappingFileName)) {
// Trigger an exception for files that are explicitly mentioned and could not be found
// DEFAULT_ORM_XML in particular may be mentioned only implicitly,
// in which case it's fine if we cannot find it.
throw new IllegalStateException("Cannot find ORM mapping file '" + mappingFileName
+ "' in the classpath");
}
continue;
}
RecordableXmlMapping mapping = mappingOptional.get();
if (mapping.getOrmXmlRoot() != null) {
enlistOrmXmlMapping(collector, mapping.getOrmXmlRoot());
}
if (mapping.getHbmXmlRoot() != null) {
enlistHbmXmlMapping(collector, mapping.getHbmXmlRoot());
}
collector.xmlMappingsByPU
.computeIfAbsent(persistenceUnitContribution.persistenceUnitName, ignored -> new ArrayList<>())
.add(mapping);
}
}
}
private void enlistOrmXmlMapping(Collector collector, JaxbEntityMappings mapping) {
String packageName = mapping.getPackage();
String packagePrefix = packageName == null ? "" : packageName + ".";
JaxbPersistenceUnitMetadata metadata = mapping.getPersistenceUnitMetadata();
JaxbPersistenceUnitDefaults defaults = metadata == null ? null : metadata.getPersistenceUnitDefaults();
if (defaults != null) {
enlistOrmXmlMappingListeners(collector, packagePrefix, defaults.getEntityListeners());
}
for (JaxbEntity entity : mapping.getEntities()) {
enlistOrmXmlMappingManagedClass(collector, packagePrefix, entity, "entity");
}
for (JaxbMappedSuperclass mappedSuperclass : mapping.getMappedSuperclasses()) {
enlistOrmXmlMappingManagedClass(collector, packagePrefix, mappedSuperclass, "mapped-superclass");
}
for (JaxbEmbeddable embeddable : mapping.getEmbeddables()) {
String name = safeGetClassName(packagePrefix, embeddable, "embeddable");
enlistExplicitClass(collector, name);
}
for (JaxbConverter converter : mapping.getConverters()) {
collector.potentialCdiBeanTypes.add(DotName.createSimple(qualifyIfNecessary(packagePrefix, converter.getClazz())));
}
}
private void enlistOrmXmlMappingManagedClass(Collector collector, String packagePrefix, EntityOrMappedSuperclass managed,
String nodeName) {
String name = safeGetClassName(packagePrefix, managed, nodeName);
enlistExplicitClass(collector, name);
if (managed instanceof JaxbEntity) {
// The call to 'enlistExplicitClass' above may not
// detect that this class is an entity if it is not annotated
collector.entityTypes.add(name);
}
enlistOrmXmlMappingListeners(collector, packagePrefix, managed.getEntityListeners());
}
private void enlistOrmXmlMappingListeners(Collector collector, String packagePrefix, JaxbEntityListeners entityListeners) {
if (entityListeners == null) {
return;
}
for (JaxbEntityListener listener : entityListeners.getEntityListener()) {
collector.potentialCdiBeanTypes.add(DotName.createSimple(qualifyIfNecessary(packagePrefix, listener.getClazz())));
}
}
private static String safeGetClassName(String packagePrefix, ManagedType managedType, String nodeName) {
String name = managedType.getClazz();
if (name == null) {
throw new IllegalArgumentException("Missing attribute '" + nodeName + ".class'");
}
return qualifyIfNecessary(packagePrefix, name);
}
// See org.hibernate.cfg.annotations.reflection.internal.XMLContext.buildSafeClassName(java.lang.String, java.lang.String)
private static String qualifyIfNecessary(String packagePrefix, String name) {
if (name.indexOf('.') < 0) {
return packagePrefix + name;
} else {
// The class name is already qualified; don't apply the package prefix.
return name;
}
}
private void enlistHbmXmlMapping(Collector collector, JaxbHbmHibernateMapping mapping) {
String packageValue = mapping.getPackage();
String packagePrefix = packageValue == null ? "" : packageValue + ".";
for (JaxbHbmRootEntityType entity : mapping.getClazz()) {
enlistHbmXmlEntity(collector, packagePrefix, entity, entity.getAttributes());
for (JaxbHbmDiscriminatorSubclassEntityType subclass : entity.getSubclass()) {
enlistHbmXmlEntity(collector, packagePrefix, subclass, subclass.getAttributes());
}
for (JaxbHbmUnionSubclassEntityType subclass : entity.getUnionSubclass()) {
enlistHbmXmlEntity(collector, packagePrefix, subclass, subclass.getAttributes());
}
for (JaxbHbmJoinedSubclassEntityType subclass : entity.getJoinedSubclass()) {
enlistHbmXmlEntity(collector, packagePrefix, subclass, subclass.getAttributes());
}
}
// For some reason the format also allows specifying subclasses at the top level.
for (JaxbHbmDiscriminatorSubclassEntityType subclass : mapping.getSubclass()) {
enlistHbmXmlEntity(collector, packagePrefix, subclass, subclass.getAttributes());
}
for (JaxbHbmUnionSubclassEntityType subclass : mapping.getUnionSubclass()) {
enlistHbmXmlEntity(collector, packagePrefix, subclass, subclass.getAttributes());
}
for (JaxbHbmJoinedSubclassEntityType subclass : mapping.getJoinedSubclass()) {
enlistHbmXmlEntity(collector, packagePrefix, subclass, subclass.getAttributes());
}
}
private void enlistHbmXmlEntity(Collector collector, String packagePrefix,
JaxbHbmEntityBaseDefinition entityDefinition, List<?> attributes) {
String name = packagePrefix + entityDefinition.getName();
enlistExplicitClass(collector, name);
// The call to 'enlistExplicitClass' above may not
// detect that this class is an entity if it is not annotated
collector.entityTypes.add(name);
collectHbmXmlEmbeddedTypes(collector, packagePrefix, attributes);
}
private void collectHbmXmlEmbeddedTypes(Collector collector, String packagePrefix, List<?> attributes) {
for (Object attribute : attributes) {
if (attribute instanceof JaxbHbmCompositeAttributeType) {
JaxbHbmCompositeAttributeType compositeAttribute = (JaxbHbmCompositeAttributeType) attribute;
String name = packagePrefix + compositeAttribute.getClazz();
enlistExplicitClass(collector, name);
// The call to 'enlistExplicitClass' above may not
// detect that this class is an entity if it is not annotated
collector.entityTypes.add(name);
collectHbmXmlEmbeddedTypes(collector, packagePrefix, compositeAttribute.getAttributes());
}
}
}
private void enlistExplicitClass(Collector collector, String className) {
DotName dotName = DotName.createSimple(className);
// This will also take care of adding the class to unindexedClasses if necessary.
addClassHierarchyToReflectiveList(collector, dotName);
}
private void enlistEmbeddedsAndElementCollections(Collector collector) throws BuildException {
Set<DotName> embeddedTypes = new HashSet<>();
for (DotName embeddedAnnotation : EMBEDDED_ANNOTATIONS) {
for (AnnotationInstance annotation : index.getAnnotations(embeddedAnnotation)) {
AnnotationTarget target = annotation.target();
switch (target.kind()) {
case FIELD:
var field = target.asField();
collectEmbeddedType(embeddedTypes, field.declaringClass(), field, field.type(), true);
break;
case METHOD:
var method = target.asMethod();
if (method.isBridge()) {
// Generated by javac for covariant return type override.
// There's another method with a more specific return type, ignore this one.
continue;
}
collectEmbeddedType(embeddedTypes, method.declaringClass(), method, method.returnType(), true);
break;
default:
throw new IllegalStateException(
"[internal error] " + embeddedAnnotation + " placed on a unknown element: " + target);
}
}
}
for (AnnotationInstance annotation : index.getAnnotations(ClassNames.ELEMENT_COLLECTION)) {
AnnotationTarget target = annotation.target();
switch (target.kind()) {
case FIELD:
var field = target.asField();
collectElementCollectionTypes(embeddedTypes, field.declaringClass(), field, field.type());
break;
case METHOD:
var method = target.asMethod();
if (method.isBridge()) {
// Generated by javac for covariant return type override.
// There's another method with a more specific return type, ignore this one.
continue;
}
collectElementCollectionTypes(embeddedTypes, method.declaringClass(), method, method.returnType());
break;
default:
throw new IllegalStateException(
"[internal error] " + ClassNames.ELEMENT_COLLECTION + " placed on a unknown element: " + target);
}
}
for (DotName embeddedType : embeddedTypes) {
addClassHierarchyToReflectiveList(collector, embeddedType);
}
}
private void enlistJPAModelAnnotatedPackages(Collector collector, DotName dotName) {
Collection<AnnotationInstance> jpaAnnotations = index.getAnnotations(dotName);
if (jpaAnnotations == null) {
return;
}
for (AnnotationInstance annotation : jpaAnnotations) {
if (annotation.target().kind() != AnnotationTarget.Kind.CLASS) {
continue; // Annotation on field, method, etc.
}
ClassInfo klass = annotation.target().asClass();
if (!klass.simpleName().equals("package-info")) {
continue; // Annotation on an actual class, not a package.
}
collectPackage(collector, klass);
}
}
private void enlistJPAModelClasses(Collector collector, DotName dotName) {
Collection<AnnotationInstance> jpaAnnotations = index.getAnnotations(dotName);
if (jpaAnnotations == null) {
return;
}
for (AnnotationInstance annotation : jpaAnnotations) {
ClassInfo klass = annotation.target().asClass();
DotName targetDotName = klass.name();
addClassHierarchyToReflectiveList(collector, targetDotName);
collectModelType(collector, klass);
}
}
private void enlistJPAModelIdClasses(Collector collector, DotName dotName) {
Collection<AnnotationInstance> jpaAnnotations = index.getAnnotations(dotName);
if (jpaAnnotations == null) {
return;
}
for (AnnotationInstance annotation : jpaAnnotations) {
DotName targetDotName = annotation.value().asClass().name();
addClassHierarchyToReflectiveList(collector, targetDotName);
collector.modelTypes.add(targetDotName.toString());
}
}
private void enlistPotentialCdiBeanClasses(Collector collector, DotName dotName) {
Collection<AnnotationInstance> jpaAnnotations = index.getAnnotations(dotName);
if (jpaAnnotations == null) {
return;
}
for (AnnotationInstance annotation : jpaAnnotations) {
AnnotationTarget target = annotation.target();
ClassInfo beanType;
switch (target.kind()) {
case CLASS:
beanType = target.asClass();
break;
case FIELD:
beanType = target.asField().declaringClass();
break;
case METHOD:
beanType = target.asMethod().declaringClass();
break;
case METHOD_PARAMETER:
case TYPE:
case RECORD_COMPONENT:
default:
throw new IllegalArgumentException(
"Annotation " + dotName + " was not expected on a target of kind " + target.kind());
}
DotName beanTypeDotName = beanType.name();
collector.potentialCdiBeanTypes.add(beanTypeDotName);
}
}
/**
* Add the class to the reflective list with full method and field access.
* Add the superclasses recursively as well as the interfaces.
* Un-indexed classes/interfaces are accumulated to be thrown as a configuration error in the top level caller method
* <p>
* TODO should we also return the return types of all methods and fields? It could contain Enums for example.
*/
private void addClassHierarchyToReflectiveList(Collector collector, DotName className) {
if (className == null || isIgnored(className)) {
// bail out if java.lang.Object or a class we want to ignore
return;
}
// if the class is in the java. package and is not ignored, we want to register it for reflection
if (isInJavaPackage(className)) {
collector.javaTypes.add(className.toString());
return;
}
ClassInfo classInfo = index.getClassByName(className);
if (classInfo == null) {
collector.unindexedClasses.add(className);
return;
}
// we need to check for enums
for (FieldInfo fieldInfo : classInfo.fields()) {
Type fieldType = fieldInfo.type();
if (Type.Kind.CLASS != fieldType.kind()) {
// skip primitives and arrays
continue;
}
DotName fieldClassName = fieldInfo.type().name();
ClassInfo fieldTypeClassInfo = index.getClassByName(fieldClassName);
if (fieldTypeClassInfo != null && ClassNames.ENUM.equals(fieldTypeClassInfo.superName())) {
collector.enumTypes.add(fieldClassName.toString());
}
}
//Capture this one (for various needs: Reflective access enablement, Hibernate enhancement, JPA Template)
collectModelType(collector, classInfo);
// add superclass recursively
addClassHierarchyToReflectiveList(collector, classInfo.superName());
// add interfaces recursively
for (DotName interfaceDotName : classInfo.interfaceNames()) {
addClassHierarchyToReflectiveList(collector, interfaceDotName);
}
}
private static void collectPackage(Collector collector, ClassInfo classOrPackageInfo) {
String classOrPackageInfoName = classOrPackageInfo.name().toString();
String packageName = classOrPackageInfoName.substring(0, classOrPackageInfoName.lastIndexOf('.'));
collector.packages.add(packageName);
}
private static void collectModelType(Collector collector, ClassInfo modelClass) {
String name = modelClass.name().toString();
collector.modelTypes.add(name);
if (modelClass.declaredAnnotation(ClassNames.JPA_ENTITY) != null) {
collector.entityTypes.add(name);
}
}
private void collectEmbeddedType(Set<DotName> embeddedTypes, ClassInfo declaringClass,
Declaration attribute, Type attributeType, boolean validate)
throws BuildException {
DotName className;
switch (attributeType.kind()) {
case CLASS:
className = attributeType.asClassType().name();
break;
case PARAMETERIZED_TYPE:
className = attributeType.name();
break;
default:
// do nothing
return;
}
if (validate && !index.getClassByName(className).hasAnnotation(ClassNames.EMBEDDABLE)) {
throw new BuildException(
"Type " + className + " must be annotated with @Embeddable, because it is used as an embeddable."
+ " This type is used in class " + declaringClass
+ " for attribute " + attribute + ".");
}
embeddedTypes.add(attributeType.name());
}
private void collectElementCollectionTypes(Set<DotName> embeddedTypes, ClassInfo declaringClass,
Declaration attribute, Type attributeType)
throws BuildException {
switch (attributeType.kind()) {
case CLASS:
// Raw collection type, nothing we can do
break;
case PARAMETERIZED_TYPE:
embeddedTypes.add(attributeType.name());
var typeArguments = attributeType.asParameterizedType().arguments();
for (Type typeArgument : typeArguments) {
// We don't validate @Embeddable annotations on element collections at the moment
// See https://github.com/quarkusio/quarkus/pull/35822
collectEmbeddedType(embeddedTypes, declaringClass, attribute, typeArgument, false);
}
break;
case ARRAY:
collectEmbeddedType(embeddedTypes, declaringClass, attribute, attributeType.asArrayType().constituent(), true);
break;
default:
// do nothing
break;
}
}
private static boolean isIgnored(DotName classDotName) {
String className = classDotName.toString();
if (className.startsWith("java.util.") || className.startsWith("java.lang.")
|| className.startsWith("org.hibernate.engine.spi.")
|| className.startsWith("jakarta.persistence.")
|| className.startsWith("jakarta.persistence.")) {
return true;
}
return false;
}
private static boolean isInJavaPackage(DotName classDotName) {
String className = classDotName.toString();
if (className.startsWith("java.")) {
return true;
}
return false;
}
private static class Collector {
final Set<String> packages = new HashSet<>();
final Set<String> entityTypes = new HashSet<>();
final Set<DotName> potentialCdiBeanTypes = new HashSet<>();
final Set<String> modelTypes = new HashSet<>();
final Set<String> enumTypes = new HashSet<>();
final Set<String> javaTypes = new HashSet<>();
final Set<DotName> unindexedClasses = new HashSet<>();
final Map<String, List<RecordableXmlMapping>> xmlMappingsByPU = new HashMap<>();
}
}