lastAdded = throwBlock.getEnd() - offset;
} else if (block.getType() == JavaBlockDescr.BlockType.IF) {
// adding previous chunk up to the start of this block
consequence.append(originalCode.substring(lastAdded,
block.getStart() - 1 - offset));
JavaIfBlockDescr ifDescr = (JavaIfBlockDescr) block;
lastAdded = ifDescr.getEnd() - offset;
stripBlockDescr(originalCode,
consequence,
ifDescr,
offset);
} else if (block.getType() == JavaBlockDescr.BlockType.ELSE) {
// adding previous chunk up to the start of this block
consequence.append(originalCode.substring(lastAdded,
block.getStart() - 1 - offset));
JavaElseBlockDescr elseDescr = (JavaElseBlockDescr) block;
lastAdded = elseDescr.getEnd() - offset;
stripBlockDescr(originalCode,
consequence,
elseDescr,
offset);
} else if (block.getType() == JavaBlockDescr.BlockType.WHILE) {
// adding previous chunk up to the start of this block
consequence.append(originalCode.substring(lastAdded,
block.getStart() - 1 - offset));
JavaWhileBlockDescr whileDescr = (JavaWhileBlockDescr) block;
lastAdded = whileDescr.getEnd() - offset;
stripBlockDescr(originalCode,
consequence,
whileDescr,
offset);
} else if (block.getType() == JavaBlockDescr.BlockType.FOR) {
// adding previous chunk up to the start of this block
consequence.append(originalCode.substring(lastAdded,
block.getStart() - 1 - offset));
JavaForBlockDescr forDescr = (JavaForBlockDescr) block;
lastAdded = forDescr.getEnd() - offset;
stripBlockDescr(originalCode,
consequence,
forDescr,
offset);
}
}
consequence.append(originalCode.substring(lastAdded));
// We need to do this as MVEL doesn't recognise "modify"
MacroProcessor macroProcessor = new MacroProcessor();
Map<String, Macro> macros = new HashMap<String, Macro>(MVELConsequenceBuilder.macros);
macros.put("modify",
new Macro() {
public String doMacro() {
return "with ";
}
});
macroProcessor.setMacros(macros);
String mvelCode = macroProcessor.parse(consequence.toString());
Map<String, Class<?>> inputs = (Map<String, Class<?>>) getInputs(context, mvelCode, bindings, parentVars);
inputs.putAll(parentVars);
parentBlock.setInputs(inputs);
// now go depth, set inputs for each nested container
// set inputs for current container blocks to be rewritten
for (JavaBlockDescr block : parentBlock.getJavaBlockDescrs()) {
if (block.getType() == JavaBlockDescr.BlockType.TRY) {
JavaTryBlockDescr tryBlock = (JavaTryBlockDescr) block;
setContainerBlockInputs(context,
descrs,
tryBlock,
originalCode.substring(tryBlock.getTextStart() - offset, tryBlock.getEnd() - 1 - offset),
bindings,
inputs,
tryBlock.getTextStart());
for (JavaCatchBlockDescr catchBlock : tryBlock.getCatches()) {
setContainerBlockInputs(context,
descrs,
catchBlock,
catchBlock.getClause() + "=null;" + originalCode.substring(catchBlock.getTextStart() - offset, catchBlock.getEnd() - 1 - offset),
bindings,
inputs,
tryBlock.getTextStart());
}
if (tryBlock.getFinal() != null) {
JavaFinalBlockDescr finalBlock = tryBlock.getFinal();
setContainerBlockInputs(context,
descrs,
finalBlock,
originalCode.substring(finalBlock.getTextStart() - offset, finalBlock.getEnd() - 1 - offset),
bindings,
inputs,
tryBlock.getTextStart());
}
} else if (block.getType() == JavaBlockDescr.BlockType.IF) {
JavaIfBlockDescr ifBlock = (JavaIfBlockDescr) block;
int adjustBlock = (originalCode.charAt(ifBlock.getTextStart() - offset - 1) == '{') ? 0 : 1;
setContainerBlockInputs(context,
descrs,
ifBlock,
originalCode.substring(ifBlock.getTextStart() - offset + adjustBlock, ifBlock.getEnd() - 1 - offset - adjustBlock),
bindings,
inputs,
ifBlock.getTextStart());
} else if (block.getType() == JavaBlockDescr.BlockType.ELSE) {
JavaElseBlockDescr elseBlock = (JavaElseBlockDescr) block;
int adjustBlock = (originalCode.charAt(elseBlock.getTextStart() - offset - 1) == '{') ? 0 : 1;
setContainerBlockInputs(context,
descrs,