/** 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()); } } }
/** * 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()); } } }
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; }
/** * The confluence operator is essentially the union of all of the inItems. However, if two or more * of the initCount maps from the inItems each have a MinMaxInitCounts entry for the same * VarInstance, the conflict must be resolved, by using the minimum of all mins and the maximum of * all maxs. */ public Item confluence(List inItems, Term node, FlowGraph graph) { // Resolve any conflicts pairwise. Iterator iter = inItems.iterator(); Map m = null; while (iter.hasNext()) { Item itm = (Item) iter.next(); if (itm == BOTTOM) continue; if (m == null) { m = new HashMap(((DataFlowItem) itm).initStatus); } else { Map n = ((DataFlowItem) itm).initStatus; for (Iterator iter2 = n.entrySet().iterator(); iter2.hasNext(); ) { Map.Entry entry = (Map.Entry) iter2.next(); VarInstance v = (VarInstance) entry.getKey(); MinMaxInitCount initCount1 = (MinMaxInitCount) m.get(v); MinMaxInitCount initCount2 = (MinMaxInitCount) entry.getValue(); m.put(v, MinMaxInitCount.join(initCount1, initCount2)); } } } if (m == null) return BOTTOM; return new DataFlowItem(m); }
/** 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()); } }
/** * 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()); } } } } }
/** * 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()); } } } } }
/** 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); }
/** 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); } }
/** 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; }
/** 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()); } } } }