Package com.graphql_java_generator
Class GraphqlUtils
java.lang.Object
com.graphql_java_generator.GraphqlUtils
@Component
public class GraphqlUtils
extends java.lang.Object
- Author:
- EtienneSF
-
Field Summary
Fields Modifier and Type Field Description static GraphqlUtils
graphqlUtils
This singleton is usable in default method, within interfacesstatic java.lang.Character
JAVA_KEYWORD_PREFIX
-
Constructor Summary
Constructors Constructor Description GraphqlUtils()
-
Method Summary
Modifier and Type Method Description <T> java.lang.reflect.Method
getGetter(java.lang.Class<T> clazz, java.lang.reflect.Field field)
Retrieves the getter for the given field on the given field<T> T
getInputObject(java.util.Map<java.lang.String,java.lang.Object> map, java.lang.Class<T> clazz)
This method returns a GraphQL input object, as defined in the GraphQL schema, from the Map that has been read from the JSON object sent to the server.java.lang.String
getJavaName(java.lang.String name)
Returns a valid java identifier for the given name.<T> java.util.List<T>
getListInputObjects(java.util.List<java.util.Map<java.lang.String,java.lang.Object>> list, java.lang.Class<T> clazz)
This method returns a list of instances of the given class, from a list ofMap
.java.lang.reflect.Method
getMethod(java.lang.String methodName, java.lang.Class<?> clazz, java.lang.Class<?>... parameterTypes)
Retrieves the asked method, from its name, class and parameters.java.lang.String
getPascalCase(java.lang.String name)
Returns the given name in PascalCase.<T> java.lang.reflect.Method
getSetter(java.lang.Class<T> clazz, java.lang.reflect.Field field)
Retrieves the setter for the given field on the given fieldjava.lang.Object
invokeGetter(java.lang.Object object, java.lang.String fieldName)
Invoke the getter for the given field name, on the given object.java.lang.Object
invokeMethod(java.lang.reflect.Method method, java.lang.Object o, java.lang.Object... args)
Invoke the given setter on the given object, with the given value.void
invokeSetter(java.lang.Object object, java.lang.reflect.Field field, java.lang.Object value)
Invoke the setter for the given field, on the given object.void
invokeSetter(java.lang.Object object, java.lang.String fieldName, java.lang.Object value)
Invoke the setter for theField
of the given name, on the given object.boolean
isJavaReservedWords(java.lang.String name)
Returns true if name is a reserved java keyword
-
Field Details
-
graphqlUtils
This singleton is usable in default method, within interfaces -
JAVA_KEYWORD_PREFIX
public static java.lang.Character JAVA_KEYWORD_PREFIX
-
-
Constructor Details
-
GraphqlUtils
public GraphqlUtils()
-
-
Method Details
-
getPascalCase
public java.lang.String getPascalCase(java.lang.String name)Returns the given name in PascalCase. For instance: theName -> TheName- Parameters:
name
-- Returns:
-
getInputObject
public <T> T getInputObject(java.util.Map<java.lang.String,java.lang.Object> map, java.lang.Class<T> clazz)This method returns a GraphQL input object, as defined in the GraphQL schema, from the Map that has been read from the JSON object sent to the server.- Type Parameters:
T
- The class expected to be returned- Parameters:
map
- The map, read from the JSON in the GraphQL request. Only the part of the map, related to the expected class is sent.t
- An empty instance of the expected type. This instance's fields will be set by this method, from the value in the map- Returns:
- An instance of the expected class. If the map is null, null is returned. Of the map is empty, anew instance is returned, with all its fields are left empty
-
getListInputObjects
public <T> java.util.List<T> getListInputObjects(java.util.List<java.util.Map<java.lang.String,java.lang.Object>> list, java.lang.Class<T> clazz)This method returns a list of instances of the given class, from a list ofMap
. This is used on server-side, to map the input read from the JSON into the InputType that have been declared in the GraphQL schema.- Type Parameters:
T
-- Parameters:
list
-clazz
-- Returns:
-
getSetter
public <T> java.lang.reflect.Method getSetter(java.lang.Class<T> clazz, java.lang.reflect.Field field)Retrieves the setter for the given field on the given field- Type Parameters:
T
-- Parameters:
t
-field
-- Returns:
-
getGetter
public <T> java.lang.reflect.Method getGetter(java.lang.Class<T> clazz, java.lang.reflect.Field field)Retrieves the getter for the given field on the given field- Type Parameters:
T
-- Parameters:
t
-field
-- Returns:
-
invokeGetter
public java.lang.Object invokeGetter(java.lang.Object object, java.lang.String fieldName)Invoke the getter for the given field name, on the given object. All check exceptions are hidden in aRuntimeException
- Parameters:
object
-fieldName
-- Returns:
- the field's value for the given object
- Throws:
java.lang.RuntimeException
- If any exception occurs
-
invokeSetter
public void invokeSetter(java.lang.Object object, java.lang.reflect.Field field, java.lang.Object value)Invoke the setter for the given field, on the given object. All check exceptions are hidden in aRuntimeException
- Parameters:
object
-field
-value
-- Throws:
java.lang.RuntimeException
- If any exception occurs
-
invokeSetter
public void invokeSetter(java.lang.Object object, java.lang.String fieldName, java.lang.Object value)Invoke the setter for theField
of the given name, on the given object. All check exceptions are hidden in aRuntimeException
- Parameters:
object
-fieldName
-value
-- Throws:
java.lang.RuntimeException
- If any exception occurs
-
getMethod
public java.lang.reflect.Method getMethod(java.lang.String methodName, java.lang.Class<?> clazz, java.lang.Class<?>... parameterTypes)Retrieves the asked method, from its name, class and parameters. This method hides the exception that could be thrown, into aRuntimeException
- Type Parameters:
T
-- Parameters:
t
-field
-- Returns:
- Throws:
java.lang.RuntimeException
- When an exception occurs while getting the method
-
invokeMethod
public java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object o, java.lang.Object... args)Invoke the given setter on the given object, with the given value. This method hides the exception that could be thrown, into aRuntimeException
- Parameters:
method
-o
-value
-- Throws:
java.lang.RuntimeException
- When an exception occurs while accessing the setter
-
getJavaName
public java.lang.String getJavaName(java.lang.String name)Returns a valid java identifier for the given name.- Parameters:
name
-- Returns:
- If name is a default java keyword (so it is not a valid java identifier), then the return prefixed by a
JAVA_KEYWORD_PREFIX
. Otherwise (which is generally the case), the name is valid, and returned as is the given name
-
isJavaReservedWords
public boolean isJavaReservedWords(java.lang.String name)Returns true if name is a reserved java keyword- Parameters:
name
-- Returns:
-