Beispiel #1
0
  /**
   * Check that each non static final field has been initialized exactly once, taking into account
   * the fact that constructors may call other constructors.
   *
   * @param cb The ClassBody of the class declaring the fields to check.
   * @throws SemanticException
   */
  protected void checkNonStaticFinalFieldsInit(ClassBody cb) throws SemanticException {
    // for each non-static final field instance, check that all
    // constructors intialize it exactly once, taking into account constructor calls.
    for (Iterator iter = currCBI.currClassFinalFieldInitCounts.keySet().iterator();
        iter.hasNext(); ) {
      FieldInstance fi = (FieldInstance) iter.next();
      if (fi.flags().isFinal() && !fi.flags().isStatic()) {
        // the field is final and not static
        // it must be initialized exactly once.
        // navigate up through all of the the constructors
        // that this constructor calls.

        boolean fieldInitializedBeforeConstructors = false;
        MinMaxInitCount ic = (MinMaxInitCount) currCBI.currClassFinalFieldInitCounts.get(fi);
        if (ic != null && !InitCount.ZERO.equals(ic.getMin())) {
          fieldInitializedBeforeConstructors = true;
        }

        for (Iterator iter2 = currCBI.allConstructors.iterator(); iter2.hasNext(); ) {
          ConstructorDecl cd = (ConstructorDecl) iter2.next();
          ConstructorInstance ciStart = cd.constructorInstance();
          ConstructorInstance ci = ciStart;

          boolean isInitialized = fieldInitializedBeforeConstructors;

          while (ci != null) {
            Set s = (Set) currCBI.fieldsConstructorInitializes.get(ci);
            if (s != null && s.contains(fi)) {
              if (isInitialized) {
                throw new SemanticException(
                    "field \"" + fi.name() + "\" might have already been initialized",
                    cd.position());
              }
              isInitialized = true;
            }
            ci = (ConstructorInstance) currCBI.constructorCalls.get(ci);
          }
          if (!isInitialized) {
            throw new SemanticException(
                "field \"" + fi.name() + "\" might not have been initialized", ciStart.position());
          }
        }
      }
    }
  }
Beispiel #2
0
  /** Perform necessary actions upon seeing the ConstructorDecl <code>cd</code>. */
  protected void finishConstructorDecl(
      FlowGraph graph, ConstructorDecl cd, DataFlowItem dfIn, DataFlowItem dfOut) {
    ConstructorInstance ci = cd.constructorInstance();

    // we need to set currCBI.fieldsConstructorInitializes correctly.
    // It is meant to contain the non-static final fields that the
    // constructor ci initializes.
    //
    // Note that dfOut.initStatus contains only the MinMaxInitCounts
    // for _normal_ termination of the constructor (see the
    // method confluence). This means that if dfOut says the min
    // count of the initialization for a final non-static field
    // is one, and that is different from what is recoreded in
    // currCBI.currClassFinalFieldInitCounts (which is the counts
    // of the initializations performed by initializers), then
    // the constructor does indeed initialize the field.

    Set s = new HashSet();

    // go through every final non-static field in dfOut.initStatus
    Iterator iter = dfOut.initStatus.entrySet().iterator();
    while (iter.hasNext()) {
      Entry e = (Entry) iter.next();
      if (e.getKey() instanceof FieldInstance
          && ((FieldInstance) e.getKey()).flags().isFinal()
          && !((FieldInstance) e.getKey()).flags().isStatic()) {
        // we have a final non-static field
        FieldInstance fi = (FieldInstance) e.getKey();
        MinMaxInitCount initCount = (MinMaxInitCount) e.getValue();
        MinMaxInitCount origInitCount =
            (MinMaxInitCount) currCBI.currClassFinalFieldInitCounts.get(fi);
        if (initCount.getMin() == InitCount.ONE
            && (origInitCount == null || origInitCount.getMin() == InitCount.ZERO)) {
          // the constructor initialized this field
          s.add(fi);
        }
      }
    }
    if (!s.isEmpty()) {
      currCBI.fieldsConstructorInitializes.put(ci, s);
    }
  }