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.beans.factory.annotation;
018
019import org.springframework.beans.factory.support.GenericBeanDefinition;
020import org.springframework.core.type.AnnotationMetadata;
021import org.springframework.core.type.MethodMetadata;
022import org.springframework.core.type.StandardAnnotationMetadata;
023import org.springframework.util.Assert;
024
025/**
026 * Extension of the {@link org.springframework.beans.factory.support.GenericBeanDefinition}
027 * class, adding support for annotation metadata exposed through the
028 * {@link AnnotatedBeanDefinition} interface.
029 *
030 * <p>This GenericBeanDefinition variant is mainly useful for testing code that expects
031 * to operate on an AnnotatedBeanDefinition, for example strategy implementations
032 * in Spring's component scanning support (where the default definition class is
033 * {@link org.springframework.context.annotation.ScannedGenericBeanDefinition},
034 * which also implements the AnnotatedBeanDefinition interface).
035 *
036 * @author Juergen Hoeller
037 * @author Chris Beams
038 * @since 2.5
039 * @see AnnotatedBeanDefinition#getMetadata()
040 * @see org.springframework.core.type.StandardAnnotationMetadata
041 */
042@SuppressWarnings("serial")
043public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implements AnnotatedBeanDefinition {
044
045        private final AnnotationMetadata metadata;
046
047        private MethodMetadata factoryMethodMetadata;
048
049
050        /**
051         * Create a new AnnotatedGenericBeanDefinition for the given bean class.
052         * @param beanClass the loaded bean class
053         */
054        public AnnotatedGenericBeanDefinition(Class<?> beanClass) {
055                setBeanClass(beanClass);
056                this.metadata = new StandardAnnotationMetadata(beanClass, true);
057        }
058
059        /**
060         * Create a new AnnotatedGenericBeanDefinition for the given annotation metadata,
061         * allowing for ASM-based processing and avoidance of early loading of the bean class.
062         * Note that this constructor is functionally equivalent to
063         * {@link org.springframework.context.annotation.ScannedGenericBeanDefinition
064         * ScannedGenericBeanDefinition}, however the semantics of the latter indicate that a
065         * bean was discovered specifically via component-scanning as opposed to other means.
066         * @param metadata the annotation metadata for the bean class in question
067         * @since 3.1.1
068         */
069        public AnnotatedGenericBeanDefinition(AnnotationMetadata metadata) {
070                Assert.notNull(metadata, "AnnotationMetadata must not be null");
071                if (metadata instanceof StandardAnnotationMetadata) {
072                        setBeanClass(((StandardAnnotationMetadata) metadata).getIntrospectedClass());
073                }
074                else {
075                        setBeanClassName(metadata.getClassName());
076                }
077                this.metadata = metadata;
078        }
079
080        /**
081         * Create a new AnnotatedGenericBeanDefinition for the given annotation metadata,
082         * based on an annotated class and a factory method on that class.
083         * @param metadata the annotation metadata for the bean class in question
084         * @param factoryMethodMetadata metadata for the selected factory method
085         * @since 4.1.1
086         */
087        public AnnotatedGenericBeanDefinition(AnnotationMetadata metadata, MethodMetadata factoryMethodMetadata) {
088                this(metadata);
089                Assert.notNull(factoryMethodMetadata, "MethodMetadata must not be null");
090                setFactoryMethodName(factoryMethodMetadata.getMethodName());
091                this.factoryMethodMetadata = factoryMethodMetadata;
092        }
093
094
095        @Override
096        public final AnnotationMetadata getMetadata() {
097                 return this.metadata;
098        }
099
100        @Override
101        public final MethodMetadata getFactoryMethodMetadata() {
102                return this.factoryMethodMetadata;
103        }
104
105}