|
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | 7 | * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
27 | 27 | import java.util.Map; |
28 | 28 |
|
29 | 29 | import static graphql.annotations.processor.util.NamingKit.toGraphqlName; |
| 30 | +import static graphql.annotations.processor.util.PrefixesUtil.addPrefixToPropertyName; |
30 | 31 | import static graphql.annotations.processor.util.ReflectionKit.constructNewInstance; |
31 | 32 | import static graphql.annotations.processor.util.ReflectionKit.newInstance; |
32 | 33 |
|
@@ -58,7 +59,7 @@ public T get(DataFetchingEnvironment environment) { |
58 | 59 | } |
59 | 60 |
|
60 | 61 | if (obj == null && environment.getSource() != null) { |
61 | | - Object value = getFieldValue(environment.getSource(), method.getName()); |
| 62 | + Object value = getGraphQLFieldValue(environment.getSource(), environment.getField().getName()); |
62 | 63 | return (T) value; |
63 | 64 | } |
64 | 65 |
|
@@ -135,9 +136,59 @@ private Object buildArg(Type p, GraphQLType graphQLType, Object arg) { |
135 | 136 | } |
136 | 137 | } |
137 | 138 |
|
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; |
142 | 155 | } |
| 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 | + |
143 | 194 | } |
0 commit comments