Skip to content

Commit 4a57e26

Browse files
committed
Polishing
1 parent b6191f6 commit 4a57e26

File tree

11 files changed

+124
-57
lines changed

11 files changed

+124
-57
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -280,7 +280,7 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final
280280
"Resolution of declared constructors on bean Class [" + beanClass.getName() +
281281
"] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
282282
}
283-
List<Constructor<?>> candidates = new ArrayList<Constructor<?>>(rawCandidates.length);
283+
List<Constructor<?>> candidates = new ArrayList<>(rawCandidates.length);
284284
Constructor<?> requiredConstructor = null;
285285
Constructor<?> defaultConstructor = null;
286286
Constructor<?> primaryConstructor = BeanUtils.findPrimaryConstructor(beanClass);

spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -43,6 +43,7 @@
4343
import org.springframework.beans.factory.BeanCreationException;
4444
import org.springframework.beans.factory.BeanFactory;
4545
import org.springframework.beans.factory.BeanNameAware;
46+
import org.springframework.beans.factory.DisposableBean;
4647
import org.springframework.beans.factory.FactoryBean;
4748
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
4849
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
@@ -152,6 +153,35 @@ public void testExtendedResourceInjection() {
152153
assertEquals("nestedTestBean", depBeans[1]);
153154
}
154155

