Skip to content

Commit 3db0bd2

Browse files
committed
Polishing
1 parent a37fda5 commit 3db0bd2

File tree

5 files changed

+80
-78
lines changed

5 files changed

+80
-78
lines changed

spring-beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,7 +70,7 @@ public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements
7070
private CachedIntrospectionResults cachedIntrospectionResults;
7171

7272
/**
73-
* The security context used for invoking the property methods
73+
* The security context used for invoking the property methods.
7474
*/
7575
@Nullable
7676
private AccessControlContext acc;
@@ -97,7 +97,7 @@ public BeanWrapperImpl(boolean registerDefaultEditors) {
9797

9898
/**
9999
* Create a new BeanWrapperImpl for the given object.
100-
* @param object object wrapped by this BeanWrapper
100+
* @param object the object wrapped by this BeanWrapper
101101
*/
102102
public BeanWrapperImpl(Object object) {
103103
super(object);
@@ -114,7 +114,7 @@ public BeanWrapperImpl(Class<?> clazz) {
114114
/**
115115
* Create a new BeanWrapperImpl for the given object,
116116
* registering a nested path that the object is in.
117-
* @param object object wrapped by this BeanWrapper
117+
* @param object the object wrapped by this BeanWrapper
118118
* @param nestedPath the nested path of the object
119119
* @param rootObject the root object at the top of the path
120120
*/
@@ -125,7 +125,7 @@ public BeanWrapperImpl(Object object, String nestedPath, Object rootObject) {
125125
/**
126126
* Create a new BeanWrapperImpl for the given object,
127127
* registering a nested path that the object is in.
128-
* @param object object wrapped by this BeanWrapper
128+
* @param object the object wrapped by this BeanWrapper
129129
* @param nestedPath the nested path of the object
130130
* @param parent the containing BeanWrapper (must not be {@code null})
131131
*/
@@ -166,7 +166,7 @@ protected void setIntrospectionClass(Class<?> clazz) {
166166
}
167167

168168
/**
169-
* Obtain a lazily initializted CachedIntrospectionResults instance
169+
* Obtain a lazily initialized CachedIntrospectionResults instance
170170
* for the wrapped object.
171171
*/
172172
private CachedIntrospectionResults getCachedIntrospectionResults() {
@@ -283,21 +283,21 @@ public TypeDescriptor toTypeDescriptor() {
283283
@Override
284284
@Nullable
285285
public TypeDescriptor nested(int level) {
286-
return TypeDescriptor.nested(property(pd), level);
286+
return TypeDescriptor.nested(property(this.pd), level);
287287
}
288288

289289
@Override
290290
@Nullable
291291
public Object getValue() throws Exception {
292-
final Method readMethod = this.pd.getReadMethod();
292+
Method readMethod = this.pd.getReadMethod();
293293
if (System.getSecurityManager() != null) {
294294
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
295295
ReflectionUtils.makeAccessible(readMethod);
296296
return null;
297297
});
298298
try {
299-
return AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () ->
300-
readMethod.invoke(getWrappedInstance(), (Object[]) null), acc);
299+
return AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
300+
() -> readMethod.invoke(getWrappedInstance(), (Object[]) null), acc);
301301
}
302302
catch (PrivilegedActionException pae) {
303303
throw pae.getException();
@@ -310,8 +310,8 @@ public Object getValue() throws Exception {
310310
}
311311

312312
@Override
313-
public void setValue(final @Nullable Object value) throws Exception {
314-
final Method writeMethod = (this.pd instanceof GenericTypeAwarePropertyDescriptor ?
313+
public void setValue(@Nullable Object value) throws Exception {
314+
Method writeMethod = (this.pd instanceof GenericTypeAwarePropertyDescriptor ?
315315
((GenericTypeAwarePropertyDescriptor) this.pd).getWriteMethodForActualAccess() :
316316
this.pd.getWriteMethod());
317317
if (System.getSecurityManager() != null) {
@@ -320,8 +320,8 @@ public void setValue(final @Nullable Object value) throws Exception {
320320
return null;
321321
});
322322
try {
323-
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () ->
324-
writeMethod.invoke(getWrappedInstance(), value), acc);
323+
AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
324+
() -> writeMethod.invoke(getWrappedInstance(), value), acc);
325325
}
326326
catch (PrivilegedActionException ex) {
327327
throw ex.getException();

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -305,7 +305,7 @@ public <T> T createBean(Class<T> beanClass) throws BeansException {
305305
public void autowireBean(Object existingBean) {
306306
// Use non-singleton bean definition, to avoid registering bean as dependent bean.
307307
RootBeanDefinition bd = new RootBeanDefinition(ClassUtils.getUserClass(existingBean));
308-
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
308+
bd.setScope(SCOPE_PROTOTYPE);
309309
bd.allowCaching = ClassUtils.isCacheSafe(bd.getBeanClass(), getBeanClassLoader());
310310
BeanWrapper bw = new BeanWrapperImpl(existingBean);
311311
initBeanWrapper(bw);
@@ -325,7 +325,7 @@ public Object configureBean(Object existingBean, String beanName) throws BeansEx
325325
bd = new RootBeanDefinition(mbd);
326326
}
327327
if (!bd.isPrototype()) {
328-
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
328+
bd.setScope(SCOPE_PROTOTYPE);
329329
bd.allowCaching = ClassUtils.isCacheSafe(ClassUtils.getUserClass(existingBean), getBeanClassLoader());
330330
}
331331
BeanWrapper bw = new BeanWrapperImpl(existingBean);
@@ -349,28 +349,27 @@ public Object resolveDependency(DependencyDescriptor descriptor, @Nullable Strin
349349
public Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException {
350350
// Use non-singleton bean definition, to avoid registering bean as dependent bean.
351351
RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck);
352-
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
352+
bd.setScope(SCOPE_PROTOTYPE);
353353
return createBean(beanClass.getName(), bd, null);
354354
}
355355

356356
@Override
357357
public Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException {
358358
// Use non-singleton bean definition, to avoid registering bean as dependent bean.
359-
final RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck);
360-
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
359+
RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck);
360+
bd.setScope(SCOPE_PROTOTYPE);
361361
if (bd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR) {
362362
return autowireConstructor(beanClass.getName(), bd, null, null).getWrappedInstance();
363363
}
364364
else {
365365
Object bean;
366-
final BeanFactory parent = this;
367366
if (System.getSecurityManager() != null) {
368-
bean = AccessController.doPrivileged((PrivilegedAction<Object>) () ->
369-
getInstantiationStrategy().instantiate(bd, null, parent),
367+
bean = AccessController.doPrivileged(
368+
(PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(bd, null, this),
370369
getAccessControlContext());
371370
}
372371
else {
373-
bean = getInstantiationStrategy().instantiate(bd, null, parent);
372+
bean = getInstantiationStrategy().instantiate(bd, null, this);
374373
}
375374
populateBean(beanClass.getName(), bd, new BeanWrapperImpl(bean));
376375
return bean;
@@ -387,7 +386,7 @@ public void autowireBeanProperties(Object existingBean, int autowireMode, boolea
387386
// Use non-singleton bean definition, to avoid registering bean as dependent bean.
388387
RootBeanDefinition bd =
389388
new RootBeanDefinition(ClassUtils.getUserClass(existingBean), autowireMode, dependencyCheck);
390-
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
389+
bd.setScope(SCOPE_PROTOTYPE);
391390
BeanWrapper bw = new BeanWrapperImpl(existingBean);
392391
initBeanWrapper(bw);
393392
populateBean(bd.getBeanClass().getName(), bd, bw);
@@ -523,7 +522,7 @@ protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable O
523522
* @see #instantiateUsingFactoryMethod
524523
* @see #autowireConstructor
525524
*/
526-
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
525+
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
527526
throws BeanCreationException {
528527

529528
// Instantiate the bean.
@@ -534,7 +533,7 @@ protected Object doCreateBean(final String beanName, final RootBeanDefinition mb
534533
if (instanceWrapper == null) {
535534
instanceWrapper = createBeanInstance(beanName, mbd, args);
536535
}
537-
final Object bean = instanceWrapper.getWrappedInstance();
536+
Object bean = instanceWrapper.getWrappedInstance();
538537
Class<?> beanType = instanceWrapper.getWrappedClass();
539538
if (beanType != NullBean.class) {
540539
mbd.resolvedTargetType = beanType;
@@ -603,7 +602,7 @@ else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
603602
"] in its raw version as part of a circular reference, but has eventually been " +
604603
"wrapped. This means that said other beans do not use the final version of the " +
605604
"bean. This is often the result of over-eager type matching - consider using " +
606-
"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
605+
"'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");
607606
}
608607
}
609608
}
@@ -860,7 +859,7 @@ protected Class<?> getTypeForFactoryBean(String beanName, RootBeanDefinition mbd
860859
* @return the common {@code FactoryBean} object type, or {@code null} if none
861860
*/
862861
@Nullable
863-
private Class<?> getTypeForFactoryBeanFromMethod(Class<?> beanClass, final String factoryMethodName) {
862+
private Class<?> getTypeForFactoryBeanFromMethod(Class<?> beanClass, String factoryMethodName) {
864863
class Holder { @Nullable Class<?> value = null; }
865864
final Holder objectType = new Holder();
866865

@@ -1206,17 +1205,16 @@ protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable
12061205
* @param mbd the bean definition for the bean
12071206
* @return a BeanWrapper for the new instance
12081207
*/
1209-
protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
1208+
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
12101209
try {
12111210
Object beanInstance;
1212-
final BeanFactory parent = this;
12131211
if (System.getSecurityManager() != null) {
1214-
beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->
1215-
getInstantiationStrategy().instantiate(mbd, beanName, parent),
1212+
beanInstance = AccessController.doPrivileged(
1213+
(PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
12161214
getAccessControlContext());
12171215
}
12181216
else {
1219-
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
1217+
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
12201218
}
12211219
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
12221220
initBeanWrapper(bw);
@@ -1663,7 +1661,7 @@ private Object convertForProperty(
16631661
* @see #invokeInitMethods
16641662
* @see #applyBeanPostProcessorsAfterInitialization
16651663
*/
1666-
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
1664+
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
16671665
if (System.getSecurityManager() != null) {
16681666
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
16691667
invokeAwareMethods(beanName, bean);
@@ -1694,7 +1692,7 @@ protected Object initializeBean(final String beanName, final Object bean, @Nulla
16941692
return wrappedBean;
16951693
}
16961694

1697-
private void invokeAwareMethods(final String beanName, final Object bean) {
1695+
private void invokeAwareMethods(String beanName, Object bean) {
16981696
if (bean instanceof Aware) {
16991697
if (bean instanceof BeanNameAware) {
17001698
((BeanNameAware) bean).setBeanName(beanName);
@@ -1723,7 +1721,7 @@ private void invokeAwareMethods(final String beanName, final Object bean) {
17231721
* @throws Throwable if thrown by init methods or by the invocation process
17241722
* @see #invokeCustomInitMethod
17251723
*/
1726-
protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
1724+
protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
17271725
throws Throwable {
17281726

17291727
boolean isInitializingBean = (bean instanceof InitializingBean);
@@ -1764,12 +1762,12 @@ protected void invokeInitMethods(String beanName, final Object bean, @Nullable R
17641762
* methods with arguments.
17651763
* @see #invokeInitMethods
17661764
*/
1767-
protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd)
1765+
protected void invokeCustomInitMethod(String beanName, Object bean, RootBeanDefinition mbd)
17681766
throws Throwable {
17691767

17701768
String initMethodName = mbd.getInitMethodName();
17711769
Assert.state(initMethodName != null, "No init method set");
1772-
final Method initMethod = (mbd.isNonPublicAccessAllowed() ?
1770+
Method initMethod = (mbd.isNonPublicAccessAllowed() ?
17731771
BeanUtils.findMethod(bean.getClass(), initMethodName) :
17741772
ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName));
17751773

@@ -1798,8 +1796,8 @@ protected void invokeCustomInitMethod(String beanName, final Object bean, RootBe
17981796
return null;
17991797
});
18001798
try {
1801-
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () ->
1802-
initMethod.invoke(bean), getAccessControlContext());
1799+
AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
1800+
() -> initMethod.invoke(bean), getAccessControlContext());
18031801
}
18041802
catch (PrivilegedActionException pae) {
18051803
InvocationTargetException ex = (InvocationTargetException) pae.getException();

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,11 @@ public <T> T getBean(String name, @Nullable Class<T> requiredType, @Nullable Obj
236236
* @throws BeansException if the bean could not be created
237237
*/
238238
@SuppressWarnings("unchecked")
239-
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
240-
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
239+
protected <T> T doGetBean(
240+
String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
241+
throws BeansException {
241242

242-
final String beanName = transformedBeanName(name);
243+
String beanName = transformedBeanName(name);
243244
Object bean;
244245

245246
// Eagerly check singleton cache for manually registered singletons.
@@ -288,7 +289,7 @@ else if (args != null) {
288289
}
289290

290291
try {
291-
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
292+
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
292293
checkMergedBeanDefinition(mbd, beanName, args);
293294

294295
// Guarantee initialization of beans that the current bean depends on.
@@ -342,7 +343,10 @@ else if (mbd.isPrototype()) {
342343

343344
else {
344345
String scopeName = mbd.getScope();
345-
final Scope scope = this.scopes.get(scopeName);
346+
if (!StringUtils.hasLength(scopeName)) {
347+
throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
348+
}
349+
Scope scope = this.scopes.get(scopeName);
346350
if (scope == null) {
347351
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
348352
}
@@ -466,10 +470,12 @@ public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
466470
return false;
467471
}
468472
if (isFactoryBean(beanName, mbd)) {
469-
final FactoryBean<?> fb = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
473+
FactoryBean<?> fb = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
470474
if (System.getSecurityManager() != null) {
471-
return AccessController.doPrivileged((PrivilegedAction<Boolean>) () ->
472-
((fb instanceof SmartFactoryBean && ((SmartFactoryBean<?>) fb).isPrototype()) || !fb.isSingleton()),
475+
return AccessController.doPrivileged(
476+
(PrivilegedAction<Boolean>) () ->
477+
((fb instanceof SmartFactoryBean && ((SmartFactoryBean<?>) fb).isPrototype()) ||
478+
!fb.isSingleton()),
473479
getAccessControlContext());
474480
}
475481
else {
@@ -1366,16 +1372,16 @@ public void clearMetadataCache() {
13661372
* @throws CannotLoadBeanClassException if we failed to load the class
13671373
*/
13681374
@Nullable
1369-
protected Class<?> resolveBeanClass(final RootBeanDefinition mbd, String beanName, final Class<?>... typesToMatch)
1375+
protected Class<?> resolveBeanClass(RootBeanDefinition mbd, String beanName, Class<?>... typesToMatch)
13701376
throws CannotLoadBeanClassException {
13711377

13721378
try {
13731379
if (mbd.hasBeanClass()) {
13741380
return mbd.getBeanClass();
13751381
}
13761382
if (System.getSecurityManager() != null) {
1377-
return AccessController.doPrivileged((PrivilegedExceptionAction<Class<?>>) () ->
1378-
doResolveBeanClass(mbd, typesToMatch), getAccessControlContext());
1383+
return AccessController.doPrivileged((PrivilegedExceptionAction<Class<?>>)
1384+
() -> doResolveBeanClass(mbd, typesToMatch), getAccessControlContext());
13791385
}
13801386
else {
13811387
return doResolveBeanClass(mbd, typesToMatch);
@@ -1614,7 +1620,7 @@ protected boolean hasBeanCreationStarted() {
16141620
* Get the object for the given bean instance, either the bean
16151621
* instance itself or its created object in case of a FactoryBean.
16161622
* @param beanInstance the shared bean instance
1617-
* @param name name that may include factory dereference prefix
1623+
* @param name the name that may include factory dereference prefix
16181624
* @param beanName the canonical bean name
16191625
* @param mbd the merged bean definition
16201626
* @return the object to expose for the bean

0 commit comments

Comments
 (0)