Class CodeJavaOrKotlinGenerator

java.lang.Object
org.evomaster.client.java.controller.problem.rpc.CodeJavaOrKotlinGenerator

public class CodeJavaOrKotlinGenerator extends Object
a set of util for generating instance creation and assertion with java in tests in order to make endpoint invocation
  • Constructor Details

    • CodeJavaOrKotlinGenerator

      public CodeJavaOrKotlinGenerator()
  • Method Details

    • handleClassNameWithGeneric

      public static String handleClassNameWithGeneric(String fullName, List<String> genericTypes)
      representation of class with generic
    • handleEscapeCharInString

      public static String handleEscapeCharInString(String orgValue, boolean isJava)
      handle escape char in string in java https://docs.oracle.com/javase/tutorial/java/data/characters.html
      Parameters:
      orgValue - is an original string
      isJava -
      Returns:
      string with handled escape char
    • typeNameOfArrayOrCollection

      public static String typeNameOfArrayOrCollection(String collectionType, boolean isArray, String genericType, boolean isJava)
    • enumValue

      public static String enumValue(String enumTypeName, String itemName)
      Parameters:
      enumTypeName - the enum name
      itemName - the item name of the enum
      Returns:
      a string which could retrieve the item of a Enum type, eg, Gender.Female
    • oneLineInstance

      public static String oneLineInstance(boolean isDeclaration, boolean doesIncludeName, String fullName, String varName, String value, boolean isJava, boolean isNullable)
      create an instance with one line eg, fullName varName = value;
      Parameters:
      isDeclaration - whether the instance is also for declaration
      doesIncludeName - whether to include variable name
      fullName - is the full name of the variable
      varName - is the variable name
      value - is string to create the instance
      isJava -
      isNullable -
      Returns:
      a string which could create the instance
    • oneLineInstance

      public static String oneLineInstance(boolean isDeclaration, boolean doesIncludeName, String fullName, String varName, String value, Boolean isPrimitive, boolean isJava, boolean isNullable)
      create an instance with one line eg, fullName varName = value;
      Parameters:
      isDeclaration - whether the instance is also for declaration
      doesIncludeName - whether to include variable name
      fullName - is the full name of the variable
      varName - is the variable name
      value - is string to create the instance
      isPrimitive - indicates whether it is primitive type
      isJava -
      isNullable -
      Returns:
      a string which could create the instance
    • oneLineSetterInstance

      public static String oneLineSetterInstance(String setterMethodName, String fullName, String varName, String value, boolean isJava, boolean isNullable)
      set instance with setter eg, varName.setterMethodName((fullName)value)
      Parameters:
      setterMethodName - is the setter method name
      fullName - is full name of the instance
      varName - is variable name
      value - of the instance
      isJava -
      isNullable -
      Returns:
      a string which set instance
    • setInstanceObject

      public static String setInstanceObject(String fullName, String varName, boolean isJava)
      process [varName] = new Object()
      Parameters:
      fullName - specifies the full name of the class
      varName - specifies the variable name
      isJava -
      Returns:
      code to set value with new object which has default constructor
    • getStatementLast

      public static String getStatementLast(boolean isJava)
      handle last of statement for Java or kotlin
      Returns:
      eg, a string which appends ; at last for Java
    • newBuilderProto3

      public static String newBuilderProto3(String fullName, String varBuilderName, boolean isJava)
      Parameters:
      fullName - is the full name of the dto
      varBuilderName - is the variable name for the builder
      isJava -
      Returns:
      code to instantiate builder for proto3 dto
    • setInstance

      public static String setInstance(String varName, String instance, boolean isJava)
      process [varName] = [instance]
      Parameters:
      varName - specifies the variable name
      instance - specifies the instance
      isJava -
      Returns:
      code to set value
    • setInstance

      public static String setInstance(boolean includeVarName, String varName, String instance, boolean isJava)
      process [varName] = [instance]; or [instance];
      Parameters:
      includeVarName - specifies whether to set instance to variable
      varName - specifies the variable name
      instance - specifies the instance
      isJava -
      Returns:
      code to set value
    • newObjectConsParams

      public static String newObjectConsParams(String fullName, String params, boolean isJava)
      process new Object(p1, p2)
      Parameters:
      fullName - is a full name of the type of object
      params - is a string which could represent a list of params divided with ,
      isJava -
      Returns:
      code to new object with params constructor
    • newArray

      public static String newArray(String fullName, int length, boolean isJava)
      Parameters:
      fullName - is full name of the type
      length - is length of array to new
      isJava -
      Returns:
      a string which could create a new array, eg, new String[5]
    • newSet

      public static String newSet(boolean isJava, String genericType)
      Returns:
      a string which could create a new HashSet
    • newMap

      public static String newMap(boolean isJava, String keyGenericType, String valueGenericType)
      Returns:
      a string which could create a new HashMap
    • newList

      public static String newList(boolean isJava, String genericType)
      Returns:
      a string which could create a new ArrayList
    • getIndent

      public static String getIndent(int indent)
      Parameters:
      indent - is a number of indent (here we use space)
      Returns:
      a string which contains [indent] space
    • getStringListWithIndent

      public static List<String> getStringListWithIndent(List<String> codes, int indent)
      Parameters:
      codes - a list of codes, could be a block
      indent - is a number of indent for the codes
      Returns:
      a list of indented codes
    • addCode

      public static List<String> addCode(List<String> codes, String code, int indent)
      Parameters:
      codes - is a list of current code
      code - is one line code to be appended to the [codes]
      indent - is a number of indent for the [code]
      Returns:
      a list of codes appended with the [code]
    • codeBlockStart

      public static String codeBlockStart(boolean isJava)
    • codeBlockEnd

      public static String codeBlockEnd(boolean isJava)
    • addComment

      public static List<String> addComment(List<String> codes, String comment, int indent)
      Parameters:
      codes - is a list of current code
      comment - is one line comment
      indent - is a number of indent for the [code]
      Returns:
      a list of codes with comment
    • castToType

      public static String castToType(String typeName, String objCode, boolean isJava)
      cast object to a type
      Parameters:
      typeName - to cast
      objCode - is the code representing object to cast
      isJava -
      Returns:
      a java code which casts obj to a type
    • getGetClientMethod

      public static String getGetClientMethod(String controllerVarName, String interfaceName)
      process the code to get RPC client
      Parameters:
      controllerVarName - specifies the controller variable name
      interfaceName - specifies the interface name to get its corresponding client
      Returns:
      code which enables getting RPC client
    • methodInvocation

      public static String methodInvocation(String obj, String methodName, String params, boolean isJava, boolean objIsNullable, boolean isNotNullAsserted)
      Parameters:
      obj - specifies an object which owns the method. it is nullable
      methodName - specifies a name of the method
      params - specifies a list of params
      isJava -
      objIsNullable -
      isNotNullAsserted -
      Returns:
      code to invoke the method
    • fieldAccess

      public static String fieldAccess(String obj, String filedName, boolean isJava, boolean objIsNullable, boolean isNonNullAsserted)
    • junitAssertEquals

      public static String junitAssertEquals(String expectedValue, String variableName, boolean isJava)
      Parameters:
      expectedValue - is the excepted value
      variableName - is the variable name to be checked
      isJava -
      Returns:
      an assertion for equal with junit
    • junitAssertNull

      public static String junitAssertNull(String variableName, boolean isJava)
      Parameters:
      variableName - is the variable name to be checked
      isJava -
      Returns:
      a null assertion with junit
    • withSizeInAssertion

      public static String withSizeInAssertion(String variableName, boolean isJava, boolean isNullable)
      Parameters:
      variableName - is the variable which has size() method
      isJava -
      isNullable -
      Returns:
      a string to get size() of the variable
    • withLengthInAssertion

      public static String withLengthInAssertion(String variableName, boolean isJava, boolean isNullable)
      Parameters:
      variableName - is the variable which has length field
      isJava -
      isNullable -
      Returns:
      a string to get length of the variable
    • junitAssertNumbersMatch

      public static String junitAssertNumbersMatch(String expectedValue, String variableName, boolean isJava)
      junit does not support equal assertions for double type, then employ numbersMatch here
      Parameters:
      expectedValue - is the excepted value
      variableName - is the variable name to be checked
      isJava -
      Returns:
      a True assertion with numbersMatch method.
    • handleVariableName

      public static String handleVariableName(String original)
      some variable name might contain invalid symbol such as ., then need to reformat them
      Parameters:
      original - is the original name of the variable
      Returns:
      a reformatted variable name
    • randomNInt

      public static List<Integer> randomNInt(int max, int n)
      select n int from a range [0, max) Note that this is only used for deciding a number of data in collection to be asserted then it should not have any side-effect to randomness of search
      Parameters:
      max -
      n -
      Returns: