Package com.graphql_java_generator
Class GraphqlUtils
java.lang.Object
com.graphql_java_generator.GraphqlUtils
@Component
public class GraphqlUtils
extends java.lang.Object
- Author:
- etienne-sf
-
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.util.stream.Stream<T>
concatStreams(java.lang.Class<T> clazz, boolean parallelStreams, java.util.List<?>... lists)
Concatenate a non limited number of lists into a stream.java.lang.String
getCamelCase(java.lang.String name)
Convert the given name, to a camel case name.java.lang.String
getClassSimpleName(java.lang.String classFullName)
Extract the simple name for a class (without the package name), from its full class name (with the package name)java.lang.reflect.Field
getDeclaredField(java.lang.Class<?> owningClass, java.lang.String fieldName, boolean mustFindField)
Returns aField
from the given class.java.lang.Class<?>
getFieldType(java.lang.Class<?> owningClass, java.lang.String fieldName, boolean returnIsMandatory)
Retrieves the class of the fieldName field of the owningClass class.<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
getPackageName(java.lang.String classFullName)
Extract the package name for a class, from its full class name (with the package name)java.lang.String
getPascalCase(java.lang.String name)
Convert the given name, which is supposed to be in camel case (for instance: thisIsCamelCase) to a pascal case string (for instance: ThisIsCamelCase).<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.java.lang.Object
invokeMethod(java.lang.String methodName, java.lang.Object object)
Calls the 'methodName' method on the given object.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<I> java.util.List<I>
iterableConcreteClassToListInterface(java.lang.Iterable<? extends I> iterable)
Transform anIterable
(which can be aList
) of a concrete class, into aList
of the I interface or superclass.<I> java.util.List<I>
iterableToList(java.lang.Iterable<I> iterable)
Transform anIterable
(which can be aList
), into aList
of items of the same type.<T> T
optionalToObject(java.util.Optional<T> optional)
Transform anOptional
, as returned by Spring Data repositories, into a standard Java, which is null if there is no value.
-
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
-
getCamelCase
public java.lang.String getCamelCase(java.lang.String name)Convert the given name, to a camel case name. Currenly very simple : it puts the first character in lower case.- Returns:
-
getPascalCase
public java.lang.String getPascalCase(java.lang.String name)Convert the given name, which is supposed to be in camel case (for instance: thisIsCamelCase) to a pascal case string (for instance: ThisIsCamelCase).- Returns:
-
iterableToList
public <I> java.util.List<I> iterableToList(java.lang.Iterable<I> iterable)Transform anIterable
(which can be aList
), into aList
of items of the same type. It's usefull to transform the native type from Spring Data repositories (which needs concrete class to map into) into the list of relevant GraphQL interface- Type Parameters:
I
-- Parameters:
iterable
-- Returns:
-
iterableConcreteClassToListInterface
public <I> java.util.List<I> iterableConcreteClassToListInterface(java.lang.Iterable<? extends I> iterable)Transform anIterable
(which can be aList
) of a concrete class, into aList
of the I interface or superclass. It's usefull to transform the native type from Spring Data repositories (which needs concrete class to map into) into the list of relevant GraphQL interface- Type Parameters:
I
-- Parameters:
iterable
-- Returns:
-
optionalToObject
public <T> T optionalToObject(java.util.Optional<T> optional)Transform anOptional
, as returned by Spring Data repositories, into a standard Java, which is null if there is no value.- Parameters:
optional
-- Returns:
-
getFieldType
public java.lang.Class<?> getFieldType(java.lang.Class<?> owningClass, java.lang.String fieldName, boolean returnIsMandatory) throws GraphQLRequestPreparationExceptionRetrieves the class of the fieldName field of the owningClass class.- Parameters:
owningClass
-fieldName
-returnIsMandatory
- If true, aGraphQLRequestPreparationException
is thrown if the field is not found.- Returns:
- The class of the field. Or null of the field doesn't exist, and returnIdMandatory is false
- Throws:
GraphQLRequestPreparationException
-
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:
-
getDeclaredField
public java.lang.reflect.Field getDeclaredField(java.lang.Class<?> owningClass, java.lang.String fieldName, boolean mustFindField) throws GraphQLRequestPreparationExceptionReturns aField
from the given class.- Parameters:
owningClass
- The class that should contain this field. If the class's name finishes by Response, as an empty XxxResponse class is created for each Query/Mutation/Subscription (to be compatible with previsous version), then this method also looks in the owningClass's superclass.fieldName
- The name of the searched fieldmustFindField
- If true and the field is not found, aGraphQLRequestPreparationException
is thrown.
If false an the field is not found, the method returns null- Returns:
- Throws:
GraphQLRequestPreparationException
-
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.String methodName, java.lang.Object object)Calls the 'methodName' method on the given object.- Parameters:
methodName
- The name of the method. This method should have no parameterobject
- The given node, on which the 'methodName' method is to be called- Returns:
-
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:
-
getClassSimpleName
public java.lang.String getClassSimpleName(java.lang.String classFullName)Extract the simple name for a class (without the package name), from its full class name (with the package name)- Parameters:
classFullName
- The full class name, for instance java.util.Date- Returns:
- The simple class name (in the above sample: Date)
-
getPackageName
public java.lang.String getPackageName(java.lang.String classFullName)Extract the package name for a class, from its full class name (with the package name)- Parameters:
classFullName
- The full class name, for instance java.util.Date- Returns:
- The simple class name (in the above sample: java.util)
-
concatStreams
public <T> java.util.stream.Stream<T> concatStreams(java.lang.Class<T> clazz, boolean parallelStreams, java.util.List<?>... lists)Concatenate a non limited number of lists into a stream.- Type Parameters:
T
-- Parameters:
clazz
- The T classparallelStreams
- true if the returned stream should be a parallel onelists
-- Returns:
-