Use lambdas when possible

Replace anonymous inner classes with lambda declarations (when possible
using method references).

See gh-9781
This commit is contained in:
Emanuel Campolo
2017-07-24 23:11:47 -07:00
committed by Phillip Webb
parent d16af43664
commit 2626a3a795
150 changed files with 983 additions and 2596 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,7 +27,6 @@ import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.MethodCallback;
/**
* {@link ContextCustomizerFactory} to allow {@code @Import} annotations to be used
@@ -49,16 +48,12 @@ class ImportsContextCustomizerFactory implements ContextCustomizerFactory {
}
private void assertHasNoBeanMethods(Class<?> testClass) {
ReflectionUtils.doWithMethods(testClass, new MethodCallback() {
@Override
public void doWith(Method method) {
Assert.state(!AnnotatedElementUtils.isAnnotated(method, Bean.class),
"Test classes cannot include @Bean methods");
}
});
ReflectionUtils.doWithMethods(testClass, this::assertHasNoBeanMethods);
}
private void assertHasNoBeanMethods(Method method) {
Assert.state(!AnnotatedElementUtils.isAnnotated(method, Bean.class),
"Test classes cannot include @Bean methods");
}
}

View File

@@ -28,7 +28,6 @@ import java.lang.reflect.Field;
import org.assertj.core.api.Assertions;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.ByteArrayResource;
@@ -38,7 +37,6 @@ import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
/**
* Base class for AssertJ based JSON marshal testers. Exposes specific Asserts following a
@@ -370,29 +368,15 @@ public abstract class AbstractJsonMarshalTester<T> {
public void initFields(final Object testInstance, final M marshaller) {
Assert.notNull(testInstance, "TestInstance must not be null");
Assert.notNull(marshaller, "Marshaller must not be null");
initFields(testInstance, new ObjectFactory<M>() {
@Override
public M getObject() throws BeansException {
return marshaller;
}
});
initFields(testInstance, () -> marshaller);
}
public void initFields(final Object testInstance,
final ObjectFactory<M> marshaller) {
Assert.notNull(testInstance, "TestInstance must not be null");
Assert.notNull(marshaller, "Marshaller must not be null");
ReflectionUtils.doWithFields(testInstance.getClass(), new FieldCallback() {
@Override
public void doWith(Field field)
throws IllegalArgumentException, IllegalAccessException {
doWithField(field, testInstance, marshaller);
}
});
ReflectionUtils.doWithFields(testInstance.getClass(),
(field) -> doWithField(field, testInstance, marshaller));
}
protected void doWithField(Field field, Object test,

View File

@@ -29,7 +29,6 @@ import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
import org.springframework.util.StringUtils;
/**
@@ -60,15 +59,7 @@ class DefinitionsParser {
public void parse(Class<?> source) {
parseElement(source);
ReflectionUtils.doWithFields(source, new FieldCallback() {
@Override
public void doWith(Field field)
throws IllegalArgumentException, IllegalAccessException {
parseElement(field);
}
});
ReflectionUtils.doWithFields(source, this::parseElement);
}
private void parseElement(AnnotatedElement element) {

View File

@@ -61,7 +61,6 @@ import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
import org.springframework.util.StringUtils;
/**
@@ -371,15 +370,8 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
public PropertyValues postProcessPropertyValues(PropertyValues pvs,
PropertyDescriptor[] pds, final Object bean, String beanName)
throws BeansException {
ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
@Override
public void doWith(Field field)
throws IllegalArgumentException, IllegalAccessException {
postProcessField(bean, field);
}
});
ReflectionUtils.doWithFields(bean.getClass(),
(field) -> postProcessField(bean, field));
return pvs;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -1307,14 +1307,7 @@ public class JsonContentAssertTests {
}
private AssertProvider<JsonContentAssert> forJson(final String json) {
return new AssertProvider<JsonContentAssert>() {
@Override
public JsonContentAssert assertThat() {
return new JsonContentAssert(JsonContentAssertTests.class, json);
}
};
return () -> new JsonContentAssert(JsonContentAssertTests.class, json);
}
}

View File

@@ -73,14 +73,7 @@ public class ObjectContentAssertTests {
}
private AssertProvider<ObjectContentAssert<Object>> forObject(final Object source) {
return new AssertProvider<ObjectContentAssert<Object>>() {
@Override
public ObjectContentAssert<Object> assertThat() {
return new ObjectContentAssert<>(source);
}
};
return () -> new ObjectContentAssert<>(source);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -71,14 +71,7 @@ public class MockBeanForBeanFactoryIntegrationTests {
@Override
public TestBean getObject() throws Exception {
return new TestBean() {
@Override
public String hello() {
return "normal";
}
};
return () -> "normal";
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
package org.springframework.boot.test.mock.web;
import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
@@ -87,12 +86,8 @@ public class SpringBootMockServletContextTests implements ServletContextAware {
assertThat(resource).isNotEqualTo(nullValue());
File file = new File(URLDecoder.decode(resource.getPath(), "UTF-8"));
assertThat(file).exists().isDirectory();
String[] contents = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return !(".".equals(name) || "..".equals(name));
}
});
String[] contents = file
.list((dir, name) -> !(".".equals(name) || "..".equals(name)));
assertThat(contents).isNotEqualTo(nullValue());
assertThat(contents.length).isEqualTo(0);
}

View File

@@ -154,13 +154,7 @@ public class TestRestTemplateTests {
return mock(type);
}
}, new ReflectionUtils.MethodFilter() {
@Override
public boolean matches(Method method) {
return Modifier.isPublic(method.getModifiers());
}
});
}, (method) -> Modifier.isPublic(method.getModifiers()));
}
@@ -217,209 +211,99 @@ public class TestRestTemplateTests {
@Test
public void deleteHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.delete(relativeUri);
}
});
verifyRelativeUriHandling(TestRestTemplate::delete);
}
@Test
public void exchangeWithRequestEntityAndClassHandlesRelativeUris()
throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.exchange(
new RequestEntity<String>(HttpMethod.GET, relativeUri),
String.class);
}
});
verifyRelativeUriHandling((testRestTemplate, relativeUri) -> testRestTemplate
.exchange(new RequestEntity<String>(HttpMethod.GET, relativeUri),
String.class));
}
@Test
public void exchangeWithRequestEntityAndParameterizedTypeReferenceHandlesRelativeUris()
throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.exchange(
new RequestEntity<String>(HttpMethod.GET, relativeUri),
verifyRelativeUriHandling((testRestTemplate, relativeUri) -> testRestTemplate
.exchange(new RequestEntity<String>(HttpMethod.GET, relativeUri),
new ParameterizedTypeReference<String>() {
});
}
});
}));
}
@Test
public void exchangeHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.exchange(relativeUri, HttpMethod.GET,
new HttpEntity<>(new byte[0]), String.class);
}
});
verifyRelativeUriHandling(
(testRestTemplate, relativeUri) -> testRestTemplate.exchange(relativeUri,
HttpMethod.GET, new HttpEntity<>(new byte[0]), String.class));
}
@Test
public void exchangeWithParameterizedTypeReferenceHandlesRelativeUris()
throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.exchange(relativeUri, HttpMethod.GET,
new HttpEntity<>(new byte[0]),
verifyRelativeUriHandling(
(testRestTemplate, relativeUri) -> testRestTemplate.exchange(relativeUri,
HttpMethod.GET, new HttpEntity<>(new byte[0]),
new ParameterizedTypeReference<String>() {
});
}
});
}));
}
@Test
public void executeHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.execute(relativeUri, HttpMethod.GET, null, null);
}
});
verifyRelativeUriHandling((testRestTemplate, relativeUri) -> testRestTemplate
.execute(relativeUri, HttpMethod.GET, null, null));
}
@Test
public void getForEntityHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.getForEntity(relativeUri, String.class);
}
});
verifyRelativeUriHandling((testRestTemplate, relativeUri) -> testRestTemplate
.getForEntity(relativeUri, String.class));
}
@Test
public void getForObjectHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.getForObject(relativeUri, String.class);
}
});
verifyRelativeUriHandling((testRestTemplate, relativeUri) -> testRestTemplate
.getForObject(relativeUri, String.class));
}
@Test
public void headForHeadersHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.headForHeaders(relativeUri);
}
});
verifyRelativeUriHandling(TestRestTemplate::headForHeaders);
}
@Test
public void optionsForAllowHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.optionsForAllow(relativeUri);
}
});
verifyRelativeUriHandling(TestRestTemplate::optionsForAllow);
}
@Test
public void patchForObjectHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.patchForObject(relativeUri, "hello", String.class);
}
});
verifyRelativeUriHandling((testRestTemplate, relativeUri) -> testRestTemplate
.patchForObject(relativeUri, "hello", String.class));
}
@Test
public void postForEntityHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.postForEntity(relativeUri, "hello", String.class);
}
});
verifyRelativeUriHandling((testRestTemplate, relativeUri) -> testRestTemplate
.postForEntity(relativeUri, "hello", String.class));
}
@Test
public void postForLocationHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.postForLocation(relativeUri, "hello");
}
});
verifyRelativeUriHandling((testRestTemplate, relativeUri) -> testRestTemplate
.postForLocation(relativeUri, "hello"));
}
@Test
public void postForObjectHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.postForObject(relativeUri, "hello", String.class);
}
});
verifyRelativeUriHandling((testRestTemplate, relativeUri) -> testRestTemplate
.postForObject(relativeUri, "hello", String.class));
}
@Test
public void putHandlesRelativeUris() throws IOException {
verifyRelativeUriHandling(new TestRestTemplateCallback() {
@Override
public void doWithTestRestTemplate(TestRestTemplate testRestTemplate,
URI relativeUri) {
testRestTemplate.put(relativeUri, "hello");
}
});
verifyRelativeUriHandling((testRestTemplate, relativeUri) -> testRestTemplate
.put(relativeUri, "hello"));
}
private void verifyRelativeUriHandling(TestRestTemplateCallback callback)