Skip to content
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ org.gradle.jvmargs=-Dfile.encoding=UTF-8

bintray.user=DUMMY_USER
bintray.key=DUMMY_KEY
version = 5.2
version = 5.3
Empty file added secret2.gpg
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 {
Copy link
Contributor

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

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be "getDeclaredField" or "getField"?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -32,13 +32,13 @@ public MethodNameBuilder(Method method) {
@Override
public String build() {
if (method.isAnnotationPresent(GraphQLPrettify.class) && !method.isAnnotationPresent(GraphQLName.class)) {
return toGraphqlName(pretifyName(method.getName()));
return toGraphqlName(prettifyName(method.getName()));
}
GraphQLName name = method.getAnnotation(GraphQLName.class);
return toGraphqlName(name == null ? method.getName() : name.value());
}

private String pretifyName(String originalName) {
private String prettifyName(String originalName) {
String name = originalName.replaceFirst("^(is|get|set)(.+)", "$2");
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/graphql/annotations/processor/util/PrefixesUtil.java
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);
}
}
Loading