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 | |
public class ASTVarRef |
31 | |
extends SimpleNode |
32 | |
implements NodeType, OrderedReturn |
33 | |
{ |
34 | |
|
35 | |
private String name; |
36 | |
|
37 | |
protected Class getterClass; |
38 | |
|
39 | |
protected String core; |
40 | |
|
41 | |
protected String last; |
42 | |
|
43 | |
public ASTVarRef( int id ) |
44 | |
{ |
45 | 204 | super( id ); |
46 | 204 | } |
47 | |
|
48 | |
public ASTVarRef( OgnlParser p, int id ) |
49 | |
{ |
50 | 0 | super( p, id ); |
51 | 0 | } |
52 | |
|
53 | |
void setName( String name ) |
54 | |
{ |
55 | 204 | this.name = name; |
56 | 204 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
String getName() |
64 | |
{ |
65 | 0 | return name; |
66 | |
} |
67 | |
|
68 | |
protected Object getValueBody( OgnlContext context, Object source ) |
69 | |
throws OgnlException |
70 | |
{ |
71 | 164 | return context.get( name ); |
72 | |
} |
73 | |
|
74 | |
protected void setValueBody( OgnlContext context, Object target, Object value ) |
75 | |
throws OgnlException |
76 | |
{ |
77 | 47 | context.put( name, value ); |
78 | 47 | } |
79 | |
|
80 | |
public Class getGetterClass() |
81 | |
{ |
82 | 30 | return getterClass; |
83 | |
} |
84 | |
|
85 | |
public Class getSetterClass() |
86 | |
{ |
87 | 0 | return null; |
88 | |
} |
89 | |
|
90 | |
public String getCoreExpression() |
91 | |
{ |
92 | 1 | return core; |
93 | |
} |
94 | |
|
95 | |
public String getLastExpression() |
96 | |
{ |
97 | 8 | return last; |
98 | |
} |
99 | |
|
100 | |
public String toGetSourceString( OgnlContext context, Object target ) |
101 | |
{ |
102 | 30 | Object value = context.get( name ); |
103 | |
|
104 | 30 | if ( value != null ) |
105 | |
{ |
106 | |
|
107 | 14 | getterClass = value.getClass(); |
108 | |
} |
109 | |
|
110 | 30 | context.setCurrentType( getterClass ); |
111 | 30 | context.setCurrentAccessor( context.getClass() ); |
112 | |
|
113 | 30 | context.setCurrentObject( value ); |
114 | |
|
115 | |
|
116 | 30 | if ( context.getCurrentObject() == null ) |
117 | |
{ |
118 | 16 | throw new UnsupportedCompilationException( "Current context object is null, can't compile var reference." ); |
119 | |
} |
120 | |
|
121 | 14 | String pre = ""; |
122 | 14 | String post = ""; |
123 | 14 | if ( context.getCurrentType() != null ) |
124 | |
{ |
125 | 14 | pre = "((" + OgnlRuntime.getCompiler( context ).getInterfaceClass( context.getCurrentType() ).getName() + ")"; |
126 | 14 | post = ")"; |
127 | |
} |
128 | |
|
129 | 14 | if ( parent != null && ASTAssign.class.isInstance( parent ) ) |
130 | |
{ |
131 | 1 | core = "$1.put(\"" + name + "\","; |
132 | 1 | last = pre + "$1.get(\"" + name + "\")" + post; |
133 | |
|
134 | 1 | return core; |
135 | |
} |
136 | |
|
137 | 13 | return pre + "$1.get(\"" + name + "\")" + post; |
138 | |
} |
139 | |
|
140 | |
public String toSetSourceString( OgnlContext context, Object target ) |
141 | |
{ |
142 | 2 | return toGetSourceString( context, target ); |
143 | |
} |
144 | |
|
145 | |
public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data ) |
146 | |
throws OgnlException |
147 | |
{ |
148 | 0 | return visitor.visit( this, data ); |
149 | |
} |
150 | |
} |