9
9
import org .hibernate .processor .model .Metamodel ;
10
10
11
11
import javax .annotation .processing .FilerException ;
12
+ import javax .lang .model .element .AnnotationMirror ;
13
+ import javax .lang .model .element .AnnotationValue ;
12
14
import javax .lang .model .element .Element ;
13
15
import javax .lang .model .element .ElementKind ;
14
16
import javax .lang .model .element .Modifier ;
15
17
import javax .lang .model .element .PackageElement ;
16
18
import javax .lang .model .element .TypeElement ;
19
+ import javax .lang .model .element .VariableElement ;
17
20
import javax .tools .Diagnostic ;
18
21
import javax .tools .FileObject ;
19
22
import java .io .IOException ;
@@ -102,7 +105,11 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
102
105
if ( context .addSuppressWarningsAnnotation () ) {
103
106
pw .println ( writeSuppressWarnings (context ) );
104
107
}
105
- entity .inheritedAnnotations ().forEach (pw ::println );
108
+ entity .inheritedAnnotations ()
109
+ .forEach ( annotation -> {
110
+ printAnnotation ( annotation , pw );
111
+ pw .print ('\n' );
112
+ } );
106
113
107
114
printClassDeclaration ( entity , pw );
108
115
@@ -134,9 +141,10 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
134
141
pw .println ('\t' + line );
135
142
if ( line .trim ().startsWith ("@Override" ) ) {
136
143
metaMember .inheritedAnnotations ()
137
- .forEach (x -> {
144
+ .forEach (annotation -> {
138
145
pw .print ('\t' );
139
- pw .println (x );
146
+ printAnnotation ( annotation , pw );
147
+ pw .print ('\n' );
140
148
});
141
149
}
142
150
});
@@ -149,6 +157,60 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
149
157
}
150
158
}
151
159
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
+
152
214
private static void printClassDeclaration (Metamodel entity , PrintWriter pw ) {
153
215
if ( isMemberType ( entity .getElement () ) ) {
154
216
final Set <Modifier > modifiers = entity .getElement ().getModifiers ();
0 commit comments