forked from Enigmatis/graphql-java-annotations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLObjectInfoRetriever.java
More file actions
72 lines (59 loc) · 2.38 KB
/
GraphQLObjectInfoRetriever.java
File metadata and controls
72 lines (59 loc) · 2.38 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
/**
* Copyright 2016 Yurii Rashkovskii
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
package graphql.annotations.processor.retrievers;
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;
import org.osgi.service.component.annotations.Component;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static graphql.annotations.processor.util.NamingKit.toGraphqlName;
@Component(service = GraphQLObjectInfoRetriever.class, immediate = true)
public class GraphQLObjectInfoRetriever {
public String getTypeName(Class<?> objectClass) {
GraphQLName name = objectClass.getAnnotation(GraphQLName.class);
return toGraphqlName(name == null ? objectClass.getSimpleName() : name.value());
}
public List<Method> getOrderedMethods(Class<?> c) {
var methods = new LinkedHashMap<String, Method>();
collectMethods(c, methods);
return methods.values().stream()
.sorted(Comparator.comparing(Method::getName))
.collect(Collectors.toList());
}
private void collectMethods(Class<?> c, Map<String, Method> methods) {
if (c == null) {
return;
}
Arrays.stream(c.getDeclaredMethods())
.forEach(method -> methods.putIfAbsent(method.getName(), method));
for (Class<?> iface : c.getInterfaces()) {
collectMethods(iface, methods);
}
collectMethods(c.getSuperclass(), methods);
}
public Boolean isGraphQLField(AnnotatedElement element) {
GraphQLField annotation = element.getAnnotation(GraphQLField.class);
if (annotation == null) {
return null;
}
return annotation.value();
}
}