View Javadoc
1   /*
2    * Copyright 2002-2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.context.annotation;
18  
19  import org.springframework.beans.factory.parsing.Problem;
20  import org.springframework.beans.factory.parsing.ProblemReporter;
21  import org.springframework.core.type.MethodMetadata;
22  
23  /**
24   * Represents a {@link Configuration} class method marked with the
25   * {@link Bean} annotation.
26   *
27   * @author Chris Beams
28   * @author Juergen Hoeller
29   * @since 3.0
30   * @see ConfigurationClass
31   * @see ConfigurationClassParser
32   * @see ConfigurationClassBeanDefinitionReader
33   */
34  final class BeanMethod extends ConfigurationMethod {
35  
36  	public BeanMethod(MethodMetadata metadata, ConfigurationClass configurationClass) {
37  		super(metadata, configurationClass);
38  	}
39  
40  	@Override
41  	public void validate(ProblemReporter problemReporter) {
42  		if (getMetadata().isStatic()) {
43  			// static @Bean methods have no constraints to validate -> return immediately
44  			return;
45  		}
46  
47  		if (this.configurationClass.getMetadata().isAnnotated(Configuration.class.getName())) {
48  			if (!getMetadata().isOverridable()) {
49  				// instance @Bean methods within @Configuration classes must be overridable to accommodate CGLIB
50  				problemReporter.error(new NonOverridableMethodError());
51  			}
52  		}
53  	}
54  
55  
56  	private class NonOverridableMethodError extends Problem {
57  
58  		public NonOverridableMethodError() {
59  			super(String.format("@Bean method '%s' must not be private or final; change the method's modifiers to continue",
60  					getMetadata().getMethodName()), getResourceLocation());
61  		}
62  	}
63  }