8
8
import com .google .common .base .Joiner ;
9
9
import com .google .common .base .Predicate ;
10
10
import com .google .common .collect .FluentIterable ;
11
+ import com .google .common .collect .ImmutableList ;
11
12
import com .google .common .collect .Lists ;
12
13
import com .squareup .javapoet .AnnotationSpec ;
13
14
import com .squareup .javapoet .ClassName ;
24
25
import java .lang .reflect .InvocationTargetException ;
25
26
import java .lang .reflect .Method ;
26
27
import java .util .ArrayList ;
27
- import java .util .Arrays ;
28
28
import java .util .Collection ;
29
29
import java .util .Collections ;
30
30
import java .util .HashSet ;
48
48
import javax .lang .model .type .TypeMirror ;
49
49
import javax .lang .model .type .TypeVariable ;
50
50
import javax .lang .model .util .ElementFilter ;
51
+ import javax .lang .model .util .Elements ;
51
52
import javax .lang .model .util .Types ;
52
53
import javax .tools .Diagnostic ;
53
54
@@ -62,17 +63,17 @@ final class ProcessorUtil {
62
63
GLIDE_MODULE_PACKAGE_NAME + "." + LIBRARY_GLIDE_MODULE_SIMPLE_NAME ;
63
64
private static final String COMPILER_PACKAGE_NAME =
64
65
GlideAnnotationProcessor .class .getPackage ().getName ();
65
- private static final ClassName NONNULL_ANNOTATION =
66
+ private static final ClassName SUPPORT_NONNULL_ANNOTATION =
66
67
ClassName .get ("android.support.annotation" , "NonNull" );
67
68
private static final ClassName JETBRAINS_NOTNULL_ANNOTATION =
68
69
ClassName .get ("org.jetbrains.annotations" , "NotNull" );
69
70
private static final ClassName ANDROIDX_NONNULL_ANNOTATION =
70
71
ClassName .get ("androidx.annotation" , "NonNull" );
71
- private static final ClassName CHECK_RESULT_ANNOTATION =
72
+ private static final ClassName SUPPORT_CHECK_RESULT_ANNOTATION =
72
73
ClassName .get ("android.support.annotation" , "CheckResult" );
73
74
private static final ClassName ANDROIDX_CHECK_RESULT_ANNOTATION =
74
75
ClassName .get ("androidx.annotation" , "CheckResult" );
75
- private static final ClassName VISIBLE_FOR_TESTING =
76
+ private static final ClassName SUPPORT_VISIBLE_FOR_TESTING =
76
77
ClassName .get ("android.support.annotation" , "VisibleForTesting" );
77
78
private static final ClassName ANDROIDX_VISIBLE_FOR_TESTING =
78
79
ClassName .get ("androidx.annotation" , "VisibleForTesting" );
@@ -457,36 +458,33 @@ private static List<AnnotationSpec> getAnnotations(VariableElement element) {
457
458
return result ;
458
459
}
459
460
460
- static ClassName visibleForTesting () {
461
- try {
462
- Class .forName (ANDROIDX_VISIBLE_FOR_TESTING .reflectionName ());
463
- return ANDROIDX_VISIBLE_FOR_TESTING ;
464
- } catch (ClassNotFoundException e ) {
465
- return VISIBLE_FOR_TESTING ;
466
- }
461
+ ClassName visibleForTesting () {
462
+ return findAnnotationClassName (ANDROIDX_VISIBLE_FOR_TESTING , SUPPORT_VISIBLE_FOR_TESTING );
467
463
}
468
464
469
- static ClassName nonNull () {
470
- try {
471
- Class .forName (ANDROIDX_NONNULL_ANNOTATION .reflectionName ());
472
- return ANDROIDX_NONNULL_ANNOTATION ;
473
- } catch (ClassNotFoundException e ) {
474
- return NONNULL_ANNOTATION ;
475
- }
465
+ ClassName nonNull () {
466
+ return findAnnotationClassName (ANDROIDX_NONNULL_ANNOTATION , SUPPORT_NONNULL_ANNOTATION );
476
467
}
477
468
478
- static ClassName checkResult () {
479
- try {
480
- Class .forName (ANDROIDX_CHECK_RESULT_ANNOTATION .reflectionName ());
481
- return ANDROIDX_CHECK_RESULT_ANNOTATION ;
482
- } catch (ClassNotFoundException e ) {
483
- return CHECK_RESULT_ANNOTATION ;
484
- }
469
+ ClassName checkResult () {
470
+ return findAnnotationClassName (
471
+ ANDROIDX_CHECK_RESULT_ANNOTATION , SUPPORT_CHECK_RESULT_ANNOTATION );
485
472
}
486
473
487
474
static List <ClassName > nonNulls () {
488
- return Arrays .asList (
489
- NONNULL_ANNOTATION , JETBRAINS_NOTNULL_ANNOTATION , ANDROIDX_NONNULL_ANNOTATION );
475
+ return ImmutableList .of (
476
+ SUPPORT_NONNULL_ANNOTATION , JETBRAINS_NOTNULL_ANNOTATION , ANDROIDX_NONNULL_ANNOTATION );
477
+ }
478
+
479
+ private ClassName findAnnotationClassName (ClassName androidxName , ClassName supportName ) {
480
+ Elements elements = processingEnv .getElementUtils ();
481
+ TypeElement visibleForTestingTypeElement =
482
+ elements .getTypeElement (androidxName .reflectionName ());
483
+ if (visibleForTestingTypeElement != null ) {
484
+ return androidxName ;
485
+ }
486
+
487
+ return supportName ;
490
488
}
491
489
492
490
List <ExecutableElement > findInstanceMethodsReturning (TypeElement clazz , TypeMirror returnType ) {
0 commit comments