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;
018
019import org.springframework.core.AttributeAccessorSupport;
020import org.springframework.lang.Nullable;
021
022/**
023 * Extension of {@link org.springframework.core.AttributeAccessorSupport},
024 * holding attributes as {@link BeanMetadataAttribute} objects in order
025 * to keep track of the definition source.
026 *
027 * @author Juergen Hoeller
028 * @since 2.5
029 */
030@SuppressWarnings("serial")
031public class BeanMetadataAttributeAccessor extends AttributeAccessorSupport implements BeanMetadataElement {
032
033        @Nullable
034        private Object source;
035
036
037        /**
038         * Set the configuration source {@code Object} for this metadata element.
039         * <p>The exact type of the object will depend on the configuration mechanism used.
040         */
041        public void setSource(@Nullable Object source) {
042                this.source = source;
043        }
044
045        @Override
046        @Nullable
047        public Object getSource() {
048                return this.source;
049        }
050
051
052        /**
053         * Add the given BeanMetadataAttribute to this accessor's set of attributes.
054         * @param attribute the BeanMetadataAttribute object to register
055         */
056        public void addMetadataAttribute(BeanMetadataAttribute attribute) {
057                super.setAttribute(attribute.getName(), attribute);
058        }
059
060        /**
061         * Look up the given BeanMetadataAttribute in this accessor's set of attributes.
062         * @param name the name of the attribute
063         * @return the corresponding BeanMetadataAttribute object,
064         * or {@code null} if no such attribute defined
065         */
066        @Nullable
067        public BeanMetadataAttribute getMetadataAttribute(String name) {
068                return (BeanMetadataAttribute) super.getAttribute(name);
069        }
070
071        @Override
072        public void setAttribute(String name, @Nullable Object value) {
073                super.setAttribute(name, new BeanMetadataAttribute(name, value));
074        }
075
076        @Override
077        @Nullable
078        public Object getAttribute(String name) {
079                BeanMetadataAttribute attribute = (BeanMetadataAttribute) super.getAttribute(name);
080                return (attribute != null ? attribute.getValue() : null);
081        }
082
083        @Override
084        @Nullable
085        public Object removeAttribute(String name) {
086                BeanMetadataAttribute attribute = (BeanMetadataAttribute) super.removeAttribute(name);
087                return (attribute != null ? attribute.getValue() : null);
088        }
089
090}