3
3
4
4
import com .github .therapi .runtimejavadoc .BaseJavadoc ;
5
5
import com .github .therapi .runtimejavadoc .ClassJavadoc ;
6
+ import com .github .therapi .runtimejavadoc .FieldJavadoc ;
6
7
import com .github .therapi .runtimejavadoc .MethodJavadoc ;
7
8
import com .github .therapi .runtimejavadoc .RuntimeJavadoc ;
8
9
14
15
import java .util .ArrayList ;
15
16
import java .util .Arrays ;
16
17
import java .util .Collection ;
17
- import java .util .Collections ;
18
18
import java .util .Enumeration ;
19
19
import java .util .HashMap ;
20
20
import java .util .List ;
21
21
import java .util .Map ;
22
+ import java .util .Map .Entry ;
22
23
import java .util .Objects ;
23
24
import java .util .Optional ;
24
- import java .util .WeakHashMap ;
25
25
import java .util .function .BiFunction ;
26
+ import java .util .function .Supplier ;
26
27
import java .util .jar .JarEntry ;
27
28
import java .util .jar .JarFile ;
28
29
import java .util .stream .Collectors ;
29
30
import java .util .stream .Stream ;
30
31
31
32
import org .scijava .discovery .Discoverer ;
32
33
import org .scijava .discovery .Discovery ;
34
+ import org .scijava .parse2 .ParseService ;
33
35
34
36
public class TherapiDiscoverer implements Discoverer {
35
37
38
+ private ParseService parser ;
39
+
40
+ public TherapiDiscoverer (ParseService parser ) {
41
+ this .parser = parser ;
42
+ }
43
+
36
44
@ Override
37
45
public List <Discovery <AnnotatedElement >> elementsTaggedWith (String tagType ) {
38
46
// combine class and module path resources into a single list
@@ -144,7 +152,6 @@ private static Class<?> getClass(String name) throws ClassNotFoundException {
144
152
* <p>
145
153
* Forked from SciJava Common's Context class.
146
154
*
147
- * @implNote test
148
155
* @see Thread#getContextClassLoader()
149
156
* @see ClassLoader#getSystemClassLoader()
150
157
*/
@@ -183,116 +190,89 @@ private static List<String> getJarContent(String jarPath) throws IOException {
183
190
return content ;
184
191
}
185
192
186
- private ThreadLocal <WeakHashMap <BaseJavadoc , String >> funcJavadocMap =
187
- new ThreadLocal <>()
188
- {
189
-
190
- @ Override
191
- protected WeakHashMap <BaseJavadoc , String > initialValue () {
192
- return new WeakHashMap <>();
193
- }
194
-
195
- };
196
-
197
- BiFunction <BaseJavadoc , String , Optional <String >> getTag = (javadoc ,
193
+ private BiFunction <BaseJavadoc , String , Optional <String >> getTag = (javadoc ,
198
194
tagType ) -> {
199
195
return javadoc .getOther ().stream () //
200
196
.filter (m -> m .getName ().equals ("implNote" ) && m .getComment ().toString ()
201
197
.startsWith (tagType )).map (m -> m .getComment ().toString ()).findFirst ();
202
-
203
198
};
204
199
205
- private ThreadLocal <WeakHashMap <MethodJavadoc , String >> methodJavadocMap =
206
- new ThreadLocal <>()
207
- {
208
-
209
- @ Override
210
- protected WeakHashMap <MethodJavadoc , String > initialValue () {
211
- return new WeakHashMap <>();
212
- }
213
-
214
- };
215
-
216
- private final BiFunction <Map .Entry <BaseJavadoc , String >, Field [], Discovery <AnnotatedElement >> fieldFinder =
217
- (e , fields ) -> {
218
- Optional <Field > taggedField = Arrays .stream (fields ).filter (field -> e
219
- .getKey ().getName ().equals (field .getName ())).findAny (); //
220
- if (taggedField .isEmpty ()) return null ;
221
- return new Discovery <>(taggedField .get (), e .getValue ());
222
- };
223
-
224
- private final BiFunction <Map .Entry <ClassJavadoc , String >, String , List <Discovery <AnnotatedElement >>> taggedFieldFinder =
225
- (entry , tagType ) -> {
226
- // find each field with the tag in the given Class
227
- funcJavadocMap .get ().clear ();
228
- entry .getKey ().getFields ().parallelStream () //
229
- .forEach (j -> {
230
- Optional <String > tag = getTag .apply (j , tagType );
231
- if (tag .isPresent ()) {
232
- funcJavadocMap .get ().put (j , tag .get ());
233
- }
200
+ private Map <String , ?> itemsFromTag (String tagType , String tag ) {
201
+ String tagBody = tag .substring (tag .indexOf (tagType ) + tagType .length ()).trim ();
202
+ System .out .println ("Parser: " + parser );
203
+ System .out .println ("Tag Body: " + tagBody );
204
+ return parser .parse (tagBody .replaceAll ("\\ s+" ,"" ), true ).asMap ();
205
+ }
234
206
235
- });
236
- if (funcJavadocMap .get ().isEmpty ()) return Collections .emptyList ();
207
+ private Discovery <AnnotatedElement > mapFieldToDiscovery (FieldJavadoc javadoc ,
208
+ String tagType , Field [] fields )
209
+ {
210
+ Optional <String > tag = getTag .apply (javadoc , tagType );
211
+ if (tag .isEmpty ()) return null ;
212
+
213
+ Optional <Field > taggedField = Arrays .stream (fields ) //
214
+ .filter (field -> javadoc .getName ().equals (field .getName ())) //
215
+ .findAny ();
216
+ if (taggedField .isEmpty ()) return null ;
217
+ Supplier <Map <String , ?>> tagOptions = () -> itemsFromTag (tagType , tag
218
+ .get ());
219
+ return new Discovery <>(taggedField .get (), tagType , tagOptions );
220
+ }
237
221
238
- // match each tagged FieldJavadoc with the Field it was generated from
239
- Field [] fields ;
240
- try {
241
- fields = getClass (entry .getValue ()).getDeclaredFields ();
242
- }
243
- catch (ClassNotFoundException exc ) {
244
- return Collections .emptyList ();
245
- }
246
- List <Discovery <AnnotatedElement >> taggedClassFields = funcJavadocMap .get ()
247
- .entrySet ().parallelStream () //
248
- .map (e -> fieldFinder .apply (e , fields )) //
222
+ private List <Discovery <AnnotatedElement >> fieldsToDiscoveries
223
+ (Map .Entry <ClassJavadoc , String > entry , String tagType ) {
224
+ Field [] fields = extractDeclaredFields (entry );
225
+ // stream FieldJavadocs of the given ClassJavadoc
226
+ return entry .getKey ().getFields ().parallelStream () //
227
+ .map (j -> mapFieldToDiscovery (j , tagType , fields )) //
249
228
.filter (Objects ::nonNull ) //
250
229
.collect (Collectors .toList ());
251
- return taggedClassFields ;
252
-
253
- };
230
+ }
254
231
255
- private final BiFunction <Map .Entry <MethodJavadoc , String >, Method [], Discovery <AnnotatedElement >> methodFinder =
256
- (e , methods ) -> {
257
- Optional <Method > taggedMethod = Arrays .stream (methods ).filter (m -> e
258
- .getKey ().matches (m )).findAny (); //
259
- if (taggedMethod .isEmpty ()) return null ;
260
- return new Discovery <>(taggedMethod .get (), e .getValue ());
261
- };
232
+ private Discovery <AnnotatedElement > mapMethodToDiscovery (
233
+ MethodJavadoc javadoc , String tagType , Method [] methods )
234
+ {
235
+ Optional <String > tag = getTag .apply (javadoc , tagType );
236
+ if (tag .isEmpty ()) return null ;
237
+
238
+ Optional <Method > taggedMethod = Arrays .stream (methods ).filter (m -> javadoc
239
+ .matches (m )).findAny (); //
240
+ if (taggedMethod .isEmpty ()) return null ;
241
+ Supplier <Map <String , ?>> tagOptions = () -> itemsFromTag (tagType , tag
242
+ .get ());
243
+ return new Discovery <>(taggedMethod .get (), tagType , tagOptions );
244
+ }
262
245
263
246
/**
264
247
* Using a string {@code className}, finds a list of tagged methods
265
248
*/
266
- private final BiFunction <Map .Entry <ClassJavadoc , String >, String , List <Discovery <AnnotatedElement >>> taggedMethodFinder =
267
- (entry , tagType ) -> {
268
- // finds each tagged method in the Class
269
- methodJavadocMap .get ().clear ();
270
- entry .getKey ().getMethods ().parallelStream () //
271
- .forEach (j -> {
272
- Optional <String > tag = getTag .apply (j , tagType );
273
- if (tag .isPresent ()) {
274
- methodJavadocMap .get ().put (j , tag .get ());
275
- }
276
-
277
- });
278
- if (methodJavadocMap .get ().isEmpty ()) return Collections .emptyList ();
249
+ private List <Discovery <AnnotatedElement >> methodsToDiscoveries (Map .Entry <ClassJavadoc , String > entry , String tagType ) {
250
+ Method [] methods = extractDeclaredMethods (entry );
279
251
280
- // maps each MethodJavadoc to the method it was scraped from
281
- Method [] methods ;
282
- try {
283
- methods = getClass (entry .getValue ()).getDeclaredMethods ();
284
- }
285
- catch (ClassNotFoundException exc ) {
286
- return Collections .emptyList ();
287
- }
288
- List <Discovery <AnnotatedElement >> taggedClassMethods = methodJavadocMap
289
- .get ().entrySet ().parallelStream () //
290
- .map (e -> methodFinder .apply (e , methods )) //
252
+ // stream MethodJavadocs of the given ClassJavadoc
253
+ return entry .getKey ().getMethods ().parallelStream () //
254
+ .map (j -> mapMethodToDiscovery (j , tagType , methods )) //
291
255
.filter (Objects ::nonNull ) //
292
256
.collect (Collectors .toList ());
293
- return taggedClassMethods ;
257
+ }
294
258
295
- };
259
+ private Method [] extractDeclaredMethods (Entry <ClassJavadoc , String > entry ) {
260
+ try {
261
+ return getClass (entry .getValue ()).getDeclaredMethods ();
262
+ }
263
+ catch (ClassNotFoundException exc ) {
264
+ return new Method [0 ];
265
+ }
266
+ }
267
+
268
+ private Field [] extractDeclaredFields (Entry <ClassJavadoc , String > entry ) {
269
+ try {
270
+ return getClass (entry .getValue ()).getDeclaredFields ();
271
+ }
272
+ catch (ClassNotFoundException exc ) {
273
+ return new Field [0 ];
274
+ }
275
+ }
296
276
297
277
private List <Discovery <AnnotatedElement >> discoverTaggedClasses (
298
278
String tagType , Map <ClassJavadoc , String > javadocData )
@@ -309,7 +289,8 @@ private List<Discovery<AnnotatedElement>> discoverTaggedClasses(
309
289
Class <?> taggedClass = getClass (e .getValue ());
310
290
Optional <String > tag = getTag .apply (e .getKey (), tagType );
311
291
if (tag .isEmpty ()) return null ;
312
- return new Discovery <>(taggedClass , tag .get ());
292
+ Supplier <Map <String , ?>> tagOptions = () -> itemsFromTag (tagType , e .getValue ());
293
+ return new Discovery <>(taggedClass , tagType , tagOptions );
313
294
}
314
295
catch (ClassNotFoundException exc ) {
315
296
return null ;
@@ -321,7 +302,7 @@ private List<Discovery<AnnotatedElement>> discoverTaggedFields(String tagType,
321
302
Map <ClassJavadoc , String > javadocData )
322
303
{
323
304
return javadocData .entrySet ().parallelStream () //
324
- .map (e -> taggedFieldFinder . apply (e , tagType )) //
305
+ .map (e -> fieldsToDiscoveries (e , tagType )) //
325
306
.flatMap (list -> list .parallelStream ()) //
326
307
.collect (Collectors .toList ());
327
308
}
@@ -330,7 +311,7 @@ private List<Discovery<AnnotatedElement>> discoverTaggedMethods(
330
311
String tagType , Map <ClassJavadoc , String > javadocData )
331
312
{
332
313
return javadocData .entrySet ().parallelStream () //
333
- .map (e -> taggedMethodFinder . apply (e , tagType )) //
314
+ .map (e -> methodsToDiscoveries (e , tagType )) //
334
315
.flatMap (list -> list .parallelStream ()) //
335
316
.collect (Collectors .toList ());
336
317
}
0 commit comments