Class GraphqlUtils

java.lang.Object
com.graphql_java_generator.util.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 interfaces
    static java.lang.Character JAVA_KEYWORD_PREFIX  
  • Constructor Summary

    Constructors 
    Constructor Description
    GraphqlUtils()  
  • Method Summary

    Modifier and Type Method Description
    void addImport​(java.util.Set<java.lang.String> imports, java.lang.String targetPackageName, java.lang.String classname)
    Adds, if necessary the import calculated from the given parameters, into the given set of imports.
    <T> java.util.stream.Stream<T> concatStreams​(java.lang.Class<T> clazz, boolean parallelStreams, T t1, T t2, T t3, java.util.List<? extends T>... lists)
    Concatenate a non limited number of lists into a stream.
    java.lang.Object getArgument​(java.lang.Object jsonParsedValue, java.lang.String graphQLTypeName, java.lang.String javaTypeForIDType, java.lang.Class<?> clazz)
    This method returns a GraphQL argument into the relevant Java object, within a data fetcher, from what has been parsed by the graphql-java engine from the incoming JSON request
    java.lang.String getCamelCase​(java.lang.String name)
    Convert the given name, to a camel case name.
    java.lang.String getClassSimpleName​(java.lang.String classFullNameParam)
    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 a Field 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.
    java.lang.reflect.Method getGetter​(java.lang.Class<?> clazz, java.lang.reflect.Field field)
    Retrieves the getter for the given field on the given field
    java.lang.String getJavaName​(java.lang.String name)
    Returns a valid java identifier for the given name.
    java.lang.Long getLastModified​(java.io.File fileOrFolder, boolean maxValue)
    Returns the maximum or minimum value for the lastModified of the given file, or of all the files (not folders) contained into this folder.
    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 classFullNameParam)
    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).
    java.lang.String getQuotedScanBasePackages​(java.lang.String scanBasePackages)  
    java.lang.String getRuntimeVersion()
    Returns the version of the runtime, that is used to check that the runtime's version is the same as the Maven or Gradle plugin's version.
    <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
    java.lang.String getValueAsString​(graphql.language.Value<?> value)
    Returns the given value, as string, as it can be written into the GraphQLDirective.parameterValues() of the GraphQLDirective java annotation.
    A str string default value will be returned as "str",a JEDI enum value will be returned as "JEDI", an object will be returned as "{name:\"specific name\",appearsIn:[NEWHOPE,EMPIRE],type:\"Human\"}"...
    java.lang.String getValueAsText​(graphql.language.Value<?> value)
    Returns the given value, as text, as it can be written into a generated GraphQL schema.
    A str string default value will be returned as "str",a JEDI enum value will be returned as JEDI, ...
    java.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, java.lang.Object... args)
    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 the Field of the given name, on the given object.
    java.lang.Object invokeStaticMethod​(java.lang.String methodName, java.lang.Class<?> clazz)
    Calls the 'methodName' method on the given class.
    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 an Iterable (which can be a List) of a concrete class, into a List of the I interface or superclass.
    <I> java.util.List<I> iterableToList​(java.lang.Iterable<I> iterable)
    Transform an Iterable (which can be a List), into a List of items of the same type.
    <T> T optionalToObject​(java.util.Optional<T> optional)
    Transform an Optional, as returned by Spring Data repositories, into a standard Java, which is null if there is no value.
    <T> java.util.List<T> orderList​(java.util.List<?> keys, java.util.List<T> unorderedList, java.lang.String keyFieldName)
    Reads a non ordered list, and return the same content sorted according the keys list.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • getRuntimeVersion

      public java.lang.String getRuntimeVersion()
      Returns the version of the runtime, that is used to check that the runtime's version is the same as the Maven or Gradle plugin's version.
    • getCamelCase

      public java.lang.String getCamelCase​(java.lang.String name)
      Convert the given name, to a camel case name. Currently 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 an Iterable (which can be a List), into a List 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 an Iterable (which can be a List) of a concrete class, into a List 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 an Optional, as returned by Spring Data repositories, into a standard Java, which is null if there is no value.
      Parameters:
      optional -
      Returns:
    • orderList

      public <T> java.util.List<T> orderList​(java.util.List<?> keys, java.util.List<T> unorderedList, java.lang.String keyFieldName)
      Reads a non ordered list, and return the same content sorted according the keys list. This method is used for batch loader method: they must return their result in the exact same order as the provided keys, so that the returned values are properly dispatched in the server's response.
      Type Parameters:
      T - The type of items in these list
      Parameters:
      keys - The list which ordered must be respected.
      unorderedList - A list of items in any order. Each item in this list must have a key which is in the keys list.
      There may be missing values (for instance if a key doesn't match an item in the database). In this case, this value is replaced by a null value.
      keyFieldName - The name of the field, that contain the key, that is: the T's attribute the can be matched against the keys list. For instance: "id"
      Returns:
      A list of T instances coming from the unorderedList, where the key (retrieved by the getter method) of these instances is in the exact same order as the keys list. Missing values in the unorderedList list are replaced by null.
    • getFieldType

      public java.lang.Class<?> getFieldType​(java.lang.Class<?> owningClass, java.lang.String fieldName, boolean returnIsMandatory) throws GraphQLRequestPreparationException
      Retrieves the class of the fieldName field of the owningClass class.
      Parameters:
      owningClass -
      fieldName -
      returnIsMandatory - If true, a GraphQLRequestPreparationException 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
    • getArgument

      public java.lang.Object getArgument​(java.lang.Object jsonParsedValue, java.lang.String graphQLTypeName, java.lang.String javaTypeForIDType, java.lang.Class<?> clazz)
      This method returns a GraphQL argument into the relevant Java object, within a data fetcher, from what has been parsed by the graphql-java engine from the incoming JSON request
      Type Parameters:
      T - The class expected to be returned
      Parameters:
      jsonParsedValue - The value, read from the JSON in the GraphQL request. Only the part of the JSON map, related to the expected class is sent. It can be:
      • A Map. This map will be transformed into an input object, as defined in the GraphQL schema, from the Map that has been read from the JSON object sent to the server.
      • A List. In this case, returns a list of instances of the given clazz type.
      • Otherwise, the value is a scalar. At this stage, Custom Scalars have already been transformed into the relevant Java Type. So it must be a standard scalar. It is then mapped to the asked java type
      graphQLTypeName - The name of the GraphQL type, as defined in the GraphQL schema. This can be guessed from the given class for input types and objects, but not for scalars. So it must be provided.
      javaTypeForIDType - Value of the plugin parameter of the same name. This is necessary to properly manage fields of the ID GraphQL type, which must be transformed to this java type. This is useful only when mapping into input types.
      clazz - The class of the expected type. A new instance of this type will be returned, with its fields having been 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
    • getDeclaredField

      public java.lang.reflect.Field getDeclaredField​(java.lang.Class<?> owningClass, java.lang.String fieldName, boolean mustFindField) throws GraphQLRequestPreparationException
      Returns a Field 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 field
      mustFindField - If true and the field is not found, a GraphQLRequestPreparationException 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 java.lang.reflect.Method getGetter​(java.lang.Class<?> 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 a RuntimeException
      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 a RuntimeException
      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 the Field of the given name, on the given object. All check exceptions are hidden in a RuntimeException
      Parameters:
      object -
      fieldName -
      value -
      Throws:
      java.lang.RuntimeException - If any exception occurs
    • addImport

      public void addImport​(java.util.Set<java.lang.String> imports, java.lang.String targetPackageName, java.lang.String classname)
      Adds, if necessary the import calculated from the given parameters, into the given set of imports.
      Parameters:
      imports - The set of import, in which the import for the given parameters is to be added
      targetPackageName - The package in which is the class that will contain this import
      classname - the full classname of the class to import
    • 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 a RuntimeException
      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, java.lang.Object... args)
      Calls the 'methodName' method on the given object.
      Parameters:
      methodName - The name of the method. This method should have no parameter
      object - The given object, 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 a RuntimeException
      Parameters:
      method -
      o -
      value -
      Throws:
      java.lang.RuntimeException - When an exception occurs while accessing the setter
    • invokeStaticMethod

      public java.lang.Object invokeStaticMethod​(java.lang.String methodName, java.lang.Class<?> clazz)
      Calls the 'methodName' method on the given class.
      Parameters:
      methodName - The name of the method. This method should have no parameter
      clazz - The given class, on which the 'methodName' method is to be called
      Returns:
    • 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 classFullNameParam)
      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 classFullNameParam)
      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

      @SafeVarargs public final <T> java.util.stream.Stream<T> concatStreams​(java.lang.Class<T> clazz, boolean parallelStreams, T t1, T t2, T t3, java.util.List<? extends T>... lists)
      Concatenate a non limited number of lists into a stream.
      Type Parameters:
      T -
      Parameters:
      clazz - The T class
      parallelStreams - true if the returned stream should be a parallel one
      t1 - An optional item, that'll be added to the returned stream (if not null)
      t2 - An optional item, that'll be added to the returned stream (if not null)
      t3 - An optional item, that'll be added to the returned stream (if not null)
      lists -
      Returns:
    • getValueAsText

      public java.lang.String getValueAsText​(graphql.language.Value<?> value)
      Returns the given value, as text, as it can be written into a generated GraphQL schema.
      A str string default value will be returned as "str",a JEDI enum value will be returned as JEDI, ...
      Returns:
    • getValueAsString

      public java.lang.String getValueAsString​(graphql.language.Value<?> value)
      Returns the given value, as string, as it can be written into the GraphQLDirective.parameterValues() of the GraphQLDirective java annotation.
      A str string default value will be returned as "str",a JEDI enum value will be returned as "JEDI", an object will be returned as "{name:\"specific name\",appearsIn:[NEWHOPE,EMPIRE],type:\"Human\"}"...
      Returns:
    • getLastModified

      public java.lang.Long getLastModified​(java.io.File fileOrFolder, boolean maxValue)
      Returns the maximum or minimum value for the lastModified of the given file, or of all the files (not folders) contained into this folder.
      Parameters:
      fileOrFolder - A file or a folder
      maxValue - If true and fileOrFolder is a folder, then this method returns the maximum File.lastModified() found for all its files. If false, then the minimum value is returned.
      Returns:
      if fileOrFolder doesn't exist, then returns null. If fileOrFolder is a file, then returns its File.lastModified() value. Otherwise its a folder. Then it loops into this folder, its subfolders (and so on), and returns the maximum or the minimum (depending on the value of maxValue) lastModified date found for the files found. The date of the directories are ignored.
    • getQuotedScanBasePackages

      public java.lang.String getQuotedScanBasePackages​(java.lang.String scanBasePackages)