KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.springframework.beans.MutablePropertyValues;
24 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
25 import org.springframework.beans.factory.config.BeanDefinition;
26 import org.springframework.beans.factory.config.ConstructorArgumentValues;
27 import org.springframework.core.AttributeAccessorSupport;
28 import org.springframework.util.Assert;
29 import org.springframework.util.ClassUtils;
30 import org.springframework.util.ObjectUtils;
31
32 /**
33  * Base class for concrete, full-fledged
34  * {@link org.springframework.beans.factory.config.BeanDefinition} classes,
35  * factoring out common properties of {@link RootBeanDefinition} and
36  * {@link ChildBeanDefinition}.
37  *
38  * <p>The autowire constants match the ones defined in the
39  * {@link org.springframework.beans.factory.config.AutowireCapableBeanFactory}
40  * interface.
41  *
42  * @author Rod Johnson
43  * @author Juergen Hoeller
44  * @author Rob Harrop
45  * @see RootBeanDefinition
46  * @see ChildBeanDefinition
47  */

48 public abstract class AbstractBeanDefinition extends AttributeAccessorSupport implements BeanDefinition {
49
50     /**
51      * Constant that indicates no autowiring at all.
52      * @see #setAutowireMode
53      */

54     public static final int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO;
55
56     /**
57      * Constant that indicates autowiring bean properties by name.
58      * @see #setAutowireMode
59      */

60     public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
61
62     /**
63      * Constant that indicates autowiring bean properties by type.
64      * @see #setAutowireMode
65      */

66     public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
67
68     /**
69      * Constant that indicates autowiring a constructor.
70      * @see #setAutowireMode
71      */

72     public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
73
74     /**
75      * Constant that indicates determining an appropriate autowire strategy
76      * through introspection of the bean class.
77      * @see #setAutowireMode
78      */

79     public static final int AUTOWIRE_AUTODETECT = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;
80
81
82     /**
83      * Constant that indicates no dependency check at all.
84      * @see #setDependencyCheck
85      */

86     public static final int DEPENDENCY_CHECK_NONE = 0;
87
88     /**
89      * Constant that indicates dependency checking for object references.
90      * @see #setDependencyCheck
91      */

92     public static final int DEPENDENCY_CHECK_OBJECTS = 1;
93
94     /**
95      * Constant that indicates dependency checking for "simple" properties.
96      * @see #setDependencyCheck
97      * @see org.springframework.beans.BeanUtils#isSimpleProperty
98      */

99     public static final int DEPENDENCY_CHECK_SIMPLE = 2;
100
101     /**
102      * Constant that indicates dependency checking for all properties
103      * (object references as well as "simple" properties).
104      * @see #setDependencyCheck
105      */

106     public static final int DEPENDENCY_CHECK_ALL = 3;
107
108
109     private Object JavaDoc beanClass;
110
111     private String JavaDoc scope = SCOPE_SINGLETON;
112
113     private boolean abstractFlag = false;
114
115     private boolean lazyInit = false;
116
117     private boolean autowireCandidate = true;
118
119     private int autowireMode = AUTOWIRE_NO;
120
121     private int dependencyCheck = DEPENDENCY_CHECK_NONE;
122
123     private String JavaDoc[] dependsOn;
124
125     private ConstructorArgumentValues constructorArgumentValues;
126
127     private MutablePropertyValues propertyValues;
128
129     private MethodOverrides methodOverrides = new MethodOverrides();
130
131     private String JavaDoc factoryBeanName;
132
133     private String JavaDoc factoryMethodName;
134
135     private String JavaDoc initMethodName;
136
137     private String JavaDoc destroyMethodName;
138
139     private boolean enforceInitMethod = true;
140
141     private boolean enforceDestroyMethod = true;
142
143     private boolean synthetic = false;
144
145     private String JavaDoc resourceDescription;
146
147     private Object JavaDoc source;
148
149     private int role = BeanDefinition.ROLE_APPLICATION;
150
151
152     /**
153      * Create a new AbstractBeanDefinition with default settings.
154      */

155     protected AbstractBeanDefinition() {
156         this(null, null);
157     }
158
159     /**
160      * Create a new AbstractBeanDefinition with the given
161      * constructor argument values and property values.
162      */

163     protected AbstractBeanDefinition(ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
164         setConstructorArgumentValues(cargs);
165         setPropertyValues(pvs);
166     }
167
168     /**
169      * Create a new AbstractBeanDefinition as deep copy of the given
170      * bean definition.
171      * @param original the original bean definition to copy from
172      */

173     protected AbstractBeanDefinition(AbstractBeanDefinition original) {
174         this.beanClass = original.beanClass;
175
176         setScope(original.getScope());
177         setAbstract(original.isAbstract());
178         setLazyInit(original.isLazyInit());
179
180         setAutowireCandidate(original.isAutowireCandidate());
181         setAutowireMode(original.getAutowireMode());
182         setDependencyCheck(original.getDependencyCheck());
183         setDependsOn(original.getDependsOn());
184
185         setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues()));
186         setPropertyValues(new MutablePropertyValues(original.getPropertyValues()));
187         setMethodOverrides(new MethodOverrides(original.getMethodOverrides()));
188
189         setFactoryBeanName(original.getFactoryBeanName());
190         setFactoryMethodName(original.getFactoryMethodName());
191         setInitMethodName(original.getInitMethodName());
192         setEnforceInitMethod(original.isEnforceInitMethod());
193         setDestroyMethodName(original.getDestroyMethodName());
194         setEnforceDestroyMethod(original.isEnforceDestroyMethod());
195
196         setSynthetic(original.isSynthetic());
197         setResourceDescription(original.getResourceDescription());
198         setSource(original.getSource());
199         setRole(original.getRole());
200
201         copyAttributesFrom(original);
202     }
203
204     /**
205      * Override settings in this bean definition (assumably a copied parent
206      * from a parent-child inheritance relationship) from the given bean
207      * definition (assumably the child).
208      * <p><ul>
209      * <li>Will override beanClass if specified in the given bean definition.
210      * <li>Will always take abstract, singleton, lazyInit, autowireMode,
211      * dependencyCheck, dependsOn from the given bean definition.
212      * <li>Will add constructorArgumentValues, propertyValues, methodOverrides
213      * from the given bean definition to existing ones.
214      * <li>Will override factoryBeanName, factoryMethodName, initMethodName,
215      * destroyMethodName if specified in the given bean definition.
216      * </ul>
217      */

