001/*
002 * Created on May 3, 2005
003 */
004
005package daikon.chicory;
006
007import java.util.EnumSet;
008import java.util.List;
009
010/**
011 * The DaikonClassInfo class is a subtype of DaikonVariableInfo used for variables which represent
012 * the run-time type of a variable. They will have a VarType of CLASSNAME and their VarInfoName will
013 * end with the class_suffix: ".getClass().getName()".
014 */
015public class DaikonClassInfo extends DaikonVariableInfo {
016
017  /**
018   * Constructs a DaikonClassInfo object.
019   *
020   * @param theName the name of the variable
021   * @param typeName the name of the type
022   * @param repTypeName the name of the representation type
023   * @param function_args arguments used to create a function
024   * @param isArr true iff the variable represents an array of run-time classes
025   */
026  public DaikonClassInfo(
027      String theName, String typeName, String repTypeName, String function_args, boolean isArr) {
028    super(theName, typeName, repTypeName, isArr);
029
030    this.function_args = function_args;
031  }
032
033  // .class variables are derived, so just keep the parent value
034  @Override
035  public Object getMyValFromParentVal(Object value) {
036    return value;
037  }
038
039  @Override
040  public String getDTraceValueString(Object val) {
041    if (isArray) {
042      if (val instanceof NonsensicalObject) {
043        return "nonsensical" + DaikonWriter.lineSep + "2";
044      }
045
046      // A list of the run-time type of each value in the array.
047      @SuppressWarnings("unchecked")
048      List<String> name_list = DTraceWriter.getTypeNameList((List<Object>) val);
049      if (name_list == null) {
050        return "nonsensical" + DaikonWriter.lineSep + "2";
051      }
052      return StringInfo.getStringList(name_list);
053    } else {
054      return getValueStringNonArr(val);
055    }
056  }
057
058  /**
059   * Get a String representation of the given Object's run-time type and the corresponding
060   * "modified" value.
061   *
062   * @param val the Object whose run-time class we wish to get a String representation of
063   * @return string representation (suitable for a {@code .dtrace} file) of the given Object's
064   *     run-time type, and the "modified" value (modbit)
065   */
066  public String getValueStringNonArr(Object val) {
067    String valString;
068
069    if (val == null || val instanceof NonsensicalObject) {
070      valString = "nonsensical" + DaikonWriter.lineSep + "2";
071    } else {
072      valString =
073          ("\"" + DTraceWriter.stdClassName(val.getClass()) + "\"") + DaikonWriter.lineSep + "1";
074    }
075
076    return valString;
077  }
078
079  /** Returns function since essentially this is a call to a pure function. */
080  @Override
081  public VarKind get_var_kind() {
082    return VarKind.FUNCTION;
083  }
084
085  /** Returns the name of this field. */
086  @Override
087  public String get_relative_name() {
088    // need to skip the leading "."
089    return DaikonVariableInfo.class_suffix_relative_name;
090  }
091
092  @Override
093  public EnumSet<VarFlags> get_var_flags() {
094    EnumSet<VarFlags> flags = super.get_var_flags();
095    flags.add(VarFlags.SYNTHETIC);
096    flags.add(VarFlags.CLASSNAME);
097    flags.add(VarFlags.NON_NULL);
098    return flags;
099  }
100}