MVELDialect mvel = (MVELDialect) context.getDialect( "mvel" );
TreeSet blocks = new TreeSet( new Comparator() {
public int compare(Object o1,
Object o2) {
JavaModifyBlockDescr d1 = (JavaModifyBlockDescr) o1;
JavaModifyBlockDescr d2 = (JavaModifyBlockDescr) o2;
return d1.getStart() - d2.getStart();
}
} );
for ( Iterator it = analysis.getModifyBlocks().iterator(); it.hasNext(); ) {
blocks.add( it.next() );
}
StringBuffer consequence = new StringBuffer();
int lastAdded = 0;
for ( Iterator it = blocks.iterator(); it.hasNext(); ) {
JavaModifyBlockDescr d = (JavaModifyBlockDescr) it.next();
// adding chunk
consequence.append( originalCode.substring( lastAdded,
d.getStart() - 1 ) );
lastAdded = d.getEnd();
Dialect.AnalysisResult mvelAnalysis = mvel.analyzeBlock( context,
context.getRuleDescr(),
mvel.getInterceptors(),
d.getModifyExpression(),
new Set[]{context.getDeclarationResolver().getDeclarations(context.getRule()).keySet(), context.getPkg().getGlobals().keySet()},
null );
final ExecutableStatement expr = (ExecutableStatement) mvel.compile( d.getModifyExpression(),
mvelAnalysis,
mvel.getInterceptors(),
null,
null,
context );
Class ret = expr.getKnownEgressType();
if( ret == null ) {
// not possible to evaluate expression return value
context.getErrors().add( new DescrBuildError( context.getParentDescr(),
context.getRuleDescr(),
originalCode,
"Unable to determine the resulting type of the expression: " + d.getModifyExpression()+"\n" ) );
return null;
}
String retString = ClassUtils.canonicalName( ret );
// adding modify expression
consequence.append( "{ " );
consequence.append( retString );
consequence.append( " __obj__ = (" );
consequence.append( retString );
consequence.append( ") " );
consequence.append( d.getModifyExpression() );
consequence.append( "; " );
// adding the modifyRetract call:
consequence.append( "modifyRetract( __obj__ ); " );
// the following is a hack to preserve line breaks.
String originalBlock = originalCode.substring( d.getStart()-1,
d.getEnd() );
int end = originalBlock.indexOf( "{" );
addLineBreaks( consequence,
originalBlock.substring( 0,
end ) );
int start = end+1;
// adding each of the expressions:
for ( Iterator exprIt = d.getExpressions().iterator(); exprIt.hasNext(); ) {
String exprStr = (String) exprIt.next();
end = originalBlock.indexOf( exprStr,
start );
addLineBreaks( consequence,
originalBlock.substring( start,