Example #1
0
  /**
   * Check that the set of <code>LocalInstance</code>s <code>localsUsed</code>, which is the set of
   * locals used in the inner class declared by <code>cb</code> are initialized before the class
   * declaration.
   */
  protected void checkLocalsUsedByInnerClass(
      FlowGraph graph, ClassBody cb, Set localsUsed, DataFlowItem dfIn, DataFlowItem dfOut)
      throws SemanticException {
    for (Iterator iter = localsUsed.iterator(); iter.hasNext(); ) {
      LocalInstance li = (LocalInstance) iter.next();
      MinMaxInitCount initCount = (MinMaxInitCount) dfOut.initStatus.get(li);
      if (!currCBI.localDeclarations.contains(li)) {
        // the local wasn't defined in this scope.
        currCBI.outerLocalsUsed.add(li);
      } else if (initCount == null || InitCount.ZERO.equals(initCount.getMin())) {
        // initCount will in general not be null, as the local variable
        // li is declared in the current class; however, if the inner
        // class is declared in the initializer of the local variable
        // declaration, then initCount could in fact be null, as we
        // leave the inner class before we have performed flowLocalDecl
        // for the local variable declaration.

        throw new SemanticException(
            "Local variable \""
                + li.name()
                + "\" must be initialized before the class "
                + "declaration.",
            cb.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
  /** 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);
    }
  }
Example #4
0
  public Map flow(Item in, FlowGraph graph, Term n, Set succEdgeKeys) {
    if (in == DataFlowItem.NOT_REACHABLE) {
      return itemToMap(in, succEdgeKeys);
    }

    // in is either REACHABLE or REACHABLE_EX_ONLY.
    // return a map where all exception edges are REACHABLE_EX_ONLY,
    // and all non-exception edges are REACHABLE.
    Map m = itemToMap(DataFlowItem.REACHABLE_EX_ONLY, succEdgeKeys);

    if (succEdgeKeys.contains(FlowGraph.EDGE_KEY_OTHER)) {
      m.put(FlowGraph.EDGE_KEY_OTHER, DataFlowItem.REACHABLE);
    }
    if (succEdgeKeys.contains(FlowGraph.EDGE_KEY_TRUE)) {
      m.put(FlowGraph.EDGE_KEY_TRUE, DataFlowItem.REACHABLE);
    }
    if (succEdgeKeys.contains(FlowGraph.EDGE_KEY_FALSE)) {
      m.put(FlowGraph.EDGE_KEY_FALSE, DataFlowItem.REACHABLE);
    }

    return m;
  }