Class EnumConstant

All Implemented Interfaces:
Operation

public class EnumConstant extends CallableOperation
EnumConstant is an Operation representing a constant value from an enum.

Using the formal notation in Operation, a constant named BLUE from the enum Colors is an operation BLUE : [] → Colors.

Execution simply returns the constant value.

  • Field Details

    • value

      private Enum<?> value
  • Constructor Details

    • EnumConstant

      public EnumConstant(Enum<?> value)
  • Method Details

    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • equalsEnumConstant

      public boolean equalsEnumConstant(EnumConstant e)
      Returns true if this equals the given object.
      Parameters:
      e - the value to compare against
      Returns:
      true iff this equals the given value
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getName

      public String getName()
      Description copied from interface: Operation
      Returns the name for the operation.
      Returns:
      the name for this operation
    • execute

      public ExecutionOutcome execute(Object[] statementInput)
      Performs this operation using the array of input values. Returns the results of execution as an ResultOrException object and can output results to specified PrintStream.
      Specified by:
      execute in class CallableOperation
      Parameters:
      statementInput - array containing appropriate inputs to operation
      Returns:
      a NormalExecution object holding the value of the enum constant
    • appendCode

      public void appendCode(Type declaringType, TypeTuple inputTypes, Type outputType, List<Variable> inputVars, StringBuilder b)
      Produces a Java source code representation of this statement and appends it to the given StringBuilder.

      Adds qualified name of enum constant.

      Specified by:
      appendCode in class CallableOperation
      Parameters:
      declaringType - the declaring type for this operation
      inputTypes - the input types for this operation
      outputType - the output type for this operation
      inputVars - the list of variables that are inputs to operation
      b - the StringBuilder to which code is added
    • toParsableString

      public String toParsableString(Type declaringType, TypeTuple inputTypes, Type outputType)
      Returns a string representation of this Operation, which can be read by static parse method for class. For a class C implementing the Operation interface, this method should return a String s such that parsing the string returns an object equivalent to this object, i.e. C.parse(this.s).equals(this).

      Issues a string representation of an enum constant as a type-value pair. The parse function should return an equivalent object.

      Specified by:
      toParsableString in class CallableOperation
      Parameters:
      declaringType - the declaring type for this operation
      inputTypes - the input types for this operation
      outputType - the output type for this operation
      Returns:
      a string representation of this operation
      See Also:
    • parse

      public static TypedClassOperation parse(String desc) throws OperationParseException
      Parses the description of an enum constant value in a string as returned by toParsableString(Type, TypeTuple, Type).

      Valid strings may be of the form EnumType:EnumValue, or OuterClass$InnerEnum:EnumValue for an enum that is an inner type of a class.

      Parameters:
      desc - string representing type-value pair for an enum constant
      Returns:
      the enum constant operation for the string descriptor
      Throws:
      OperationParseException - if desc does not match expected form
    • value

      public Enum<?> value()
      value
      Returns:
      object for value of enum constant
    • getValue

      public Object getValue()
      Returns the "value" of an operation that is actually a ground term, meaning a constant of some form. Only null if value is null, otherwise throws an exception if there is not a reasonable meaning of value for type of operation.

      This is a hack to allow randoop.main.GenBranchDir to do mutation.

      Specified by:
      getValue in interface Operation
      Overrides:
      getValue in class CallableOperation
      Returns:
      value of enum constant
    • valueOf

      private static Enum<?> valueOf(Class<?> type, String valueName)
      valueOf searches the enum constant list of a class for a constant with the given name. Note: cannot make this work using valueOf method of Enum due to typing.
      Parameters:
      type - class that is already known to be an enum
      valueName - name for value that may be a constant of the enum
      Returns:
      reference to actual constant value, or null if none exists in type