1010import java .lang .reflect .Modifier ;
1111import java .lang .reflect .ParameterizedType ;
1212import java .lang .reflect .Type ;
13+ import java .lang .reflect .WildcardType ;
1314import java .util .Arrays ;
1415import java .util .List ;
1516import java .util .Map ;
@@ -38,6 +39,8 @@ public static String getCanonicalName(@Nonnull final Type type)
3839 return getCanonicalName ((Class <?>) type );
3940 else if (type instanceof ParameterizedType )
4041 return getCanonicalName ((ParameterizedType ) type );
42+ else if (type instanceof WildcardType )
43+ return getCanonicalName ((WildcardType ) type );
4144 else
4245 throw new RuntimeException ("Unhandled type " + type + " -- " + type .getClass ());
4346 }
@@ -48,6 +51,32 @@ private static String getCanonicalName(@Nonnull final Class<?> clazz)
4851 return clazz .getCanonicalName () + typeParameterWildCards (clazz );
4952 }
5053
54+ @ Nonnull
55+ private static String getCanonicalName (@ Nonnull final WildcardType type )
56+ {
57+ final Type [] upper = type .getUpperBounds ();
58+ if (upper .length ==1 )
59+ {
60+ if (type .getLowerBounds ().length !=0 )
61+ throw new RuntimeException (Arrays .toString (type .getLowerBounds ()));
62+
63+ if (Object .class .equals (upper [0 ]))
64+ return "?" ;
65+
66+ return "? extends " + getCanonicalName (upper [0 ]);
67+ }
68+
69+ final Type [] lower = type .getLowerBounds ();
70+ if (lower .length ==1 )
71+ {
72+ if (upper .length !=0 )
73+ throw new RuntimeException (Arrays .toString (upper ));
74+ return "? super " + getCanonicalName (lower [0 ]);
75+ }
76+
77+ throw new RuntimeException (Arrays .asList (upper ).toString () + Arrays .asList (lower ));
78+ }
79+
5180 @ Nonnull
5281 private static String getCanonicalName (@ Nonnull final ParameterizedType type )
5382 {
@@ -122,6 +151,8 @@ public static boolean isVisible(final String packageName, final Type type)
122151 return isVisible (packageName , (Class <?>) type );
123152 else if (type instanceof ParameterizedType )
124153 return isVisible (packageName , (ParameterizedType ) type );
154+ else if (type instanceof WildcardType )
155+ return isVisible (packageName , (WildcardType ) type );
125156 else
126157 throw new RuntimeException (type .getTypeName () + ' ' + type .getClass ());
127158 }
@@ -149,6 +180,19 @@ private static boolean isVisible(final String packageName, final Class<?> clazz)
149180 return packageName .equals (clazz .getPackage ().getName ());
150181 }
151182
183+ private static boolean isVisible (final String packageName , final WildcardType type )
184+ {
185+ for (final Type argument : type .getUpperBounds ())
186+ if (!isVisible (packageName , argument ))
187+ return false ;
188+
189+ for (final Type argument : type .getLowerBounds ())
190+ if (!isVisible (packageName , argument ))
191+ return false ;
192+
193+ return true ;
194+ }
195+
152196 private TypeUtil ()
153197 {
154198 // prevent instantiation
0 commit comments