1010import com .google .common .collect .FluentIterable ;
1111import com .google .common .collect .ImmutableBiMap ;
1212import com .google .common .collect .ImmutableList ;
13+ import com .google .common .collect .ImmutableSet ;
1314import com .google .common .collect .Lists ;
1415import com .squareup .javapoet .AnnotationSpec ;
1516import com .squareup .javapoet .ClassName ;
2021import com .squareup .javapoet .TypeName ;
2122import com .squareup .javapoet .TypeSpec ;
2223import com .squareup .javapoet .TypeVariableName ;
23- import com .sun .tools .javac .code .Attribute ;
24- import com .sun .tools .javac .code .Type .ClassType ;
2524import java .lang .annotation .Annotation ;
26- import java .lang .reflect .InvocationTargetException ;
27- import java .lang .reflect .Method ;
2825import java .util .ArrayList ;
2926import java .util .Collection ;
30- import java .util .Collections ;
3127import java .util .HashSet ;
3228import java .util .LinkedHashSet ;
3329import java .util .List ;
34- import java .util .Map ;
30+ import java .util .Locale ;
3531import java .util .Set ;
3632import javax .annotation .Nullable ;
3733import javax .annotation .processing .ProcessingEnvironment ;
4642import javax .lang .model .element .TypeElement ;
4743import javax .lang .model .element .TypeParameterElement ;
4844import javax .lang .model .element .VariableElement ;
45+ import javax .lang .model .type .DeclaredType ;
46+ import javax .lang .model .type .TypeKind ;
4947import javax .lang .model .type .TypeMirror ;
5048import javax .lang .model .type .TypeVariable ;
5149import javax .lang .model .util .ElementFilter ;
@@ -408,7 +406,7 @@ private static String computeParameterName(VariableElement parameter, TypeName t
408406 }
409407 }
410408 if (allCaps ) {
411- name = rawClassName .toLowerCase ();
409+ name = rawClassName .toLowerCase (Locale . ROOT );
412410 } else {
413411 int indexOfLastWordStart = 0 ;
414412 char [] chars = rawClassName .toCharArray ();
@@ -431,7 +429,7 @@ private static String computeParameterName(VariableElement parameter, TypeName t
431429
432430 private static String getSmartPrimitiveParameterName (VariableElement parameter ) {
433431 for (AnnotationMirror annotation : parameter .getAnnotationMirrors ()) {
434- String annotationName = annotation .getAnnotationType ().toString ().toUpperCase ();
432+ String annotationName = annotation .getAnnotationType ().toString ().toUpperCase (Locale . ROOT );
435433 if (annotationName .endsWith ("RES" )) {
436434 // Catch annotations like StringRes
437435 return "id" ;
@@ -539,7 +537,7 @@ List<ExecutableElement> findStaticMethods(TypeElement clazz) {
539537 .toList ();
540538 }
541539
542- Set <String > findClassValuesFromAnnotationOnClassAsNames (
540+ ImmutableSet <String > findClassValuesFromAnnotationOnClassAsNames (
543541 Element clazz , Class <? extends Annotation > annotationClass ) {
544542 String annotationClassName = annotationClass .getName ();
545543 AnnotationValue excludedModuleAnnotationValue = null ;
@@ -549,67 +547,48 @@ Set<String> findClassValuesFromAnnotationOnClassAsNames(
549547 if (!annotationClassName .equals (annotationMirror .getAnnotationType ().toString ())) {
550548 continue ;
551549 }
552- Set <? extends Map .Entry <? extends ExecutableElement , ? extends AnnotationValue >> values =
553- annotationMirror .getElementValues ().entrySet ();
554- // Excludes has only one value. If we ever change that, we'd need to iterate over all
555- // values in the entry set and compare the keys to whatever our Annotation's attribute is
556- // (usually value).
557- if (values .size () != 1 ) {
558- throw new IllegalArgumentException ("Expected single value, but found: " + values );
550+
551+ var entries = annotationMirror .getElementValues ().entrySet ();
552+ if (entries .size () != 1 ) {
553+ throw new IllegalArgumentException ("Expected single value, but found: " + entries );
559554 }
560- excludedModuleAnnotationValue = values .iterator ().next ().getValue ();
561- if (excludedModuleAnnotationValue == null
562- || excludedModuleAnnotationValue instanceof Attribute .UnresolvedClass ) {
555+ excludedModuleAnnotationValue = entries .iterator ().next ().getValue ();
556+ if (excludedModuleAnnotationValue == null ) {
563557 throw new IllegalArgumentException (
564558 "Failed to find value for: "
565559 + annotationClass
566560 + " from mirrors: "
567561 + clazz .getAnnotationMirrors ());
568562 }
569563 }
564+
570565 if (excludedModuleAnnotationValue == null ) {
571- return Collections . emptySet ();
566+ return ImmutableSet . of ();
572567 }
568+
573569 Object value = excludedModuleAnnotationValue .getValue ();
574570 if (value instanceof List ) {
575- List <?> values = ( List <?>) value ;
576- Set < String > result = new HashSet <>( values . size ());
577- for ( Object current : values ) {
578- result .add (getExcludedModuleClassFromAnnotationAttribute ( clazz , current ));
571+ LinkedHashSet < String > out = new LinkedHashSet <>() ;
572+ for ( Object o : ( List <?>) value ) {
573+ AnnotationValue av = ( AnnotationValue ) o ;
574+ out .add (qualifiedNameFromTypeMirror (( TypeMirror ) av . getValue () ));
579575 }
580- return result ;
576+ return ImmutableSet . copyOf ( out ) ;
581577 } else {
582- ClassType classType = (ClassType ) value ;
583- return Collections .singleton (classType .toString ());
578+ return ImmutableSet .of (qualifiedNameFromTypeMirror ((TypeMirror ) value ));
584579 }
585580 }
586581
587- // We should be able to cast to Attribute.Class rather than use reflection, but there are some
588- // compilers that seem to break when we do so. See #2673 for an example.
589- private static String getExcludedModuleClassFromAnnotationAttribute (
590- Element clazz , Object attribute ) {
591- if (attribute .getClass ().getSimpleName ().equals ("UnresolvedClass" )) {
592- throw new IllegalArgumentException (
593- "Failed to parse @Excludes for: "
594- + clazz
595- + ", one or more excluded Modules could not be found at compile time. Ensure that all"
596- + "excluded Modules are included in your classpath." );
582+ static String qualifiedNameFromTypeMirror (TypeMirror type ) {
583+ if (type .getKind () == TypeKind .ERROR ) {
584+ throw new IllegalArgumentException ("Unresolved class type in annotation: " + type );
597585 }
598- Method [] methods = attribute .getClass ().getDeclaredMethods ();
599- if (methods == null || methods .length == 0 ) {
600- throw new IllegalArgumentException (
601- "Failed to parse @Excludes for: " + clazz + ", invalid exclude: " + attribute );
602- }
603- for (Method method : methods ) {
604- if (method .getName ().equals ("getValue" )) {
605- try {
606- return method .invoke (attribute ).toString ();
607- } catch (IllegalAccessException | InvocationTargetException e ) {
608- throw new IllegalArgumentException ("Failed to parse @Excludes for: " + clazz , e );
609- }
610- }
586+ if (type .getKind () == TypeKind .DECLARED ) {
587+ DeclaredType dt = (DeclaredType ) type ;
588+ TypeElement te = (TypeElement ) dt .asElement ();
589+ return te .getQualifiedName ().toString ();
611590 }
612- throw new IllegalArgumentException ( "Failed to parse @Excludes for: " + clazz );
591+ return type . toString ( );
613592 }
614593
615594 private enum MethodType {
0 commit comments