View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.expression.spel.ast;
18  
19  import org.springframework.expression.EvaluationException;
20  import org.springframework.expression.TypedValue;
21  import org.springframework.expression.spel.ExpressionState;
22  
23  /**
24   * Represents a dot separated sequence of strings that indicate a package qualified type
25   * reference.
26   *
27   * <p>Example: "java.lang.String" as in the expression "new java.lang.String('hello')"
28   *
29   * @author Andy Clement
30   * @since 3.0
31   */
32  public class QualifiedIdentifier extends SpelNodeImpl {
33  
34  	// TODO safe to cache? dont think so
35  	private TypedValue value;
36  
37  
38  	public QualifiedIdentifier(int pos, SpelNodeImpl... operands) {
39  		super(pos, operands);
40  	}
41  
42  
43  	@Override
44  	public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
45  		// Cache the concatenation of child identifiers
46  		if (this.value == null) {
47  			StringBuilder sb = new StringBuilder();
48  			for (int i = 0; i < getChildCount(); i++) {
49  				Object value = this.children[i].getValueInternal(state).getValue();
50  				if (i > 0 && !value.toString().startsWith("$")) {
51  					sb.append(".");
52  				}
53  				sb.append(value);
54  			}
55  			this.value = new TypedValue(sb.toString());
56  		}
57  		return this.value;
58  	}
59  
60  	@Override
61  	public String toStringAST() {
62  		StringBuilder sb = new StringBuilder();
63  		if (this.value != null) {
64  			sb.append(this.value.getValue());
65  		}
66  		else {
67  			for (int i = 0; i < getChildCount(); i++) {
68  				if (i > 0) {
69  					sb.append(".");
70  				}
71  				sb.append(getChild(i).toStringAST());
72  			}
73  		}
74  		return sb.toString();
75  	}
76  
77  }