Skip to content

Commit 9138a04

Browse files
committed
Polishing
1 parent e46ccd7 commit 9138a04

File tree

5 files changed

+70
-70
lines changed

5 files changed

+70
-70
lines changed

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

Lines changed: 8 additions & 8 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.
@@ -289,15 +289,15 @@ public TypeDescriptor nested(int level) {
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: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -356,21 +356,20 @@ public Object createBean(Class<?> beanClass, int autowireMode, boolean dependenc
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);
359+
RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck);
360360
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;
@@ -545,7 +544,7 @@ protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable O
545544
* @see #instantiateUsingFactoryMethod
546545
* @see #autowireConstructor
547546
*/
548-
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
547+
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
549548
throws BeanCreationException {
550549

551550
// Instantiate the bean.
@@ -556,7 +555,7 @@ protected Object doCreateBean(final String beanName, final RootBeanDefinition mb
556555
if (instanceWrapper == null) {
557556
instanceWrapper = createBeanInstance(beanName, mbd, args);
558557
}
559-
final Object bean = instanceWrapper.getWrappedInstance();
558+
Object bean = instanceWrapper.getWrappedInstance();
560559
Class<?> beanType = instanceWrapper.getWrappedClass();
561560
if (beanType != NullBean.class) {
562561
mbd.resolvedTargetType = beanType;
@@ -1299,17 +1298,16 @@ protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable
12991298
* @param mbd the bean definition for the bean
13001299
* @return a BeanWrapper for the new instance
13011300
*/
1302-
protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
1301+
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
13031302
try {
13041303
Object beanInstance;
1305-
final BeanFactory parent = this;
13061304
if (System.getSecurityManager() != null) {
1307-
beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->
1308-
getInstantiationStrategy().instantiate(mbd, beanName, parent),
1305+
beanInstance = AccessController.doPrivileged(
1306+
(PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
13091307
getAccessControlContext());
13101308
}
13111309
else {
1312-
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
1310+
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
13131311
}
13141312
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
13151313
initBeanWrapper(bw);
@@ -1772,7 +1770,7 @@ private Object convertForProperty(
17721770
* @see #invokeInitMethods
17731771
* @see #applyBeanPostProcessorsAfterInitialization
17741772
*/
1775-
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
1773+
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
17761774
if (System.getSecurityManager() != null) {
17771775
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
17781776
invokeAwareMethods(beanName, bean);
@@ -1803,7 +1801,7 @@ protected Object initializeBean(final String beanName, final Object bean, @Nulla
18031801
return wrappedBean;
18041802
}
18051803

1806-
private void invokeAwareMethods(final String beanName, final Object bean) {
1804+
private void invokeAwareMethods(String beanName, Object bean) {
18071805
if (bean instanceof Aware) {
18081806
if (bean instanceof BeanNameAware) {
18091807
((BeanNameAware) bean).setBeanName(beanName);
@@ -1832,7 +1830,7 @@ private void invokeAwareMethods(final String beanName, final Object bean) {
18321830
* @throws Throwable if thrown by init methods or by the invocation process
18331831
* @see #invokeCustomInitMethod
18341832
*/
1835-
protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
1833+
protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
18361834
throws Throwable {
18371835

18381836
boolean isInitializingBean = (bean instanceof InitializingBean);
@@ -1873,7 +1871,7 @@ protected void invokeInitMethods(String beanName, final Object bean, @Nullable R
18731871
* methods with arguments.
18741872
* @see #invokeInitMethods
18751873
*/
1876-
protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd)
1874+
protected void invokeCustomInitMethod(String beanName, Object bean, RootBeanDefinition mbd)
18771875
throws Throwable {
18781876

18791877
String initMethodName = mbd.getInitMethodName();
@@ -1908,8 +1906,8 @@ protected void invokeCustomInitMethod(String beanName, final Object bean, RootBe
19081906
return null;
19091907
});
19101908
try {
1911-
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () ->
1912-
methodToInvoke.invoke(bean), getAccessControlContext());
1909+
AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
1910+
() -> methodToInvoke.invoke(bean), getAccessControlContext());
19131911
}
19141912
catch (PrivilegedActionException pae) {
19151913
InvocationTargetException ex = (InvocationTargetException) pae.getException();

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,11 @@ public <T> T getBean(String name, @Nullable Class<T> requiredType, @Nullable Obj
239239
* @throws BeansException if the bean could not be created
240240
*/
241241
@SuppressWarnings("unchecked")
242-
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
243-
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
242+
protected <T> T doGetBean(
243+
String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
244+
throws BeansException {
244245

245-
final String beanName = transformedBeanName(name);
246+
String beanName = transformedBeanName(name);
246247
Object bean;
247248

248249
// Eagerly check singleton cache for manually registered singletons.
@@ -294,7 +295,7 @@ else if (requiredType != null) {
294295
}
295296

296297
try {
297-
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
298+
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
298299
checkMergedBeanDefinition(mbd, beanName, args);
299300

300301
// Guarantee initialization of beans that the current bean depends on.
@@ -348,7 +349,7 @@ else if (mbd.isPrototype()) {
348349

349350
else {
350351
String scopeName = mbd.getScope();
351-
final Scope scope = this.scopes.get(scopeName);
352+
Scope scope = this.scopes.get(scopeName);
352353
if (scope == null) {
353354
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
354355
}
@@ -472,10 +473,12 @@ public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
472473
return false;
473474
}
474475
if (isFactoryBean(beanName, mbd)) {
475-
final FactoryBean<?> fb = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
476+
FactoryBean<?> fb = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
476477
if (System.getSecurityManager() != null) {
477-
return AccessController.doPrivileged((PrivilegedAction<Boolean>) () ->
478-
((fb instanceof SmartFactoryBean && ((SmartFactoryBean<?>) fb).isPrototype()) || !fb.isSingleton()),
478+
return AccessController.doPrivileged(
479+
(PrivilegedAction<Boolean>) () ->
480+
((fb instanceof SmartFactoryBean && ((SmartFactoryBean<?>) fb).isPrototype()) ||
481+
!fb.isSingleton()),
479482
getAccessControlContext());
480483
}
481484
else {
@@ -1457,16 +1460,16 @@ public void clearMetadataCache() {
14571460
* @throws CannotLoadBeanClassException if we failed to load the class
14581461
*/
14591462
@Nullable
1460-
protected Class<?> resolveBeanClass(final RootBeanDefinition mbd, String beanName, final Class<?>... typesToMatch)
1463+
protected Class<?> resolveBeanClass(RootBeanDefinition mbd, String beanName, Class<?>... typesToMatch)
14611464
throws CannotLoadBeanClassException {
14621465

14631466
try {
14641467
if (mbd.hasBeanClass()) {
14651468
return mbd.getBeanClass();
14661469
}
14671470
if (System.getSecurityManager() != null) {
1468-
return AccessController.doPrivileged((PrivilegedExceptionAction<Class<?>>) () ->
1469-
doResolveBeanClass(mbd, typesToMatch), getAccessControlContext());
1471+
return AccessController.doPrivileged((PrivilegedExceptionAction<Class<?>>)
1472+
() -> doResolveBeanClass(mbd, typesToMatch), getAccessControlContext());
14701473
}
14711474
else {
14721475
return doResolveBeanClass(mbd, typesToMatch);
@@ -1655,7 +1658,7 @@ else if (mbd.isLazyInit()) {
16551658
logger.trace(LogMessage.format("Bean creation exception on lazy FactoryBean type check: %s", ex));
16561659
}
16571660
else {
1658-
logger.debug(LogMessage.format("Bean creation exception on non-lazy FactoryBean type check: %s", ex));
1661+
logger.debug(LogMessage.format("Bean creation exception on eager FactoryBean type check: %s", ex));
16591662
}
16601663
onSuppressedException(ex);
16611664
}

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,12 @@ public Comparator<Object> getDependencyComparator() {
294294
* when deciding whether a bean definition should be considered as a
295295
* candidate for autowiring.
296296
*/
297-
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
297+
public void setAutowireCandidateResolver(AutowireCandidateResolver autowireCandidateResolver) {
298298
Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
299299
if (autowireCandidateResolver instanceof BeanFactoryAware) {
300300
if (System.getSecurityManager() != null) {
301301
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
302-
((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(DefaultListableBeanFactory.this);
302+
((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
303303
return null;
304304
}, getAccessControlContext());
305305
}
@@ -512,8 +512,7 @@ private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSi
512512

513513
// Check all bean definitions.
514514
for (String beanName : this.beanDefinitionNames) {
515-
// Only consider bean as eligible if the bean name
516-
// is not defined as alias for some other bean.
515+
// Only consider bean as eligible if the bean name is not defined as alias for some other bean.
517516
if (!isAlias(beanName)) {
518517
try {
519518
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
@@ -524,8 +523,8 @@ private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSi
524523
boolean isFactoryBean = isFactoryBean(beanName, mbd);
525524
BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
526525
boolean matchFound = false;
527-
boolean allowFactoryBeanInit = allowEagerInit || containsSingleton(beanName);
528-
boolean isNonLazyDecorated = dbd != null && !mbd.isLazyInit();
526+
boolean allowFactoryBeanInit = (allowEagerInit || containsSingleton(beanName));
527+
boolean isNonLazyDecorated = (dbd != null && !mbd.isLazyInit());
529528
if (!isFactoryBean) {
530529
if (includeNonSingletons || isSingleton(beanName, mbd, dbd)) {
531530
matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
@@ -552,9 +551,9 @@ private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSi
552551
throw ex;
553552
}
554553
// Probably a placeholder: let's ignore it for type matching purposes.
555-
LogMessage message = (ex instanceof CannotLoadBeanClassException) ?
554+
LogMessage message = (ex instanceof CannotLoadBeanClassException ?
556555
LogMessage.format("Ignoring bean class loading failure for bean '%s'", beanName) :
557-
LogMessage.format("Ignoring unresolvable metadata in bean definition '%s'", beanName);
556+
LogMessage.format("Ignoring unresolvable metadata in bean definition '%s'", beanName));
558557
logger.trace(message, ex);
559558
onSuppressedException(ex);
560559
}
@@ -582,7 +581,8 @@ private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSi
582581
}
583582
catch (NoSuchBeanDefinitionException ex) {
584583
// Shouldn't happen - probably a result of circular reference resolution...
585-
logger.trace(LogMessage.format("Failed to check manually registered singleton with name '%s'", beanName), ex);
584+
logger.trace(LogMessage.format(
585+
"Failed to check manually registered singleton with name '%s'", beanName), ex);
586586
}
587587
}
588588

@@ -611,8 +611,8 @@ public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansEx
611611

612612
@Override
613613
@SuppressWarnings("unchecked")
614-
public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
615-
throws BeansException {
614+
public <T> Map<String, T> getBeansOfType(
615+
@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException {
616616

617617
String[] beanNames = getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
618618
Map<String, T> result = new LinkedHashMap<>(beanNames.length);
@@ -753,12 +753,13 @@ public boolean isAutowireCandidate(String beanName, DependencyDescriptor descrip
753753
* @param resolver the AutowireCandidateResolver to use for the actual resolution algorithm
754754
* @return whether the bean should be considered as autowire candidate
755755
*/
756-
protected boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor, AutowireCandidateResolver resolver)
756+
protected boolean isAutowireCandidate(
757+
String beanName, DependencyDescriptor descriptor, AutowireCandidateResolver resolver)
757758
throws NoSuchBeanDefinitionException {
758759

759-
String beanDefinitionName = BeanFactoryUtils.transformedBeanName(beanName);
760-
if (containsBeanDefinition(beanDefinitionName)) {
761-
return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(beanDefinitionName), descriptor, resolver);
760+
String bdName = BeanFactoryUtils.transformedBeanName(beanName);
761+
if (containsBeanDefinition(bdName)) {
762+
return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(bdName), descriptor, resolver);
762763
}
763764
else if (containsSingleton(beanName)) {
764765
return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor, resolver);
@@ -790,15 +791,15 @@ else if (parent instanceof ConfigurableListableBeanFactory) {
790791
protected boolean isAutowireCandidate(String beanName, RootBeanDefinition mbd,
791792
DependencyDescriptor descriptor, AutowireCandidateResolver resolver) {
792793

793-
String beanDefinitionName = BeanFactoryUtils.transformedBeanName(beanName);
794-
resolveBeanClass(mbd, beanDefinitionName);
794+
String bdName = BeanFactoryUtils.transformedBeanName(beanName);
795+
resolveBeanClass(mbd, bdName);
795796
if (mbd.isFactoryMethodUnique && mbd.factoryMethodToIntrospect == null) {
796797
new ConstructorResolver(this).resolveFactoryMethodIfPossible(mbd);
797798
}
798-
BeanDefinitionHolder holder = (beanName.equals(beanDefinitionName) ?
799+
BeanDefinitionHolder holder = (beanName.equals(bdName) ?
799800
this.mergedBeanDefinitionHolders.computeIfAbsent(beanName,
800-
key -> new BeanDefinitionHolder(mbd, beanName, getAliases(beanDefinitionName))) :
801-
new BeanDefinitionHolder(mbd, beanName, getAliases(beanDefinitionName)));
801+
key -> new BeanDefinitionHolder(mbd, beanName, getAliases(bdName))) :
802+
new BeanDefinitionHolder(mbd, beanName, getAliases(bdName)));
802803
return resolver.isAutowireCandidate(holder, descriptor);
803804
}
804805

@@ -873,11 +874,11 @@ public void preInstantiateSingletons() throws BeansException {
873874
if (isFactoryBean(beanName)) {
874875
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
875876
if (bean instanceof FactoryBean) {
876-
final FactoryBean<?> factory = (FactoryBean<?>) bean;
877+
FactoryBean<?> factory = (FactoryBean<?>) bean;
877878
boolean isEagerInit;
878879
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
879-
isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
880-
((SmartFactoryBean<?>) factory)::isEagerInit,
880+
isEagerInit = AccessController.doPrivileged(
881+
(PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
881882
getAccessControlContext());
882883
}
883884
else {
@@ -899,7 +900,7 @@ public void preInstantiateSingletons() throws BeansException {
899900
for (String beanName : beanNames) {
900901
Object singletonInstance = getSingleton(beanName);
901902
if (singletonInstance instanceof SmartInitializingSingleton) {
902-
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
903+
SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
903904
if (System.getSecurityManager() != null) {
904905
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
905906
smartSingleton.afterSingletonsInstantiated();
@@ -1324,7 +1325,7 @@ public Object doResolveDependency(DependencyDescriptor descriptor, @Nullable Str
13241325
private Object resolveMultipleBeans(DependencyDescriptor descriptor, @Nullable String beanName,
13251326
@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) {
13261327

1327-
final Class<?> type = descriptor.getDependencyType();
1328+
Class<?> type = descriptor.getDependencyType();
13281329

13291330
if (descriptor instanceof StreamDependencyDescriptor) {
13301331
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);

0 commit comments

Comments
 (0)