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