in src/main/java/org/apache/commons/ognl/enhance/ExpressionCompiler.java [575:646]
protected String generateGetter( OgnlContext context, CtClass newClass, CtClass objClass, ClassPool classPool,
CtMethod valueGetter, Node expression, Object root )
throws Exception
{
String pre = "";
String post = "";
String body;
context.setRoot( root );
// the ExpressionAccessor API has to reference the generic Object class for get/set operations, so this sets up
// that known
// type beforehand
context.remove( PRE_CAST );
// Recursively generate the java source code representation of the top level expression
String getterCode = expression.toGetSourceString( context, root );
if ( getterCode == null || getterCode.trim().isEmpty()
&& !ASTVarRef.class.isAssignableFrom( expression.getClass() ) )
{
getterCode = "null";
}
String castExpression = (String) context.get( PRE_CAST );
if ( context.getCurrentType() == null || context.getCurrentType().isPrimitive()
|| Character.class.isAssignableFrom( context.getCurrentType() )
|| Object.class == context.getCurrentType() )
{
pre = pre + " ($w) (";
post = post + ")";
}
String rootExpr = !"null".equals( getterCode ) ? getRootExpression( expression, root, context ) : "";
String noRoot = (String) context.remove( "_noRoot" );
if ( noRoot != null )
{
rootExpr = "";
}
createLocalReferences( context, classPool, newClass, objClass, valueGetter.getParameterTypes() );
if ( expression instanceof OrderedReturn
&& ( (OrderedReturn) expression ).getLastExpression() != null )
{
body = "{ " + ( expression instanceof ASTMethod || expression instanceof ASTChain
? rootExpr
: "" ) + ( castExpression != null ? castExpression : "" )
+ ( (OrderedReturn) expression ).getCoreExpression() + " return " + pre
+ ( (OrderedReturn) expression ).getLastExpression() + post + ";}";
}
else
{
body =
"{ return " + pre + ( castExpression != null ? castExpression : "" ) + rootExpr + getterCode + post
+ ";}";
}
body = body.replaceAll( "\\.\\.", "." );
// System.out.println("Getter Body: ===================================\n" + body);
valueGetter.setBody( body );
newClass.addMethod( valueGetter );
return body;
}