in src/main/java/org/apache/commons/ognl/ASTTest.java [60:145]
public String toGetSourceString( OgnlContext context, Object target )
{
if ( target == null )
{
throw new UnsupportedCompilationException( "evaluation resulted in null expression." );
}
if ( children.length != 3 )
{
throw new UnsupportedCompilationException( "Can only compile test expressions with two children."
+ children.length );
}
String result = "";
try
{
String first = OgnlRuntime.getChildSource( context, target, children[0] );
if ( !OgnlRuntime.isBoolean( first ) && !context.getCurrentType().isPrimitive() )
{
first = OgnlRuntime.getCompiler( context ).createLocalReference( context, first, context.getCurrentType() );
}
if (children[0] instanceof ExpressionNode)
{
first = "(" + first + ")";
}
String second = OgnlRuntime.getChildSource( context, target, children[1] );
Class secondType = context.getCurrentType();
if ( !OgnlRuntime.isBoolean( second ) && !context.getCurrentType().isPrimitive() )
{
second = OgnlRuntime.getCompiler( context ).createLocalReference( context, second, context.getCurrentType() );
}
if (children[1] instanceof ExpressionNode)
{
second = "(" + second + ")";
}
String third = OgnlRuntime.getChildSource( context, target, children[2] );
Class thirdType = context.getCurrentType();
if ( !OgnlRuntime.isBoolean( third ) && !context.getCurrentType().isPrimitive() )
{
third = OgnlRuntime.getCompiler( context ).createLocalReference( context, third, context.getCurrentType() );
}
if (children[2] instanceof ExpressionNode)
{
third = "(" + third + ")";
}
boolean mismatched =
( secondType.isPrimitive( ) && !thirdType.isPrimitive( ) ) || ( !secondType.isPrimitive( )
&& thirdType.isPrimitive( ) );
result += "org.apache.commons.ognl.OgnlOps.booleanValue(" + first + ")";
result += " ? ";
result += ( mismatched ? " ($w) " : "" ) + second;
result += " : ";
result += ( mismatched ? " ($w) " : "" ) + third;
context.setCurrentObject( target );
context.setCurrentType( mismatched ? Object.class : secondType );
return result;
}
catch ( NullPointerException e )
{
// expected to happen in some instances
throw new UnsupportedCompilationException( "evaluation resulted in null expression." );
}
catch ( Throwable t )
{
throw OgnlOps.castToRuntime( t );
}
}