44 */
55package org .hibernate .processor ;
66
7- import jakarta .annotation .Nullable ;
8- import org .hibernate .processor .annotation .AnnotationMetaEntity ;
97import org .hibernate .processor .annotation .InnerClassMetaAttribute ;
108import org .hibernate .processor .model .MetaAttribute ;
119import org .hibernate .processor .model .Metamodel ;
10+ import org .hibernate .processor .util .StringUtil ;
1211
1312import javax .annotation .processing .FilerException ;
13+ import javax .lang .model .element .Element ;
1414import javax .lang .model .element .ElementKind ;
1515import javax .lang .model .element .Modifier ;
16+ import javax .lang .model .element .PackageElement ;
1617import javax .lang .model .element .TypeElement ;
1718import javax .tools .Diagnostic ;
1819import javax .tools .FileObject ;
2728import java .util .Set ;
2829import java .util .stream .Collectors ;
2930
31+ import static org .hibernate .processor .util .TypeUtils .isMemberType ;
3032
3133/**
3234 * Helper class to write the actual metamodel class using the {@link javax.annotation.processing.Filer} API.
@@ -40,7 +42,6 @@ private ClassWriter() {
4042 }
4143
4244 public static void writeFile (Metamodel entity , Context context ) {
43- if (entity .hasParent ()) throw new IllegalStateException ();
4445 try {
4546 String metaModelPackage = entity .getPackageName ();
4647 // need to generate the body first, since this will also update
@@ -105,7 +106,7 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
105106 }
106107 entity .inheritedAnnotations ().forEach (pw ::println );
107108
108- printClassDeclaration ( entity , pw , context );
109+ printClassDeclaration ( entity , pw );
109110
110111 pw .println ();
111112
@@ -115,6 +116,7 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
115116 generateBody ( innerClass .getMetaEntity (), context )
116117 .toString ().lines ()
117118 .forEach (line -> pw .println ('\t' + line ));
119+ context .markGenerated ( innerClass .getMetaEntity () );
118120 }
119121 }
120122
@@ -149,12 +151,13 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
149151 }
150152 }
151153
152- private static void printClassDeclaration (Metamodel entity , PrintWriter pw , Context context ) {
153- if ( entity .hasParent ( ) ) {
154+ private static void printClassDeclaration (Metamodel entity , PrintWriter pw ) {
155+ if ( isMemberType ( entity .getElement () ) ) {
154156 final Set <Modifier > modifiers = entity .getElement ().getModifiers ();
155- if (modifiers .contains ( Modifier .PUBLIC )) {
157+ if ( modifiers .contains ( Modifier .PUBLIC ) ) {
156158 pw .print ( "public " );
157- }else if (modifiers .contains ( Modifier .PROTECTED )) {
159+ }
160+ else if ( modifiers .contains ( Modifier .PROTECTED ) ) {
158161 pw .print ( "protected " );
159162 }
160163 pw .print ( "static " );
@@ -168,70 +171,49 @@ private static void printClassDeclaration(Metamodel entity, PrintWriter pw, Cont
168171 pw .print ( entity .isJakartaDataStyle () ? "interface " : "class " );
169172 pw .print ( getGeneratedClassName (entity ) );
170173
171- String superClassName = entity .getSupertypeName ();
172- if ( superClassName != null ) {
173- Metamodel superClassEntity = context .getMetaEntity ( superClassName );
174- if (superClassEntity == null ) {
175- superClassEntity = context .getMetaEmbeddable ( superClassName );
176- }
177- final String superclassName = getGeneratedSuperclassName (
178- superClassName , superClassEntity , entity .isJakartaDataStyle () );
179- if (entity .getSimpleName ().equals ( "OffsetDateTimeTest" )) {
180- System .err .printf ( "Extends (?) %s - superClassName is not null%n" , superClassName );
181- }
182- pw .print ( " extends " + entity .importType (superclassName ) );
174+ final Element superTypeElement = entity .getSuperTypeElement ();
175+ if ( superTypeElement != null ) {
176+ pw .print ( " extends " +
177+ entity .importType (getGeneratedSuperclassName ( superTypeElement , entity .isJakartaDataStyle () )) );
183178 }
184179 if ( entity .isImplementation () ) {
185180 pw .print ( entity .getElement ().getKind () == ElementKind .CLASS ? " extends " : " implements " );
186181 pw .print ( entity .getSimpleName () );
187- if (entity .getSimpleName ().equals ( "OffsetDateTimeTest" )) {
188- System .err .printf ( "Extends (?) %s -- implementation (?!?)%n" , entity .getSimpleName () );
189- }
190182 }
191183
192184 pw .println ( " {" );
193185 }
194186
195187 private static String getFullyQualifiedClassName (Metamodel entity ) {
196188 final String metaModelPackage = entity .getPackageName ();
197- String fullyQualifiedClassName = "" ;
198- if ( !metaModelPackage .isEmpty () ) {
199- fullyQualifiedClassName = fullyQualifiedClassName + metaModelPackage + "." ;
200- }
189+ final String packageNamePrefix = !metaModelPackage .isEmpty () ? metaModelPackage + "." : "" ;
201190 final String className ;
202191 if ( entity .getElement ().getKind () == ElementKind .PACKAGE ) {
203192 className = getGeneratedClassName ( entity );
204193 }
205194 else {
206195 className = Arrays .stream (
207- entity .getQualifiedName ().substring ( fullyQualifiedClassName .length () ).split ( "\\ ." ) )
208- .map ( AnnotationMetaEntity ::removeDollar )
196+ entity .getQualifiedName ().substring ( packageNamePrefix .length () ).split ( "\\ ." ) )
197+ .map ( StringUtil ::removeDollar )
209198 .map ( part -> entity .isJakartaDataStyle () ? '_' + part : part + '_' )
210199 .collect ( Collectors .joining ( "." ) );
211200 }
212- return fullyQualifiedClassName + className ;
201+ return packageNamePrefix + className ;
213202 }
214203
215204 private static String getGeneratedClassName (Metamodel entity ) {
216205 final String className = entity .getSimpleName ();
217- return entity .isJakartaDataStyle () ? '_' + className : className + '_' ; // TODO : check inner class!!!
206+ return entity .isJakartaDataStyle () ? '_' + className : className + '_' ;
218207 }
219208
220- private static String getGeneratedSuperclassName (String superClassName , @ Nullable Metamodel superClassEntity , boolean jakartaDataStyle ) {
221- if ( jakartaDataStyle ) {
222- int lastDot = superClassName .lastIndexOf ('.' );
223- if ( lastDot <0 ) {
224- return '_' + superClassName ;
225- }
226- else {
227- return superClassName .substring (0 ,lastDot +1 )
228- + '_' + superClassName .substring (lastDot +1 );
229- }
230- }
231- else {
232- return superClassEntity == null ? superClassName + '_'
233- : getFullyQualifiedClassName ( superClassEntity );
234- }
209+ private static String getGeneratedSuperclassName (Element superClassElement , boolean jakartaDataStyle ) {
210+ final TypeElement typeElement = (TypeElement ) superClassElement ;
211+ final String simpleName = typeElement .getSimpleName ().toString ();
212+ final Element enclosingElement = typeElement .getEnclosingElement ();
213+ return (enclosingElement instanceof TypeElement
214+ ? getGeneratedSuperclassName ( enclosingElement , jakartaDataStyle )
215+ : ((PackageElement ) enclosingElement ).getQualifiedName ().toString ())
216+ + "." + (jakartaDataStyle ? '_' + simpleName : simpleName + '_' );
235217 }
236218
237219 private static String writeGeneratedAnnotation (Metamodel entity , Context context ) {
0 commit comments