218     public void overrideFrom(AbstractBeanDefinition other) {
219         if (other.beanClass != null) {
220             this.beanClass = other.beanClass;
221         }
222
223         setScope(other.getScope());
224         setAbstract(other.isAbstract());
225         setLazyInit(other.isLazyInit());
226
227         setAutowireCandidate(other.isAutowireCandidate());
228         setAutowireMode(other.getAutowireMode());
229         setDependencyCheck(other.getDependencyCheck());
230         setDependsOn(other.getDependsOn());
231
232         getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues());
233         getPropertyValues().addPropertyValues(other.getPropertyValues());
234         getMethodOverrides().addOverrides(other.getMethodOverrides());
235
236         if (other.getFactoryBeanName() != null) {
237             setFactoryBeanName(other.getFactoryBeanName());
238         }
239         if (other.getFactoryMethodName() != null) {
240             setFactoryMethodName(other.getFactoryMethodName());
241         }
242         if (other.getInitMethodName() != null) {
243             setInitMethodName(other.getInitMethodName());
244             setEnforceInitMethod(other.isEnforceInitMethod());
245         }
246         if (other.getDestroyMethodName() != null) {
247             setDestroyMethodName(other.getDestroyMethodName());
248             setEnforceDestroyMethod(other.isEnforceDestroyMethod());
249         }
250
251         setSynthetic(other.isSynthetic());
252         setResourceDescription(other.getResourceDescription());
253         setSource(other.getSource());
254         setRole(other.getRole());
255
256         copyAttributesFrom(other);
257     }
258
259
260     /**
261      * Return whether this definition specifies a bean class.
262      */

263     public boolean hasBeanClass() {
264         return (this.beanClass instanceof Class JavaDoc);
265     }
266
267     /**
268      * Specify the class for this bean.
269      */

