| 1 | |
package org.apache.commons.ognl; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
import org.apache.commons.ognl.enhance.OrderedReturn; |
| 23 | |
import org.apache.commons.ognl.enhance.UnsupportedCompilationException; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
class ASTAssign |
| 31 | |
extends SimpleNode |
| 32 | |
{ |
| 33 | |
public ASTAssign( int id ) |
| 34 | |
{ |
| 35 | 38 | super( id ); |
| 36 | 38 | } |
| 37 | |
|
| 38 | |
public ASTAssign( OgnlParser p, int id ) |
| 39 | |
{ |
| 40 | 0 | super( p, id ); |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
protected Object getValueBody( OgnlContext context, Object source ) |
| 44 | |
throws OgnlException |
| 45 | |
{ |
| 46 | 51 | Object result = children[1].getValue( context, source ); |
| 47 | 51 | children[0].setValue( context, source, result ); |
| 48 | 51 | return result; |
| 49 | |
} |
| 50 | |
|
| 51 | |
public String toGetSourceString( OgnlContext context, Object target ) |
| 52 | |
{ |
| 53 | 14 | String result = ""; |
| 54 | |
|
| 55 | 14 | String first = children[0].toGetSourceString( context, target ); |
| 56 | 2 | String second = ""; |
| 57 | |
|
| 58 | 2 | if ( ASTProperty.class.isInstance( children[1] ) ) |
| 59 | |
{ |
| 60 | 0 | second += "((" + OgnlRuntime.getCompiler( context ).getClassName( target.getClass() ) + ")$2)."; |
| 61 | |
} |
| 62 | |
|
| 63 | 2 | second += children[1].toGetSourceString( context, target ); |
| 64 | |
|
| 65 | 2 | if ( ASTSequence.class.isAssignableFrom( children[1].getClass() ) ) |
| 66 | |
{ |
| 67 | 0 | ASTSequence seq = (ASTSequence) children[1]; |
| 68 | |
|
| 69 | 0 | context.setCurrentType( Object.class ); |
| 70 | |
|
| 71 | 0 | String core = seq.getCoreExpression(); |
| 72 | 0 | if ( core.endsWith( ";" ) ) |
| 73 | |
{ |
| 74 | 0 | core = core.substring( 0, core.lastIndexOf( ";" ) ); |
| 75 | |
} |
| 76 | |
|
| 77 | 0 | second = |
| 78 | |
OgnlRuntime.getCompiler( context ).createLocalReference( context, |
| 79 | |
"org.apache.commons.ognl.OgnlOps.returnValue(($w)" |
| 80 | |
+ core + ", ($w) " + seq.getLastExpression() + ")", |
| 81 | |
Object.class ); |
| 82 | |
} |
| 83 | |
|
| 84 | 2 | if ( NodeType.class.isInstance( children[1] ) && !ASTProperty.class.isInstance( children[1] ) |
| 85 | |
&& ( (NodeType) children[1] ).getGetterClass() != null && !OrderedReturn.class.isInstance( children[1] ) ) |
| 86 | |
{ |
| 87 | |
|
| 88 | 2 | second = "new " + ( (NodeType) children[1] ).getGetterClass().getName() + "(" + second + ")"; |
| 89 | |
} |
| 90 | |
|
| 91 | 2 | if ( OrderedReturn.class.isAssignableFrom( children[0].getClass() ) |
| 92 | |
&& ( (OrderedReturn) children[0] ).getCoreExpression() != null ) |
| 93 | |
{ |
| 94 | 1 | context.setCurrentType( Object.class ); |
| 95 | |
|
| 96 | 1 | result = first + second + ")"; |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | 1 | result = |
| 101 | |
OgnlRuntime |
| 102 | |
.getCompiler( context ) |
| 103 | |
.createLocalReference( context, |
| 104 | |
"org.apache.commons.ognl.OgnlOps.returnValue(($w)" + result + ", ($w)" |
| 105 | |
+ ( (OrderedReturn) children[0] ).getLastExpression() + ")", |
| 106 | |
Object.class ); |
| 107 | |
} |
| 108 | |
|
| 109 | 2 | return result; |
| 110 | |
} |
| 111 | |
|
| 112 | |
public String toSetSourceString( OgnlContext context, Object target ) |
| 113 | |
{ |
| 114 | 2 | String result = ""; |
| 115 | |
|
| 116 | 2 | result += children[0].toSetSourceString( context, target ); |
| 117 | |
|
| 118 | 0 | if ( ASTProperty.class.isInstance( children[1] ) ) |
| 119 | |
{ |
| 120 | 0 | result += "((" + OgnlRuntime.getCompiler( context ).getClassName( target.getClass() ) + ")$2)."; |
| 121 | |
} |
| 122 | |
|
| 123 | 0 | String value = children[1].toSetSourceString( context, target ); |
| 124 | |
|
| 125 | 0 | if ( value == null ) |
| 126 | |
{ |
| 127 | 0 | throw new UnsupportedCompilationException( |
| 128 | |
"Value for assignment is null, can't enhance statement to bytecode." ); |
| 129 | |
} |
| 130 | |
|
| 131 | 0 | if ( ASTSequence.class.isAssignableFrom( children[1].getClass() ) ) |
| 132 | |
{ |
| 133 | 0 | ASTSequence seq = (ASTSequence) children[1]; |
| 134 | 0 | result = seq.getCoreExpression() + result; |
| 135 | 0 | value = seq.getLastExpression(); |
| 136 | |
} |
| 137 | |
|
| 138 | 0 | if ( NodeType.class.isInstance( children[1] ) && !ASTProperty.class.isInstance( children[1] ) |
| 139 | |
&& ( (NodeType) children[1] ).getGetterClass() != null ) |
| 140 | |
{ |
| 141 | |
|
| 142 | 0 | value = "new " + ( (NodeType) children[1] ).getGetterClass().getName() + "(" + value + ")"; |
| 143 | |
} |
| 144 | |
|
| 145 | 0 | return result + value + ")"; |
| 146 | |
} |
| 147 | |
|
| 148 | |
public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data ) |
| 149 | |
throws OgnlException |
| 150 | |
{ |
| 151 | 0 | return visitor.visit( this, data ); |
| 152 | |
} |
| 153 | |
} |