private static CtIf originalLoopReplacement(Factory factory, LoopMonitor monitor, While loop) { CtExpression<Boolean> monitoredCondition = newExpressionFromSnippet(factory, conditionName(monitor), Boolean.class); CtIf newIf = newIf(factory, monitoredCondition, loop.loopBody(), newBreak(factory)); CtStatement increment = newStatementFromSnippet(factory, format("%s += 1", counterName(monitor))); insertBeforeUnderSameParent(increment, newIf.getThenStatement()); if (loop.isUnbreakable()) { newIf.setElseStatement(null); } return newIf; }
public static void instrument(LoopMonitor monitor, RuntimeValues<?> runtimeValues) { While loop = monitor.loop(); CtWhile astLoop = loop.astLoop(); Factory factory = astLoop.getFactory(); CollectableValueFinder finder = CollectableValueFinder.valueFinderFromWhile(astLoop); CtStatement catchCallback = appendMonitorCallbacks(factory, monitor, loop, astLoop); CtIf newIf = loopBodyWrapper(factory, monitor, loop, astLoop, catchCallback); declareOriginalConditionEvaluation(factory, monitor, loop, newIf); declareConditionEvaluation(factory, monitor, loop, newIf); declareEntrancesCounter(factory, monitor, astLoop); traceReachableValues(monitor, loop, newIf, finder, runtimeValues); }
private static CtStatement appendMonitorCallbacks( Factory factory, LoopMonitor monitor, While loop, CtWhile astLoop) { String counterName = counterName(monitor); if (!loop.isUnbreakable()) { insertAfterUnderSameParent( newStatementFromSnippet(factory, monitor.invocationOnMonitoringEnd(counterName)), astLoop); } appendMonitoredReturnExit(factory, monitor, loop.returnStatements()); appendMonitoredBreakExit(factory, monitor, loop.breakStatements()); return newStatementFromSnippet(factory, monitor.invocationOnLoopError(counterName)); }
private static void declareOriginalConditionEvaluation( Factory factory, LoopMonitor monitor, While loop, CtIf newIf) { CtLocalVariable<Boolean> localVariable = newLocalVariableDeclaration( factory, boolean.class, originalConditionName(monitor), loop.loopingCondition()); insertBeforeUnderSameParent(localVariable, newIf); }
private static void traceReachableValues( LoopMonitor monitor, While loop, CtIf newIf, CollectableValueFinder finder, RuntimeValues<?> runtimeValues) { Multimap<String, String> getters = finder.accessibleGetters(); Collection<String> reachableVariables = finder.reachableVariables(); Map<String, String> inputMap = MetaMap.autoMap(reachableVariables); inputMap.put(loop.loopingCondition(), originalConditionName(monitor)); RuntimeValuesInstrumenter.runtimeCollectionBefore( newIf, inputMap, getters, conditionName(monitor), runtimeValues); }