Skip to content

Commit e2b74e6

Browse files
committed
Polishing
1 parent 5382260 commit e2b74e6

File tree

17 files changed

+98
-108
lines changed

17 files changed

+98
-108
lines changed

spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void customAsyncAnnotationIsPropagated() {
160160
Object bean = ctx.getBean(CustomAsyncBean.class);
161161
assertTrue(AopUtils.isAopProxy(bean));
162162
boolean isAsyncAdvised = false;
163-
for (Advisor advisor : ((Advised)bean).getAdvisors()) {
163+
for (Advisor advisor : ((Advised) bean).getAdvisors()) {
164164
if (advisor instanceof AsyncAnnotationAdvisor) {
165165
isAsyncAdvised = true;
166166
break;
@@ -364,7 +364,8 @@ public AsyncBean asyncBean() {
364364
@EnableAsync
365365
static class AsyncConfigWithMockito {
366366

367-
@Bean @Lazy
367+
@Bean
368+
@Lazy
368369
public AsyncBean asyncBean() {
369370
return Mockito.mock(AsyncBean.class);
370371
}

spring-context/src/test/java/org/springframework/tests/mock/jndi/ExpectedLookupTemplate.java

Lines changed: 13 additions & 14 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.
@@ -23,28 +23,29 @@
2323
import org.springframework.jndi.JndiTemplate;
2424

2525
/**
26-
* Simple extension of the JndiTemplate class that always returns
27-
* a given object. Very useful for testing. Effectively a mock object.
26+
* Simple extension of the JndiTemplate class that always returns a given object.
27+
*
28+
* <p>Very useful for testing. Effectively a mock object.
2829
*
2930
* @author Rod Johnson
3031
* @author Juergen Hoeller
3132
*/
3233
public class ExpectedLookupTemplate extends JndiTemplate {
3334

34-
private final Map<String, Object> jndiObjects = new ConcurrentHashMap<>();
35+
private final Map<String, Object> jndiObjects = new ConcurrentHashMap<>(16);
3536

3637

3738
/**
38-
* Construct a new JndiTemplate that will always return given objects
39-
* for given names. To be populated through {@code addObject} calls.
39+
* Construct a new JndiTemplate that will always return given objects for
40+
* given names. To be populated through {@code addObject} calls.
4041
* @see #addObject(String, Object)
4142
*/
4243
public ExpectedLookupTemplate() {
4344
}
4445

4546
/**
46-
* Construct a new JndiTemplate that will always return the
47-
* given object, but honour only requests for the given name.
47+
* Construct a new JndiTemplate that will always return the given object,
48+
* but honour only requests for the given name.
4849
* @param name the name the client is expected to look up
4950
* @param object the object that will be returned
5051
*/
@@ -54,20 +55,18 @@ public ExpectedLookupTemplate(String name, Object object) {
5455

5556

5657
/**
57-
* Add the given object to the list of JNDI objects that this
58-
* template will expose.
58+
* Add the given object to the list of JNDI objects that this template will expose.
5959
* @param name the name the client is expected to look up
6060
* @param object the object that will be returned
6161
*/
6262
public void addObject(String name, Object object) {
6363
this.jndiObjects.put(name, object);
6464
}
6565

66-
6766
/**
68-
* If the name is the expected name specified in the constructor,
69-
* return the object provided in the constructor. If the name is
70-
* unexpected, a respective NamingException gets thrown.
67+
* If the name is the expected name specified in the constructor, return the
68+
* object provided in the constructor. If the name is unexpected, a
69+
* respective NamingException gets thrown.
7170
*/
7271
@Override
7372
public Object lookup(String name) throws NamingException {

spring-context/src/test/java/org/springframework/tests/mock/jndi/SimpleNamingContext.java

Lines changed: 9 additions & 5 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.
@@ -33,6 +33,7 @@
3333
import org.apache.commons.logging.Log;
3434
import org.apache.commons.logging.LogFactory;
3535

36+
import org.springframework.lang.Nullable;
3637
import org.springframework.util.StringUtils;
3738

3839
/**
@@ -80,7 +81,9 @@ public SimpleNamingContext(String root) {
8081
* Create a new naming context with the given naming root,
8182
* the given name/object map, and the JNDI environment entries.
8283
*/
83-
public SimpleNamingContext(String root, Hashtable<String, Object> boundObjects, Hashtable<String, Object> env) {
84+
public SimpleNamingContext(
85+
String root, Hashtable<String, Object> boundObjects, @Nullable Hashtable<String, Object> env) {
86+
8487
this.root = root;
8588
this.boundObjects = boundObjects;
8689
if (env != null) {
@@ -206,6 +209,7 @@ public Hashtable<String, Object> getEnvironment() {
206209
}
207210

208211
@Override
212+
@Nullable
209213
public Object addToEnvironment(String propName, Object propVal) {
210214
return this.environment.put(propName, propVal);
211215
}
@@ -293,7 +297,7 @@ public Name composeName(Name name, Name prefix) throws NamingException {
293297
}
294298

295299

296-
private static abstract class AbstractNamingEnumeration<T> implements NamingEnumeration<T> {
300+
private abstract static class AbstractNamingEnumeration<T> implements NamingEnumeration<T> {
297301

298302
private Iterator<T> iterator;
299303

@@ -353,7 +357,7 @@ public void close() {
353357
}
354358

355359

356-
private static class NameClassPairEnumeration extends AbstractNamingEnumeration<NameClassPair> {
360+
private static final class NameClassPairEnumeration extends AbstractNamingEnumeration<NameClassPair> {
357361

358362
private NameClassPairEnumeration(SimpleNamingContext context, String root) throws NamingException {
359363
super(context, root);
@@ -366,7 +370,7 @@ protected NameClassPair createObject(String strippedName, Object obj) {
366370
}
367371

368372

369-
private static class BindingEnumeration extends AbstractNamingEnumeration<Binding> {
373+
private static final class BindingEnumeration extends AbstractNamingEnumeration<Binding> {
370374

371375
private BindingEnumeration(SimpleNamingContext context, String root) throws NamingException {
372376
super(context, root);

spring-context/src/test/java/org/springframework/tests/mock/jndi/SimpleNamingContextBuilder.java

Lines changed: 20 additions & 21 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.
@@ -26,7 +26,10 @@
2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
2828

29+
import org.springframework.lang.Nullable;
30+
import org.springframework.util.Assert;
2931
import org.springframework.util.ClassUtils;
32+
import org.springframework.util.ReflectionUtils;
3033

3134
/**
3235
* Simple implementation of a JNDI naming context builder.
@@ -42,7 +45,7 @@
4245
* <ul>
4346
* <li>{@code SingleConnectionDataSource} (using the same Connection for all getConnection calls)
4447
* <li>{@code DriverManagerDataSource} (creating a new Connection on each getConnection call)
45-
* <li>Apache's Jakarta Commons DBCP offers {@code org.apache.commons.dbcp.BasicDataSource} (a real pool)
48+
* <li>Apache's Commons DBCP offers {@code org.apache.commons.dbcp.BasicDataSource} (a real pool)
4649
* </ul>
4750
*
4851
* <p>Typical usage in bootstrap code:
@@ -80,7 +83,8 @@
8083
*/
8184
public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder {
8285

83-
/** An instance of this class bound to JNDI */
86+
/** An instance of this class bound to JNDI. */
87+
@Nullable
8488
private static volatile SimpleNamingContextBuilder activated;
8589

8690
private static boolean initialized = false;
@@ -93,31 +97,33 @@ public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder
9397
* @return the current SimpleNamingContextBuilder instance,
9498
* or {@code null} if none
9599
*/
100+
@Nullable
96101
public static SimpleNamingContextBuilder getCurrentContextBuilder() {
97102
return activated;
98103
}
99104

100105
/**
101106
* If no SimpleNamingContextBuilder is already configuring JNDI,
102-
* create and activate one. Otherwise take the existing activate
107+
* create and activate one. Otherwise take the existing activated
103108
* SimpleNamingContextBuilder, clear it and return it.
104109
* <p>This is mainly intended for test suites that want to
105110
* reinitialize JNDI bindings from scratch repeatedly.
106111
* @return an empty SimpleNamingContextBuilder that can be used
107112
* to control JNDI bindings
108113
*/
109114
public static SimpleNamingContextBuilder emptyActivatedContextBuilder() throws NamingException {
110-
if (activated != null) {
115+
SimpleNamingContextBuilder builder = activated;
116+
if (builder != null) {
111117
// Clear already activated context builder.
112-
activated.clear();
118+
builder.clear();
113119
}
114120
else {
115121
// Create and activate new context builder.
116-
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
122+
builder = new SimpleNamingContextBuilder();
117123
// The activate() call will cause an assignment to the activated variable.
118124
builder.activate();
119125
}
120-
return activated;
126+
return builder;
121127
}
122128

123129

@@ -138,12 +144,10 @@ public void activate() throws IllegalStateException, NamingException {
138144
logger.info("Activating simple JNDI environment");
139145
synchronized (initializationLock) {
140146
if (!initialized) {
141-
if (NamingManager.hasInitialContextFactoryBuilder()) {
142-
throw new IllegalStateException(
147+
Assert.state(!NamingManager.hasInitialContextFactoryBuilder(),
143148
"Cannot activate SimpleNamingContextBuilder: there is already a JNDI provider registered. " +
144149
"Note that JNDI is a JVM-wide service, shared at the JVM system class loader level, " +
145150
"with no reset option. As a consequence, a JNDI provider must only be registered once per JVM.");
146-
}
147151
NamingManager.setInitialContextFactoryBuilder(this);
148152
initialized = true;
149153
}
@@ -192,7 +196,8 @@ public void bind(String name, Object obj) {
192196
* @see SimpleNamingContext
193197
*/
194198
@Override
195-
public InitialContextFactory createInitialContextFactory(Hashtable<?,?> environment) {
199+
@SuppressWarnings("unchecked")
200+
public InitialContextFactory createInitialContextFactory(@Nullable Hashtable<?,?> environment) {
196201
if (activated == null && environment != null) {
197202
Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY);
198203
if (icf != null) {
@@ -212,22 +217,16 @@ else if (icf instanceof String) {
212217
"Specified class does not implement [" + InitialContextFactory.class.getName() + "]: " + icf);
213218
}
214219
try {
215-
return (InitialContextFactory) icfClass.newInstance();
220+
return (InitialContextFactory) ReflectionUtils.accessibleConstructor(icfClass).newInstance();
216221
}
217222
catch (Throwable ex) {
218-
throw new IllegalStateException("Cannot instantiate specified InitialContextFactory: " + icf, ex);
223+
throw new IllegalStateException("Unable to instantiate specified InitialContextFactory: " + icf, ex);
219224
}
220225
}
221226
}
222227

223228
// Default case...
224-
return new InitialContextFactory() {
225-
@Override
226-
@SuppressWarnings("unchecked")
227-
public Context getInitialContext(Hashtable<?,?> environment) {
228-
return new SimpleNamingContext("", boundObjects, (Hashtable<String, Object>) environment);
229-
}
230-
};
229+
return env -> new SimpleNamingContext("", this.boundObjects, (Hashtable<String, Object>) env);
231230
}
232231

233232
}

spring-core/src/main/java/org/springframework/util/ObjectUtils.java

Lines changed: 8 additions & 10 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.
@@ -199,7 +199,7 @@ public static boolean containsElement(@Nullable Object[] array, Object element)
199199
/**
200200
* Check whether the given array of enum constants contains a constant with the given name,
201201
* ignoring case when determining a match.
202-
* @param enumValues the enum values to check, typically the product of a call to MyEnum.values()
202+
* @param enumValues the enum values to check, typically obtained via {@code MyEnum.values()}
203203
* @param constant the constant name to find (must not be null or empty string)
204204
* @return whether the constant has been found in the given array
205205
*/
@@ -209,15 +209,14 @@ public static boolean containsConstant(Enum<?>[] enumValues, String constant) {
209209

210210
/**
211211
* Check whether the given array of enum constants contains a constant with the given name.
212-
* @param enumValues the enum values to check, typically the product of a call to MyEnum.values()
212+
* @param enumValues the enum values to check, typically obtained via {@code MyEnum.values()}
213213
* @param constant the constant name to find (must not be null or empty string)
214214
* @param caseSensitive whether case is significant in determining a match
215215
* @return whether the constant has been found in the given array
216216
*/
217217
public static boolean containsConstant(Enum<?>[] enumValues, String constant, boolean caseSensitive) {
218218
for (Enum<?> candidate : enumValues) {
219-
if (caseSensitive ?
220-
candidate.toString().equals(constant) :
219+
if (caseSensitive ? candidate.toString().equals(constant) :
221220
candidate.toString().equalsIgnoreCase(constant)) {
222221
return true;
223222
}
@@ -228,7 +227,7 @@ public static boolean containsConstant(Enum<?>[] enumValues, String constant, bo
228227
/**
229228
* Case insensitive alternative to {@link Enum#valueOf(Class, String)}.
230229
* @param <E> the concrete Enum type
231-
* @param enumValues the array of all Enum constants in question, usually per Enum.values()
230+
* @param enumValues the array of all Enum constants in question, usually per {@code Enum.values()}
232231
* @param constant the constant to get the enum value of
233232
* @throws IllegalArgumentException if the given constant is not found in the given array
234233
* of enum values. Use {@link #containsConstant(Enum[], String)} as a guard to avoid this exception.
@@ -239,9 +238,8 @@ public static <E extends Enum<?>> E caseInsensitiveValueOf(E[] enumValues, Strin
239238
return candidate;
240239
}
241240
}
242-
throw new IllegalArgumentException(
243-
String.format("constant [%s] does not exist in enum type %s",
244-
constant, enumValues.getClass().getComponentType().getName()));
241+
throw new IllegalArgumentException("Constant [" + constant + "] does not exist in enum type " +
242+
enumValues.getClass().getComponentType().getName());
245243
}
246244

247245
/**
@@ -251,7 +249,7 @@ public static <E extends Enum<?>> E caseInsensitiveValueOf(E[] enumValues, Strin
251249
* @param obj the object to append
252250
* @return the new array (of the same component type; never {@code null})
253251
*/
254-
public static <A, O extends A> A[] addObjectToArray(@Nullable A[] array, @Nullable O obj) {
252+
public static <A, O extends A> A[] addObjectToArray(@Nullable A[] array, @Nullable O obj) {
255253
Class<?> compType = Object.class;
256254
if (array != null) {
257255
compType = array.getClass().getComponentType();

spring-core/src/main/java/org/springframework/util/ReflectionUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.sql.SQLException;
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
28-
import java.util.LinkedList;
2928
import java.util.List;
3029
import java.util.Map;
3130

@@ -697,7 +696,7 @@ private static List<Method> findConcreteMethodsOnInterfaces(Class<?> clazz) {
697696
for (Method ifcMethod : ifc.getMethods()) {
698697
if (!Modifier.isAbstract(ifcMethod.getModifiers())) {
699698
if (result == null) {
700-
result = new LinkedList<>();
699+
result = new ArrayList<>();
701700
}
702701
result.add(ifcMethod);
703702
}

0 commit comments

Comments
 (0)