Example #1
0
 /** Check that the assignment to a field is correct. */
 protected void checkFieldAssign(
     FlowGraph graph, FieldAssign a, DataFlowItem dfIn, DataFlowItem dfOut)
     throws SemanticException {
   Field f = (Field) a.left();
   FieldInstance fi = f.fieldInstance();
   if (fi.flags().isFinal()) {
     if ((currCBI.currCodeDecl instanceof ConstructorDecl
             || currCBI.currCodeDecl instanceof Initializer)
         && isFieldsTargetAppropriate(f)) {
       // we are in a constructor or initializer block and
       // if the field is static then the target is the class
       // at hand, and if it is not static then the
       // target of the field is this.
       // So a final field in this situation can be
       // assigned to at most once.
       MinMaxInitCount initCount = (MinMaxInitCount) dfOut.initStatus.get(fi);
       if (InitCount.MANY.equals(initCount.getMax())) {
         throw new SemanticException(
             "field \"" + fi.name() + "\" might already have been assigned to", a.position());
       }
     } else {
       // not in a constructor or intializer, or the target is
       // not appropriate. So we cannot assign
       // to a final field at all.
       throw new SemanticException(
           "Cannot assign a value " + "to final field \"" + fi.name() + "\"", a.position());
     }
   }
 }
Example #2
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());
          }
        }
      }
    }
  }
Example #3
0
 /**
  * Check that each static final field is initialized exactly once.
  *
  * @param cb The ClassBody of the class declaring the fields to check.
  * @throws SemanticException
  */
 protected void checkStaticFinalFieldsInit(ClassBody cb) throws SemanticException {
   // check that all static fields have been initialized exactly once.
   for (Iterator iter = currCBI.currClassFinalFieldInitCounts.entrySet().iterator();
       iter.hasNext(); ) {
     Map.Entry e = (Map.Entry) iter.next();
     if (e.getKey() instanceof FieldInstance) {
       FieldInstance fi = (FieldInstance) e.getKey();
       if (fi.flags().isStatic() && fi.flags().isFinal()) {
         MinMaxInitCount initCount = (MinMaxInitCount) e.getValue();
         if (InitCount.ZERO.equals(initCount.getMin())) {
           throw new SemanticException(
               "field \"" + fi.name() + "\" might not have been initialized", cb.position());
         }
       }
     }
   }
 }