Skip to content

Commit 5e5e8b9

Browse files
committed
fixing code review notes
1 parent 8c7caef commit 5e5e8b9

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2016 Yurii Rashkovskii
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -27,7 +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.createPrefix;
30+
import static graphql.annotations.processor.util.PrefixesUtil.addPrefixToPropertyName;
3131
import static graphql.annotations.processor.util.ReflectionKit.constructNewInstance;
3232
import static graphql.annotations.processor.util.ReflectionKit.newInstance;
3333

@@ -137,33 +137,30 @@ private Object buildArg(Type p, GraphQLType graphQLType, Object arg) {
137137
}
138138

139139
private Object getGraphQLFieldValue(Object source, String fieldName) throws IllegalAccessException, NoSuchFieldException, InvocationTargetException {
140-
Method method = getMethod(source.getClass(), fieldName, "");
141-
Method getMethod = getMethod(source.getClass(), fieldName, "get");
142-
Method isMethod = getMethod(source.getClass(), fieldName, "is");
143-
if (method != null) {
144-
return method.invoke(source);
145-
} else if (getMethod != null) {
146-
return getMethod.invoke(source);
147-
} else if (isMethod != null) {
148-
return isMethod.invoke(source);
149-
} else {
150-
Field field = getField(source.getClass(), fieldName);
151-
if (field != null) {
152-
field.setAccessible(true);
153-
return field.get(source);
154-
} else {
155-
throw new NoSuchFieldException("No GraphQL field found");
140+
String[] orderedPrefixes = new String[]{"", "get", "is"};
141+
for (String orderedPrefix : orderedPrefixes) {
142+
Method method = getMethod(source.getClass(), fieldName, orderedPrefix);
143+
if (method != null) {
144+
return method.invoke(source);
156145
}
157146
}
147+
148+
Field field = getField(source.getClass(), fieldName);
149+
if (field != null) {
150+
field.setAccessible(true);
151+
return field.get(source);
152+
} else {
153+
throw new NoSuchFieldException("No GraphQL field found");
154+
}
158155
}
159156

160157
private Method getMethod(Class<?> clazz, String name, String prefix) {
161-
String prefixedName = createPrefix(prefix, name);
158+
String prefixedName = addPrefixToPropertyName(prefix, name);
162159
Method method = null;
163160
while (clazz != null && method == null) {
164161
try {
165162
method = clazz.getDeclaredMethod(prefixedName);
166-
} catch (Exception e) {
163+
} catch (Exception ignored) {
167164
}
168165
clazz = clazz.getSuperclass();
169166
}
@@ -176,7 +173,7 @@ private Field getField(Class<?> clazz, String name) {
176173
while (clazz != null && field == null) {
177174
try {
178175
field = clazz.getDeclaredField(name);
179-
} catch (Exception e) {
176+
} catch (Exception ignored) {
180177
}
181178
clazz = clazz.getSuperclass();
182179
}

src/main/java/graphql/annotations/processor/util/PrefixesUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package graphql.annotations.processor.util;
1616

1717
public class PrefixesUtil {
18-
public static String createPrefix(String prefix, String propertyName) {
18+
public static String addPrefixToPropertyName(String prefix, String propertyName) {
1919
return prefix + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
2020
}
2121
}

0 commit comments

Comments
 (0)