270     public void setBeanClass(Class JavaDoc beanClass) {
271         this.beanClass = beanClass;
272     }
273
274     /**
275      * Return the class of the wrapped bean.
276      * @throws IllegalStateException if the bean definition does not define a bean class,
277      * or a specified bean class name has not been resolved into an actual Class
278      */

279     public Class JavaDoc getBeanClass() throws IllegalStateException JavaDoc {
280         if (this.beanClass == null) {
281             throw new IllegalStateException JavaDoc("No bean class specified on bean definition");
282         }
283         if (!(this.beanClass instanceof Class JavaDoc)) {
284             throw new IllegalStateException JavaDoc(
285                     "Bean class name [" + this.beanClass + "] has not been resolved into an actual Class");
286         }
287         return (Class JavaDoc) this.beanClass;
288     }
289
290     /**
291      * Specify the class name for this bean.
292      */

293     public void setBeanClassName(String JavaDoc beanClassName) {
294         this.beanClass = beanClassName;
295     }
296
297     /**
298      * Return the class name of the wrapped bean.
299      */

300     public String JavaDoc getBeanClassName() {
301         if (this.beanClass instanceof Class JavaDoc) {
302             return ((Class JavaDoc) this.beanClass).getName();
303         }
304         else {
305             return (String JavaDoc) this.beanClass;
306         }
307     }
308
309     /**
310      * Determine the class of the wrapped bean, resolving it from a
311      * specified class name if necessary. Will also reload a specified
312      * Class from its name when called with the bean class already resolved.
313      * @param classLoader the ClassLoader to use for resolving a (potential) class name
314      * @return the resolved bean class
315      * @throws ClassNotFoundException if the class name could be resolved
316      */

317     public Class JavaDoc resolveBeanClass(ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
318         if (this.beanClass == null) {
319             return null;
320         }
321         Class JavaDoc resolvedClass = ClassUtils.forName(getBeanClassName(), classLoader);
322         this.beanClass = resolvedClass;
323         return resolvedClass;
324     }
325
326
327     /**
328      * Set the name of the target scope for the bean.
329      * <p>Default is "singleton"; the out-of-the-box alternative is "prototype".
330      * Extended bean factories might support further scopes.
331      * @see #SCOPE_SINGLETON
332      * @see #SCOPE_PROTOTYPE
333      */

334     public void setScope(String JavaDoc scope) {
335         Assert.notNull(scope, "Scope must not be null");
336         this.scope = scope;
337     }
338
339     /**
340      * Return the name of the target scope for the bean.
341      */

342     public String JavaDoc getScope() {
343         return this.scope;
344     }
345
346     /**
347      * Set if this a <b>Singleton</b>, with a single, shared instance returned
348      * on all calls. In case of "false", the BeanFactory will apply the <b>Prototype</b>
349      * design pattern, with each caller requesting an instance getting an independent
350      * instance. How this is exactly defined will depend on the BeanFactory.
351      * <p>"Singletons" are the commoner type, so the default is "true".
352      * Note that as of Spring 2.0, this flag is just an alternative way to
353      * specify scope="singleton" or scope="prototype".
354      * @see #setScope
355      * @see #SCOPE_SINGLETON
356      * @see #SCOPE_PROTOTYPE
357      */

358     public void setSingleton(boolean singleton) {
359         this.scope = (singleton ? SCOPE_SINGLETON : SCOPE_PROTOTYPE);
360     }
361
362     /**
363      * Return whether this a <b>Singleton</b>, with a single shared instance
364      * returned from all calls.
365      * @see #SCOPE_SINGLETON
366      */

367     public boolean isSingleton() {
368         return (SCOPE_SINGLETON.equals(this.scope));
369     }
370
371     /**
372      * Return whether this a <b>Prototype</b>, with an independent instance
373      * returned for each call.
374      * @see #SCOPE_PROTOTYPE
375      */

376     public boolean isPrototype() {
377         return (SCOPE_PROTOTYPE.equals(this.scope));
378     }
379
380     /**
381      * Set if this bean is "abstract", i.e. not meant to be instantiated itself but
382      * rather just serving as parent for concrete child bean definitions.
383      * <p>Default is "false". Specify true to tell the bean factory to not try to
384      * instantiate that particular bean in any case.
385      */

