Skip to content

Commit 4b1478d

Browse files
diguagesnicoll
authored andcommitted
Replace relevant code with lambda
See gh-1454
1 parent 7381605 commit 4b1478d

File tree

52 files changed

+523
-846
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+523
-846
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/DeclareParentsAdvisor.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,7 @@ private DeclareParentsAdvisor(Class<?> interfaceType, String typePattern, Class<
7676
ClassFilter typePatternFilter = new TypePatternClassFilter(typePattern);
7777

7878
// Excludes methods implemented.
79-
ClassFilter exclusion = new ClassFilter() {
80-
@Override
81-
public boolean matches(Class<?> clazz) {
82-
return !(introducedInterface.isAssignableFrom(clazz));
83-
}
84-
};
79+
ClassFilter exclusion = clazz -> !(introducedInterface.isAssignableFrom(clazz));
8580

8681
this.typePatternClassFilter = ClassFilters.intersection(typePatternFilter, exclusion);
8782
this.advice = advice;

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,12 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto
7676
Comparator<Method> adviceKindComparator = new ConvertingComparator<>(
7777
new InstanceComparator<>(
7878
Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class),
79-
new Converter<Method, Annotation>() {
80-
@Override
81-
public Annotation convert(Method method) {
82-
AspectJAnnotation<?> annotation =
83-
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method);
84-
return (annotation != null ? annotation.getAnnotation() : null);
85-
}
86-
});
87-
Comparator<Method> methodNameComparator = new ConvertingComparator<>(
88-
new Converter<Method, String>() {
89-
@Override
90-
public String convert(Method method) {
91-
return method.getName();
92-
}
79+
(Converter<Method, Annotation>) method -> {
80+
AspectJAnnotation<?> annotation =
81+
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method);
82+
return (annotation != null ? annotation.getAnnotation() : null);
9383
});
84+
Comparator<Method> methodNameComparator = new ConvertingComparator<>(Method::getName);
9485
METHOD_COMPARATOR = adviceKindComparator.thenComparing(methodNameComparator);
9586
}
9687

@@ -157,13 +148,10 @@ public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstan
157148

