Exemple #1
0
 static MinMaxInitCount join(MinMaxInitCount initCount1, MinMaxInitCount initCount2) {
   if (initCount1 == null) {
     return initCount2;
   }
   if (initCount2 == null) {
     return initCount1;
   }
   MinMaxInitCount t =
       new MinMaxInitCount(
           InitCount.min(initCount1.getMin(), initCount2.getMin()),
           InitCount.max(initCount1.getMax(), initCount2.getMax()));
   return t;
 }
Exemple #2
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());
     }
   }
 }
Exemple #3
0
  /** Check that the assignment to a local variable is correct. */
  protected void checkLocalAssign(
      FlowGraph graph, LocalAssign a, DataFlowItem dfIn, DataFlowItem dfOut)
      throws SemanticException {
    LocalInstance li = ((Local) a.left()).localInstance();
    if (!currCBI.localDeclarations.contains(li)) {
      throw new SemanticException(
          "Final local variable \"" + li.name() + "\" cannot be assigned to in an inner class.",
          a.position());
    }

    MinMaxInitCount initCount = (MinMaxInitCount) dfOut.initStatus.get(li);

    if (li.flags().isFinal() && InitCount.MANY.equals(initCount.getMax())) {
      throw new SemanticException(
          "variable \"" + li.name() + "\" might already have been assigned to", a.position());
    }
  }
Exemple #4
0
  /** Perform the appropriate flow operations for assignment to a local variable */
  protected Map flowLocalAssign(
      DataFlowItem inItem, FlowGraph graph, LocalAssign a, Set succEdgeKeys) {
    Local l = (Local) a.left();
    Map m = new HashMap(inItem.initStatus);
    MinMaxInitCount initCount = (MinMaxInitCount) m.get(l.localInstance());

    // initcount could be null if the local is defined in the outer
    // class, or if we have not yet seen its declaration (i.e. the
    // local is used in its own initialization)
    if (initCount == null) {
      initCount = new MinMaxInitCount(InitCount.ZERO, InitCount.ZERO);
    }

    initCount = new MinMaxInitCount(initCount.getMin().increment(), initCount.getMax().increment());

    m.put(l.localInstance(), initCount);
    return itemToMap(new DataFlowItem(m), succEdgeKeys);
  }
Exemple #5
0
  /** Perform the appropriate flow operations for assignment to a field */
  protected Map flowFieldAssign(
      DataFlowItem inItem, FlowGraph graph, FieldAssign a, Set succEdgeKeys) {
    Field f = (Field) a.left();
    FieldInstance fi = f.fieldInstance();

    if (fi.flags().isFinal() && isFieldsTargetAppropriate(f)) {
      // this field is final and the target for this field is
      // appropriate for what we are interested in.
      Map m = new HashMap(inItem.initStatus);
      MinMaxInitCount initCount = (MinMaxInitCount) m.get(fi);
      // initCount may be null if the field is defined in an
      // outer class.
      if (initCount != null) {
        initCount =
            new MinMaxInitCount(initCount.getMin().increment(), initCount.getMax().increment());
        m.put(fi, initCount);
        return itemToMap(new DataFlowItem(m), succEdgeKeys);
      }
    }
    return null;
  }