1919import graphql .schema .GraphQLList ;
2020import graphql .schema .GraphQLType ;
2121import graphql .schema .GraphQLTypeReference ;
22- import org .osgi .service .component .annotations .*;
22+ import org .osgi .service .component .annotations .Activate ;
23+ import org .osgi .service .component .annotations .Component ;
24+ import org .osgi .service .component .annotations .Reference ;
25+ import org .osgi .service .component .annotations .ReferencePolicyOption ;
26+ import org .osgi .service .component .annotations .ServiceScope ;
2327
2428import java .lang .reflect .AnnotatedParameterizedType ;
2529import java .lang .reflect .AnnotatedType ;
2630import java .lang .reflect .Field ;
2731import java .lang .reflect .ParameterizedType ;
28- import java .util .*;
32+ import java .util .AbstractCollection ;
33+ import java .util .AbstractList ;
34+ import java .util .AbstractSet ;
35+ import java .util .ArrayList ;
36+ import java .util .Arrays ;
37+ import java .util .Collection ;
38+ import java .util .Collections ;
39+ import java .util .List ;
40+ import java .util .Map ;
41+ import java .util .Optional ;
42+ import java .util .Set ;
2943import java .util .concurrent .ConcurrentHashMap ;
3044import java .util .function .BiFunction ;
3145import java .util .stream .Collectors ;
3246import java .util .stream .Stream ;
3347
3448import static graphql .schema .GraphQLEnumType .newEnum ;
3549
36- @ Component (scope = ServiceScope .SINGLETON , property = "type=default" )
50+ @ Component (scope = ServiceScope .SINGLETON , property = "type=default" )
3751public class DefaultTypeFunction implements TypeFunction {
3852
3953 @ Reference (target = "(!(type=default))" ,
40- policyOption = ReferencePolicyOption .GREEDY )
54+ policyOption = ReferencePolicyOption .GREEDY )
4155 protected List <TypeFunction > otherFunctions = new ArrayList <>();
4256
43- @ Override public Collection <Class <?>> getAcceptedTypes () {
57+ @ Override
58+ public Collection <Class <?>> getAcceptedTypes () {
4459 List <Class <?>> list = registry .keySet ().stream ().collect (Collectors .toList ());
4560 List <Class <?>> others = otherFunctions .stream ().flatMap (tf -> tf .getAcceptedTypes ().stream ())
46- .collect (Collectors .toList ());
61+ .collect (Collectors .toList ());
4762 list .addAll (others );
4863 return list ;
4964 }
@@ -63,7 +78,8 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
6378 return Scalars .GraphQLString ;
6479 }
6580
66- @ Override public Collection <Class <?>> getAcceptedTypes () {
81+ @ Override
82+ public Collection <Class <?>> getAcceptedTypes () {
6783 return Collections .singletonList (String .class );
6884 }
6985 }
@@ -75,7 +91,8 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
7591 return Scalars .GraphQLBoolean ;
7692 }
7793
78- @ Override public Collection <Class <?>> getAcceptedTypes () {
94+ @ Override
95+ public Collection <Class <?>> getAcceptedTypes () {
7996 return Arrays .asList (Boolean .class , boolean .class );
8097 }
8198 }
@@ -87,7 +104,8 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
87104 return Scalars .GraphQLFloat ;
88105 }
89106
90- @ Override public Collection <Class <?>> getAcceptedTypes () {
107+ @ Override
108+ public Collection <Class <?>> getAcceptedTypes () {
91109 return Arrays .asList (Float .class , float .class , Double .class , double .class );
92110 }
93111 }
@@ -99,7 +117,8 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
99117 return Scalars .GraphQLInt ;
100118 }
101119
102- @ Override public Collection <Class <?>> getAcceptedTypes () {
120+ @ Override
121+ public Collection <Class <?>> getAcceptedTypes () {
103122 return Arrays .asList (Integer .class , int .class );
104123 }
105124 }
@@ -111,12 +130,16 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
111130 return Scalars .GraphQLLong ;
112131 }
113132
114- @ Override public Collection <Class <?>> getAcceptedTypes () {
133+ @ Override
134+ public Collection <Class <?>> getAcceptedTypes () {
115135 return Arrays .asList (Long .class , long .class );
116136 }
117137 }
118138
119- private class ListFunction implements TypeFunction {
139+ /**
140+ * Support for the Iterable things like Lists / Sets / Collections and so on..
141+ */
142+ private class IterableFunction implements TypeFunction {
120143
121144 @ Override
122145 public GraphQLType apply (Class <?> aClass , AnnotatedType annotatedType ) {
@@ -127,38 +150,24 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
127150 AnnotatedType arg = parameterizedType .getAnnotatedActualTypeArguments ()[0 ];
128151 Class <?> klass ;
129152 if (arg .getType () instanceof ParameterizedType ) {
130- klass = (Class <?>)((ParameterizedType )(arg .getType ())).getRawType ();
153+ klass = (Class <?>) ((ParameterizedType ) (arg .getType ())).getRawType ();
131154 } else {
132155 klass = (Class <?>) arg .getType ();
133156 }
134157 return new GraphQLList (DefaultTypeFunction .this .apply (klass , arg ));
135158 }
136159
137- @ Override public Collection <Class <?>> getAcceptedTypes () {
138- return Arrays .asList (List .class , AbstractList .class );
139- }
140- }
141-
142- private class SetFunction implements TypeFunction {
143-
144160 @ Override
145- public GraphQLType apply (Class <?> aClass , AnnotatedType annotatedType ) {
146- if (!(annotatedType instanceof AnnotatedParameterizedType )) {
147- throw new IllegalArgumentException ("List type parameter should be specified" );
148- }
149- AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType ) annotatedType ;
150- AnnotatedType arg = parameterizedType .getAnnotatedActualTypeArguments ()[0 ];
151- Class <?> klass ;
152- if (arg .getType () instanceof ParameterizedType ) {
153- klass = (Class <?>)((ParameterizedType )(arg .getType ())).getRawType ();
154- } else {
155- klass = (Class <?>) arg .getType ();
156- }
157- return new GraphQLList (DefaultTypeFunction .this .apply (klass , arg ));
158- }
159-
160- @ Override public Collection <Class <?>> getAcceptedTypes () {
161- return Arrays .asList (Set .class );
161+ public Collection <Class <?>> getAcceptedTypes () {
162+ return Arrays .asList (
163+ List .class ,
164+ AbstractList .class ,
165+ Set .class ,
166+ AbstractSet .class ,
167+ Collection .class ,
168+ AbstractCollection .class ,
169+ Iterable .class
170+ );
162171 }
163172 }
164173
@@ -173,14 +182,15 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
173182 AnnotatedType arg = parameterizedType .getAnnotatedActualTypeArguments ()[0 ];
174183 Class <?> klass ;
175184 if (arg .getType () instanceof ParameterizedType ) {
176- klass = (Class <?>)((ParameterizedType )(arg .getType ())).getRawType ();
185+ klass = (Class <?>) ((ParameterizedType ) (arg .getType ())).getRawType ();
177186 } else {
178187 klass = (Class <?>) arg .getType ();
179188 }
180189 return new GraphQLList (DefaultTypeFunction .this .apply (klass , arg ));
181190 }
182191
183- @ Override public Collection <Class <?>> getAcceptedTypes () {
192+ @ Override
193+ public Collection <Class <?>> getAcceptedTypes () {
184194 return Collections .singletonList (Stream .class );
185195 }
186196 }
@@ -196,14 +206,15 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
196206 AnnotatedType arg = parameterizedType .getAnnotatedActualTypeArguments ()[0 ];
197207 Class <?> klass ;
198208 if (arg .getType () instanceof ParameterizedType ) {
199- klass = (Class <?>)((ParameterizedType )(arg .getType ())).getRawType ();
209+ klass = (Class <?>) ((ParameterizedType ) (arg .getType ())).getRawType ();
200210 } else {
201211 klass = (Class <?>) arg .getType ();
202212 }
203213 return DefaultTypeFunction .this .apply (klass , arg );
204214 }
205215
206- @ Override public Collection <Class <?>> getAcceptedTypes () {
216+ @ Override
217+ public Collection <Class <?>> getAcceptedTypes () {
207218 return Collections .singletonList (Optional .class );
208219 }
209220 }
@@ -219,12 +230,13 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
219230
220231 if (types .containsKey (typeName )) {
221232 return types .get (typeName );
222- } else if (processing .containsKey (typeName )) {
233+ } else if (processing .containsKey (typeName )) {
223234 return processing .getOrDefault (typeName , new GraphQLTypeReference (typeName ));
224235 } else {
225236
226237 processing .put (typeName , new GraphQLTypeReference (typeName ));
227238
239+ //noinspection unchecked
228240 Class <? extends Enum > enumClass = (Class <? extends Enum >) aClass ;
229241 GraphQLEnumType .Builder builder = newEnum ();
230242 builder .name (typeName );
@@ -244,18 +256,20 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
244256 Enum constant = constants .stream ().filter (c -> c .name ().contentEquals (n )).findFirst ().get ();
245257 String name_ = fieldName == null ? n : fieldName .value ();
246258 builder .value (name_ , constant , fieldDescription == null ? name_ : fieldDescription .value ());
247- } catch (NoSuchFieldException e ) {
259+ } catch (NoSuchFieldException ignore ) {
248260 }
249261 });
250262
251263 final GraphQLEnumType type = builder .build ();
252264 types .put (typeName , type );
265+ //noinspection SuspiciousMethodCalls
253266 processing .remove (type );
254267 return type ;
255268 }
256269 }
257270
258- @ Override public Collection <Class <?>> getAcceptedTypes () {
271+ @ Override
272+ public Collection <Class <?>> getAcceptedTypes () {
259273 return Collections .singletonList (Enum .class );
260274 }
261275 }
@@ -287,7 +301,8 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
287301 }
288302 }
289303
290- @ Override public Collection <Class <?>> getAcceptedTypes () {
304+ @ Override
305+ public Collection <Class <?>> getAcceptedTypes () {
291306 return Collections .singletonList (Object .class );
292307 }
293308 }
@@ -302,9 +317,8 @@ public DefaultTypeFunction() {
302317
303318 register (new LongFunction ());
304319
305- register (new ListFunction ());
320+ register (new IterableFunction ());
306321 register (new StreamFunction ());
307- register (new SetFunction ());
308322
309323 register (new EnumFunction ());
310324
@@ -342,7 +356,7 @@ public GraphQLType apply(Class<?> klass, AnnotatedType annotatedType) {
342356 GraphQLType result = registry .get (t ).apply (klass , annotatedType );
343357
344358 if (klass .getAnnotation (GraphQLNonNull .class ) != null ||
345- (annotatedType != null && annotatedType .getAnnotation (GraphQLNonNull .class ) != null )) {
359+ (annotatedType != null && annotatedType .getAnnotation (GraphQLNonNull .class ) != null )) {
346360 result = new graphql .schema .GraphQLNonNull (result );
347361 }
348362
0 commit comments