コード例 #1
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());
          }
        }
      }
    }
  }
コード例 #2
0
ファイル: AnonymousDecl.java プロジェクト: Bludge0n/AREsoft
  // Declared in AnonymousClasses.jrag at line 52
  private AnonymousDecl rewriteRule0() {
    {
      setModifiers(new Modifiers(new List().add(new Modifier("final"))));

      ConstructorDecl constructor = new ConstructorDecl();
      addBodyDecl(constructor);

      constructor.setModifiers((Modifiers) constructorDecl().getModifiers().fullCopy());
      String name = "Anonymous" + nextAnonymousIndex();
      setID(name);
      constructor.setID(name);

      List parameterList = new List();
      for (int i = 0; i < constructorDecl().getNumParameter(); i++) {
        parameterList.add(
            new ParameterDeclaration(
                constructorDecl().getParameter(i).type().createBoundAccess(),
                constructorDecl().getParameter(i).name()));
      }
      constructor.setParameterList(parameterList);

      List argList = new List();
      for (int i = 0; i < constructor.getNumParameter(); i++)
        argList.add(new VarAccess(constructor.getParameter(i).name()));
      constructor.setConstructorInvocation(
          new ExprStmt(new SuperConstructorAccess("super", argList)));
      constructor.setBlock(new Block());

      HashSet set = new HashSet();
      for (int i = 0; i < getNumBodyDecl(); i++) {
        if (getBodyDecl(i) instanceof InstanceInitializer) {
          InstanceInitializer init = (InstanceInitializer) getBodyDecl(i);
          set.addAll(init.exceptions());
        } else if (getBodyDecl(i) instanceof FieldDeclaration) {
          FieldDeclaration f = (FieldDeclaration) getBodyDecl(i);
          if (f.isInstanceVariable()) {
            set.addAll(f.exceptions());
          }
        }
      }
      List exceptionList = new List();
      for (Iterator iter = set.iterator(); iter.hasNext(); ) {
        TypeDecl exceptionType = (TypeDecl) iter.next();
        if (exceptionType.isNull()) exceptionType = typeNullPointerException();
        exceptionList.add(exceptionType.createQualifiedAccess());
      }
      constructor.setExceptionList(exceptionList);
      return this;
    }
  }
コード例 #3
0
ファイル: InitChecker.java プロジェクト: tianhuat/polyglot
  /** 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);
    }
  }