001 package org.apache.commons.ognl;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.commons.ognl.enhance.OrderedReturn;
023 import org.apache.commons.ognl.enhance.UnsupportedCompilationException;
024
025 /**
026 * $Id: ASTVarRef.java 1194869 2011-10-29 11:10:16Z mcucchiara $
027 * @author Luke Blanshard (blanshlu@netscape.net)
028 * @author Drew Davidson (drew@ognl.org)
029 */
030 public class ASTVarRef
031 extends SimpleNode
032 implements NodeType, OrderedReturn
033 {
034
035 private String name;
036
037 protected Class getterClass;
038
039 protected String core;
040
041 protected String last;
042
043 public ASTVarRef( int id )
044 {
045 super( id );
046 }
047
048 public ASTVarRef( OgnlParser p, int id )
049 {
050 super( p, id );
051 }
052
053 void setName( String name )
054 {
055 this.name = name;
056 }
057 /**
058 * Get the variable name.
059 *
060 * @return the variable name.
061 * @since 4.0
062 */
063 String getName()
064 {
065 return name;
066 }
067
068 protected Object getValueBody( OgnlContext context, Object source )
069 throws OgnlException
070 {
071 return context.get( name );
072 }
073
074 protected void setValueBody( OgnlContext context, Object target, Object value )
075 throws OgnlException
076 {
077 context.put( name, value );
078 }
079
080 public Class getGetterClass()
081 {
082 return getterClass;
083 }
084
085 public Class getSetterClass()
086 {
087 return null;
088 }
089
090 public String getCoreExpression()
091 {
092 return core;
093 }
094
095 public String getLastExpression()
096 {
097 return last;
098 }
099
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 // context.setRoot(context.get(name));
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 }