156+
@Test
157+
public void testExtendedResourceInjectionWithDestruction() {
158+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
159+
bf.registerResolvableDependency(BeanFactory.class, bf);
160+
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
161+
bpp.setBeanFactory(bf);
162+
bf.addBeanPostProcessor(bpp);
163+
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(TypedExtendedResourceInjectionBean.class));
164+
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
165+
NestedTestBean ntb = new NestedTestBean();
166+
bf.registerSingleton("nestedTestBean", ntb);
167+
168+
TestBean tb = bf.getBean("testBean", TestBean.class);
169+
TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean");
170+
assertSame(tb, bean.getTestBean());
171+
assertSame(tb, bean.getTestBean2());
172+
assertSame(tb, bean.getTestBean3());
173+
assertSame(tb, bean.getTestBean4());
174+
assertSame(ntb, bean.getNestedTestBean());
175+
assertSame(bf, bean.getBeanFactory());
176+
177+
assertArrayEquals(new String[] {"testBean", "nestedTestBean"}, bf.getDependenciesForBean("annotatedBean"));
178+
bf.destroySingleton("testBean");
179+
assertFalse(bf.containsSingleton("testBean"));
180+
assertFalse(bf.containsSingleton("annotatedBean"));
181+
assertTrue(bean.destroyed);
182+
assertSame(0, bf.getDependenciesForBean("annotatedBean").length);
183+
}
184+
155185
@Test
156186
public void testExtendedResourceInjectionWithOverriding() {
157187
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -1286,9 +1316,6 @@ public void testObjectFactoryQualifierProviderInjection() {
12861316
bf.destroySingletons();
12871317
}
12881318

1289-
@Qualifier("testBean")
1290-
private void testBeanQualifierProvider() {}
1291-
12921319
@Test
12931320
public void testObjectFactorySerialization() throws Exception {
12941321
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -2494,6 +2521,9 @@ public void testFactoryBeanSelfInjectionViaFactoryMethod() {
24942521
}
24952522

24962523

2524+
@Qualifier("testBean")
2525+
private void testBeanQualifierProvider() {}
2526+
24972527
@Qualifier("integerRepo")
24982528
private Repository<?> integerRepositoryQualifierProvider;
24992529

@@ -2579,7 +2609,15 @@ public BeanFactory getBeanFactory() {
25792609
}
25802610

25812611

2582-
public static class TypedExtendedResourceInjectionBean extends NonPublicResourceInjectionBean<NestedTestBean> {
2612+
public static class TypedExtendedResourceInjectionBean extends NonPublicResourceInjectionBean<NestedTestBean>
2613+
implements DisposableBean {
2614+
2615+
public boolean destroyed = false;
2616+
2617+
@Override
2618+
public void destroy() {
2619+
this.destroyed = true;
2620+
}
25832621
}
25842622

25852623

spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReaderTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -49,8 +49,7 @@ public void setParserClassSunnyDay() {
4949
@Test(expected = BeanDefinitionStoreException.class)
5050
public void withOpenInputStream() {
5151
SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
52-
Resource resource = new InputStreamResource(getClass().getResourceAsStream(
53-
"test.xml"));
52+
Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml"));
5453
new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource);
5554
}
5655

@@ -122,16 +121,16 @@ private void testBeanDefinitions(BeanDefinitionRegistry registry) {
122121
}
123122

124123
@Test
125-
public void dtdValidationAutodetect() throws Exception {
124+
public void dtdValidationAutodetect() {
126125
doTestValidation("validateWithDtd.xml");
127126
}
128127

129128
@Test
130-
public void xsdValidationAutodetect() throws Exception {
129+
public void xsdValidationAutodetect() {
131130
doTestValidation("validateWithXsd.xml");
132131
}
133132

134-
private void doTestValidation(String resourceName) throws Exception {
133+
private void doTestValidation(String resourceName) {
135134
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
136135
Resource resource = new ClassPathResource(resourceName, getClass());
137136
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(resource);

spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -458,8 +458,8 @@ public void generateCode(MethodVisitor mv, CodeFlow cf) {
458458
mv.visitInsn(DUP);
459459

460460
// children[0] is the type of the constructor, don't want to include that in argument processing
461-
SpelNodeImpl[] arguments = new SpelNodeImpl[children.length - 1];
462-
System.arraycopy(children, 1, arguments, 0, children.length - 1);
461+
SpelNodeImpl[] arguments = new SpelNodeImpl[this.children.length - 1];
462+
System.arraycopy(this.children, 1, arguments, 0, this.children.length - 1);
463463
generateCodeForArguments(mv, cf, constructor, arguments);
464464
mv.visitMethodInsn(INVOKESPECIAL, classDesc, "<init>", CodeFlow.createSignatureDescriptor(constructor), false);
465465
cf.pushDescriptor(this.exitTypeDescriptor);

spring-expression/src/main/java/org/springframework/expression/spel/ast/SpelNodeImpl.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -39,14 +39,15 @@
3939
* format expression.
4040
*
4141
* @author Andy Clement
42+
* @author Juergen Hoeller
4243
* @since 3.0
4344
*/
4445
public abstract class SpelNodeImpl implements SpelNode, Opcodes {
4546

4647
private static SpelNodeImpl[] NO_CHILDREN = new SpelNodeImpl[0];
4748

4849

49-
protected int pos; // start = top 16bits, end = bottom 16bits
50+
protected int pos; // start = top 16bits, end = bottom 16bits
5051

5152
protected SpelNodeImpl[] children = SpelNodeImpl.NO_CHILDREN;
5253

@@ -81,7 +82,7 @@ public SpelNodeImpl(int pos, SpelNodeImpl... operands) {
8182

8283

8384
/**
84-
* @return true if the next child is one of the specified classes
85+
* Return {@code true} if the next child is one of the specified classes.
8586
*/
8687
protected boolean nextChildIs(Class<?>... clazzes) {
8788
if (this.parent != null) {
@@ -123,8 +124,7 @@ public boolean isWritable(ExpressionState expressionState) throws EvaluationExce
123124

124125
@Override
125126
public void setValue(ExpressionState expressionState, @Nullable Object newValue) throws EvaluationException {
126-
throw new SpelEvaluationException(getStartPosition(),
127-
SpelMessage.SETVALUE_NOT_SUPPORTED, getClass());
127+
throw new SpelEvaluationException(getStartPosition(), SpelMessage.SETVALUE_NOT_SUPPORTED, getClass());
128128
}
129129

130130
@Override
@@ -281,4 +281,5 @@ else if (!paramDesc.equals(lastDesc)) {
281281
}
282282
cf.exitCompilationScope();
283283
}
284+
284285
}

spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -4988,7 +4988,7 @@ public Class<?> getPropertyType() {
49884988
}
49894989

49904990
@Override
4991-
public void generateCode(String propertyName, MethodVisitor mv,CodeFlow cf) {
4991+
public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
49924992
if (method == null) {
49934993
try {
49944994
method = Payload2.class.getDeclaredMethod("getField", String.class);
@@ -5005,7 +5005,8 @@ public void generateCode(String propertyName, MethodVisitor mv,CodeFlow cf) {
50055005
mv.visitTypeInsn(CHECKCAST, memberDeclaringClassSlashedDescriptor);
50065006
}
50075007
mv.visitLdcInsn(propertyName);
5008-
mv.visitMethodInsn(INVOKEVIRTUAL, memberDeclaringClassSlashedDescriptor, method.getName(),CodeFlow.createSignatureDescriptor(method),false);
5008+
mv.visitMethodInsn(INVOKEVIRTUAL, memberDeclaringClassSlashedDescriptor, method.getName(),
5009+
CodeFlow.createSignatureDescriptor(method), false);
50095010
}
50105011
}
50115012

spring-jms/src/test/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessorTests.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -39,6 +39,7 @@
3939
import org.springframework.jms.config.MessageListenerTestContainer;
4040
import org.springframework.jms.config.MethodJmsListenerEndpoint;
4141
import org.springframework.jms.listener.SimpleMessageListenerContainer;
42+
import org.springframework.messaging.handler.annotation.Header;
4243
import org.springframework.messaging.handler.annotation.SendTo;
4344
import org.springframework.stereotype.Component;
4445
import org.springframework.transaction.PlatformTransactionManager;
@@ -108,9 +109,9 @@ public void metaAnnotationIsDiscovered() throws Exception {
108109
}
109110

110111
@Test
111-
public void sendToAnnotationFoundOnProxy() throws Exception {
112+
public void sendToAnnotationFoundOnInterfaceProxy() throws Exception {
112113
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
113-
Config.class, ProxyConfig.class, ProxyTestBean.class);
114+
Config.class, ProxyConfig.class, InterfaceProxyTestBean.class);
114115
try {
115116
JmsListenerContainerTestFactory factory = context.getBean(JmsListenerContainerTestFactory.class);
116117
assertEquals("one container should have been registered", 1, factory.getListenerContainers().size());
@@ -120,8 +121,34 @@ public void sendToAnnotationFoundOnProxy() throws Exception {
120121
MethodJmsListenerEndpoint methodEndpoint = (MethodJmsListenerEndpoint) endpoint;
121122
assertTrue(AopUtils.isJdkDynamicProxy(methodEndpoint.getBean()));
122123
assertTrue(methodEndpoint.getBean() instanceof SimpleService);
123-
assertEquals(SimpleService.class.getMethod("handleIt", String.class), methodEndpoint.getMethod());
124-
assertEquals(ProxyTestBean.class.getMethod("handleIt", String.class), methodEndpoint.getMostSpecificMethod());
124+
assertEquals(SimpleService.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMethod());
125+
assertEquals(InterfaceProxyTestBean.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMostSpecificMethod());
126+
127+
Method m = ReflectionUtils.findMethod(endpoint.getClass(), "getDefaultResponseDestination");
128+
ReflectionUtils.makeAccessible(m);
129+
Object destination = ReflectionUtils.invokeMethod(m, endpoint);
130+
assertEquals("SendTo annotation not found on proxy", "foobar", destination);
131+
}
132+
finally {
133+
context.close();
134+
}
135+
}
136+
137+
@Test
138+
public void sendToAnnotationFoundOnCglibProxy() throws Exception {
139+
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
140+
Config.class, ProxyConfig.class, ClassProxyTestBean.class);
141+
try {
142+
JmsListenerContainerTestFactory factory = context.getBean(JmsListenerContainerTestFactory.class);
143+
assertEquals("one container should have been registered", 1, factory.getListenerContainers().size());
144+
145+
JmsListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint();
146+
assertEquals("Wrong endpoint type", MethodJmsListenerEndpoint.class, endpoint.getClass());
147+
MethodJmsListenerEndpoint methodEndpoint = (MethodJmsListenerEndpoint) endpoint;
148+
assertTrue(AopUtils.isCglibProxy(methodEndpoint.getBean()));
149+
assertTrue(methodEndpoint.getBean() instanceof ClassProxyTestBean);
150+
assertEquals(ClassProxyTestBean.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMethod());
151+
assertEquals(ClassProxyTestBean.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMostSpecificMethod());
125152

126153
Method m = ReflectionUtils.findMethod(endpoint.getClass(), "getDefaultResponseDestination");
127154
ReflectionUtils.makeAccessible(m);
@@ -204,18 +231,29 @@ public PlatformTransactionManager transactionManager() {
204231

205232
interface SimpleService {
206233

207-
void handleIt(String body);
234+
void handleIt(String value, String body);
208235
}
209236

210237

211238
@Component
212-
static class ProxyTestBean implements SimpleService {
239+
static class InterfaceProxyTestBean implements SimpleService {
213240

214241
@Override
215242
@Transactional
216243
@JmsListener(destination = "testQueue")
217244
@SendTo("foobar")
218-
public void handleIt(String body) {
245+
public void handleIt(@Header String value, String body) {
246+
}
247+
}
248+
249+
250+
@Component
251+
static class ClassProxyTestBean {
252+
253+
@Transactional
254+
@JmsListener(destination = "testQueue")
255+
@SendTo("foobar")
256+
public void handleIt(@Header String value, String body) {
219257
}
220258
}
221259

@@ -224,7 +262,7 @@ public void handleIt(String body) {
224262
static class InvalidProxyTestBean implements SimpleService {
225263

226264
@Override
227-
public void handleIt(String body) {
265+
public void handleIt(String value, String body) {
228266
}
229267

230268
@Transactional

spring-test/src/main/java/org/springframework/test/web/reactive/server/HeaderAssertions.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -25,9 +25,7 @@
2525
import org.springframework.http.MediaType;
2626
import org.springframework.lang.Nullable;
2727

28-
import static org.springframework.test.util.AssertionErrors.assertEquals;
29-
import static org.springframework.test.util.AssertionErrors.assertTrue;
30-
import static org.springframework.test.util.AssertionErrors.fail;
28+
import static org.springframework.test.util.AssertionErrors.*;
3129

3230
/**
3331
* Assertions on headers of the response.
@@ -125,10 +123,9 @@ public WebTestClient.ResponseSpec contentType(String mediaType) {
125123
*/
126124
public WebTestClient.ResponseSpec contentTypeCompatibleWith(MediaType mediaType) {
127125
MediaType actual = getHeaders().getContentType();
128-
String message = getMessage("Content-Type") + "=[" + actual.toString() + "]"
129-
+ " is not compatible with [" + mediaType.toString() + "]";
126+
String message = getMessage("Content-Type") + "=[" + actual + "] is not compatible with [" + mediaType + "]";
130127
this.exchangeResult.assertWithDiagnostics(() ->
131-
assertTrue(message, actual.isCompatibleWith(mediaType)));
128+
assertTrue(message, (actual != null && actual.isCompatibleWith(mediaType))));
132129
return this.responseSpec;
133130
}
134131

spring-web/src/main/java/org/springframework/web/server/ServerWebExchange.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -142,8 +142,8 @@ default <T> T getAttributeOrDefault(String name, T defaultValue) {
142142
* if it was initialized with one via
143143
* {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext
144144
* WebHttpHandlerBuilder#applicationContext}.
145-
* @see org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext(ApplicationContext)
146145
* @since 5.0.3
146+
* @see org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext(ApplicationContext)
147147
*/
148148
@Nullable
149149
ApplicationContext getApplicationContext();

0 commit comments

Comments
 (0)