001/*
002 * Copyright 2002-2017 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.beans.factory.config;
018
019import org.springframework.lang.Nullable;
020import org.springframework.util.Assert;
021
022/**
023 * Immutable placeholder class used for a property value object when it's a
024 * reference to another bean name in the factory, to be resolved at runtime.
025 *
026 * @author Juergen Hoeller
027 * @since 2.0
028 * @see RuntimeBeanReference
029 * @see BeanDefinition#getPropertyValues()
030 * @see org.springframework.beans.factory.BeanFactory#getBean
031 */
032public class RuntimeBeanNameReference implements BeanReference {
033
034        private final String beanName;
035
036        @Nullable
037        private Object source;
038
039
040        /**
041         * Create a new RuntimeBeanNameReference to the given bean name.
042         * @param beanName name of the target bean
043         */
044        public RuntimeBeanNameReference(String beanName) {
045                Assert.hasText(beanName, "'beanName' must not be empty");
046                this.beanName = beanName;
047        }
048
049        @Override
050        public String getBeanName() {
051                return this.beanName;
052        }
053
054        /**
055         * Set the configuration source {@code Object} for this metadata element.
056         * <p>The exact type of the object will depend on the configuration mechanism used.
057         */
058        public void setSource(@Nullable Object source) {
059                this.source = source;
060        }
061
062        @Override
063        @Nullable
064        public Object getSource() {
065                return this.source;
066        }
067
068
069        @Override
070        public boolean equals(@Nullable Object other) {
071                if (this == other) {
072                        return true;
073                }
074                if (!(other instanceof RuntimeBeanNameReference)) {
075                        return false;
076                }
077                RuntimeBeanNameReference that = (RuntimeBeanNameReference) other;
078                return this.beanName.equals(that.beanName);
079        }
080
081        @Override
082        public int hashCode() {
083                return this.beanName.hashCode();
084        }
085
086        @Override
087        public String toString() {
088                return '<' + getBeanName() + '>';
089        }
090
091}