Example #1
0
  /** Write the coverage table */
  public void writeCoverageTable(
      Environment env, ClassDefinition c, DataOutputStream out, ConstantPool tab, long whereField)
      throws IOException {
    Vector TableLot = new Vector(); /* Coverage table */
    boolean begseg = false;
    boolean begmeth = false;
    long whereClass = ((SourceClass) c).getWhere();
    Vector whereTry = new Vector();
    int numberTry = 0;
    int count = 0;

    for (Instruction inst = first; inst != null; inst = inst.next) {
      long n = (inst.where >> WHEREOFFSETBITS);
      if (n > 0 && inst.opc != opc_label) {
        if (!begmeth) {
          if (whereClass == inst.where)
            TableLot.addElement(new Cover(CT_FIKT_METHOD, whereField, inst.pc));
          else TableLot.addElement(new Cover(CT_METHOD, whereField, inst.pc));
          count++;
          begmeth = true;
        }
        if (!begseg && !inst.flagNoCovered) {
          boolean findTry = false;
          for (Enumeration e = whereTry.elements(); e.hasMoreElements(); ) {
            if (((Long) (e.nextElement())).longValue() == inst.where) {
              findTry = true;
              break;
            }
          }
          if (!findTry) {
            TableLot.addElement(new Cover(CT_BLOCK, inst.where, inst.pc));
            count++;
            begseg = true;
          }
        }
      }
      switch (inst.opc) {
        case opc_label:
          begseg = false;
          break;
        case opc_ifeq:
        case opc_ifne:
        case opc_ifnull:
        case opc_ifnonnull:
        case opc_ifgt:
        case opc_ifge:
        case opc_iflt:
        case opc_ifle:
        case opc_if_icmpeq:
        case opc_if_icmpne:
        case opc_if_icmpgt:
        case opc_if_icmpge:
        case opc_if_icmplt:
        case opc_if_icmple:
        case opc_if_acmpeq:
        case opc_if_acmpne:
          {
            if (inst.flagCondInverted) {
              TableLot.addElement(new Cover(CT_BRANCH_TRUE, inst.where, inst.pc));
              TableLot.addElement(new Cover(CT_BRANCH_FALSE, inst.where, inst.pc));
            } else {
              TableLot.addElement(new Cover(CT_BRANCH_FALSE, inst.where, inst.pc));
              TableLot.addElement(new Cover(CT_BRANCH_TRUE, inst.where, inst.pc));
            }
            count += 2;
            begseg = false;
            break;
          }

        case opc_goto:
          {
            begseg = false;
            break;
          }

        case opc_ret:
        case opc_return:
        case opc_ireturn:
        case opc_lreturn:
        case opc_freturn:
        case opc_dreturn:
        case opc_areturn:
        case opc_athrow:
          {
            break;
          }

        case opc_try:
          {
            whereTry.addElement(new Long(inst.where));
            begseg = false;
            break;
          }

        case opc_tableswitch:
          {
            SwitchData sw = (SwitchData) inst.value;
            for (int i = sw.minValue; i <= sw.maxValue; i++) {
              TableLot.addElement(new Cover(CT_CASE, sw.whereCase(new Integer(i)), inst.pc));
              count++;
            }
            if (!sw.getDefault()) {
              TableLot.addElement(new Cover(CT_SWITH_WO_DEF, inst.where, inst.pc));
              count++;
            } else {
              TableLot.addElement(new Cover(CT_CASE, sw.whereCase("default"), inst.pc));
              count++;
            }
            begseg = false;
            break;
          }
        case opc_lookupswitch:
          {
            SwitchData sw = (SwitchData) inst.value;
            for (Enumeration e = sw.sortedKeys(); e.hasMoreElements(); ) {
              Integer v = (Integer) e.nextElement();
              TableLot.addElement(new Cover(CT_CASE, sw.whereCase(v), inst.pc));
              count++;
            }
            if (!sw.getDefault()) {
              TableLot.addElement(new Cover(CT_SWITH_WO_DEF, inst.where, inst.pc));
              count++;
            } else {
              TableLot.addElement(new Cover(CT_CASE, sw.whereCase("default"), inst.pc));
              count++;
            }
            begseg = false;
            break;
          }
      }
    }
    Cover Lot;
    long ln, pos;

    out.writeShort(count);
    for (int i = 0; i < count; i++) {
      Lot = (Cover) TableLot.elementAt(i);
      ln = (Lot.Addr >> WHEREOFFSETBITS);
      pos = (Lot.Addr << (64 - WHEREOFFSETBITS)) >> (64 - WHEREOFFSETBITS);
      out.writeShort(Lot.NumCommand);
      out.writeShort(Lot.Type);
      out.writeInt((int) ln);
      out.writeInt((int) pos);

      if (!(Lot.Type == CT_CASE && Lot.Addr == 0)) {
        JcovClassCountArray[Lot.Type]++;
      }
    }
  }