1
1
package io .avaje .inject .generator ;
2
2
3
+ import static java .util .stream .Collectors .toList ;
4
+
3
5
import java .util .List ;
6
+ import java .util .Map .Entry ;
4
7
5
8
import javax .lang .model .element .AnnotationMirror ;
6
9
import javax .lang .model .element .AnnotationValue ;
7
10
import javax .lang .model .element .Element ;
11
+ import javax .lang .model .element .ExecutableElement ;
8
12
import javax .lang .model .element .VariableElement ;
9
13
10
14
final class AnnotationCopier {
11
15
private AnnotationCopier () {}
12
16
13
- public static void copyAnnotations (Append writer , Element element , boolean newLines ) {
17
+ static void copyAnnotations (Append writer , Element element , boolean newLines ) {
14
18
copyAnnotations (writer , element , "" , newLines );
15
19
}
16
20
17
- public static void copyAnnotations (Append writer , Element element , String indent , boolean newLines ) {
21
+ static void copyAnnotations (Append writer , Element element , String indent , boolean newLines ) {
18
22
for (final AnnotationMirror annotationMirror : element .getAnnotationMirrors ()) {
19
23
final var type = annotationMirror .getAnnotationType ().asElement ().asType ().toString ();
20
24
if (type .startsWith ("io.avaje.inject.Assist" )) {
21
25
continue ;
22
26
}
23
- final String annotationName = annotationMirror .getAnnotationType ().toString ();
24
- final StringBuilder sb = new StringBuilder (indent ).append ("@" ).append (annotationName ).append ("(" );
25
- boolean first = true ;
26
-
27
- for (final var entry : annotationMirror .getElementValues ().entrySet ()) {
28
- if (!first ) {
29
- sb .append (", " );
30
- }
31
- sb .append (entry .getKey ().getSimpleName ()).append ("=" );
32
- writeVal (sb , entry .getValue ());
33
- first = false ;
34
- }
35
27
36
- sb .append (")" );
37
- final String annotationString = sb .toString ();
28
+ final String annotationString = toAnnotationString (indent , annotationMirror , false );
38
29
writer .append (annotationString );
39
30
40
31
if (newLines ) {
@@ -45,8 +36,41 @@ public static void copyAnnotations(Append writer, Element element, String indent
45
36
}
46
37
}
47
38
39
+ static String toSimpleAnnotationString (AnnotationMirror annotationMirror ) {
40
+ return Util .trimAnnotationString (toAnnotationString ("" , annotationMirror , true )).substring (1 );
41
+ }
42
+
43
+ static String toAnnotationString (String indent , AnnotationMirror annotationMirror , boolean simpleEnums ) {
44
+ final String annotationName = annotationMirror .getAnnotationType ().toString ();
45
+
46
+ final StringBuilder sb = new StringBuilder (indent ).append ("@" ).append (annotationName ).append ("(" );
47
+ boolean first = true ;
48
+
49
+ for (final var entry : sortedValues (annotationMirror )) {
50
+ if (!first ) {
51
+ sb .append (", " );
52
+ }
53
+ sb .append (entry .getKey ().getSimpleName ()).append ("=" );
54
+ writeVal (sb , entry .getValue (), simpleEnums );
55
+ first = false ;
56
+ }
57
+
58
+ return sb .append (")" ).toString ().replace ("()" , "" );
59
+ }
60
+
61
+ private static List <Entry <? extends ExecutableElement , ? extends AnnotationValue >> sortedValues (AnnotationMirror annotationMirror ) {
62
+ return APContext .elements ().getElementValuesWithDefaults (annotationMirror ).entrySet ().stream ()
63
+ .sorted (AnnotationCopier ::compareBySimpleName )
64
+ .collect (toList ());
65
+ }
66
+
67
+ private static int compareBySimpleName (Entry <? extends ExecutableElement , ? extends AnnotationValue > entry1 ,
68
+ Entry <? extends ExecutableElement , ? extends AnnotationValue > entry2 ) {
69
+ return entry1 .getKey ().getSimpleName ().toString ().compareTo (entry2 .getKey ().getSimpleName ().toString ());
70
+ }
71
+
48
72
@ SuppressWarnings ("unchecked" )
49
- private static void writeVal (final StringBuilder sb , final AnnotationValue annotationValue ) {
73
+ private static void writeVal (final StringBuilder sb , final AnnotationValue annotationValue , boolean simpleEnums ) {
50
74
final var value = annotationValue .getValue ();
51
75
if (value instanceof List ) {
52
76
// handle array values
@@ -56,7 +80,7 @@ private static void writeVal(final StringBuilder sb, final AnnotationValue annot
56
80
if (!first ) {
57
81
sb .append (", " );
58
82
}
59
- writeVal (sb , listValue );
83
+ writeVal (sb , listValue , simpleEnums );
60
84
first = false ;
61
85
}
62
86
sb .append ("}" );
@@ -65,7 +89,8 @@ private static void writeVal(final StringBuilder sb, final AnnotationValue annot
65
89
// Handle enum values
66
90
final var element = (VariableElement ) value ;
67
91
final var type = element .asType ();
68
- sb .append (type .toString () + "." + element );
92
+ final var str = simpleEnums ? element : type .toString () + "." + element ;
93
+ sb .append (str );
69
94
70
95
} else if (value instanceof AnnotationMirror ) {
71
96
// handle annotation values
@@ -74,12 +99,12 @@ private static void writeVal(final StringBuilder sb, final AnnotationValue annot
74
99
sb .append ("@" ).append (annotationName ).append ("(" );
75
100
boolean first = true ;
76
101
77
- for (final var entry : mirror . getElementValues (). entrySet ( )) {
102
+ for (final var entry : sortedValues ( mirror )) {
78
103
if (!first ) {
79
104
sb .append (", " );
80
105
}
81
106
sb .append (entry .getKey ().getSimpleName ()).append ("=" );
82
- writeVal (sb , entry .getValue ());
107
+ writeVal (sb , entry .getValue (), simpleEnums );
83
108
first = false ;
84
109
}
85
110
sb .append (")" );
0 commit comments