Skip to content

Commit ec7ba9b

Browse files
committed
adsf
2 parents e5fbde0 + 47c97da commit ec7ba9b

File tree

5 files changed

+326
-12
lines changed

5 files changed

+326
-12
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ org.gradle.jvmargs=-Dfile.encoding=UTF-8
77

88
bintray.user=DUMMY_USER
99
bintray.key=DUMMY_KEY
10-
version = 5.3
10+
version = 5.3.1

src/main/java/graphql/annotations/dataFetchers/MethodDataFetcher.java

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -27,6 +27,7 @@
2727
import java.util.Map;
2828

2929
import static graphql.annotations.processor.util.NamingKit.toGraphqlName;
30+
import static graphql.annotations.processor.util.PrefixesUtil.addPrefixToPropertyName;
3031
import static graphql.annotations.processor.util.ReflectionKit.constructNewInstance;
3132
import static graphql.annotations.processor.util.ReflectionKit.newInstance;
3233

@@ -58,7 +59,7 @@ public T get(DataFetchingEnvironment environment) {
5859
}
5960

6061
if (obj == null && environment.getSource() != null) {
61-
Object value = getFieldValue(environment.getSource(), method.getName());
62+
Object value = getGraphQLFieldValue(environment.getSource(), environment.getField().getName());
6263
return (T) value;
6364
}
6465

@@ -135,9 +136,59 @@ private Object buildArg(Type p, GraphQLType graphQLType, Object arg) {
135136
}
136137
}
137138

138-
private Object getFieldValue(Object source, String fieldName) throws IllegalAccessException, NoSuchFieldException {
139-
Field field = source.getClass().getDeclaredField(fieldName);
140-
field.setAccessible(true);
141-
return field.get(source);
139+
private Object getGraphQLFieldValue(Object source, String fieldName) throws IllegalAccessException, NoSuchFieldException, InvocationTargetException {
140+
Object methodValue = getValueFromMethod(source, fieldName);
141+
if (methodValue != null) return methodValue;
142+
143+
Field field = getField(source.getClass(), fieldName);
144+
if (getValueFromField(field)) return field.get(source);
145+
146+
throw new NoSuchFieldException("No GraphQL field found");
147+
}
148+
149+
private boolean getValueFromField(Field field) throws IllegalAccessException {
150+
if (field != null) {
151+
field.setAccessible(true);
152+
return true;
153+
}
154+
return false;
142155
}
156+
157+
private Object getValueFromMethod(Object source, String fieldName) throws IllegalAccessException, InvocationTargetException {
158+
String[] orderedPrefixes = new String[]{"", "get", "is"};
159+
for (String orderedPrefix : orderedPrefixes) {
160+
Method method = getMethod(source.getClass(), fieldName, orderedPrefix);
161+
if (method != null) {
162+
return method.invoke(source);
163+
}
164+
}
165+
return null;
166+
}
167+
168+
private Method getMethod(Class<?> clazz, String name, String prefix) {
169+
String prefixedName = addPrefixToPropertyName(prefix, name);
170+
Method method = null;
171+
while (clazz != null && method == null) {
172+
try {
173+
method = clazz.getDeclaredMethod(prefixedName);
174+
} catch (Exception ignored) {
175+
}
176+
clazz = clazz.getSuperclass();
177+
}
178+
179+
return method;
180+
}
181+
182+
private Field getField(Class<?> clazz, String name) {
183+
Field field = null;
184+
while (clazz != null && field == null) {
185+
try {
186+
field = clazz.getDeclaredField(name);
187+
} catch (Exception ignored) {
188+
}
189+
clazz = clazz.getSuperclass();
190+
}
191+
return field;
192+
}
193+
143194
}

src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public MethodNameBuilder(Method method) {
3232
@Override
3333
public String build() {
3434
if (method.isAnnotationPresent(GraphQLPrettify.class) && !method.isAnnotationPresent(GraphQLName.class)) {
35-
return toGraphqlName(pretifyName(method.getName()));
35+
return toGraphqlName(prettifyName(method.getName()));
3636
}
3737
GraphQLName name = method.getAnnotation(GraphQLName.class);
3838
return toGraphqlName(name == null ? method.getName() : name.value());
3939
}
4040

41-
private String pretifyName(String originalName) {
41+
private String prettifyName(String originalName) {
4242
String name = originalName.replaceFirst("^(is|get|set)(.+)", "$2");
4343
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
4444
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations.processor.util;
16+
17+
public class PrefixesUtil {
18+
public static String addPrefixToPropertyName(String prefix, String propertyName) {
19+
return prefix + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
20+
}
21+
}

0 commit comments

Comments
 (0)