Skip to content

Commit 458d49b

Browse files
committed
Polishing
(cherry picked from commit acbb254)
1 parent e76ed49 commit 458d49b

File tree

13 files changed

+134
-146
lines changed

13 files changed

+134
-146
lines changed

spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -77,18 +77,18 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
7777

7878
private EventExpressionEvaluator evaluator;
7979

80-
private String condition;
81-
8280
private EventListener eventListener;
8381

82+
private String condition;
83+
8484

8585
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
8686
this.beanName = beanName;
8787
this.method = method;
8888
this.targetClass = targetClass;
8989
this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
9090
this.declaredEventTypes = resolveDeclaredEventTypes();
91-
this.methodKey = new AnnotatedElementKey(this.method, this.targetClass);
91+
this.methodKey = new AnnotatedElementKey(method, targetClass);
9292
}
9393

9494

@@ -165,8 +165,8 @@ protected Object[] resolveArguments(ApplicationEvent event) {
165165
if (this.method.getParameterTypes().length == 0) {
166166
return new Object[0];
167167
}
168-
if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass())
169-
&& event instanceof PayloadApplicationEvent) {
168+
if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass()) &&
169+
event instanceof PayloadApplicationEvent) {
170170
return new Object[] {((PayloadApplicationEvent) event).getPayload()};
171171
}
172172
else {
@@ -213,10 +213,6 @@ private boolean shouldHandle(ApplicationEvent event, Object[] args) {
213213
return true;
214214
}
215215

216-
protected <A extends Annotation> A getMethodAnnotation(Class<A> annotationType) {
217-
return AnnotationUtils.findAnnotation(this.method, annotationType);
218-
}
219-
220216
/**
221217
* Invoke the event listener method with the given argument values.
222218
*/
@@ -254,6 +250,10 @@ protected Object getTargetBean() {
254250
return this.applicationContext.getBean(this.beanName);
255251
}
256252

253+
protected <A extends Annotation> A getMethodAnnotation(Class<A> annotationType) {
254+
return AnnotationUtils.findAnnotation(this.method, annotationType);
255+
}
256+
257257
protected EventListener getEventListener() {
258258
if (this.eventListener == null) {
259259
this.eventListener = AnnotatedElementUtils.findMergedAnnotation(this.method, EventListener.class);
@@ -269,7 +269,7 @@ protected EventListener getEventListener() {
269269
*/
270270
protected String getCondition() {
271271
if (this.condition == null) {
272-
EventListener eventListener = AnnotatedElementUtils.findMergedAnnotation(this.method, EventListener.class);
272+
EventListener eventListener = getEventListener();
273273
if (eventListener != null) {
274274
this.condition = eventListener.condition();
275275
}

spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -31,9 +31,6 @@
3131
import org.springframework.util.ObjectUtils;
3232
import org.springframework.util.StringUtils;
3333

34-
import static java.lang.String.*;
35-
import static org.springframework.util.StringUtils.*;
36-
3734
/**
3835
* Abstract base class for {@link Environment} implementations. Supports the notion of
3936
* reserved default profile names and enables specifying active and default profiles
@@ -124,7 +121,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
124121
public AbstractEnvironment() {
125122
customizePropertySources(this.propertySources);
126123
if (this.logger.isDebugEnabled()) {
127-
this.logger.debug(format(
124+
this.logger.debug(String.format(
128125
"Initialized %s with PropertySources %s", getClass().getSimpleName(), this.propertySources));
129126
}
130127
}
@@ -242,7 +239,8 @@ protected Set<String> doGetActiveProfiles() {
242239
if (this.activeProfiles.isEmpty()) {
243240
String profiles = getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
244241
if (StringUtils.hasText(profiles)) {
245-
setActiveProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles)));
242+
setActiveProfiles(StringUtils.commaDelimitedListToStringArray(
243+
StringUtils.trimAllWhitespace(profiles)));
246244
}
247245
}
248246
return this.activeProfiles;
@@ -264,7 +262,7 @@ public void setActiveProfiles(String... profiles) {
264262
@Override
265263
public void addActiveProfile(String profile) {
266264
if (this.logger.isDebugEnabled()) {
267-
this.logger.debug(format("Activating profile '%s'", profile));
265+
this.logger.debug(String.format("Activating profile '%s'", profile));
268266
}
269267
validateProfile(profile);
270268
doGetActiveProfiles();
@@ -296,7 +294,8 @@ protected Set<String> doGetDefaultProfiles() {
296294
if (this.defaultProfiles.equals(getReservedDefaultProfiles())) {
297295
String profiles = getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
298296
if (StringUtils.hasText(profiles)) {
299-
setDefaultProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles)));
297+
setDefaultProfiles(StringUtils.commaDelimitedListToStringArray(
298+
StringUtils.trimAllWhitespace(profiles)));
300299
}
301300
}
302301
return this.defaultProfiles;
@@ -393,7 +392,7 @@ protected String getSystemAttribute(String attributeName) {
393392
}
394393
catch (AccessControlException ex) {
395394
if (logger.isInfoEnabled()) {
396-
logger.info(format("Caught AccessControlException when accessing system " +
395+
logger.info(String.format("Caught AccessControlException when accessing system " +
397396
"environment variable [%s]; its value will be returned [null]. Reason: %s",
398397
attributeName, ex.getMessage()));
399398
}
@@ -434,7 +433,7 @@ protected String getSystemAttribute(String attributeName) {
434433
}
435434
catch (AccessControlException ex) {
436435
if (logger.isInfoEnabled()) {
437-
logger.info(format("Caught AccessControlException when accessing system " +
436+
logger.info(String.format("Caught AccessControlException when accessing system " +
438437
"property [%s]; its value will be returned [null]. Reason: %s",
439438
attributeName, ex.getMessage()));
440439
}
@@ -574,7 +573,7 @@ public String resolveRequiredPlaceholders(String text) throws IllegalArgumentExc
574573

575574
@Override
576575
public String toString() {
577-
return format("%s {activeProfiles=%s, defaultProfiles=%s, propertySources=%s}",
576+
return String.format("%s {activeProfiles=%s, defaultProfiles=%s, propertySources=%s}",
578577
getClass().getSimpleName(), this.activeProfiles, this.defaultProfiles,
579578
this.propertySources);
580579
}

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public void setContentLength(int contentLength) {
297297
}
298298

299299
public void setHeartbeat(long cx, long cy) {
300-
setNativeHeader(STOMP_HEARTBEAT_HEADER, StringUtils.arrayToCommaDelimitedString(new Object[]{cx, cy}));
300+
setNativeHeader(STOMP_HEARTBEAT_HEADER, cx + "," + cy);
301301
}
302302

303303
public void setAck(String ack) {

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -223,9 +223,14 @@ public String getPasscode() {
223223
* Applies to the CONNECT and CONNECTED frames.
224224
*/
225225
public void setHeartbeat(long[] heartbeat) {
226-
Assert.notNull(heartbeat);
226+
if (heartbeat == null || heartbeat.length != 2) {
227+
throw new IllegalArgumentException("Heart-beat array must be of length 2, not " +
228+
(heartbeat != null ? heartbeat.length : "null"));
229+
}
227230
String value = heartbeat[0] + "," + heartbeat[1];
228-
Assert.isTrue(heartbeat[0] >= 0 && heartbeat[1] >= 0, "Heart-beat values cannot be negative: " + value);
231+
if (heartbeat[0] < 0 || heartbeat[1] < 0) {
232+
throw new IllegalArgumentException("Heart-beat values cannot be negative: " + value);
233+
}
229234
set(HEARTBEAT, value);
230235
}
231236

@@ -497,14 +502,8 @@ public Set<Entry<String, List<String>>> entrySet() {
497502

498503
@Override
499504
public boolean equals(Object other) {
500-
if (this == other) {
501-
return true;
502-
}
503-
if (!(other instanceof StompHeaders)) {
504-
return false;
505-
}
506-
StompHeaders otherHeaders = (StompHeaders) other;
507-
return this.headers.equals(otherHeaders.headers);
505+
return (this == other || (other instanceof StompHeaders &&
506+
this.headers.equals(((StompHeaders) other).headers)));
508507
}
509508

510509
@Override

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ private void parent(MockHttpServletRequest request, RequestBuilder parent) {
196196
* {@link HttpServletRequest#getContextPath()} which states it can be
197197
* an empty string, or it must start with a "/" and not end with a "/".
198198
* @param contextPath a valid contextPath
199-
* @throws IllegalArgumentException if the contextPath is not a valid {@link HttpServletRequest#getContextPath()}
199+
* @throws IllegalArgumentException if the contextPath is not a valid
200+
* {@link HttpServletRequest#getContextPath()}
200201
*/
201202
public void setContextPath(String contextPath) {
202203
MockMvcWebConnection.validateContextPath(contextPath);
@@ -210,8 +211,7 @@ public void setForwardPostProcessor(RequestPostProcessor forwardPostProcessor) {
210211
private void authType(MockHttpServletRequest request) {
211212
String authorization = header("Authorization");
212213
if (authorization != null) {
213-
String[] authzParts = authorization.split(": ");
214-
request.setAuthType(authzParts[0]);
214+
request.setAuthType(authorization.split(": ")[0]);
215215
}
216216
}
217217

@@ -261,8 +261,8 @@ private void cookies(MockHttpServletRequest request) {
261261
while (tokens.hasMoreTokens()) {
262262
String cookieName = tokens.nextToken().trim();
263263
if (!tokens.hasMoreTokens()) {
264-
throw new IllegalArgumentException("Expected value for cookie name '" + cookieName
265-
+ "'. Full cookie was " + cookieHeaderValue);
264+
throw new IllegalArgumentException("Expected value for cookie name '" + cookieName +
265+
"'. Full cookie was " + cookieHeaderValue);
266266
}
267267
String cookieValue = tokens.nextToken().trim();
268268
processCookie(request, cookies, new Cookie(cookieName, cookieValue));
@@ -437,16 +437,12 @@ public boolean isMergeEnabled() {
437437

438438
@Override
439439
public Object merge(Object parent) {
440-
if (parent == null) {
441-
return this;
442-
}
443440
if (parent instanceof RequestBuilder) {
444441
this.parentBuilder = (RequestBuilder) parent;
442+
if (parent instanceof SmartRequestBuilder) {
443+
this.parentPostProcessor = (SmartRequestBuilder) parent;
444+
}
445445
}
446-
if (parent instanceof SmartRequestBuilder) {
447-
this.parentPostProcessor = (SmartRequestBuilder) parent;
448-
}
449-
450446
return this;
451447
}
452448

spring-tx/src/main/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapter.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -18,9 +18,6 @@
1818

1919
import java.lang.reflect.Method;
2020

21-
import org.apache.commons.logging.Log;
22-
import org.apache.commons.logging.LogFactory;
23-
2421
import org.springframework.context.ApplicationEvent;
2522
import org.springframework.context.event.ApplicationListenerMethodAdapter;
2623
import org.springframework.context.event.EventListener;
@@ -35,20 +32,19 @@
3532
* an event to a {@link TransactionalEventListener} annotated method. Supports
3633
* the exact same features as any regular {@link EventListener} annotated method
3734
* but is aware of the transactional context of the event publisher.
38-
* <p>
39-
* Processing of {@link TransactionalEventListener} is enabled automatically when
40-
* Spring's transaction management is enabled. For other cases, registering a
41-
* bean of type {@link TransactionalEventListenerFactory} is required.
35+
*
36+
* <p>Processing of {@link TransactionalEventListener} is enabled automatically
37+
* when Spring's transaction management is enabled. For other cases, registering
38+
* a bean of type {@link TransactionalEventListenerFactory} is required.
4239
*
4340
* @author Stephane Nicoll
41+
* @author Juergen Hoeller
4442
* @since 4.2
4543
* @see ApplicationListenerMethodAdapter
4644
* @see TransactionalEventListener
4745
*/
4846
class ApplicationListenerMethodTransactionalAdapter extends ApplicationListenerMethodAdapter {
4947

50-
protected final Log logger = LogFactory.getLog(getClass());
51-
5248
private final TransactionalEventListener annotation;
5349

5450

@@ -65,14 +61,15 @@ public void onApplicationEvent(ApplicationEvent event) {
6561
TransactionSynchronizationManager.registerSynchronization(transactionSynchronization);
6662
}
6763
else if (this.annotation.fallbackExecution()) {
68-
if (this.annotation.phase() == TransactionPhase.AFTER_ROLLBACK) {
69-
logger.warn("Processing '" + event + "' as a fallback execution on AFTER_ROLLBACK phase.");
64+
if (this.annotation.phase() == TransactionPhase.AFTER_ROLLBACK && logger.isWarnEnabled()) {
65+
logger.warn("Processing " + event + " as a fallback execution on AFTER_ROLLBACK phase");
7066
}
7167
processEvent(event);
7268
}
7369
else {
70+
// No transactional event execution at all
7471
if (logger.isDebugEnabled()) {
75-
logger.debug("No transaction is running, skipping '" + event + "' for '" + this + "'");
72+
logger.debug("No transaction is active - skipping " + event);
7673
}
7774
}
7875
}
@@ -85,7 +82,7 @@ static TransactionalEventListener findAnnotation(Method method) {
8582
TransactionalEventListener annotation =
8683
AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class);
8784
if (annotation == null) {
88-
throw new IllegalStateException("No TransactionalEventListener annotation found on '" + method + "'");
85+
throw new IllegalStateException("No TransactionalEventListener annotation found on method: " + method);
8986
}
9087
return annotation;
9188
}

0 commit comments

Comments
 (0)