99import org .hibernate .processor .model .Metamodel ;
1010
1111import javax .annotation .processing .FilerException ;
12+ import javax .lang .model .element .AnnotationMirror ;
13+ import javax .lang .model .element .AnnotationValue ;
1214import javax .lang .model .element .Element ;
1315import javax .lang .model .element .ElementKind ;
1416import javax .lang .model .element .Modifier ;
1517import javax .lang .model .element .PackageElement ;
1618import javax .lang .model .element .TypeElement ;
19+ import javax .lang .model .element .VariableElement ;
1720import javax .tools .Diagnostic ;
1821import javax .tools .FileObject ;
1922import java .io .IOException ;
@@ -102,7 +105,11 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
102105 if ( context .addSuppressWarningsAnnotation () ) {
103106 pw .println ( writeSuppressWarnings (context ) );
104107 }
105- entity .inheritedAnnotations ().forEach (pw ::println );
108+ entity .inheritedAnnotations ()
109+ .forEach ( annotation -> {
110+ printAnnotation ( annotation , pw );
111+ pw .print ('\n' );
112+ } );
106113
107114 printClassDeclaration ( entity , pw );
108115
@@ -134,9 +141,10 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
134141 pw .println ('\t' + line );
135142 if ( line .trim ().startsWith ("@Override" ) ) {
136143 metaMember .inheritedAnnotations ()
137- .forEach (x -> {
144+ .forEach (annotation -> {
138145 pw .print ('\t' );
139- pw .println (x );
146+ printAnnotation ( annotation , pw );
147+ pw .print ('\n' );
140148 });
141149 }
142150 });
@@ -149,6 +157,60 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
149157 }
150158 }
151159
160+ private static void printAnnotation (AnnotationMirror annotation , PrintWriter pw ) {
161+ pw .print ('@' );
162+ final TypeElement type = (TypeElement ) annotation .getAnnotationType ().asElement ();
163+ pw .print ( type .getQualifiedName ().toString () );
164+ var elementValues = annotation .getElementValues ();
165+ if (!elementValues .isEmpty ()) {
166+ pw .print ('(' );
167+ boolean first = true ;
168+ for (var entry : elementValues .entrySet ()) {
169+ if (first ) {
170+ first = false ;
171+ }
172+ else {
173+ pw .print (',' );
174+ }
175+ pw .print ( entry .getKey ().getSimpleName () );
176+ pw .print ( '=' );
177+ printAnnotationValue ( pw , entry .getValue () );
178+ }
179+ pw .print (')' );
180+ }
181+ }
182+
183+ private static void printAnnotationValue (PrintWriter pw , AnnotationValue value ) {
184+ final Object argument = value .getValue ();
185+ if (argument instanceof VariableElement variable ) {
186+ pw .print ( variable .getEnclosingElement () );
187+ pw .print ('.' );
188+ pw .print ( variable .getSimpleName ().toString () );
189+ }
190+ else if (argument instanceof AnnotationMirror childAnnotation ) {
191+ printAnnotation ( childAnnotation , pw );
192+ }
193+ else if (argument instanceof List ) {
194+ final List <? extends AnnotationValue > list =
195+ (List <? extends AnnotationValue >) argument ;
196+ pw .print ('{' );
197+ boolean first = true ;
198+ for (AnnotationValue listedValue : list ) {
199+ if (first ) {
200+ first = false ;
201+ }
202+ else {
203+ pw .print (',' );
204+ }
205+ printAnnotationValue ( pw , listedValue );
206+ }
207+ pw .print ('}' );
208+ }
209+ else {
210+ pw .print ( argument );
211+ }
212+ }
213+
152214 private static void printClassDeclaration (Metamodel entity , PrintWriter pw ) {
153215 if ( isMemberType ( entity .getElement () ) ) {
154216 final Set <Modifier > modifiers = entity .getElement ().getModifiers ();
0 commit comments