Remove:
- auto-boxing
- unnecessary array creation
- public keyword on interface methods
- unnecessary throws clauses

Use lambda expressions
This commit is contained in:
Rossen Stoyanchev
2018-02-08 15:47:21 -05:00
parent d66c25ffd0
commit 6d5a8debd5
213 changed files with 987 additions and 1124 deletions

View File

@@ -220,16 +220,8 @@ public class DefaultConversionServiceTests extends TestCase {
DefaultConversionService service = new DefaultConversionService();
service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", Principal[].class, List.class);
final Principal princy1 = new Principal() {
public String getName() {
return "princy1";
}
};
final Principal princy2 = new Principal() {
public String getName() {
return "princy2";
}
};
final Principal princy1 = () -> "princy1";
final Principal princy2 = () -> "princy2";
List<String> p = (List<String>) executor.execute(new Principal[] { princy1, princy2 });
assertEquals("princy1", p.get(0));
assertEquals("princy2", p.get(1));
@@ -262,16 +254,8 @@ public class DefaultConversionServiceTests extends TestCase {
DefaultConversionService service = new DefaultConversionService();
service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", List.class, String[].class);
final Principal princy1 = new Principal() {
public String getName() {
return "princy1";
}
};
final Principal princy2 = new Principal() {
public String getName() {
return "princy2";
}
};
final Principal princy1 = () -> "princy1";
final Principal princy2 = () -> "princy2";
List<Principal> princyList = new ArrayList<>();
princyList.add(princy1);
princyList.add(princy2);
@@ -303,11 +287,7 @@ public class DefaultConversionServiceTests extends TestCase {
DefaultConversionService service = new DefaultConversionService();
service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", Principal.class, String[].class);
final Principal princy1 = new Principal() {
public String getName() {
return "princy1";
}
};
final Principal princy1 = () -> "princy1";
String[] p = (String[]) executor.execute(princy1);
assertEquals("princy1", p[0]);
}
@@ -359,11 +339,7 @@ public class DefaultConversionServiceTests extends TestCase {
DefaultConversionService service = new DefaultConversionService();
service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", Principal.class, List.class);
final Principal princy1 = new Principal() {
public String getName() {
return "princy1";
}
};
final Principal princy1 = () -> "princy1";
List<String> list = (List<String>) executor.execute(princy1);
assertEquals("princy1", list.get(0));
}
@@ -386,16 +362,8 @@ public class DefaultConversionServiceTests extends TestCase {
DefaultConversionService service = new DefaultConversionService();
service.addConverter("princy", new CustomTwoWayConverter());
ConversionExecutor executor = service.getConversionExecutor("princy", List.class, List.class);
final Principal princy1 = new Principal() {
public String getName() {
return "princy1";
}
};
final Principal princy2 = new Principal() {
public String getName() {
return "princy2";
}
};
final Principal princy1 = () -> "princy1";
final Principal princy2 = () -> "princy2";
List<Principal> princyList = new ArrayList<>();
princyList.add(princy1);
princyList.add(princy2);
@@ -598,21 +566,17 @@ public class DefaultConversionServiceTests extends TestCase {
super(List.class);
}
protected Object toObject(String string, Class<?> targetClass) throws Exception {
protected Object toObject(String string, Class<?> targetClass) {
List<Principal> principals = new ArrayList<>();
StringTokenizer tokenizer = new StringTokenizer(string, ",");
while (tokenizer.hasMoreTokens()) {
final String name = tokenizer.nextToken();
principals.add(new Principal() {
public String getName() {
return name;
}
});
principals.add(() -> name);
}
return principals;
}
protected String toString(Object object) throws Exception {
protected String toString(Object object) {
throw new UnsupportedOperationException("No implemented");
}

View File

@@ -26,7 +26,7 @@ public class StaticConversionExecutorImplTests extends TestCase {
private StaticConversionExecutor conversionExecutor;
protected void setUp() throws Exception {
protected void setUp() {
StringToDate stringToDate = new StringToDate();
conversionExecutor = new StaticConversionExecutor(String.class, Date.class, stringToDate);
}

View File

@@ -29,7 +29,7 @@ public class ELExpressionParserTests extends TestCase {
public void testParseSimpleEvalExpressionNoParserContext() {
String expressionString = "3 + 4";
Expression exp = parser.parseExpression(expressionString, null);
assertEquals(new Long(7), exp.getValue(null));
assertEquals(7L, exp.getValue(null));
}
public void testParseNullExpressionString() {
@@ -61,7 +61,7 @@ public class ELExpressionParserTests extends TestCase {
String expressionString = "3 + 4";
Expression exp = parser
.parseExpression(expressionString, new FluentParserContext().expectResult(Integer.class));
assertEquals(new Integer(7), exp.getValue(null));
assertEquals(7, exp.getValue(null));
}
public void testParseBeanEvalExpressionNoParserContext() {
@@ -73,7 +73,7 @@ public class ELExpressionParserTests extends TestCase {
public void testParseEvalExpressionWithContextTypeCoersion() {
String expressionString = "maximum";
Expression exp = parser.parseExpression(expressionString, new FluentParserContext().expectResult(Long.class));
assertEquals(new Long(2), exp.getValue(new TestBean()));
assertEquals(2L, exp.getValue(new TestBean()));
}
public void testParseEvalExpressionWithContextCustomELVariableResolver() {
@@ -119,7 +119,7 @@ public class ELExpressionParserTests extends TestCase {
Expression exp = parser.parseExpression("max", new FluentParserContext().variable(new ExpressionVariable("max",
"maximum", new FluentParserContext().expectResult(Long.class))));
TestBean target = new TestBean();
assertEquals(new Long(2), exp.getValue(target));
assertEquals(2L, exp.getValue(target));
}
public void testTemplateNestedVariables() {

View File

@@ -44,7 +44,7 @@ public class ELExpressionParserCompatibilityTests extends TestCase {
private SpringELExpressionParser parser = new SpringELExpressionParser(new SpelExpressionParser());
protected void setUp() throws Exception {
protected void setUp() {
parser.addPropertyAccessor(new SpecialPropertyAccessor());
}
@@ -82,7 +82,7 @@ public class ELExpressionParserCompatibilityTests extends TestCase {
public void testParseSimpleEvalExpressionNoEvalContextWithTypeCoersion() {
String expressionString = "3 + 4";
Expression exp = parser.parseExpression(expressionString, new FluentParserContext().expectResult(Long.class));
assertEquals(new Long(7), exp.getValue(null));
assertEquals(7L, exp.getValue(null));
}
public void testParseBeanEvalExpressionNoParserContext() {
@@ -95,7 +95,7 @@ public class ELExpressionParserCompatibilityTests extends TestCase {
String expressionString = "maximum";
Expression exp = parser
.parseExpression(expressionString, new FluentParserContext().expectResult(Integer.class));
assertEquals(new Integer(2), exp.getValue(new TestBean()));
assertEquals(2, exp.getValue(new TestBean()));
}
public void testParseEvalExpressionWithContextCustomELVariableResolver() {
@@ -198,11 +198,10 @@ public class ELExpressionParserCompatibilityTests extends TestCase {
}
private final class SpecialPropertyAccessor implements PropertyAccessor {
public void write(EvaluationContext context, Object target, String name, Object newValue)
throws AccessException {
public void write(EvaluationContext context, Object target, String name, Object newValue) {
}
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
public TypedValue read(EvaluationContext context, Object target, String name) {
return new TypedValue("Custom resolver resolved this special property!",
TypeDescriptor.valueOf(String.class));
}
@@ -211,11 +210,11 @@ public class ELExpressionParserCompatibilityTests extends TestCase {
return null;
}
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
public boolean canWrite(EvaluationContext context, Object target, String name) {
return false;
}
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
public boolean canRead(EvaluationContext context, Object target, String name) {
return "specialProperty".equals(name);
}
}

View File

@@ -34,13 +34,11 @@ public class DefaultMapperTests extends TestCase {
assertEquals(0, results.getErrorResults().size());
assertEquals("a", bean2.bar);
assertEquals("a", bean2.baz);
assertEquals(1, results.getResults(new MappingResultsCriteria() {
public boolean test(MappingResult result) {
if (result.getMapping().getTargetExpression().getExpressionString().equals("baz")) {
return true;
} else {
return false;
}
assertEquals(1, results.getResults(result -> {
if (result.getMapping().getTargetExpression().getExpressionString().equals("baz")) {
return true;
} else {
return false;
}
}).size());
}

View File

@@ -69,7 +69,7 @@ public class MessageBuilderTests extends TestCase {
}
public void testBuildCodes() {
MessageResolver resolver = builder.error().codes(new String[] { "foo" }).build();
MessageResolver resolver = builder.error().codes("foo").build();
Message message = resolver.resolveMessage(messageSource, locale);
assertEquals("bar", message.getText());
assertEquals(Severity.ERROR, message.getSeverity());
@@ -85,7 +85,7 @@ public class MessageBuilderTests extends TestCase {
}
public void testBuildArgs() {
MessageResolver resolver = builder.error().codes(new String[] { "bar" }).args(new Object[] { "baz" }).build();
MessageResolver resolver = builder.error().codes("bar").args("baz").build();
Message message = resolver.resolveMessage(messageSource, locale);
assertEquals("baz", message.getText());
assertEquals(Severity.ERROR, message.getSeverity());
@@ -113,7 +113,7 @@ public class MessageBuilderTests extends TestCase {
}
public void testBuildArgsWithNullCodes() {
MessageResolver resolver = builder.error().args(new Object[] { "baz" }).build();
MessageResolver resolver = builder.error().args("baz").build();
try {
resolver.resolveMessage(messageSource, locale);
fail("Should have failed");
@@ -122,7 +122,7 @@ public class MessageBuilderTests extends TestCase {
}
public void testBuildArgsWithNullCodesDefaultText() {
MessageResolver resolver = builder.error().args(new Object[] { "baz" }).defaultText("foo").build();
MessageResolver resolver = builder.error().args("baz").defaultText("foo").build();
Message message = resolver.resolveMessage(messageSource, locale);
assertEquals("foo", message.getText());
}
@@ -144,7 +144,7 @@ public class MessageBuilderTests extends TestCase {
}
public void testBuildResolvableArgs() {
MessageResolver resolver = builder.error().codes(new String[] { "bar" }).resolvableArgs(new Object[] { "baz" })
MessageResolver resolver = builder.error().codes("bar").resolvableArgs("baz")
.build();
Message message = resolver.resolveMessage(messageSource, locale);
assertEquals("boop", message.getText());

View File

@@ -3,8 +3,8 @@ package org.springframework.binding.message;
import java.util.Locale;
import junit.framework.TestCase;
import org.easymock.EasyMock;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.validation.MessageCodesResolver;
@@ -19,7 +19,7 @@ public class MessageContextErrorsMessageCodesTests extends TestCase {
private MessageCodesResolver resolver;
@Override
protected void setUp() throws Exception {
protected void setUp() {
StaticMessageSource messageSource = new StaticMessageSource();
messageSource.addMessage(errorCode, Locale.getDefault(), "doesntmatter");
context = new DefaultMessageContext(messageSource);
@@ -27,7 +27,7 @@ public class MessageContextErrorsMessageCodesTests extends TestCase {
resolver = EasyMock.createMock(MessageCodesResolver.class);
}
public void testRejectUsesObjectName() throws Exception {
public void testRejectUsesObjectName() {
EasyMock.expect(resolver.resolveMessageCodes(errorCode, objectName)).andReturn(new String[] {});
EasyMock.replay(resolver);
@@ -38,7 +38,7 @@ public class MessageContextErrorsMessageCodesTests extends TestCase {
EasyMock.verify(resolver);
}
public void testRejectValueUsesObjectName() throws Exception {
public void testRejectValueUsesObjectName() {
EasyMock.expect(resolver.resolveMessageCodes(errorCode, objectName, "field", null)).andReturn(new String[] {});
EasyMock.replay(resolver);
@@ -48,7 +48,7 @@ public class MessageContextErrorsMessageCodesTests extends TestCase {
EasyMock.verify(resolver);
}
public void testRejectValueEmptyField() throws Exception {
public void testRejectValueEmptyField() {
EasyMock.expect(resolver.resolveMessageCodes(errorCode, objectName)).andReturn(new String[] {});
EasyMock.replay(resolver);

View File

@@ -17,7 +17,7 @@ public class MessageContextErrorsTests extends TestCase {
private MessageContextErrors errors;
@Override
protected void setUp() throws Exception {
protected void setUp() {
StaticMessageSource messageSource = new StaticMessageSource();
messageSource.addMessage("foo", Locale.getDefault(), "bar");
messageSource.addMessage("bar", Locale.getDefault(), "{0}");

View File

@@ -29,7 +29,7 @@ public class MethodInvokerTests extends TestCase {
private MethodInvoker methodInvoker;
protected void setUp() throws Exception {
protected void setUp() {
this.methodInvoker = new MethodInvoker();
}

View File

@@ -32,34 +32,34 @@ public class MethodKeyTests extends TestCase {
private static final Method LIST_FILENAME_FILTER = safeGetMethod(File.class, "list",
new Class[] { FilenameFilter.class });
public void testGetMethodWithNoArgs() throws Exception {
MethodKey key = new MethodKey(File.class, "list", new Class[0]);
public void testGetMethodWithNoArgs() {
MethodKey key = new MethodKey(File.class, "list");
Method m = key.getMethod();
assertEquals(LIST_NO_ARGS, m);
}
public void testGetMoreGenericMethod() throws Exception {
MethodKey key = new MethodKey(Object.class, "equals", new Class[] { Long.class });
public void testGetMoreGenericMethod() {
MethodKey key = new MethodKey(Object.class, "equals", Long.class);
assertEquals(safeGetMethod(Object.class, "equals", new Class[] { Object.class }), key.getMethod());
}
public void testGetMethodWithSingleArg() throws Exception {
MethodKey key = new MethodKey(File.class, "list", new Class[] { FilenameFilter.class });
public void testGetMethodWithSingleArg() {
MethodKey key = new MethodKey(File.class, "list", FilenameFilter.class);
Method m = key.getMethod();
assertEquals(LIST_FILENAME_FILTER, m);
}
public void testGetMethodWithSingleNullArgAndValidMatch() throws Exception {
public void testGetMethodWithSingleNullArgAndValidMatch() {
MethodKey key = new MethodKey(File.class, "list", new Class[] { null });
Method m = key.getMethod();
assertEquals(LIST_FILENAME_FILTER, m);
}
public void testGetMethodWithSingleNullAndUnclearMatch() throws Exception {
public void testGetMethodWithSingleNullAndUnclearMatch() {
new MethodKey(File.class, "listFiles", new Class[] { null });
}
private static final Method safeGetMethod(Class<?> type, String name, Class<?>[] argTypes) {
private static Method safeGetMethod(Class<?> type, String name, Class<?>[] argTypes) {
try {
return type.getMethod(name, argTypes);
} catch (NoSuchMethodException e) {