KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > support > SimpleInstantiationStrategy


1 /*
2  * Copyright 2002-2007 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.beans.factory.support;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23
24 import org.springframework.beans.BeanUtils;
25 import org.springframework.beans.factory.BeanDefinitionStoreException;
26 import org.springframework.beans.factory.BeanFactory;
27 import org.springframework.util.StringUtils;
28
29 /**
30  * Simple object instantiation strategy for use in a BeanFactory.
31  *
32  * <p>Does not support Method Injection, although it provides hooks for subclasses
33  * to override to add Method Injection support, for example by overriding methods.
34  *
35  * @author Rod Johnson
36  * @since 1.1
37  */

38 public class SimpleInstantiationStrategy implements InstantiationStrategy {
39
40     public Object JavaDoc instantiate(
41             RootBeanDefinition beanDefinition, String JavaDoc beanName, BeanFactory owner) {
42
43         // Don't override the class with CGLIB if no overrides.
44
if (beanDefinition.getMethodOverrides().isEmpty()) {
45             return BeanUtils.instantiateClass(beanDefinition.getBeanClass());
46         }
47         else {
48             // Must generate CGLIB subclass.
49
return instantiateWithMethodInjection(beanDefinition, beanName, owner);
50         }
51     }
52
53     /**
54      * Subclasses can override this method, which is implemented to throw
55      * UnsupportedOperationException, if they can instantiate an object with
56      * the Method Injection specified in the given RootBeanDefinition.
57      * Instantiation should use a no-arg constructor.
58      */

59     protected Object JavaDoc instantiateWithMethodInjection(
60             RootBeanDefinition beanDefinition, String JavaDoc beanName, BeanFactory owner) {
61
62         throw new UnsupportedOperationException JavaDoc(
63                 "Method Injection not supported in SimpleInstantiationStrategy");
64     }
65
66     public Object JavaDoc instantiate(
67             RootBeanDefinition beanDefinition, String JavaDoc beanName, BeanFactory owner,
68             Constructor JavaDoc ctor, Object JavaDoc[] args) {
69
70         if (beanDefinition.getMethodOverrides().isEmpty()) {
71             return BeanUtils.instantiateClass(ctor, args);
72         }
73         else {
74             return instantiateWithMethodInjection(beanDefinition, beanName, owner, ctor, args);
75         }
76     }
77
78     /**
79      * Subclasses can override this method, which is implemented to throw
80      * UnsupportedOperationException, if they can instantiate an object with
81      * the Method Injection specified in the given RootBeanDefinition.
82      * Instantiation should use the given constructor and parameters.
83      */

84     protected Object JavaDoc instantiateWithMethodInjection(
85             RootBeanDefinition beanDefinition, String JavaDoc beanName, BeanFactory owner,
86             Constructor JavaDoc ctor, Object JavaDoc[] args) {
87
88         throw new UnsupportedOperationException JavaDoc(
89                 "Method Injection not supported in SimpleInstantiationStrategy");
90     }
91
92     public Object JavaDoc instantiate(
93             RootBeanDefinition beanDefinition, String JavaDoc beanName, BeanFactory owner,
94             Object JavaDoc factoryBean, Method JavaDoc factoryMethod, Object JavaDoc[] args) {
95
96         try {
97             // It's a static method if the target is null.
98
if (!Modifier.isPublic(factoryMethod.getModifiers()) ||
99                     !Modifier.isPublic(factoryMethod.getDeclaringClass().getModifiers())) {
100                 factoryMethod.setAccessible(true);
101             }
102             return factoryMethod.invoke(factoryBean, args);
103         }
104         catch (IllegalArgumentException JavaDoc ex) {
105             throw new BeanDefinitionStoreException(
106                     "Illegal arguments to factory method [" + factoryMethod + "]; " +
107                     "args: " + StringUtils.arrayToCommaDelimitedString(args));
108         }
109         catch (IllegalAccessException JavaDoc ex) {
110             throw new BeanDefinitionStoreException(
111                     "Cannot access factory method [" + factoryMethod + "]; is it public?");
112         }
113         catch (InvocationTargetException JavaDoc ex) {
114             throw new BeanDefinitionStoreException(
115                     "Factory method [" + factoryMethod + "] threw exception", ex.getTargetException());
116         }
117     }
118
119 }
120
Popular Tags