386     public void setAbstract(boolean abstractFlag) {
387         this.abstractFlag = abstractFlag;
388     }
389
390     /**
391      * Return whether this bean is "abstract", i.e. not meant to be instantiated
392      * itself but rather just serving as parent for concrete child bean definitions.
393      */

394     public boolean isAbstract() {
395         return this.abstractFlag;
396     }
397
398     /**
399      * Set whether this bean should be lazily initialized.
400      * <p>If <code>false</code>, the bean will get instantiated on startup by bean
401      * factories that perform eager initialization of singletons.
402      */

403     public void setLazyInit(boolean lazyInit) {
404         this.lazyInit = lazyInit;
405     }
406
407     /**
408      * Return whether this bean should be lazily initialized, i.e. not
409      * eagerly instantiated on startup. Only applicable to a singleton bean.
410      */

411     public boolean isLazyInit() {
412         return this.lazyInit;
413     }
414
415
416     /**
417      * Set whether this bean is a candidate for getting autowired into
418      * some other bean.
419      */

420     public void setAutowireCandidate(boolean autowireCandidate) {
421         this.autowireCandidate = autowireCandidate;
422     }
423
424     /**
425      * Return whether this bean is a candidate for getting autowired into
426      * some other bean.
427      */

428     public boolean isAutowireCandidate() {
429         return this.autowireCandidate;
430     }
431
432     /**
433      * Set the autowire mode. This determines whether any automagical detection
434      * and setting of bean references will happen. Default is AUTOWIRE_NO,
435      * which means there's no autowire.
436      * @param autowireMode the autowire mode to set.
437      * Must be one of the constants defined in this class.
438      * @see #AUTOWIRE_NO
439      * @see #AUTOWIRE_BY_NAME
440      * @see #AUTOWIRE_BY_TYPE
441      * @see #AUTOWIRE_CONSTRUCTOR
442      * @see #AUTOWIRE_AUTODETECT
443      */

444     public void setAutowireMode(int autowireMode) {
445         this.autowireMode = autowireMode;
446     }
447
448     /**
449      * Return the autowire mode as specified in the bean definition.
450      */

451     public int getAutowireMode() {
452         return this.autowireMode;
453     }
454
455     /**
456      * Return the resolved autowire code,
457      * (resolving AUTOWIRE_AUTODETECT to AUTOWIRE_CONSTRUCTOR or AUTOWIRE_BY_TYPE).
458      * @see #AUTOWIRE_AUTODETECT
459      * @see #AUTOWIRE_CONSTRUCTOR
460      * @see #AUTOWIRE_BY_TYPE
461      */

462     public int getResolvedAutowireMode() {
463         if (this.autowireMode == AUTOWIRE_AUTODETECT) {
464             // Work out whether to apply setter autowiring or constructor autowiring.
465
// If it has a no-arg constructor it's deemed to be setter autowiring,
466
// otherwise we'll try constructor autowiring.
467
Constructor JavaDoc[] constructors = getBeanClass().getConstructors();
468             for (int i = 0; i < constructors.length; i++) {
469                 if (constructors[i].getParameterTypes().length == 0) {
470                     return AUTOWIRE_BY_TYPE;
471                 }
472             }
473             return AUTOWIRE_CONSTRUCTOR;
474         }
475         else {
476             return this.autowireMode;
477         }
478     }
479
480     /**
481      * Set the dependency check code.
482      * @param dependencyCheck the code to set.
483      * Must be one of the four constants defined in this class.
484      * @see #DEPENDENCY_CHECK_NONE
485      * @see #DEPENDENCY_CHECK_OBJECTS
486      * @see #DEPENDENCY_CHECK_SIMPLE
487      * @see #DEPENDENCY_CHECK_ALL
488      */

489     public void setDependencyCheck(int dependencyCheck) {
490         this.dependencyCheck = dependencyCheck;
491     }
492
493     /**
494      * Return the dependency check code.
495      */

