001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.expression.spel.ast;
018
019import org.springframework.expression.EvaluationException;
020import org.springframework.expression.TypedValue;
021import org.springframework.expression.spel.ExpressionState;
022
023/**
024 * Represents a dot separated sequence of strings that indicate a package qualified type
025 * reference.
026 *
027 * <p>Example: "java.lang.String" as in the expression "new java.lang.String('hello')"
028 *
029 * @author Andy Clement
030 * @since 3.0
031 */
032public class QualifiedIdentifier extends SpelNodeImpl {
033
034        // TODO safe to cache? dont think so
035        private TypedValue value;
036
037
038        public QualifiedIdentifier(int pos, SpelNodeImpl... operands) {
039                super(pos, operands);
040        }
041
042
043        @Override
044        public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
045                // Cache the concatenation of child identifiers
046                if (this.value == null) {
047                        StringBuilder sb = new StringBuilder();
048                        for (int i = 0; i < getChildCount(); i++) {
049                                Object value = this.children[i].getValueInternal(state).getValue();
050                                if (i > 0 && !value.toString().startsWith("$")) {
051                                        sb.append(".");
052                                }
053                                sb.append(value);
054                        }
055                        this.value = new TypedValue(sb.toString());
056                }
057                return this.value;
058        }
059
060        @Override
061        public String toStringAST() {
062                StringBuilder sb = new StringBuilder();
063                if (this.value != null) {
064                        sb.append(this.value.getValue());
065                }
066                else {
067                        for (int i = 0; i < getChildCount(); i++) {
068                                if (i > 0) {
069                                        sb.append(".");
070                                }
071                                sb.append(getChild(i).toStringAST());
072                        }
073                }
074                return sb.toString();
075        }
076
077}