/* Monitor aspect for FailSafeEnum+*/ import java.util.*; import org.apache.commons.collections.map.*; class FailSafeEnumMonitorPM { Vector monitorList = new Vector(); synchronized public void create(Enumeration e, Vector v) { HashSet monitorSet = new HashSet(); monitorList.add(new FailSafeEnumMonitor()); Iterator it = monitorList.iterator(); while (it.hasNext()){ FailSafeEnumMonitor monitor = (FailSafeEnumMonitor)it.next(); monitor.create(e, v); if (monitorSet.contains(monitor) || monitor.failed()) it.remove(); else { monitorSet.add(monitor); if (monitor.suceeded()){ //System.out.println("the enumeration was changed during iterating!"); } } // for else } // for while } // end of method synchronized public void updatesource(Vector v) { HashSet monitorSet = new HashSet(); Iterator it = monitorList.iterator(); while (it.hasNext()){ FailSafeEnumMonitor monitor = (FailSafeEnumMonitor)it.next(); monitor.updatesource(v); if (monitorSet.contains(monitor) || monitor.failed()) it.remove(); else { monitorSet.add(monitor); if (monitor.suceeded()){ //System.out.println("the enumeration was changed during iterating!"); } } // for else } // for while } // end of method synchronized public void next(Enumeration e) { HashSet monitorSet = new HashSet(); Iterator it = monitorList.iterator(); while (it.hasNext()){ FailSafeEnumMonitor monitor = (FailSafeEnumMonitor)it.next(); monitor.next(e); if (monitorSet.contains(monitor) || monitor.failed()) it.remove(); else { monitorSet.add(monitor); if (monitor.suceeded()){ //System.out.println("the enumeration was changed during iterating!"); } } // for else } // for while } // end of method } class FailSafeEnumMonitor { /* %%_%_ERE_%_%% */ int state = 0; public int hashCode() { return state; } public boolean equals(Object o) { if (! (o instanceof FailSafeEnumMonitor)) return false; return state == ((FailSafeEnumMonitor)o).state; } boolean failed = false; boolean suceeded = false; public FailSafeEnumMonitor() { boolean create = false; boolean updatesource = false; boolean next = false; } synchronized public void create(Enumeration e, Vector v) { boolean create = false; boolean updatesource = false; boolean next = false; create = true; switch (state) { case 2 : state = updatesource ? 2 : next ? 3 : -1; break; case 0 : state = create ? 1 : -1; break; case 1 : state = updatesource ? 2 : next ? 1 : -1; break; default: state = -1; } failed = state == -1; suceeded = state == 3; } synchronized public void updatesource(Vector v) { boolean create = false; boolean updatesource = false; boolean next = false; updatesource = true; switch (state) { case 2 : state = updatesource ? 2 : next ? 3 : -1; break; case 0 : state = create ? 1 : -1; break; case 1 : state = updatesource ? 2 : next ? 1 : -1; break; default: state = -1; } failed = state == -1; suceeded = state == 3; } synchronized public void next(Enumeration e) { boolean create = false; boolean updatesource = false; boolean next = false; next = true; switch (state) { case 2 : state = updatesource ? 2 : next ? 3 : -1; break; case 0 : state = create ? 1 : -1; break; case 1 : state = updatesource ? 2 : next ? 1 : -1; break; default: state = -1; } failed = state == -1; suceeded = state == 3; } synchronized public boolean failed(){ return failed; } synchronized public boolean suceeded(){ return suceeded; } synchronized public void reset(){ boolean create = false; boolean updatesource = false; boolean next = false; state = 0; } } public aspect FailSafeEnum { Map makeMap(Object key){ if (key instanceof String) return new HashMap(); else return new ReferenceIdentityMap(AbstractReferenceMap.WEAK, AbstractReferenceMap.HARD, true); } List makeList(){ return new ArrayList(); } Map FailSafeEnum_e_v_Map = null; Map FailSafeEnum_v_Map = null; Map FailSafeEnum_e_Map = null; pointcut FailSafeEnum_create(Vector v) : call(* Vector+.elements())&& target(v)&& !within(dacapo..*)&& !within(FailSafeEnum)&& !within(FailSafeEnumMonitor)&& !within(FailSafeEnumMonitorPM) || call( Enumeration+.new(Vector+))&& args(v)&& !within(dacapo..*)&& !within(FailSafeEnum)&& !within(FailSafeEnumMonitor)&& !within(FailSafeEnumMonitorPM); dependent after create_FailSafeEnum(Vector v) returning(Enumeration e): FailSafeEnum_create(v) { Object obj = null; FailSafeEnumMonitorPM monitor = null; boolean toCreate = false; Map m = FailSafeEnum_e_v_Map; if (m == null) m = FailSafeEnum_e_v_Map = makeMap(e); synchronized(FailSafeEnum_e_v_Map){ obj = m.get(e); if (obj == null) { obj = makeMap(v); m.put(e, obj); } m = (Map)obj; obj = m.get(v); monitor = (FailSafeEnumMonitorPM)obj; toCreate = (monitor == null); if (toCreate){ monitor = new FailSafeEnumMonitorPM(); m.put(v, monitor); }//end of if }//end of lookup if (toCreate){ m = FailSafeEnum_v_Map; if (m == null) m = FailSafeEnum_v_Map = makeMap(v); obj = null; synchronized(FailSafeEnum_v_Map){ obj = m.get(v); List monitors = (List)obj; if (monitors == null) { monitors = makeList(); m.put(v, monitors); }//end of if monitors.add(monitor); }//end of adding m = FailSafeEnum_e_Map; if (m == null) m = FailSafeEnum_e_Map = makeMap(e); obj = null; synchronized(FailSafeEnum_e_Map){ obj = m.get(e); List monitors = (List)obj; if (monitors == null) { monitors = makeList(); m.put(e, monitors); }//end of if monitors.add(monitor); }//end of adding }//end of if { monitor.create(e, v); } } pointcut FailSafeEnum_updatesource(Vector v) : call(* Vector+.remove*(..))&& target(v)&& !within(dacapo..*)&& !within(FailSafeEnum)&& !within(FailSafeEnumMonitor)&& !within(FailSafeEnumMonitorPM) || call(* Vector+.add*(..))&& target(v)&& !within(dacapo..*)&& !within(FailSafeEnum)&& !within(FailSafeEnumMonitor)&& !within(FailSafeEnumMonitorPM) || call(* Vector+.clear())&& target(v)&& !within(dacapo..*)&& !within(FailSafeEnum)&& !within(FailSafeEnumMonitor)&& !within(FailSafeEnumMonitorPM) || call(* Vector+.insertElementAt(..))&& target(v)&& !within(dacapo..*)&& !within(FailSafeEnum)&& !within(FailSafeEnumMonitor)&& !within(FailSafeEnumMonitorPM) || call(* Vector+.retainAll(..))&& target(v)&& !within(dacapo..*)&& !within(FailSafeEnum)&& !within(FailSafeEnumMonitor)&& !within(FailSafeEnumMonitorPM) || call(* Vector+.set*(..))&& target(v)&& !within(dacapo..*)&& !within(FailSafeEnum)&& !within(FailSafeEnumMonitor)&& !within(FailSafeEnumMonitorPM); dependent before updatesource_FailSafeEnum(Vector v): FailSafeEnum_updatesource(v) { Object obj = null; FailSafeEnumMonitorPM monitor = null; Map m = FailSafeEnum_v_Map; if (m == null) m = FailSafeEnum_v_Map = makeMap(v); synchronized(FailSafeEnum_v_Map){ obj = m.get(v); }//end of lookup if (obj != null){ synchronized(obj){ Iterator monitors = ((List)obj).iterator(); while (monitors.hasNext()) { monitor = (FailSafeEnumMonitorPM)monitors.next(); { monitor.updatesource(v); } }//end of while }//end of sync }//end of if } pointcut FailSafeEnum_next(Enumeration e) : call(Object Enumeration+.nextElement())&& target(e)&& !within(dacapo..*)&& !within(FailSafeEnum)&& !within(FailSafeEnumMonitor)&& !within(FailSafeEnumMonitorPM); dependent before next_FailSafeEnum(Enumeration e): FailSafeEnum_next(e) { Object obj = null; FailSafeEnumMonitorPM monitor = null; Map m = FailSafeEnum_e_Map; if (m == null) m = FailSafeEnum_e_Map = makeMap(e); synchronized(FailSafeEnum_e_Map){ obj = m.get(e); }//end of lookup if (obj != null){ synchronized(obj){ Iterator monitors = ((List)obj).iterator(); while (monitors.hasNext()) { monitor = (FailSafeEnumMonitorPM)monitors.next(); { monitor.next(e); } }//end of while }//end of sync }//end of if } //Validation dependencies dependency{ strong create_FailSafeEnum , updatesource_FailSafeEnum , next_FailSafeEnum ; } }