コード例 #1
0
ファイル: InitChecker.java プロジェクト: tianhuat/polyglot
  /**
   * 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());
      }
    }
  }
コード例 #2
0
ファイル: InitChecker.java プロジェクト: tianhuat/polyglot
 /**
  * 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());
         }
       }
     }
   }
 }
コード例 #3
0
ファイル: InitChecker.java プロジェクト: tianhuat/polyglot
  /**
   * 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());
          }
        }
      }
    }
  }
コード例 #4
0
ファイル: InitChecker.java プロジェクト: tianhuat/polyglot
 /** Check that the local variable <code>l</code> is used correctly. */
 protected void checkLocal(FlowGraph graph, Local l, DataFlowItem dfIn, DataFlowItem dfOut)
     throws SemanticException {
   if (!currCBI.localDeclarations.contains(l.localInstance())) {
     // it's a local variable that has not been declared within
     // this scope. The only way this can arise is from an
     // inner class that is not a member of a class (typically
     // a local class, or an anonymous class declared in a method,
     // constructor or initializer).
     // We need to check that it is a final local, and also
     // keep track of it, to ensure that it has been definitely
     // assigned at this point.
     currCBI.outerLocalsUsed.add(l.localInstance());
   } else {
     MinMaxInitCount initCount = (MinMaxInitCount) dfIn.initStatus.get(l.localInstance());
     if (initCount != null && InitCount.ZERO.equals(initCount.getMin())) {
       // the local variable may not have been initialized.
       // However, we only want to complain if the local is reachable
       if (l.reachable()) {
         throw new SemanticException(
             "Local variable \"" + l.name() + "\" may not have been initialized", l.position());
       }
     }
   }
 }