496     public int getDependencyCheck() {
497         return this.dependencyCheck;
498     }
499
500     /**
501      * Set the names of the beans that this bean depends on being initialized.
502      * The bean factory will guarantee that these beans get initialized before.
503      * <p>Note that dependencies are normally expressed through bean properties or
504      * constructor arguments. This property should just be necessary for other kinds
505      * of dependencies like statics (*ugh*) or database preparation on startup.
506      */

507     public void setDependsOn(String JavaDoc[] dependsOn) {
508         this.dependsOn = dependsOn;
509     }
510
511     /**
512      * Return the bean names that this bean depends on.
513      */

514     public String JavaDoc[] getDependsOn() {
515         return this.dependsOn;
516     }
517
518
519     /**
520      * Specify constructor argument values for this bean.
521      */

522     public void setConstructorArgumentValues(ConstructorArgumentValues constructorArgumentValues) {
523         this.constructorArgumentValues =
524                 (constructorArgumentValues != null ? constructorArgumentValues : new ConstructorArgumentValues());
525     }
526
527     /**
528      * Return constructor argument values for this bean (never <code>null</code>).
529      */

530     public ConstructorArgumentValues getConstructorArgumentValues() {
531         return constructorArgumentValues;
532     }
533
534     /**
535      * Return if there are constructor argument values defined for this bean.
536      */

537     public boolean hasConstructorArgumentValues() {
538         return !this.constructorArgumentValues.isEmpty();
539     }
540
541     /**
542      * Specify property values for this bean, if any.
543      */

544     public void setPropertyValues(MutablePropertyValues propertyValues) {
545         this.propertyValues = (propertyValues != null ? propertyValues : new MutablePropertyValues());
546     }
547
548     /**
549      * Return property values for this bean (never <code>null</code>).
550      */

551     public MutablePropertyValues getPropertyValues() {
552         return this.propertyValues;
553     }
554
555     /**
556      * Specify method overrides for the bean, if any.
557      */

558     public void setMethodOverrides(MethodOverrides methodOverrides) {
559         this.methodOverrides = (methodOverrides != null ? methodOverrides : new MethodOverrides());
560     }
561
562     /**
563      * Return information about methods to be overridden by the IoC
564      * container. This will be empty if there are no method overrides.
565      * Never returns null.
566      */

567     public MethodOverrides getMethodOverrides() {
568         return this.methodOverrides;
569     }
570
571
572     /**
573      * Specify the factory bean to use, if any.
574      */

575     public void setFactoryBeanName(String JavaDoc factoryBeanName) {
576         this.factoryBeanName = factoryBeanName;
577     }
578
579     /**
580      * Returns the factory bean name, if any.
581      */

582     public String JavaDoc getFactoryBeanName() {
583         return this.factoryBeanName;
584     }
585
586     /**
587      * Specify a factory method, if any. This method will be invoked with
588      * constructor arguments, or with no arguments if none are specified.
589      * The static method will be invoked on the specifed factory bean,
590      * if any, or on the local bean class else.
591      * @param factoryMethodName static factory method name, or <code>null</code> if
592      * normal constructor creation should be used
593      * @see #getBeanClass
594      */

595     public void setFactoryMethodName(String JavaDoc factoryMethodName) {
596         this.factoryMethodName = factoryMethodName;
597     }
598
599     /**
600      * Return a factory method, if any.
601      */

602     public String JavaDoc getFactoryMethodName() {
603         return this.factoryMethodName;
604     }
605
606     /**
607      * Set the name of the initializer method. The default is <code>null</code>
608      * in which case there is no initializer method.
609      */

610     public void setInitMethodName(String JavaDoc initMethodName) {
611         this.initMethodName = initMethodName;
612     }
613
614     /**
615      * Return the name of the initializer method.
616      */

617     public String JavaDoc getInitMethodName() {
618         return this.initMethodName;
619     }
620
621     /**
622      * Specify whether or not the configured init method is the default.
623      * Default value is <code>false</code>.
624      * @see #setInitMethodName
625      */

626     public void setEnforceInitMethod(boolean enforceInitMethod) {
627         this.enforceInitMethod = enforceInitMethod;
628     }
629
630     /**
631      * Indicate whether the configured init method is the default.
632      * @see #getInitMethodName()
633      */

