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 super( id );
36 }
37
38 public ASTAssign( OgnlParser p, int id )
39 {
40 super( p, id );
41 }
42
43 protected Object getValueBody( OgnlContext context, Object source )
44 throws OgnlException
45 {
46 Object result = children[1].getValue( context, source );
47 children[0].setValue( context, source, result );
48 return result;
49 }
50
51 public String toGetSourceString( OgnlContext context, Object target )
52 {
53 String result = "";
54
55 String first = children[0].toGetSourceString( context, target );
56 String second = "";
57
58 if ( ASTProperty.class.isInstance( children[1] ) )
59 {
60 second += "((" + OgnlRuntime.getCompiler( context ).getClassName( target.getClass() ) + ")$2).";
61 }
62
63 second += children[1].toGetSourceString( context, target );
64
65 if ( ASTSequence.class.isAssignableFrom( children[1].getClass() ) )
66 {
67 ASTSequence seq = (ASTSequence) children[1];
68
69 context.setCurrentType( Object.class );
70
71 String core = seq.getCoreExpression();
72 if ( core.endsWith( ";" ) )
73 {
74 core = core.substring( 0, core.lastIndexOf( ";" ) );
75 }
76
77 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 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 second = "new " + ( (NodeType) children[1] ).getGetterClass().getName() + "(" + second + ")";
89 }
90
91 if ( OrderedReturn.class.isAssignableFrom( children[0].getClass() )
92 && ( (OrderedReturn) children[0] ).getCoreExpression() != null )
93 {
94 context.setCurrentType( Object.class );
95
96 result = first + second + ")";
97
98
99
100 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 return result;
110 }
111
112 public String toSetSourceString( OgnlContext context, Object target )
113 {
114 String result = "";
115
116 result += children[0].toSetSourceString( context, target );
117
118 if ( ASTProperty.class.isInstance( children[1] ) )
119 {
120 result += "((" + OgnlRuntime.getCompiler( context ).getClassName( target.getClass() ) + ")$2).";
121 }
122
123 String value = children[1].toSetSourceString( context, target );
124
125 if ( value == null )
126 {
127 throw new UnsupportedCompilationException(
128 "Value for assignment is null, can't enhance statement to bytecode." );
129 }
130
131 if ( ASTSequence.class.isAssignableFrom( children[1].getClass() ) )
132 {
133 ASTSequence seq = (ASTSequence) children[1];
134 result = seq.getCoreExpression() + result;
135 value = seq.getLastExpression();
136 }
137
138 if ( NodeType.class.isInstance( children[1] ) && !ASTProperty.class.isInstance( children[1] )
139 && ( (NodeType) children[1] ).getGetterClass() != null )
140 {
141
142 value = "new " + ( (NodeType) children[1] ).getGetterClass().getName() + "(" + value + ")";
143 }
144
145 return result + value + ")";
146 }
147
148 public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data )
149 throws OgnlException
150 {
151 return visitor.visit( this, data );
152 }
153 }