-
Notifications
You must be signed in to change notification settings - Fork 96
Fix method data fetcher #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
91531c6
9276b29
97cd6c8
99e7e91
cb18256
fcb79d7
4aa47eb
8c7caef
5e5e8b9
5918fda
e65fcce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
* 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 | ||
* 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, | ||
|
@@ -27,6 +27,7 @@ | |
import java.util.Map; | ||
|
||
import static graphql.annotations.processor.util.NamingKit.toGraphqlName; | ||
import static graphql.annotations.processor.util.PrefixesUtil.addPrefixToPropertyName; | ||
import static graphql.annotations.processor.util.ReflectionKit.constructNewInstance; | ||
import static graphql.annotations.processor.util.ReflectionKit.newInstance; | ||
|
||
|
@@ -58,7 +59,7 @@ public T get(DataFetchingEnvironment environment) { | |
} | ||
|
||
if (obj == null && environment.getSource() != null) { | ||
Object value = getFieldValue(environment.getSource(), method.getName()); | ||
Object value = getGraphQLFieldValue(environment.getSource(), environment.getField().getName()); | ||
return (T) value; | ||
} | ||
|
||
|
@@ -135,9 +136,59 @@ private Object buildArg(Type p, GraphQLType graphQLType, Object arg) { | |
} | ||
} | ||
|
||
private Object getFieldValue(Object source, String fieldName) throws IllegalAccessException, NoSuchFieldException { | ||
Field field = source.getClass().getDeclaredField(fieldName); | ||
field.setAccessible(true); | ||
return field.get(source); | ||
private Object getGraphQLFieldValue(Object source, String fieldName) throws IllegalAccessException, NoSuchFieldException, InvocationTargetException { | ||
Object methodValue = getValueFromMethod(source, fieldName); | ||
if (methodValue != null) return methodValue; | ||
|
||
Field field = getField(source.getClass(), fieldName); | ||
if (getValueFromField(field)) return field.get(source); | ||
|
||
throw new NoSuchFieldException("No GraphQL field found"); | ||
} | ||
|
||
private boolean getValueFromField(Field field) throws IllegalAccessException { | ||
if (field != null) { | ||
field.setAccessible(true); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private Object getValueFromMethod(Object source, String fieldName) throws IllegalAccessException, InvocationTargetException { | ||
String[] orderedPrefixes = new String[]{"", "get", "is"}; | ||
for (String orderedPrefix : orderedPrefixes) { | ||
Method method = getMethod(source.getClass(), fieldName, orderedPrefix); | ||
if (method != null) { | ||
return method.invoke(source); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private Method getMethod(Class<?> clazz, String name, String prefix) { | ||
String prefixedName = addPrefixToPropertyName(prefix, name); | ||
Method method = null; | ||
while (clazz != null && method == null) { | ||
try { | ||
method = clazz.getDeclaredMethod(prefixedName); | ||
} catch (Exception ignored) { | ||
} | ||
clazz = clazz.getSuperclass(); | ||
} | ||
|
||
return method; | ||
} | ||
|
||
private Field getField(Class<?> clazz, String name) { | ||
Field field = null; | ||
while (clazz != null && field == null) { | ||
try { | ||
field = clazz.getDeclaredField(name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it be "getDeclaredField" or "getField"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getField returns only public fields (but for entire hirearchy), but we need private ones as well. So this method runs over the entire hirearchy and searches for private methods with this name. |
||
} catch (Exception ignored) { | ||
} | ||
clazz = clazz.getSuperclass(); | ||
} | ||
return field; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* 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.util; | ||
|
||
public class PrefixesUtil { | ||
public static String addPrefixToPropertyName(String prefix, String propertyName) { | ||
return prefix + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate to few methods, one that deals with fields and one that deals with methods