634     public boolean isEnforceInitMethod() {
635         return this.enforceInitMethod;
636     }
637
638     /**
639      * Set the name of the destroy method. The default is <code>null</code>
640      * in which case there is no destroy method.
641      */

642     public void setDestroyMethodName(String JavaDoc destroyMethodName) {
643         this.destroyMethodName = destroyMethodName;
644     }
645
646     /**
647      * Return the name of the destroy method.
648      */

649     public String JavaDoc getDestroyMethodName() {
650         return this.destroyMethodName;
651     }
652
653     /**
654      * Specify whether or not the configured destroy method is the default.
655      * Default value is <code>false</code>.
656      * @see #setDestroyMethodName
657      */

658     public void setEnforceDestroyMethod(boolean enforceDestroyMethod) {
659         this.enforceDestroyMethod = enforceDestroyMethod;
660     }
661
662     /**
663      * Indicate whether the configured destroy method is the default.
664      * @see #getDestroyMethodName
665      */

666     public boolean isEnforceDestroyMethod() {
667         return this.enforceDestroyMethod;
668     }
669
670
671     /**
672      * Set whether this bean definition is 'synthetic', that is, not defined
673      * by the application itself (for example, an infrastructure bean such
674      * as a helper for auto-proxying, created through <code>&ltaop:config&gt;</code>).
675      */

676     public void setSynthetic(boolean synthetic) {
677         this.synthetic = synthetic;
678     }
679
680     /**
681      * Return whether this bean definition is 'synthetic', that is,
682      * not defined by the application itself.
683      */

684     public boolean isSynthetic() {
685         return this.synthetic;
686     }
687
688     /**
689      * Set a description of the resource that this bean definition
690      * came from (for the purpose of showing context in case of errors).
691      */

692     public void setResourceDescription(String JavaDoc resourceDescription) {
693         this.resourceDescription = resourceDescription;
694     }
695
696     /**
697      * Return a description of the resource that this bean definition
698      * came from.
699      */

700     public String JavaDoc getResourceDescription() {
701         return this.resourceDescription;
702     }
703
704     /**
705      * Set the configuration source <code>Object</code> for this metadata element.
706      * <p>The exact type of the object will depend on the configuration mechanism used.
707      */

708     public void setSource(Object JavaDoc source) {
709         this.source = source;
710     }
711
712     public Object JavaDoc getSource() {
713         return this.source;
714     }
715
716     /**
717      * Set the role hint for this <code>BeanDefinition</code>.
718      */

719     public void setRole(int role) {
720         this.role = role;
721     }
722
723     /**
724      * Return the role hint for this <code>BeanDefinition</code>.
725      */

726     public int getRole() {
727         return this.role;
728     }
729
730
731     /**
732      * Validate this bean definition.
733      * @throws BeanDefinitionValidationException in case of validation failure
734      */

735     public void validate() throws BeanDefinitionValidationException {
736         if (!getMethodOverrides().isEmpty() && getFactoryMethodName() != null) {
737             throw new BeanDefinitionValidationException(
738                     "Cannot combine static factory method with method overrides: " +
739                     "the static factory method must create the instance");
740         }
741
742         if (hasBeanClass()) {
743             prepareMethodOverrides();
744         }
745     }
746
747     /**
748      * Validate and prepare the method overrides defined for this bean.
749      * Checks for existence of a method with the specified name.
750      * @throws BeanDefinitionValidationException in case of validation failure
751      */

752     public void prepareMethodOverrides() throws BeanDefinitionValidationException {
753         // Check that lookup methods exists.
754
for (Iterator JavaDoc it = getMethodOverrides().getOverrides().iterator(); it.hasNext(); ) {
755             MethodOverride mo = (MethodOverride) it.next();
756             prepareMethodOverride(mo);
757         }
758     }
759
760     /**
761      * Validate and prepare the given method override.
762      * Checks for existence of a method with the specified name,
763      * marking it as not overloaded if none found.
764      * @param mo the MethodOverride object to validate
765      * @throws BeanDefinitionValidationException in case of validation failure
766      */

