25
25
26
26
public class ProcessingContext {
27
27
28
- private static PlatformAdapter readAdapter ;
29
- private static Messager messager ;
30
- private static Filer filer ;
31
- private static Elements elements ;
32
- private static Types types ;
33
- private static boolean openApiAvailable ;
34
- private static DocContext docContext ;
35
- private static boolean useComponent ;
36
- private static boolean useJavax ;
37
- private static String diAnnotation ;
28
+ private static ThreadLocal < PlatformAdapter > READ_ADAPTER = new ThreadLocal <>() ;
29
+ private static ThreadLocal < Messager > MESSAGER = new ThreadLocal <>() ;
30
+ private static ThreadLocal < Filer > FILER = new ThreadLocal <>() ;
31
+ private static ThreadLocal < Elements > ELEMENTS = new ThreadLocal <>() ;
32
+ private static ThreadLocal < Types > TYPES = new ThreadLocal <>() ;
33
+ private static ThreadLocal < Boolean > OPENAPI_AVAILABLE = ThreadLocal . withInitial (() -> false ) ;
34
+ private static ThreadLocal < DocContext > DOC_CONTEXT = new ThreadLocal <>() ;
35
+ private static ThreadLocal < Boolean > USE_COMPONENT = ThreadLocal . withInitial (() -> false ) ;
36
+ private static ThreadLocal < Boolean > USE_JAVAX = ThreadLocal . withInitial (() -> false ) ;
37
+ private static ThreadLocal < String > DI_ANNOTATION = new ThreadLocal <>() ;
38
38
39
39
public static void init (ProcessingEnvironment env , PlatformAdapter adapter ) {
40
40
init (env , adapter , true );
41
41
}
42
42
43
- public static void init (ProcessingEnvironment env , PlatformAdapter adapter , boolean generateOpenAPI ) {
44
- readAdapter = adapter ;
45
- messager = env .getMessager ();
46
- filer = env .getFiler ();
47
- elements = env .getElementUtils ();
48
- types = env .getTypeUtils ();
43
+ public static void init (
44
+ ProcessingEnvironment env , PlatformAdapter adapter , boolean generateOpenAPI ) {
45
+ READ_ADAPTER .set (adapter );
46
+ MESSAGER .set (env .getMessager ());
47
+ FILER .set (env .getFiler ());
48
+ ELEMENTS .set (env .getElementUtils ());
49
+ TYPES .set (env .getTypeUtils ());
49
50
50
51
if (generateOpenAPI ) {
51
- openApiAvailable = isTypeAvailable (Constants .OPENAPIDEFINITION );
52
- docContext = new DocContext (env , openApiAvailable );
52
+ OPENAPI_AVAILABLE . set ( isTypeAvailable (Constants .OPENAPIDEFINITION ) );
53
+ DOC_CONTEXT . set ( new DocContext (env , OPENAPI_AVAILABLE . get ()) );
53
54
}
54
55
55
56
final var options = env .getOptions ();
56
57
final var singletonOverride = options .get ("useSingleton" );
57
58
if (singletonOverride != null ) {
58
- useComponent = !Boolean .parseBoolean (singletonOverride );
59
+ USE_COMPONENT . set ( !Boolean .parseBoolean (singletonOverride ) );
59
60
} else {
60
- useComponent = isTypeAvailable (Constants .COMPONENT );
61
+ USE_COMPONENT . set ( isTypeAvailable (Constants .COMPONENT ) );
61
62
}
62
- diAnnotation = useComponent ? "@Component" : "@Singleton" ;
63
+ DI_ANNOTATION . set ( USE_COMPONENT . get () ? "@Component" : "@Singleton" ) ;
63
64
64
65
final var javax = isTypeAvailable (Constants .SINGLETON_JAVAX );
65
66
final var jakarta = isTypeAvailable (Constants .SINGLETON_JAKARTA );
66
67
final var override = env .getOptions ().get ("useJavax" );
67
68
if (override != null || (javax && jakarta )) {
68
- useJavax = Boolean .parseBoolean (override );
69
+ USE_JAVAX . set ( Boolean .parseBoolean (override ) );
69
70
} else {
70
- useJavax = javax ;
71
+ USE_JAVAX . set ( javax ) ;
71
72
}
72
73
}
73
74
@@ -76,81 +77,79 @@ private static boolean isTypeAvailable(String canonicalName) {
76
77
}
77
78
78
79
public static TypeElement typeElement (String canonicalName ) {
79
- return elements .getTypeElement (canonicalName );
80
+ return ELEMENTS . get () .getTypeElement (canonicalName );
80
81
}
81
82
82
83
public static boolean isOpenApiAvailable () {
83
- return openApiAvailable ;
84
+ return OPENAPI_AVAILABLE . get () ;
84
85
}
85
86
86
87
public static boolean useJavax () {
87
- return useJavax ;
88
+ return USE_JAVAX . get () ;
88
89
}
89
90
90
91
public static boolean useComponent () {
91
- return useComponent ;
92
+ return USE_COMPONENT . get () ;
92
93
}
93
94
94
95
public static void logError (Element e , String msg , Object ... args ) {
95
- messager .printMessage (Diagnostic .Kind .ERROR , String .format (msg , args ), e );
96
+ MESSAGER . get () .printMessage (Diagnostic .Kind .ERROR , String .format (msg , args ), e );
96
97
}
97
98
98
- /**
99
- * Create a file writer for the given class name.
100
- */
99
+ /** Create a file writer for the given class name. */
101
100
public static JavaFileObject createWriter (String cls , Element origin ) throws IOException {
102
- return filer .createSourceFile (cls , origin );
101
+ return FILER . get () .createSourceFile (cls , origin );
103
102
}
104
103
105
- /**
106
- * Create a file writer for the META-INF services file.
107
- */
104
+ /** Create a file writer for the META-INF services file. */
108
105
public static FileObject createMetaInfWriter (String target ) throws IOException {
109
- return filer .createResource (StandardLocation .CLASS_OUTPUT , "" , target );
106
+ return FILER . get () .createResource (StandardLocation .CLASS_OUTPUT , "" , target );
110
107
}
111
108
112
109
public static String docComment (Element param ) {
113
- return elements .getDocComment (param );
110
+ return ELEMENTS . get () .getDocComment (param );
114
111
}
115
112
116
113
public static DocContext doc () {
117
- return docContext ;
114
+ return DOC_CONTEXT . get () ;
118
115
}
119
116
120
117
public static Element asElement (TypeMirror typeMirror ) {
121
- return types .asElement (typeMirror );
118
+ return TYPES . get () .asElement (typeMirror );
122
119
}
123
120
124
121
public static TypeMirror asMemberOf (DeclaredType declaredType , Element element ) {
125
- return types .asMemberOf (declaredType , element );
122
+ return TYPES . get () .asMemberOf (declaredType , element );
126
123
}
127
124
128
125
public static List <ExecutableElement > superMethods (Element element , String methodName ) {
126
+ final Types types = TYPES .get ();
129
127
return types .directSupertypes (element .asType ()).stream ()
130
- .filter (type -> !type .toString ().contains ("java.lang.Object" ))
131
- .map (
132
- superType -> {
133
- final var superClass = (TypeElement ) types .asElement (superType );
134
- for (final ExecutableElement method : ElementFilter .methodsIn (elements .getAllMembers (superClass ))) {
135
- if (method .getSimpleName ().contentEquals (methodName )) {
136
- return method ;
137
- }
138
- }
139
- return null ;
140
- })
141
- .filter (Objects ::nonNull )
142
- .collect (Collectors .toList ());
128
+ .filter (type -> !type .toString ().contains ("java.lang.Object" ))
129
+ .map (
130
+ superType -> {
131
+ final var superClass = (TypeElement ) types .asElement (superType );
132
+ for (final ExecutableElement method :
133
+ ElementFilter .methodsIn (ELEMENTS .get ().getAllMembers (superClass ))) {
134
+ if (method .getSimpleName ().contentEquals (methodName )) {
135
+ return method ;
136
+ }
137
+ }
138
+ return null ;
139
+ })
140
+ .filter (Objects ::nonNull )
141
+ .collect (Collectors .toList ());
143
142
}
144
143
145
144
public static PlatformAdapter platform () {
146
- return readAdapter ;
145
+ return READ_ADAPTER . get () ;
147
146
}
148
147
149
148
public static void setPlatform (PlatformAdapter platform ) {
150
- readAdapter = platform ;
149
+ READ_ADAPTER . set ( platform ) ;
151
150
}
152
151
153
152
public static String diAnnotation () {
154
- return diAnnotation ;
153
+ return DI_ANNOTATION . get () ;
155
154
}
156
155
}
0 commit comments