001/*
002 * Copyright 2002-2019 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;
022import org.springframework.lang.Nullable;
023
024/**
025 * Represents a dot separated sequence of strings that indicate a package qualified type
026 * reference.
027 *
028 * <p>Example: "java.lang.String" as in the expression "new java.lang.String('hello')"
029 *
030 * @author Andy Clement
031 * @since 3.0
032 */
033public class QualifiedIdentifier extends SpelNodeImpl {
034
035        @Nullable
036        private TypedValue value;
037
038
039        public QualifiedIdentifier(int startPos, int endPos, SpelNodeImpl... operands) {
040                super(startPos, endPos, operands);
041        }
042
043
044        @Override
045        public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
046                // Cache the concatenation of child identifiers
047                if (this.value == null) {
048                        StringBuilder sb = new StringBuilder();
049                        for (int i = 0; i < getChildCount(); i++) {
050                                Object value = this.children[i].getValueInternal(state).getValue();
051                                if (i > 0 && (value == null || !value.toString().startsWith("$"))) {
052                                        sb.append(".");
053                                }
054                                sb.append(value);
055                        }
056                        this.value = new TypedValue(sb.toString());
057                }
058                return this.value;
059        }
060
061        @Override
062        public String toStringAST() {
063                StringBuilder sb = new StringBuilder();
064                if (this.value != null) {
065                        sb.append(this.value.getValue());
066                }
067                else {
068                        for (int i = 0; i < getChildCount(); i++) {
069                                if (i > 0) {
070                                        sb.append(".");
071                                }
072                                sb.append(getChild(i).toStringAST());
073                        }
074                }
075                return sb.toString();
076        }
077
078}