767     protected void prepareMethodOverride(MethodOverride mo) throws BeanDefinitionValidationException {
768         int count = ClassUtils.getMethodCountForName(getBeanClass(), mo.getMethodName());
769         if (count == 0) {
770             throw new BeanDefinitionValidationException(
771                     "Invalid method override: no method with name '" + mo.getMethodName() +
772                     "' on class [" + getBeanClassName() + "]");
773         }
774         else if (count == 1) {
775             // Mark override as not overloaded, to avoid the overhead of arg type checking.
776
mo.setOverloaded(false);
777         }
778     }
779
780
781     public boolean equals(Object JavaDoc other) {
782         if (this == other) {
783             return true;
784         }
785         if (!(other instanceof AbstractBeanDefinition)) {
786             return false;
787         }
788
789         AbstractBeanDefinition that = (AbstractBeanDefinition) other;
790
791         if (!ObjectUtils.nullSafeEquals(this.beanClass, that.beanClass)) return false;
792         if (!ObjectUtils.nullSafeEquals(this.scope, that.scope)) return false;
793         if (this.abstractFlag != that.abstractFlag) return false;
794         if (this.lazyInit != that.lazyInit) return false;
795
796         if (this.autowireCandidate != that.autowireCandidate) return false;
797         if (this.autowireMode != that.autowireMode) return false;
798         if (this.dependencyCheck != that.dependencyCheck) return false;
799         if (!Arrays.equals(this.dependsOn, that.dependsOn)) return false;
800
801         if (!ObjectUtils.nullSafeEquals(this.constructorArgumentValues, that.constructorArgumentValues)) return false;
802         if (!ObjectUtils.nullSafeEquals(this.propertyValues, that.propertyValues)) return false;
803         if (!ObjectUtils.nullSafeEquals(this.methodOverrides, that.methodOverrides)) return false;
804
805         if (!ObjectUtils.nullSafeEquals(this.factoryBeanName, that.factoryBeanName)) return false;
806         if (!ObjectUtils.nullSafeEquals(this.factoryMethodName, that.factoryMethodName)) return false;
807         if (!ObjectUtils.nullSafeEquals(this.initMethodName, that.initMethodName)) return false;
808         if (this.enforceInitMethod != that.enforceInitMethod) return false;
809         if (!ObjectUtils.nullSafeEquals(this.destroyMethodName, that.destroyMethodName)) return false;
810         if (this.enforceDestroyMethod != that.enforceDestroyMethod) return false;
811
812         if (!ObjectUtils.nullSafeEquals(this.resourceDescription, that.resourceDescription)) return false;
813         if (!ObjectUtils.nullSafeEquals(this.source, that.source)) return false;
814         if (this.role != that.role) return false;
815
816         return super.equals(other);
817     }
818
819     public int hashCode() {
820         int hashCode = ObjectUtils.nullSafeHashCode(this.beanClass);
821         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.scope);
822         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.constructorArgumentValues);
823         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.propertyValues);
824         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryBeanName);
825         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.factoryMethodName);
826         hashCode = 29 * hashCode + super.hashCode();
827         return hashCode;
828     }
829
830     public String JavaDoc toString() {
831         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("class [");
832         sb.append(getBeanClassName()).append("]");
833         sb.append("; scope=").append(this.scope);
834         sb.append("; abstract=").append(this.abstractFlag);
835         sb.append("; lazyInit=").append(this.lazyInit);
836         sb.append("; autowireCandidate=").append(this.autowireCandidate);
837         sb.append("; autowireMode=").append(this.autowireMode);
838         sb.append("; dependencyCheck=").append(this.dependencyCheck);
839         sb.append("; factoryBeanName=").append(this.factoryBeanName);
840         sb.append("; factoryMethodName=").append(this.factoryMethodName);
841         sb.append("; initMethodName=").append(this.initMethodName);
842         sb.append("; destroyMethodName=").append(this.destroyMethodName);
843         if (this.resourceDescription != null) {
844             sb.append("; defined in ").append(this.resourceDescription);
845         }
846         return sb.toString();
847     }
848
849 }
850
Popular Tags