3030import javax .lang .model .type .DeclaredType ;
3131import javax .lang .model .type .TypeKind ;
3232import javax .lang .model .type .TypeMirror ;
33+ import javax .lang .model .util .Elements ;
3334import javax .tools .Diagnostic ;
3435import javax .tools .StandardLocation ;
3536import java .io .IOException ;
4142import java .util .List ;
4243import java .util .Map ;
4344import java .util .Set ;
45+ import java .util .function .Function ;
4446
4547import static java .lang .Boolean .parseBoolean ;
4648import static javax .lang .model .util .ElementFilter .fieldsIn ;
@@ -374,7 +376,8 @@ private void processElement(Element element, @Nullable Element parent) {
374376 // skip it completely
375377 return ;
376378 }
377- else if ( isEntityOrEmbeddable ( element ) && !element .getModifiers ().contains ( Modifier .PRIVATE )) {
379+ else if ( isEntityOrEmbeddable ( element )
380+ && !element .getModifiers ().contains ( Modifier .PRIVATE ) ) {
378381 context .logMessage ( Diagnostic .Kind .OTHER , "Processing annotated entity class '" + element + "'" );
379382 handleRootElementAnnotationMirrors ( element , parent );
380383 }
@@ -409,30 +412,14 @@ else if ( element instanceof TypeElement typeElement ) {
409412 }
410413 }
411414 if ( enclosesEntityOrEmbeddable ( element ) ) {
412- AnnotationMetaEntity parentMeta = null ;
413- if ( parent instanceof TypeElement parentElement ) {
414- final String key = parentElement .getQualifiedName ().toString ();
415- if ( context .getMetamodel ( key ) instanceof AnnotationMetaEntity parentMetaEntity ) {
416- parentMeta = parentMetaEntity ;
417- }
418- }
419- final NonManagedMetamodel metaEntity =
420- NonManagedMetamodel .create (
421- typeElement , context ,
422- false , parentMeta );
415+ final NonManagedMetamodel metaEntity =
416+ NonManagedMetamodel .create ( typeElement , context , false ,
417+ parentMetadata ( parent , context ::getMetamodel ) );
423418 context .addMetaEntity ( metaEntity .getQualifiedName (), metaEntity );
424- if ( context .generateJakartaDataStaticMetamodel ()) {
425- AnnotationMetaEntity parentDataMeta = null ;
426- if ( parent instanceof TypeElement parentElement ) {
427- final String key = parentElement .getQualifiedName ().toString ();
428- if ( context .getDataMetaEntity ( key ) instanceof AnnotationMetaEntity parentMetaEntity ) {
429- parentDataMeta = parentMetaEntity ;
430- }
431- }
432- final NonManagedMetamodel dataMetaEntity =
433- NonManagedMetamodel .create (
434- typeElement , context ,
435- true , parentDataMeta );
419+ if ( context .generateJakartaDataStaticMetamodel () ) {
420+ final NonManagedMetamodel dataMetaEntity =
421+ NonManagedMetamodel .create ( typeElement , context , true ,
422+ parentMetadata ( parent , context ::getDataMetaEntity ) );
436423 context .addDataMetaEntity ( dataMetaEntity .getQualifiedName (), dataMetaEntity );
437424 }
438425
@@ -448,16 +435,28 @@ else if ( element instanceof TypeElement typeElement ) {
448435 }
449436 }
450437 catch ( ProcessLaterException processLaterException ) {
451- if ( element instanceof TypeElement ) {
438+ if ( element instanceof TypeElement typeElement ) {
452439 context .logMessage (
453440 Diagnostic .Kind .OTHER ,
454441 "Could not process '" + element + "' (will redo in next round)"
455442 );
456- context .addElementToRedo ( ( ( TypeElement ) element ) .getQualifiedName () );
443+ context .addElementToRedo ( typeElement .getQualifiedName () );
457444 }
458445 }
459446 }
460447
448+ private @ Nullable AnnotationMetaEntity parentMetadata (
449+ @ Nullable Element parent , Function <String , @ Nullable Object > metamodel ) {
450+ if ( parent instanceof TypeElement parentElement
451+ && metamodel .apply ( parentElement .getQualifiedName ().toString () )
452+ instanceof AnnotationMetaEntity parentMetaEntity ) {
453+ return parentMetaEntity ;
454+ }
455+ else {
456+ return null ;
457+ }
458+ }
459+
461460 private boolean hasPackageAnnotation (Element element , String annotation ) {
462461 final PackageElement pack = context .getElementUtils ().getPackageOf ( element ); // null for module descriptor
463462 return pack != null && hasAnnotation ( pack , annotation );
@@ -562,15 +561,18 @@ private boolean modelGenerationNeedsToBeDeferred(Collection<Metamodel> entities,
562561 }
563562
564563 private static boolean enclosesEntityOrEmbeddable (Element element ) {
565- if ( !(element instanceof TypeElement typeElement ) ) {
564+ if ( element instanceof TypeElement typeElement ) {
565+ for ( final Element enclosedElement : typeElement .getEnclosedElements () ) {
566+ if ( isEntityOrEmbeddable ( enclosedElement )
567+ || enclosesEntityOrEmbeddable ( enclosedElement ) ) {
568+ return true ;
569+ }
570+ }
566571 return false ;
567572 }
568- for ( final Element enclosedElement : typeElement .getEnclosedElements () ) {
569- if ( isEntityOrEmbeddable ( enclosedElement ) || enclosesEntityOrEmbeddable ( enclosedElement ) ) {
570- return true ;
571- }
573+ else {
574+ return false ;
572575 }
573- return false ;
574576 }
575577
576578 private static boolean isEntityOrEmbeddable (Element element ) {
@@ -621,14 +623,8 @@ private void handleRootElementAnnotationMirrors(final Element element, @Nullable
621623 + "' since XML configuration is metadata complete." );
622624 }
623625 else {
624- AnnotationMetaEntity parentMetaEntity = null ;
625- if ( parent instanceof TypeElement parentTypeElement ) {
626- if ( context .getMetamodel (
627- parentTypeElement .getQualifiedName ().toString () )
628- instanceof AnnotationMetaEntity pme ) {
629- parentMetaEntity = pme ;
630- }
631- }
626+ final AnnotationMetaEntity parentMetaEntity =
627+ parentMetadata ( parent , context ::getMetamodel );
632628 final boolean requiresLazyMemberInitialization
633629 = hasAnnotation ( element , EMBEDDABLE , MAPPED_SUPERCLASS );
634630 final AnnotationMetaEntity metaEntity =
@@ -648,13 +644,8 @@ && hasAnnotation( element, ENTITY, MAPPED_SUPERCLASS )
648644 // let a handwritten metamodel "override" the generated one
649645 // (this is used in the Jakarta Data TCK)
650646 && !hasHandwrittenMetamodel (element ) ) {
651- AnnotationMetaEntity parentDataEntity = null ;
652- if ( parent instanceof TypeElement parentTypeElement ) {
653- if ( context .getDataMetaEntity ( parentTypeElement .getQualifiedName ().toString () )
654- instanceof AnnotationMetaEntity pme ) {
655- parentDataEntity = pme ;
656- }
657- }
647+ final AnnotationMetaEntity parentDataEntity =
648+ parentMetadata ( parent , context ::getDataMetaEntity );
658649 final AnnotationMetaEntity dataMetaEntity =
659650 AnnotationMetaEntity .create ( typeElement , context ,
660651 requiresLazyMemberInitialization ,
@@ -783,13 +774,14 @@ else if ( hasAnnotation( typeElement, EMBEDDABLE ) ) {
783774
784775 private void writeIndex () {
785776 final ProcessingEnvironment processingEnvironment = context .getProcessingEnvironment ();
777+ final Elements elementUtils = processingEnvironment .getElementUtils ();
786778 context .getEntityNameMappings ().forEach ((entityName , className ) -> {
787779 try (Writer writer = processingEnvironment .getFiler ()
788780 .createResource (
789781 StandardLocation .SOURCE_OUTPUT ,
790782 ENTITY_INDEX ,
791783 entityName ,
792- processingEnvironment . getElementUtils () .getTypeElement ( className )
784+ elementUtils .getTypeElement ( className )
793785 )
794786 .openWriter ()) {
795787 writer .append (className );
@@ -801,11 +793,12 @@ private void writeIndex() {
801793 }
802794 });
803795 context .getEnumTypesByValue ().forEach ((valueName , enumTypeNames ) -> {
804- try (Writer writer = processingEnvironment .getFiler ().createResource (
796+ try (Writer writer = processingEnvironment .getFiler ()
797+ .createResource (
805798 StandardLocation .SOURCE_OUTPUT ,
806799 ENTITY_INDEX ,
807800 '.' + valueName ,
808- processingEnvironment . getElementUtils () .getTypeElement ( enumTypeNames .iterator ().next () )
801+ elementUtils .getTypeElement ( enumTypeNames .iterator ().next () )
809802 )
810803 .openWriter ()) {
811804 for (String enumTypeName : enumTypeNames ) {
0 commit comments