10
10
import org .hibernate .processor .model .Metamodel ;
11
11
12
12
import javax .annotation .processing .FilerException ;
13
+ import javax .lang .model .element .AnnotationMirror ;
14
+ import javax .lang .model .element .AnnotationValue ;
13
15
import javax .lang .model .element .ElementKind ;
14
16
import javax .lang .model .element .TypeElement ;
17
+ import javax .lang .model .element .VariableElement ;
15
18
import javax .tools .Diagnostic ;
16
19
import javax .tools .FileObject ;
17
20
import java .io .IOException ;
@@ -94,7 +97,11 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
94
97
if ( context .addSuppressWarningsAnnotation () ) {
95
98
pw .println ( writeSuppressWarnings (context ) );
96
99
}
97
- entity .inheritedAnnotations ().forEach (pw ::println );
100
+ entity .inheritedAnnotations ()
101
+ .forEach ( annotation -> {
102
+ printAnnotation ( annotation , pw );
103
+ pw .print ('\n' );
104
+ } );
98
105
99
106
printClassDeclaration ( entity , pw );
100
107
@@ -116,9 +123,10 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
116
123
pw .println ('\t' + line );
117
124
if ( line .trim ().startsWith ("@Override" ) ) {
118
125
metaMember .inheritedAnnotations ()
119
- .forEach (x -> {
126
+ .forEach (annotation -> {
120
127
pw .print ('\t' );
121
- pw .println (x );
128
+ printAnnotation ( annotation , pw );
129
+ pw .print ('\n' );
122
130
});
123
131
}
124
132
});
@@ -131,6 +139,62 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
131
139
}
132
140
}
133
141
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
+
134
198
private static void printClassDeclaration (Metamodel entity , PrintWriter pw ) {
135
199
pw .print ( "public " );
136
200
if ( !entity .isImplementation () && !entity .isJakartaDataStyle () ) {
0 commit comments