3
3
import io .avaje .inject .Bean ;
4
4
import io .avaje .inject .Primary ;
5
5
import io .avaje .inject .Secondary ;
6
-
7
6
import jakarta .inject .Inject ;
8
7
import jakarta .inject .Named ;
9
- import jakarta .inject .Qualifier ;
10
- import javax .lang .model .element .AnnotationMirror ;
11
- import javax .lang .model .element .Element ;
12
- import javax .lang .model .element .ElementKind ;
13
- import javax .lang .model .element .ExecutableElement ;
14
- import javax .lang .model .element .TypeElement ;
15
- import javax .lang .model .type .DeclaredType ;
16
- import javax .lang .model .type .TypeMirror ;
17
- import java .util .ArrayList ;
18
- import java .util .Collections ;
19
- import java .util .List ;
20
- import java .util .Set ;
21
- import java .util .TreeSet ;
8
+
9
+ import javax .lang .model .element .*;
10
+ import java .util .*;
22
11
23
12
class BeanReader {
24
13
@@ -32,31 +21,20 @@ class BeanReader {
32
21
private String name ;
33
22
34
23
private MethodReader injectConstructor ;
35
-
36
24
private final List <MethodReader > otherConstructors = new ArrayList <>();
37
-
38
25
private final List <MethodReader > factoryMethods = new ArrayList <>();
39
26
40
27
private Element postConstructMethod ;
41
-
42
28
private Element preDestroyMethod ;
43
29
44
30
private final List <FieldReader > injectFields = new ArrayList <>();
45
-
46
31
private final List <MethodReader > injectMethods = new ArrayList <>();
47
-
48
- private final List <String > interfaceTypes = new ArrayList <>();
49
-
50
- private String addForType ;
51
-
52
32
private final Set <String > importTypes = new TreeSet <>();
53
-
54
33
private final BeanRequestParams requestParams ;
55
34
35
+ private final TypeReader typeReader ;
56
36
private MethodReader constructor ;
57
37
58
- private String registrationTypes ;
59
-
60
38
private boolean writtenToFile ;
61
39
62
40
/**
@@ -72,6 +50,7 @@ class BeanReader {
72
50
this .shortName = shortName (beanType );
73
51
this .context = context ;
74
52
this .requestParams = new BeanRequestParams (type );
53
+ this .typeReader = new TypeReader (beanType , context , importTypes );
75
54
init ();
76
55
}
77
56
@@ -81,55 +60,11 @@ public String toString() {
81
60
}
82
61
83
62
private void init () {
84
- StringBuilder sb = new StringBuilder ();
85
-
86
- for (TypeMirror anInterface : beanType .getInterfaces ()) {
87
- String type = Util .unwrapProvider (anInterface .toString ());
88
- if (Constants .isBeanLifecycle (type )) {
89
- beanLifeCycle = true ;
90
- } else if (type .indexOf ('.' ) == -1 ) {
91
- context .logWarn ("skip when no package on interface " + type );
92
- } else {
93
- interfaceTypes .add (type );
94
- if (!GenericType .isGeneric (type )) {
95
- importTypes .add (type );
96
- sb .append (", " ).append (Util .shortName (type )).append (".class" );
97
- }
98
- }
99
- }
100
- if (interfaceTypes .size () == 1 ) {
101
- String ifaceType = interfaceTypes .get (0 );
102
- if (!GenericType .isGeneric (ifaceType )) {
103
- // only register for non-generic interfaces for the moment
104
- addForType = Util .addForInterface (ifaceType );
105
- }
106
- }
107
-
108
- // get class level annotations (that are not Named and Singleton)
109
- for (AnnotationMirror annotationMirror : beanType .getAnnotationMirrors ()) {
110
-
111
- DeclaredType annotationType = annotationMirror .getAnnotationType ();
112
- Qualifier qualifier = annotationType .asElement ().getAnnotation (Qualifier .class );
113
- String annType = annotationType .toString ();
114
- if (qualifier != null ) {
115
- this .name = Util .shortName (annType );
116
- } else if (annType .indexOf ('.' ) == -1 ) {
117
- context .logWarn ("skip when no package on annotation " + annType );
118
- } else {
119
- if (IncludeAnnotations .include (annType )) {
120
- importTypes .add (annType );
121
- sb .append (", " ).append (Util .shortName (annType )).append (".class" );
122
- }
123
- }
124
- }
125
- Named named = beanType .getAnnotation (Named .class );
126
- if (named != null ) {
127
- this .name = named .value ();
128
- }
129
-
63
+ typeReader .process ();
64
+ beanLifeCycle = typeReader .isBeanLifeCycle ();
65
+ name = typeReader .getName ();
130
66
primary = (beanType .getAnnotation (Primary .class ) != null );
131
67
secondary = !primary && (beanType .getAnnotation (Secondary .class ) != null );
132
- registrationTypes = sb .toString ();
133
68
}
134
69
135
70
TypeElement getBeanType () {
@@ -220,18 +155,7 @@ List<MethodReader> getFactoryMethods() {
220
155
}
221
156
222
157
List <String > getInterfaces () {
223
- return interfaceTypes ;
224
- }
225
-
226
- /**
227
- * Return all the interfaces and annotations associated with this bean.
228
- * <p>
229
- * The bean is made a 'member' of the list of beans that implement the interface or have the
230
- * annotation.
231
- * </p>
232
- */
233
- String getInterfacesAndAnnotations () {
234
- return registrationTypes ;
158
+ return typeReader .getInterfaces ();
235
159
}
236
160
237
161
private void readConstructor (Element element ) {
@@ -361,10 +285,8 @@ void buildAddFor(Append writer) {
361
285
if (name != null ) {
362
286
writer .append ("\" %s\" , " , name );
363
287
}
364
- if (addForType != null ) {
365
- writer .append (addForType ).append (".class, " );
366
- }
367
- writer .append (shortName ).append (".class)) {" ).eol ();
288
+ writer .append (typeReader .getTypesRegister ());
289
+ writer .append (")) {" ).eol ();
368
290
}
369
291
370
292
void buildRegister (Append writer ) {
@@ -375,12 +297,12 @@ void buildRegister(Append writer) {
375
297
String flags = primary ? "Primary" : secondary ? "Secondary" : "" ;
376
298
writer .append ("builder.register%s(bean, " , flags );
377
299
if (name == null ) {
378
- writer .append ("null" );
300
+ writer .append ("null, " );
379
301
} else {
380
- writer .append ("\" %s\" " , name );
302
+ writer .append ("\" %s\" , " , name );
381
303
}
382
304
// add interfaces and annotations
383
- writer .append (getInterfacesAndAnnotations ()).append (");" ).eol ();
305
+ writer .append (typeReader . getTypesRegister ()).append (");" ).eol ();
384
306
}
385
307
386
308
void buildAddLifecycle (Append writer ) {
0 commit comments