158149
private List<Method> getAdvisorMethods(Class<?> aspectClass) {
159150
final List<Method> methods = new LinkedList<>();
160-
ReflectionUtils.doWithMethods(aspectClass, new ReflectionUtils.MethodCallback() {
161-
@Override
162-
public void doWith(Method method) throws IllegalArgumentException {
163-
// Exclude pointcuts
164-
if (AnnotationUtils.getAnnotation(method, Pointcut.class) == null) {
165-
methods.add(method);
166-
}
151+
ReflectionUtils.doWithMethods(aspectClass, method -> {
152+
// Exclude pointcuts
153+
if (AnnotationUtils.getAnnotation(method, Pointcut.class) == null) {
154+
methods.add(method);
167155
}
168156
});
169157
Collections.sort(methods, METHOD_COMPARATOR);

spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionAspectSupport.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,12 @@ protected Executor getDefaultExecutor(@Nullable BeanFactory beanFactory) {
264264
@Nullable
265265
protected Object doSubmit(Callable<Object> task, AsyncTaskExecutor executor, Class<?> returnType) {
266266
if (CompletableFuture.class.isAssignableFrom(returnType)) {
267-
return CompletableFuture.supplyAsync(new Supplier<Object>() {
268-
@Override
269-
public Object get() {
270-
try {
271-
return task.call();
272-
}
273-
catch (Throwable ex) {
274-
throw new CompletionException(ex);
275-
}
267+
return CompletableFuture.supplyAsync(() -> {
268+
try {
269+
return task.call();
270+
}
271+
catch (Throwable ex) {
272+
throw new CompletionException(ex);
276273
}
277274
}, executor);
278275
}

spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionInterceptor.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,20 @@ public Object invoke(final MethodInvocation invocation) throws Throwable {
109109
"No executor specified and no default executor set on AsyncExecutionInterceptor either");
110110
}
111111

112-
Callable<Object> task = new Callable<Object>() {
113-
@Override
114-
public Object call() throws Exception {
115-
try {
116-
Object result = invocation.proceed();
117-
if (result instanceof Future) {
118-
return ((Future<?>) result).get();
119-
}
112+
Callable<Object> task = () -> {
113+
try {
114+
Object result = invocation.proceed();
115+
if (result instanceof Future) {
116+
return ((Future<?>) result).get();
120117
}
121-
catch (ExecutionException ex) {
122-
handleError(ex.getCause(), userDeclaredMethod, invocation.getArguments());
123-
}
124-
catch (Throwable ex) {
125-
handleError(ex, userDeclaredMethod, invocation.getArguments());
126-
}
127-
return null;
128118
}
119+
catch (ExecutionException ex) {
120+
handleError(ex.getCause(), userDeclaredMethod, invocation.getArguments());
121+
}
122+
catch (Throwable ex) {
123+
handleError(ex, userDeclaredMethod, invocation.getArguments());
124+
}
125+
return null;
129126
};
130127

131128
return doSubmit(task, executor, invocation.getMethod().getReturnType());

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,7 @@ private List<Method> findCandidateWriteMethods(MethodDescriptor[] methodDescript
139139
// Sort non-void returning write methods to guard against the ill effects of
140140
// non-deterministic sorting of methods returned from Class#getDeclaredMethods
141141
// under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180
142-
Collections.sort(matches, new Comparator<Method>() {
143-
@Override
144-
public int compare(Method m1, Method m2) {
145-
return m2.toString().compareTo(m1.toString());
146-
}
147-
});
142+
Collections.sort(matches, (m1, m2) -> m2.toString().compareTo(m1.toString()));
148143
return matches;
149144
}
150145

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,10 @@ public FieldPropertyMatches(String propertyName, Class<?> beanClass, int maxDist
245245

246246
private static String[] calculateMatches(final String propertyName, Class<?> beanClass, final int maxDistance) {
247247
final List<String> candidates = new ArrayList<>();
248-
ReflectionUtils.doWithFields(beanClass, new ReflectionUtils.FieldCallback() {
249-
@Override
250-
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
251-
String possibleAlternative = field.getName();
252-
if (calculateStringDistance(propertyName, possibleAlternative) <= maxDistance) {
253-
candidates.add(possibleAlternative);
254-
}
248+
ReflectionUtils.doWithFields(beanClass, field -> {
249+
String possibleAlternative = field.getName();
250+
if (calculateStringDistance(propertyName, possibleAlternative) <= maxDistance) {
251+
candidates.add(possibleAlternative);
255252
}
256253
});
257254
Collections.sort(candidates);

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

Lines changed: 49 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -239,20 +239,17 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final
239239
// Let's check for lookup methods here..
240240
if (!this.lookupMethodsChecked.contains(beanName)) {
241241
try {
242-
ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() {
243-
@Override
244-
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
245-
Lookup lookup = method.getAnnotation(Lookup.class);
246-
if (lookup != null) {
247-
LookupOverride override = new LookupOverride(method, lookup.value());
248-
try {
249-
RootBeanDefinition mbd = (RootBeanDefinition) beanFactory.getMergedBeanDefinition(beanName);
250-
mbd.getMethodOverrides().addOverride(override);
251-
}
252-
catch (NoSuchBeanDefinitionException ex) {
253-
throw new BeanCreationException(beanName,
254-
"Cannot apply @Lookup to beans without corresponding bean definition");
255-
}
242+
ReflectionUtils.doWithMethods(beanClass, method -> {
243+
Lookup lookup = method.getAnnotation(Lookup.class);
244+
if (lookup != null) {
245+
LookupOverride override = new LookupOverride(method, lookup.value());
246+
try {
247+
RootBeanDefinition mbd = (RootBeanDefinition) beanFactory.getMergedBeanDefinition(beanName);
248+
mbd.getMethodOverrides().addOverride(override);
249+
}
250+
catch (NoSuchBeanDefinitionException ex) {
251+
throw new BeanCreationException(beanName,
252+
"Cannot apply @Lookup to beans without corresponding bean definition");
256253
}
257254
}
258255
});
@@ -414,50 +411,44 @@ private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
414411
do {
415412
final LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<>();
416413

417-
ReflectionUtils.doWithLocalFields(targetClass, new ReflectionUtils.FieldCallback() {
418-
@Override
419-
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
420-
AnnotationAttributes ann = findAutowiredAnnotation(field);
421-
if (ann != null) {
422-
if (Modifier.isStatic(field.getModifiers())) {
423-
if (logger.isWarnEnabled()) {
424-
logger.warn("Autowired annotation is not supported on static fields: " + field);
425-
}
426-
return;
427-
}
428-
boolean required = determineRequiredStatus(ann);
429-
currElements.add(new AutowiredFieldElement(field, required));
430-
}
431-
}
432-
});
433-
434-
ReflectionUtils.doWithLocalMethods(targetClass, new ReflectionUtils.MethodCallback() {
435-
@Override
436-
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
437-
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
438-
if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
439-
return;
440-
}
441-
AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);
442-
if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
443-
if (Modifier.isStatic(method.getModifiers())) {
444-
if (logger.isWarnEnabled()) {
445-
logger.warn("Autowired annotation is not supported on static methods: " + method);
446-
}
447-
return;
448-
}
449-
if (method.getParameterCount() == 0) {
450-
if (logger.isWarnEnabled()) {
451-
logger.warn("Autowired annotation should only be used on methods with parameters: " +
452-
method);
453-
}
454-
}
455-
boolean required = determineRequiredStatus(ann);
456-
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
457-
currElements.add(new AutowiredMethodElement(method, required, pd));
458-
}
459-
}
460-
});
414+
ReflectionUtils.doWithLocalFields(targetClass, field -> {
415+
AnnotationAttributes ann = findAutowiredAnnotation(field);
416+
if (ann != null) {
417+
if (Modifier.isStatic(field.getModifiers())) {
418+
if (logger.isWarnEnabled()) {
419+
logger.warn("Autowired annotation is not supported on static fields: " + field);
420+
}
421+
return;
422+
}
423+
boolean required = determineRequiredStatus(ann);
424+
currElements.add(new AutowiredFieldElement(field, required));
425+
}
426+
});
427+
428+
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
429+
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
430+
if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
431+
return;
432+
}
433+
AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);
434+
if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
435+
if (Modifier.isStatic(method.getModifiers())) {
436+
if (logger.isWarnEnabled()) {
437+
logger.warn("Autowired annotation is not supported on static methods: " + method);
438+
}
439+
return;
440+
}
441+
if (method.getParameterCount() == 0) {
442+
if (logger.isWarnEnabled()) {
443+
logger.warn("Autowired annotation should only be used on methods with parameters: " +
444+
method);
445+
}
446+
}
447+
boolean required = determineRequiredStatus(ann);
448+
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
449+
currElements.add(new AutowiredMethodElement(method, required, pd));
450+
}
451+
});
461452

462453
elements.addAll(0, currElements);
463454
targetClass = targetClass.getSuperclass();

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,24 +201,21 @@ private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
201201
final LinkedList<LifecycleElement> currInitMethods = new LinkedList<>();
202202
final LinkedList<LifecycleElement> currDestroyMethods = new LinkedList<>();
203203

204-
ReflectionUtils.doWithLocalMethods(targetClass, new ReflectionUtils.MethodCallback() {
205-
@Override
206-
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
207-
if (initAnnotationType != null) {
208-
if (method.getAnnotation(initAnnotationType) != null) {
209-
LifecycleElement element = new LifecycleElement(method);
210-
currInitMethods.add(element);
211-
if (debug) {
212-
logger.debug("Found init method on class [" + clazz.getName() + "]: " + method);
213-
}
204+
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
205+
if (initAnnotationType != null) {
206+
if (method.getAnnotation(initAnnotationType) != null) {
207+
LifecycleElement element = new LifecycleElement(method);
208+
currInitMethods.add(element);
209+
if (debug) {
210+
logger.debug("Found init method on class [" + clazz.getName() + "]: " + method);
214211
}
215212
}
216-
if (destroyAnnotationType != null) {
217-
if (method.getAnnotation(destroyAnnotationType) != null) {
218-
currDestroyMethods.add(new LifecycleElement(method));
219-
if (debug) {
220-
logger.debug("Found destroy method on class [" + clazz.getName() + "]: " + method);
221-
}
213+
}
214+
if (destroyAnnotationType != null) {
215+
if (method.getAnnotation(destroyAnnotationType) != null) {
216+
currDestroyMethods.add(new LifecycleElement(method));
217+
if (debug) {
218+
logger.debug("Found destroy method on class [" + clazz.getName() + "]: " + method);
222219
}
223220
}
224221
}

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -877,20 +877,15 @@ class Holder { Class<?> value = null; }
877877

878878
// Find the given factory method, taking into account that in the case of
879879
// @Bean methods, there may be parameters present.
880-
ReflectionUtils.doWithMethods(fbClass,
881-
new ReflectionUtils.MethodCallback() {
882-
@Override
883-
public void doWith(Method method) {
884-
if (method.getName().equals(factoryMethodName) &&
885-
FactoryBean.class.isAssignableFrom(method.getReturnType())) {
886-
Class<?> currentType = GenericTypeResolver.resolveReturnTypeArgument(
887-
method, FactoryBean.class);
888-
if (currentType != null) {
889-
objectType.value = ClassUtils.determineCommonAncestor(currentType, objectType.value);
890-
}
891-
}
892-
}
893-
});
880+
ReflectionUtils.doWithMethods(fbClass, method -> {
881+
if (method.getName().equals(factoryMethodName) &&
882+
FactoryBean.class.isAssignableFrom(method.getReturnType())) {
883+
Class<?> currentType = GenericTypeResolver.resolveReturnTypeArgument(method, FactoryBean.class);
884+
if (currentType != null) {
885+
objectType.value = ClassUtils.determineCommonAncestor(currentType, objectType.value);
886+
}
887+
}
888+
});
894889

895890
return (objectType.value != null && Object.class != objectType.value ? objectType.value : null);
896891
}

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/JCacheInterceptor.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,12 @@ public class JCacheInterceptor extends JCacheAspectSupport implements MethodInte
4545
public Object invoke(final MethodInvocation invocation) throws Throwable {
4646
Method method = invocation.getMethod();
4747

48-
CacheOperationInvoker aopAllianceInvoker = new CacheOperationInvoker() {
49-
@Override
50-
public Object invoke() {
51-
try {
52-
return invocation.proceed();
53-
}
54-
catch (Throwable ex) {
55-
throw new ThrowableWrapper(ex);
56-
}
48+
CacheOperationInvoker aopAllianceInvoker = () -> {
49+
try {
50+
return invocation.proceed();
51+
}
52+
catch (Throwable ex) {
53+
throw new CacheOperationInvoker.ThrowableWrapper(ex);
5754
}
5855
};
5956

0 commit comments

Comments
 (0)