-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathClassUtils.java
More file actions
914 lines (819 loc) · 31.6 KB
/
Copy pathClassUtils.java
File metadata and controls
914 lines (819 loc) · 31.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2016 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.util;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Useful methods for working with {@link Class} objects and primitive types.
*
* @author Curtis Rueden
*/
public final class ClassUtils {
private ClassUtils() {
// prevent instantiation of utility class
}
/**
* This maps a base class (key1) to a map of annotation classes (key2), which
* then maps to a list of {@link Field} instances, being the set of fields in
* the base class with the specified annotation.
* <p>
* This map serves as a cache, as these annotations should not change at
* runtime and thus can alleviate the frequency field querying.
* </p>
*
* @see <a href="https://github.com/scijava/scijava-common/issues/142">issue
* #142</a>
*/
private static final FieldCache fieldCache = new FieldCache();
/**
* This maps a base class (key1) to a map of annotation classes (key2), which
* then maps to a list of {@link Method} instances, being the set of methods
* in the base class with the specified annotation.
* <p>
* This map serves as a cache, as these annotations should not change at
* runtime and thus can alleviate the frequency of method querying.
* </p>
*
* @see <a href="https://github.com/scijava/scijava-common/issues/142">issue
* #142</a>
*/
private static final MethodCache methodCache = new MethodCache();
// -- Class loading, querying and reflection --
/**
* Loads the class with the given name, using the current thread's context
* class loader, or null if it cannot be loaded.
*
* @param name The name of the class to load.
* @return The loaded class, or null if the class could not be loaded.
* @see #loadClass(String, ClassLoader, boolean)
*/
public static Class<?> loadClass(final String name) {
return loadClass(name, null, true);
}
/**
* Loads the class with the given name, using the specified
* {@link ClassLoader}, or null if it cannot be loaded.
*
* @param name The name of the class to load.
* @param classLoader The class loader with which to load the class; if null,
* the current thread's context class loader will be used.
* @return The loaded class, or null if the class could not be loaded.
* @see #loadClass(String, ClassLoader, boolean)
*/
public static Class<?> loadClass(final String name,
final ClassLoader classLoader)
{
return loadClass(name, classLoader, true);
}
/**
* Loads the class with the given name, using the current thread's context
* class loader.
*
* @param className the name of the class to load
* @param quietly Whether to return {@code null} (rather than throwing
* {@link IllegalArgumentException}) if something goes wrong loading
* the class
* @return The loaded class, or {@code null} if the class could not be loaded
* and the {@code quietly} flag is set.
* @see #loadClass(String, ClassLoader, boolean)
* @throws IllegalArgumentException If the class cannot be loaded and the
* {@code quietly} flag is not set.
*/
public static Class<?> loadClass(final String className,
final boolean quietly)
{
return loadClass(className, null, quietly);
}
/**
* Loads the class with the given name, using the specified
* {@link ClassLoader}, or null if it cannot be loaded.
* <p>
* This method is capable of parsing several different class name syntaxes. In
* particular, array classes (including primitives) represented using either
* square brackets or internal Java array name syntax are supported. Examples:
* </p>
* <ul>
* <li>{@code boolean} is loaded as {@code boolean.class}</li>
* <li>{@code Z} is loaded as {@code boolean.class}</li>
* <li>{@code double[]} is loaded as {@code double[].class}</li>
* <li>{@code string[]} is loaded as {@code java.lang.String.class}</li>
* <li>{@code [F} is loaded as {@code float[].class}</li>
* </ul>
*
* @param name The name of the class to load.
* @param classLoader The class loader with which to load the class; if null,
* the current thread's context class loader will be used.
* @param quietly Whether to return {@code null} (rather than throwing
* {@link IllegalArgumentException}) if something goes wrong loading
* the class
* @return The loaded class, or {@code null} if the class could not be loaded
* and the {@code quietly} flag is set.
* @throws IllegalArgumentException If the class cannot be loaded and the
* {@code quietly} flag is not set.
*/
public static Class<?> loadClass(final String name,
final ClassLoader classLoader, final boolean quietly)
{
// handle primitive types
if (name.equals("Z") || name.equals("boolean")) return boolean.class;
if (name.equals("B") || name.equals("byte")) return byte.class;
if (name.equals("C") || name.equals("char")) return char.class;
if (name.equals("D") || name.equals("double")) return double.class;
if (name.equals("F") || name.equals("float")) return float.class;
if (name.equals("I") || name.equals("int")) return int.class;
if (name.equals("J") || name.equals("long")) return long.class;
if (name.equals("S") || name.equals("short")) return short.class;
if (name.equals("V") || name.equals("void")) return void.class;
// handle built-in class shortcuts
final String className;
if (name.equals("string")) className = "java.lang.String";
else className = name;
// handle source style arrays (e.g.: "java.lang.String[]")
if (name.endsWith("[]")) {
final String elementClassName = name.substring(0, name.length() - 2);
return getArrayClass(loadClass(elementClassName, classLoader));
}
// handle non-primitive internal arrays (e.g.: "[Ljava.lang.String;")
if (name.startsWith("[L") && name.endsWith(";")) {
final String elementClassName = name.substring(2, name.length() - 1);
return getArrayClass(loadClass(elementClassName, classLoader));
}
// handle other internal arrays (e.g.: "[I", "[[I", "[[Ljava.lang.String;")
if (name.startsWith("[")) {
final String elementClassName = name.substring(1);
return getArrayClass(loadClass(elementClassName, classLoader));
}
// load the class!
try {
final ClassLoader cl =
classLoader == null ? Thread.currentThread().getContextClassLoader()
: classLoader;
return cl.loadClass(className);
}
catch (final Throwable t) {
// NB: Do not allow any failure to load the class to crash us.
// Not ClassNotFoundException.
// Not NoClassDefFoundError.
// Not UnsupportedClassVersionError!
if (quietly) return null;
throw new IllegalArgumentException("Cannot load class: " + className, t);
}
}
/**
* Gets the array class corresponding to the given element type.
* <p>
* For example, {@code getArrayClass(double.class)} returns
* {@code double[].class}.
* </p>
*/
public static Class<?> getArrayClass(final Class<?> elementClass) {
if (elementClass == null) return null;
// NB: It appears the reflection API has no built-in way to do this.
// So unfortunately, we must allocate a new object and then inspect it.
try {
return Array.newInstance(elementClass, 0).getClass();
}
catch (final IllegalArgumentException exc) {
return null;
}
}
/** Checks whether a class with the given name exists. */
public static boolean hasClass(final String className) {
return hasClass(className, null);
}
/** Checks whether a class with the given name exists. */
public static boolean hasClass(final String className,
final ClassLoader classLoader)
{
return loadClass(className, classLoader) != null;
}
/**
* Gets the base location of the given class.
* <p>
* If the class is directly on the file system (e.g.,
* "/path/to/my/package/MyClass.class") then it will return the base directory
* (e.g., "/path/to").
* </p>
* <p>
* If the class is within a JAR file (e.g.,
* "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the
* path to the JAR (e.g., "/path/to/my-jar.jar").
* </p>
*
* @param className The name of the class whose location is desired.
* @see FileUtils#urlToFile(URL) to convert the result to a {@link File}.
*/
public static URL getLocation(final String className) {
return getLocation(className, null);
}
/**
* Gets the base location of the given class.
* <p>
* If the class is directly on the file system (e.g.,
* "/path/to/my/package/MyClass.class") then it will return the base directory
* (e.g., "/path/to").
* </p>
* <p>
* If the class is within a JAR file (e.g.,
* "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the
* path to the JAR (e.g., "/path/to/my-jar.jar").
* </p>
*
* @param className The name of the class whose location is desired.
* @param classLoader The class loader to use when loading the class.
* @see FileUtils#urlToFile(URL) to convert the result to a {@link File}.
*/
public static URL getLocation(final String className,
final ClassLoader classLoader)
{
final Class<?> c = loadClass(className, classLoader);
return getLocation(c);
}
/**
* Gets the base location of the given class.
* <p>
* If the class is directly on the file system (e.g.,
* "/path/to/my/package/MyClass.class") then it will return the base directory
* (e.g., "file:/path/to").
* </p>
* <p>
* If the class is within a JAR file (e.g.,
* "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the
* path to the JAR (e.g., "file:/path/to/my-jar.jar").
* </p>
*
* @param c The class whose location is desired.
* @see FileUtils#urlToFile(URL) to convert the result to a {@link File}.
*/
public static URL getLocation(final Class<?> c) {
if (c == null) return null; // could not load the class
// try the easy way first
try {
final URL codeSourceLocation =
c.getProtectionDomain().getCodeSource().getLocation();
if (codeSourceLocation != null) return codeSourceLocation;
}
catch (final SecurityException e) {
// NB: Cannot access protection domain.
}
catch (final NullPointerException e) {
// NB: Protection domain or code source is null.
}
// NB: The easy way failed, so we try the hard way. We ask for the class
// itself as a resource, then strip the class's path from the URL string,
// leaving the base path.
// get the class's raw resource path
final URL classResource = c.getResource(c.getSimpleName() + ".class");
if (classResource == null) return null; // cannot find class resource
final String url = classResource.toString();
final String suffix = c.getCanonicalName().replace('.', '/') + ".class";
if (!url.endsWith(suffix)) return null; // weird URL
// strip the class's path from the URL string
final String base = url.substring(0, url.length() - suffix.length());
String path = base;
// remove the "jar:" prefix and "!/" suffix, if present
if (path.startsWith("jar:")) path = path.substring(4, path.length() - 2);
try {
return new URL(path);
}
catch (final MalformedURLException e) {
e.printStackTrace();
return null;
}
}
/**
* Gets the given class's {@link Method}s marked with the annotation of the
* specified class.
* <p>
* Unlike {@link Class#getMethods()}, the result will include any non-public
* methods, including methods defined in supertypes of the given class.
* </p>
*
* @param c The class to scan for annotated methods.
* @param annotationClass The type of annotation for which to scan.
* @return A new list containing all methods with the requested annotation.
*/
public static <A extends Annotation> List<Method> getAnnotatedMethods(
final Class<?> c, final Class<A> annotationClass)
{
List<Method> methods = methodCache.getList(c, annotationClass);
if (methods == null) {
methods = new ArrayList<>();
getAnnotatedMethods(c, annotationClass, methods);
}
return methods;
}
/**
* Gets the given class's {@link Method}s marked with the annotation of the
* specified class.
* <p>
* Unlike {@link Class#getMethods()}, the result will include any non-public
* methods, including methods defined in supertypes of the given class.
* </p>
*
* @param c The class to scan for annotated methods.
* @param annotationClass The type of annotation for which to scan.
* @param methods The list to which matching methods will be added.
*/
public static <A extends Annotation> void
getAnnotatedMethods(final Class<?> c, final Class<A> annotationClass,
final List<Method> methods)
{
List<Method> cachedMethods = methodCache.getList(c, annotationClass);
if (cachedMethods == null) {
final Query query = new Query();
query.put(annotationClass, Method.class);
cacheAnnotatedObjects(c, query);
cachedMethods = methodCache.getList(c, annotationClass);
}
if (cachedMethods != null) methods.addAll(cachedMethods);
}
/**
* Gets the given class's {@link Field}s marked with the annotation of the
* specified class.
* <p>
* Unlike {@link Class#getFields()}, the result will include any non-public
* fields, including fields defined in supertypes of the given class.
* </p>
*
* @param c The class to scan for annotated fields.
* @param annotationClass The type of annotation for which to scan.
* @return A new list containing all fields with the requested annotation.
*/
public static <A extends Annotation> List<Field> getAnnotatedFields(
final Class<?> c, final Class<A> annotationClass)
{
List<Field> fields = fieldCache.getList(c, annotationClass);
if (fields == null) {
fields = new ArrayList<>();
getAnnotatedFields(c, annotationClass, fields);
}
return fields;
}
/**
* Gets the given class's {@link Field}s marked with the annotation of the
* specified class.
* <p>
* Unlike {@link Class#getFields()}, the result will include any non-public
* fields, including fields defined in supertypes of the given class.
* </p>
*
* @param c The class to scan for annotated fields.
* @param annotationClass The type of annotation for which to scan.
* @param fields The list to which matching fields will be added.
*/
public static <A extends Annotation> void getAnnotatedFields(
final Class<?> c, final Class<A> annotationClass, final List<Field> fields)
{
List<Field> cachedFields = fieldCache.getList(c, annotationClass);
if (cachedFields == null) {
final Query query = new Query();
query.put(annotationClass, Field.class);
cacheAnnotatedObjects(c, query);
cachedFields = fieldCache.getList(c, annotationClass);
}
fields.addAll(cachedFields);
}
/**
* This method scans the provided class, its superclasses and interfaces for
* all supported {@link Annotation} : {@link AnnotatedElement} pairs.
* These are then cached to remove the need for future queries.
* <p>
* By combining multiple {@code Annotation : AnnotatedElement} pairs in one
* query, we can limit the number of times a class's superclass and interface
* hierarchy are traversed.
* </p>
*
* @param scannedClass Class to scan
* @param query Pairs of {@link Annotation} and {@link AnnotatedElement}s to
* discover.
*/
public static void cacheAnnotatedObjects(final Class<?> scannedClass,
final Query query)
{
// Only allow one thread at a time to populate cached objects for a given
// class. This lock is technically overkill - the minimal lock would be
// on the provided Query contents + class. Devising a way to obtain this
// lock could be useful - as if Query A and Query B were executed on the
// same class by different threads, there are three scenarios:
// 1) intersection of A + B is empty - then they can run on separate threads
// 2) A == B - whichever was received second must wait for the first to
// finish.
// 3) A != B and intersection of A + B is not empty - the intersection subset
// can be safely performed on a separate thread, but the later query must
// still wait for the earlier query to complete.
//
// NB: an alternative would be to update the getAnnotatedxxx methods to
// return Sets instead of Lists. Then threads can pretty much go nuts
// as long as you double lock the Set creation in a synchronized block.
//
// NB: another possibility would be to keep this synchronized entry point
// but divide the work for each Query into asynchronous blocks. However, it
// has not been investigated how much of a performance boost that would
// provide as it would then cause multiple traversals of the class hierarchy
// - which is exactly what the Query notation was created to avoid.
synchronized (scannedClass) {
// NB: The java.lang.Object class does not have any annotated methods.
// And even if it did, it definitely does not have any methods annotated
// with SciJava annotations such as org.scijava.event.EventHandler, which
// are the main sorts of methods we are interested in.
if (scannedClass == null || scannedClass == Object.class) return;
// Initialize step - determine which queries are solved
final Set<Class<? extends Annotation>> keysToDrop =
new HashSet<>();
for (final Class<? extends Annotation> annotationClass : query.keySet()) {
// Fields
if (fieldCache.getList(scannedClass, annotationClass) != null) {
keysToDrop.add(annotationClass);
}
else if (methodCache.getList(scannedClass, annotationClass) != null) {
keysToDrop.add(annotationClass);
}
}
// Clean up resolved keys
for (final Class<? extends Annotation> key : keysToDrop) {
query.remove(key);
}
// Stop now if we know all requested information is cached
if (query.isEmpty()) return;
final List<Class<?>> inherited = new ArrayList<>();
// cache all parents recursively
final Class<?> superClass = scannedClass.getSuperclass();
if (superClass != null) {
// Recursive step
cacheAnnotatedObjects(superClass, new Query(query));
inherited.add(superClass);
}
// cache all interfaces recursively
for (final Class<?> ifaceClass : scannedClass.getInterfaces()) {
// Recursive step
cacheAnnotatedObjects(ifaceClass, new Query(query));
inherited.add(ifaceClass);
}
// Populate supported objects for scanned class
for (final Class<? extends Annotation> annotationClass : query.keySet()) {
final Class<? extends AnnotatedElement> objectClass =
query.get(annotationClass);
try {
// Methods
if (Method.class.isAssignableFrom(objectClass)) {
populateCache(scannedClass, inherited, annotationClass, methodCache,
scannedClass.getDeclaredMethods());
}
// Fields
else if (Field.class.isAssignableFrom(objectClass)) {
populateCache(scannedClass, inherited, annotationClass, fieldCache,
scannedClass.getDeclaredFields());
}
}
catch (final Throwable t) {
// NB: No action needed?
}
}
}
}
/**
* Gets the specified field of the given class, or null if it does not exist.
*/
public static Field getField(final String className, final String fieldName) {
return getField(loadClass(className), fieldName);
}
/**
* Gets the specified field of the given class, or null if it does not exist.
*/
public static Field getField(final Class<?> c, final String fieldName) {
if (c == null) return null;
try {
return c.getDeclaredField(fieldName);
}
catch (final NoSuchFieldException e) {
return null;
}
}
/**
* Gets the given field's value of the specified object instance, or null if
* the value cannot be obtained.
*/
public static Object getValue(final Field field, final Object instance) {
try {
field.setAccessible(true);
return field.get(instance);
}
catch (final IllegalAccessException e) {
return null;
}
}
/**
* Sets the given field's value of the specified object instance.
*
* @throws IllegalArgumentException if the value cannot be set.
*/
// FIXME: Move to ConvertService and deprecate this signature.
public static void setValue(final Field field, final Object instance,
final Object value)
{
try {
field.setAccessible(true);
final Object compatibleValue;
if (value == null || field.getType().isInstance(value)) {
// the given value is compatible with the field
compatibleValue = value;
}
else {
// the given value needs to be converted to a compatible type
final Type fieldType =
GenericUtils.getFieldType(field, instance.getClass());
@SuppressWarnings("deprecation")
final Object convertedValue = ConversionUtils.convert(value, fieldType);
compatibleValue = convertedValue;
}
field.set(instance, compatibleValue);
}
catch (final IllegalAccessException e) {
throw new IllegalArgumentException("No access to field: " +
field.getName(), e);
}
}
// -- Type querying --
public static boolean isBoolean(final Class<?> type) {
return type == boolean.class || Boolean.class.isAssignableFrom(type);
}
public static boolean isByte(final Class<?> type) {
return type == byte.class || Byte.class.isAssignableFrom(type);
}
public static boolean isCharacter(final Class<?> type) {
return type == char.class || Character.class.isAssignableFrom(type);
}
public static boolean isDouble(final Class<?> type) {
return type == double.class || Double.class.isAssignableFrom(type);
}
public static boolean isFloat(final Class<?> type) {
return type == float.class || Float.class.isAssignableFrom(type);
}
public static boolean isInteger(final Class<?> type) {
return type == int.class || Integer.class.isAssignableFrom(type);
}
public static boolean isLong(final Class<?> type) {
return type == long.class || Long.class.isAssignableFrom(type);
}
public static boolean isShort(final Class<?> type) {
return type == short.class || Short.class.isAssignableFrom(type);
}
public static boolean isNumber(final Class<?> type) {
return Number.class.isAssignableFrom(type) || type == byte.class ||
type == double.class || type == float.class || type == int.class ||
type == long.class || type == short.class;
}
public static boolean isText(final Class<?> type) {
return String.class.isAssignableFrom(type) || isCharacter(type);
}
// -- Comparison --
/**
* Compares two {@link Class} objects using their fully qualified names.
* <p>
* Note: this method provides a natural ordering that may be inconsistent with
* equals. Specifically, two unequal classes may return 0 when compared in
* this fashion if they represent the same class loaded using two different
* {@link ClassLoader}s. Hence, if this method is used as a basis for
* implementing {@link Comparable#compareTo} or
* {@link java.util.Comparator#compare}, that implementation may want to
* impose logic beyond that of this method, for breaking ties, if a total
* ordering consistent with equals is always required.
* </p>
*
* @see org.scijava.Priority#compare(org.scijava.Prioritized,
* org.scijava.Prioritized)
*/
public static int compare(final Class<?> c1, final Class<?> c2) {
if (c1 == c2) return 0;
final String name1 = c1 == null ? null : c1.getName();
final String name2 = c2 == null ? null : c2.getName();
return MiscUtils.compare(name1, name2);
}
// -- Helper methods --
/**
* Populates the cache of annotated elements for a particular class by looking
* for all inherited and declared instances annotated with the specified
* annotationClass. If no matches are found, an empty mapping is created to
* mark this class complete.
*/
private static <T extends AnnotatedElement> void populateCache(
final Class<?> scannedClass, final List<Class<?>> inherited,
final Class<? extends Annotation> annotationClass,
final CacheMap<T> cacheMap, final T[] declaredElements)
{
// Add inherited elements
for (final Class<?> inheritedClass : inherited) {
final List<T> annotatedElements =
cacheMap.getList(inheritedClass, annotationClass);
if (annotatedElements != null && !annotatedElements.isEmpty()) {
final List<T> scannedElements =
cacheMap.makeList(scannedClass, annotationClass);
scannedElements.addAll(annotatedElements);
}
}
// Add declared elements
if (declaredElements != null && declaredElements.length > 0) {
List<T> scannedElements = null;
for (final T t : declaredElements) {
if (t.getAnnotation(annotationClass) != null) {
if (scannedElements == null) {
scannedElements = cacheMap.makeList(scannedClass, annotationClass);
}
scannedElements.add(t);
}
}
}
// If there were no elements for this query, map an empty
// list to mark the query complete
if (cacheMap.getList(scannedClass, annotationClass) == null) {
cacheMap.putList(scannedClass, annotationClass, Collections
.<T> emptyList());
}
}
// -- Deprecated methods --
/** @deprecated use {@link ConversionUtils#convert(Object, Class)} */
@Deprecated
public static <T> T convert(final Object value, final Class<T> type) {
return ConversionUtils.convert(value, type);
}
/** @deprecated use {@link ConversionUtils#canConvert(Class, Class)} */
@Deprecated
public static boolean canConvert(final Class<?> c, final Class<?> type) {
return ConversionUtils.canConvert(c, type);
}
/** @deprecated use {@link ConversionUtils#canConvert(Object, Class)} */
@Deprecated
public static boolean canConvert(final Object value, final Class<?> type) {
return ConversionUtils.canConvert(value, type);
}
/** @deprecated use {@link ConversionUtils#cast(Object, Class)} */
@Deprecated
public static <T> T cast(final Object obj, final Class<T> type) {
return ConversionUtils.cast(obj, type);
}
/** @deprecated use {@link ConversionUtils#canCast(Class, Class)} */
@Deprecated
public static boolean canCast(final Class<?> c, final Class<?> type) {
return ConversionUtils.canCast(c, type);
}
/** @deprecated use {@link ConversionUtils#canCast(Object, Class)} */
@Deprecated
public static boolean canCast(final Object obj, final Class<?> type) {
return ConversionUtils.canCast(obj, type);
}
/** @deprecated use {@link ConversionUtils#getNonprimitiveType(Class)} */
@Deprecated
public static <T> Class<T> getNonprimitiveType(final Class<T> type) {
return ConversionUtils.getNonprimitiveType(type);
}
/** @deprecated use {@link ConversionUtils#getNullValue(Class)} */
@Deprecated
public static <T> T getNullValue(final Class<T> type) {
return ConversionUtils.getNullValue(type);
}
/** @deprecated use {@link GenericUtils#getFieldClasses(Field, Class)} */
@Deprecated
public static List<Class<?>> getTypes(final Field field, final Class<?> type)
{
return GenericUtils.getFieldClasses(field, type);
}
/** @deprecated use {@link GenericUtils#getFieldType(Field, Class)} */
@Deprecated
public static Type getGenericType(final Field field, final Class<?> type) {
return GenericUtils.getFieldType(field, type);
}
// -- Helper classes --
/**
* Convenience class for a {@link CacheMap} that stores annotated
* {@link Field}s.
*/
private static class FieldCache extends CacheMap<Field> {
// Trivial subclass to narrow generic params
}
/**
* Convenience class for a {@link CacheMap} that stores annotated
* {@link Method}s.
*/
private static class MethodCache extends CacheMap<Method> {
// Trivial subclass to narrow generic params
}
/**
* Convenience class for {@code Map > Map > List} hierarchy. Cleans up
* generics and contains helper methods for traversing the two map levels.
* <p>
* The intent for this class is to allow subclasses to specify the generic
* parameter ultimately referenced by the at the end of these maps.
* </p>
* <p>
* The first map key is a base class, presumably with various types of
* annotations. The second map key is the annotation class, for example
* {@link Method} or {@link Field}. The list then contains all instances of
* the annotated type within the original base class.
* </p>
*
* @param <T> - The type of {@link AnnotatedElement} contained by the
* {@link List} ultimately referenced by these {@link Map}s
*/
private static class CacheMap<T extends AnnotatedElement> extends
HashMap<Class<?>, Map<Class<? extends Annotation>, List<T>>>
{
/**
* @param c Base class of interest
* @param annotationClass {@link Annotation} type within the base class
* @return A {@link List} of instances in the base class with the specified
* {@link Annotation}, or null if a cached list does not exist.
*/
public List<T> getList(final Class<?> c,
final Class<? extends Annotation> annotationClass)
{
List<T> annotatedFields = null;
final Map<Class<? extends Annotation>, List<T>> annotationTypes = get(c);
if (annotationTypes != null) {
annotatedFields = annotationTypes.get(annotationClass);
}
return annotatedFields;
}
/**
* Creates a {@code base class > annotation > list of elements} mapping to
* the provided list, creating the intermediate map if needed.
*
* @param c Base class of interest
* @param annotationClass {@link Annotation} type of interest
* @param annotatedElements List of {@link AnnotatedElement}s to map
*/
public void putList(final Class<?> c,
final Class<? extends Annotation> annotationClass,
final List<T> annotatedElements)
{
Map<Class<? extends Annotation>, List<T>> map = get(c);
if (map == null) {
map = new HashMap<>();
put(c, map);
}
map.put(annotationClass, annotatedElements);
}
/**
* Generates mappings as in {@link #putList(Class, Class, List)}, but also
* creates the {@link List} if it doesn't already exist. Returns the final
* list at this mapping, for external population.
*
* @param c Base class of interest
* @param annotationClass {@link Annotation} type of interest
* @return Cached list of {@link AnnotatedElement}s in the base class with
* the specified {@link Annotation}.
*/
public List<T> makeList(final Class<?> c,
final Class<? extends Annotation> annotationClass)
{
List<T> elements = getList(c, annotationClass);
if (elements == null) {
elements = new ArrayList<>();
putList(c, annotationClass, elements);
}
return elements;
}
}
}