in src/main/java/org/apache/commons/ognl/ASTMethod.java [260:404]
public String toSetSourceString( OgnlContext context, Object target )
{
/*
* System.out.println("current type: " + context.getCurrentType() + " target:" + target + " " +
* context.getCurrentObject() + " last child? " + lastChild(context));
*/
Method method =
OgnlRuntime.getWriteMethod( context.getCurrentType() != null ? context.getCurrentType() : target.getClass(),
methodName, children != null ? children.length : -1 );
if ( method == null )
{
throw new UnsupportedCompilationException(
"Unable to determine setter method generation for " + methodName );
}
String post = "";
StringBuilder result = new StringBuilder("." + method.getName() + "(");
if ( method.getReturnType() != void.class && method.getReturnType().isPrimitive() && ( parent == null
|| !(parent instanceof ASTTest)) )
{
Class wrapper = OgnlRuntime.getPrimitiveWrapperClass( method.getReturnType() );
ExpressionCompiler.addCastString( context, "new " + wrapper.getName() + "(" );
post = ")";
getterClass = wrapper;
}
boolean varArgs = method.isVarArgs();
if ( varArgs )
{
throw new UnsupportedCompilationException( "Javassist does not currently support varargs method calls" );
}
OgnlExpressionCompiler compiler = OgnlRuntime.getCompiler( context );
try
{
/*
* if (lastChild(context) && method.getParameterTypes().length > 0 && _children.length <= 0) throw new
* UnsupportedCompilationException("Unable to determine setter method generation for " + method);
*/
if ( ( children != null ) && ( children.length > 0 ) )
{
Class[] parms = method.getParameterTypes();
String prevCast = (String) context.remove( ExpressionCompiler.PRE_CAST );
for ( int i = 0; i < children.length; i++ )
{
if ( i > 0 )
{
result.append(", ");
}
Class prevType = context.getCurrentType();
context.setCurrentObject( context.getRoot() );
context.setCurrentType( context.getRoot() != null ? context.getRoot().getClass() : null );
context.setCurrentAccessor( null );
context.setPreviousType( null );
Node child = children[i];
Object value = child.getValue( context, context.getRoot() );
String parmString = child.toSetSourceString( context, context.getRoot() );
if ( context.getCurrentType() == Void.TYPE || context.getCurrentType() == void.class )
{
throw new UnsupportedCompilationException( "Method argument can't be a void type." );
}
if ( parmString == null || parmString.trim().length() < 1 )
{
if ( child instanceof ASTProperty || child instanceof ASTMethod
|| child instanceof ASTStaticMethod || child instanceof ASTChain)
{
throw new UnsupportedCompilationException(
"ASTMethod setter child returned null from a sub property expression." );
}
parmString = "null";
}
// to undo type setting of constants when used as method parameters
if (child instanceof ASTConst)
{
context.setCurrentType( prevType );
}
parmString = ExpressionCompiler.getRootExpression( child, context.getRoot(), context ) + parmString;
String cast = "";
if ( ExpressionCompiler.shouldCast( child ) )
{
cast = (String) context.remove( ExpressionCompiler.PRE_CAST );
}
if ( cast == null )
{
cast = "";
}
parmString = cast + parmString;
Class valueClass = value != null ? value.getClass() : null;
if ( NodeType.class.isAssignableFrom( child.getClass() ) )
{
valueClass = ( (NodeType) child ).getGetterClass();
}
if ( valueClass != parms[i] )
{
parmString =
ASTMethodUtil.getParmString( context, parms[i], parmString, child, valueClass, ".class)" );
}
result.append(parmString);
}
if ( prevCast != null )
{
context.put( ExpressionCompiler.PRE_CAST, prevCast );
}
}
}
catch ( Throwable t )
{
throw OgnlOps.castToRuntime( t );
}
try
{
Object contextObj = getValueBody( context, target );
context.setCurrentObject( contextObj );
}
catch ( Throwable t )
{
// ignore
}
context.setCurrentType( method.getReturnType() );
context.setCurrentAccessor( compiler.getSuperOrInterfaceClass( method, method.getDeclaringClass() ) );
return result + ")" + post;
}