Class GraphqlUtils

java.lang.Object
com.graphql_java_generator.util.GraphqlUtils

@Component public class GraphqlUtils extends Object
Author:
etienne-sf
  • Field Details

    • graphqlUtils

      public static GraphqlUtils graphqlUtils
      This singleton is usable in default method, within interfaces
    • JAVA_KEYWORD_PREFIX

      public static Character JAVA_KEYWORD_PREFIX
  • Constructor Details

    • GraphqlUtils

      public GraphqlUtils()
  • Method Details

    • getRuntimeVersion

      public 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 String getCamelCase(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 String getPascalCase(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> List<I> iterableToList(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> List<I> iterableConcreteClassToListInterface(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(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> List<T> orderList(List<?> keys, List<T> unorderedList, 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.
    • getSetter

      public <T> Method getSetter(Class<T> clazz, Field field)
      Retrieves the setter for the given field on the given field
      Type Parameters:
      T -
      Parameters:
      t -
      field -
      Returns:
    • getGetter

      public Method getGetter(Class<?> clazz, Field field)
      Retrieves the getter for the given field on the given field
      Type Parameters:
      T -
      Parameters:
      t -
      field -
      Returns:
    • invokeGetter

      public Object invokeGetter(Object object, 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:
      RuntimeException - If any exception occurs
    • invokeSetter

      public void invokeSetter(Object object, Field field, 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:
      RuntimeException - If any exception occurs
    • invokeSetter

      public void invokeSetter(Object object, String fieldName, 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:
      RuntimeException - If any exception occurs
    • getMethod

      public Method getMethod(String methodName, Class<?> clazz, 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:
      RuntimeException - When an exception occurs while getting the method
    • invokeMethod

      public Object invokeMethod(String methodName, Object object, 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 Object invokeMethod(Method method, Object o, 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:
      RuntimeException - When an exception occurs while accessing the setter
    • invokeStaticMethod

      public Object invokeStaticMethod(String methodName, 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 String getJavaName(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(String name)
      Returns true if name is a reserved java keyword
      Parameters:
      name -
      Returns:
    • getClassSimpleName

      public String getClassSimpleName(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 String getPackageName(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> Stream<T> concatStreams(Class<T> clazz, boolean parallelStreams, T t1, T t2, T t3, 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 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 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:
    • stringToEnumValue

      public Object stringToEnumValue(Object enumValue, Class<?> enumClass)
      Returns the given enumValue given as a String, into the enum value from the generated POJO of the given class.
      This can not be a parameterized method as the return value may be either an enum, a list of enums...
      Parameters:
      enumValue - May be null, a String, a list of String, a list of lists of String...
      enumClass - The POJO class of the enum
      Returns:
      The same kind of list, but all String representations are replaced by the relevant enum value, based on the GraphQL schema.
    • getLastModified

      public Long getLastModified(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 String getQuotedScanBasePackages(String scanBasePackages)