1010import org .hibernate .processor .model .Metamodel ;
1111
1212import javax .annotation .processing .FilerException ;
13+ import javax .lang .model .element .AnnotationMirror ;
14+ import javax .lang .model .element .AnnotationValue ;
1315import javax .lang .model .element .ElementKind ;
1416import javax .lang .model .element .TypeElement ;
17+ import javax .lang .model .element .VariableElement ;
1518import javax .tools .Diagnostic ;
1619import javax .tools .FileObject ;
1720import java .io .IOException ;
@@ -94,7 +97,11 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
9497 if ( context .addSuppressWarningsAnnotation () ) {
9598 pw .println ( writeSuppressWarnings (context ) );
9699 }
97- entity .inheritedAnnotations ().forEach (pw ::println );
100+ entity .inheritedAnnotations ()
101+ .forEach ( annotation -> {
102+ printAnnotation ( annotation , pw );
103+ pw .print ('\n' );
104+ } );
98105
99106 printClassDeclaration ( entity , pw );
100107
@@ -116,9 +123,10 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
116123 pw .println ('\t' + line );
117124 if ( line .trim ().startsWith ("@Override" ) ) {
118125 metaMember .inheritedAnnotations ()
119- .forEach (x -> {
126+ .forEach (annotation -> {
120127 pw .print ('\t' );
121- pw .println (x );
128+ printAnnotation ( annotation , pw );
129+ pw .print ('\n' );
122130 });
123131 }
124132 });
@@ -131,6 +139,62 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
131139 }
132140 }
133141
142+ private static void printAnnotation (AnnotationMirror annotation , PrintWriter pw ) {
143+ pw .print ('@' );
144+ final TypeElement type = (TypeElement ) annotation .getAnnotationType ().asElement ();
145+ pw .print ( type .getQualifiedName ().toString () );
146+ var elementValues = annotation .getElementValues ();
147+ if (!elementValues .isEmpty ()) {
148+ pw .print ('(' );
149+ boolean first = true ;
150+ for (var entry : elementValues .entrySet ()) {
151+ if (first ) {
152+ first = false ;
153+ }
154+ else {
155+ pw .print (',' );
156+ }
157+ pw .print ( entry .getKey ().getSimpleName () );
158+ pw .print ( '=' );
159+ printAnnotationValue ( pw , entry .getValue () );
160+ }
161+ pw .print (')' );
162+ }
163+ }
164+
165+ private static void printAnnotationValue (PrintWriter pw , AnnotationValue value ) {
166+ final Object argument = value .getValue ();
167+ if (argument instanceof VariableElement ) {
168+ VariableElement variable = (VariableElement ) argument ;
169+ pw .print ( variable .getEnclosingElement () );
170+ pw .print ('.' );
171+ pw .print ( variable .getSimpleName ().toString () );
172+ }
173+ else if (argument instanceof AnnotationMirror ) {
174+ AnnotationMirror childAnnotation = (AnnotationMirror ) argument ;
175+ printAnnotation ( childAnnotation , pw );
176+ }
177+ else if (argument instanceof List ) {
178+ final List <? extends AnnotationValue > list =
179+ (List <? extends AnnotationValue >) argument ;
180+ pw .print ('{' );
181+ boolean first = true ;
182+ for (AnnotationValue listedValue : list ) {
183+ if (first ) {
184+ first = false ;
185+ }
186+ else {
187+ pw .print (',' );
188+ }
189+ printAnnotationValue ( pw , listedValue );
190+ }
191+ pw .print ('}' );
192+ }
193+ else {
194+ pw .print ( argument );
195+ }
196+ }
197+
134198 private static void printClassDeclaration (Metamodel entity , PrintWriter pw ) {
135199 pw .print ( "public " );
136200 if ( !entity .isImplementation () && !entity .isJakartaDataStyle () ) {
0 commit comments