Consistent Class array vs vararg declarations (and related polishing)

This commit is contained in:
Juergen Hoeller
2018-02-14 14:44:00 +01:00
parent 46cbdff5c3
commit 3b810f3544
39 changed files with 388 additions and 413 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -34,11 +34,11 @@ import reactor.core.publisher.Mono;
import org.springframework.tests.sample.objects.TestObject;
import org.springframework.util.ClassUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Unit tests for {@link Conventions}.
*
* @author Rob Harrop
* @author Sam Brannen
*/
@@ -50,13 +50,10 @@ public class ConventionsTests {
@Test
public void simpleObject() {
assertEquals("Incorrect singular variable name",
"testObject", Conventions.getVariableName(new TestObject()));
assertEquals("Incorrect singular variable name", "testObject",
Conventions.getVariableNameForParameter(getMethodParameter(TestObject.class)));
assertEquals("Incorrect singular variable name", "testObject",
Conventions.getVariableNameForReturnType(getMethodForReturnType(TestObject.class)));
}
@@ -69,13 +66,10 @@ public class ConventionsTests {
@Test
public void list() {
assertEquals("Incorrect plural List form", "testObjectList",
Conventions.getVariableName(Collections.singletonList(new TestObject())));
assertEquals("Incorrect plural List form", "testObjectList",
Conventions.getVariableNameForParameter(getMethodParameter(List.class)));
assertEquals("Incorrect plural List form", "testObjectList",
Conventions.getVariableNameForReturnType(getMethodForReturnType(List.class)));
}
@@ -88,58 +82,47 @@ public class ConventionsTests {
@Test
public void set() {
assertEquals("Incorrect plural Set form", "testObjectList",
Conventions.getVariableName(Collections.singleton(new TestObject())));
assertEquals("Incorrect plural Set form", "testObjectList",
Conventions.getVariableNameForParameter(getMethodParameter(Set.class)));
assertEquals("Incorrect plural Set form", "testObjectList",
Conventions.getVariableNameForReturnType(getMethodForReturnType(Set.class)));
}
@Test
public void reactiveParameters() throws Exception {
public void reactiveParameters() {
assertEquals("testObjectMono",
Conventions.getVariableNameForParameter(getMethodParameter(Mono.class)));
assertEquals("testObjectFlux",
Conventions.getVariableNameForParameter(getMethodParameter(Flux.class)));
assertEquals("testObjectSingle",
Conventions.getVariableNameForParameter(getMethodParameter(Single.class)));
assertEquals("testObjectObservable",
Conventions.getVariableNameForParameter(getMethodParameter(Observable.class)));
}
@Test
public void reactiveReturnTypes() throws Exception {
public void reactiveReturnTypes() {
assertEquals("testObjectMono",
Conventions.getVariableNameForReturnType(getMethodForReturnType(Mono.class)));
assertEquals("testObjectFlux",
Conventions.getVariableNameForReturnType(getMethodForReturnType(Flux.class)));
assertEquals("testObjectSingle",
Conventions.getVariableNameForReturnType(getMethodForReturnType(Single.class)));
assertEquals("testObjectObservable",
Conventions.getVariableNameForReturnType(getMethodForReturnType(Observable.class)));
}
@Test
public void attributeNameToPropertyName() throws Exception {
public void attributeNameToPropertyName() {
assertEquals("transactionManager", Conventions.attributeNameToPropertyName("transaction-manager"));
assertEquals("pointcutRef", Conventions.attributeNameToPropertyName("pointcut-ref"));
assertEquals("lookupOnStartup", Conventions.attributeNameToPropertyName("lookup-on-startup"));
}
@Test
public void getQualifiedAttributeName() throws Exception {
public void getQualifiedAttributeName() {
String baseName = "foo";
Class<String> cls = String.class;
String desiredResult = "java.lang.String.foo";

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 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.
@@ -39,7 +39,7 @@ public class LocalVariableTableParameterNameDiscovererTests {
@Test
public void methodParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
Method getName = TestObject.class.getMethod("getName", new Class[0]);
Method getName = TestObject.class.getMethod("getName");
String[] names = discoverer.getParameterNames(getName);
assertNotNull("should find method info", names);
assertEquals("no argument names", 0, names.length);
@@ -47,7 +47,7 @@ public class LocalVariableTableParameterNameDiscovererTests {
@Test
public void methodParameterNameDiscoveryWithArgs() throws NoSuchMethodException {
Method setName = TestObject.class.getMethod("setName", new Class[] { String.class });
Method setName = TestObject.class.getMethod("setName", String.class);
String[] names = discoverer.getParameterNames(setName);
assertNotNull("should find method info", names);
assertEquals("one argument", 1, names.length);
@@ -56,7 +56,7 @@ public class LocalVariableTableParameterNameDiscovererTests {
@Test
public void consParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
Constructor<TestObject> noArgsCons = TestObject.class.getConstructor(new Class[0]);
Constructor<TestObject> noArgsCons = TestObject.class.getConstructor();
String[] names = discoverer.getParameterNames(noArgsCons);
assertNotNull("should find cons info", names);
assertEquals("no argument names", 0, names.length);
@@ -64,7 +64,7 @@ public class LocalVariableTableParameterNameDiscovererTests {
@Test
public void consParameterNameDiscoveryArgs() throws NoSuchMethodException {
Constructor<TestObject> twoArgCons = TestObject.class.getConstructor(new Class[] { String.class, int.class });
Constructor<TestObject> twoArgCons = TestObject.class.getConstructor(String.class, int.class);
String[] names = discoverer.getParameterNames(twoArgCons);
assertNotNull("should find cons info", names);
assertEquals("one argument", 2, names.length);
@@ -74,7 +74,7 @@ public class LocalVariableTableParameterNameDiscovererTests {
@Test
public void staticMethodParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
Method m = getClass().getMethod("staticMethodNoLocalVars", new Class[0]);
Method m = getClass().getMethod("staticMethodNoLocalVars");
String[] names = discoverer.getParameterNames(m);
assertNotNull("should find method info", names);
assertEquals("no argument names", 0, names.length);
@@ -84,14 +84,14 @@ public class LocalVariableTableParameterNameDiscovererTests {
public void overloadedStaticMethod() throws Exception {
Class<? extends LocalVariableTableParameterNameDiscovererTests> clazz = this.getClass();
Method m1 = clazz.getMethod("staticMethod", new Class[] { Long.TYPE, Long.TYPE });
Method m1 = clazz.getMethod("staticMethod", Long.TYPE, Long.TYPE);
String[] names = discoverer.getParameterNames(m1);
assertNotNull("should find method info", names);
assertEquals("two arguments", 2, names.length);
assertEquals("x", names[0]);
assertEquals("y", names[1]);
Method m2 = clazz.getMethod("staticMethod", new Class[] { Long.TYPE, Long.TYPE, Long.TYPE });
Method m2 = clazz.getMethod("staticMethod", Long.TYPE, Long.TYPE, Long.TYPE);
names = discoverer.getParameterNames(m2);
assertNotNull("should find method info", names);
assertEquals("three arguments", 3, names.length);
@@ -104,13 +104,13 @@ public class LocalVariableTableParameterNameDiscovererTests {
public void overloadedStaticMethodInInnerClass() throws Exception {
Class<InnerClass> clazz = InnerClass.class;
Method m1 = clazz.getMethod("staticMethod", new Class[] { Long.TYPE });
Method m1 = clazz.getMethod("staticMethod", Long.TYPE);
String[] names = discoverer.getParameterNames(m1);
assertNotNull("should find method info", names);
assertEquals("one argument", 1, names.length);
assertEquals("x", names[0]);
Method m2 = clazz.getMethod("staticMethod", new Class[] { Long.TYPE, Long.TYPE });
Method m2 = clazz.getMethod("staticMethod", Long.TYPE, Long.TYPE);
names = discoverer.getParameterNames(m2);
assertNotNull("should find method info", names);
assertEquals("two arguments", 2, names.length);
@@ -122,14 +122,14 @@ public class LocalVariableTableParameterNameDiscovererTests {
public void overloadedMethod() throws Exception {
Class<? extends LocalVariableTableParameterNameDiscovererTests> clazz = this.getClass();
Method m1 = clazz.getMethod("instanceMethod", new Class[] { Double.TYPE, Double.TYPE });
Method m1 = clazz.getMethod("instanceMethod", Double.TYPE, Double.TYPE);
String[] names = discoverer.getParameterNames(m1);
assertNotNull("should find method info", names);
assertEquals("two arguments", 2, names.length);
assertEquals("x", names[0]);
assertEquals("y", names[1]);
Method m2 = clazz.getMethod("instanceMethod", new Class[] { Double.TYPE, Double.TYPE, Double.TYPE });
Method m2 = clazz.getMethod("instanceMethod", Double.TYPE, Double.TYPE, Double.TYPE);
names = discoverer.getParameterNames(m2);
assertNotNull("should find method info", names);
assertEquals("three arguments", 3, names.length);
@@ -142,13 +142,13 @@ public class LocalVariableTableParameterNameDiscovererTests {
public void overloadedMethodInInnerClass() throws Exception {
Class<InnerClass> clazz = InnerClass.class;
Method m1 = clazz.getMethod("instanceMethod", new Class[] { String.class });
Method m1 = clazz.getMethod("instanceMethod", String.class);
String[] names = discoverer.getParameterNames(m1);
assertNotNull("should find method info", names);
assertEquals("one argument", 1, names.length);
assertEquals("aa", names[0]);
Method m2 = clazz.getMethod("instanceMethod", new Class[] { String.class, String.class });
Method m2 = clazz.getMethod("instanceMethod", String.class, String.class);
names = discoverer.getParameterNames(m2);
assertNotNull("should find method info", names);
assertEquals("two arguments", 2, names.length);
@@ -223,6 +223,7 @@ public class LocalVariableTableParameterNameDiscovererTests {
assertNull(names);
}
public static void staticMethodNoLocalVars() {
}
@@ -246,6 +247,7 @@ public class LocalVariableTableParameterNameDiscovererTests {
return u;
}
public static class InnerClass {
public int waz = 0;
@@ -278,7 +280,9 @@ public class LocalVariableTableParameterNameDiscovererTests {
}
}
public static class GenerifiedClass<K, V> {
private static long date;
static {
@@ -317,4 +321,5 @@ public class LocalVariableTableParameterNameDiscovererTests {
return date;
}
}
}

View File

@@ -56,7 +56,7 @@ public class PrioritizedParameterNameDiscovererTests {
private final Method anyMethod;
public PrioritizedParameterNameDiscovererTests() throws SecurityException, NoSuchMethodException {
anyMethod = TestObject.class.getMethod("getAge", (Class[]) null);
anyMethod = TestObject.class.getMethod("getAge");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -309,30 +309,30 @@ public class AnnotationMetadataTests {
AnnotationAttributes nestedAnno = specialAttrs.getAnnotation("nestedAnno");
assertThat("na", is(nestedAnno.getString("value")));
assertTrue(nestedAnno.getEnum("anEnum").equals(SomeEnum.LABEL1));
assertArrayEquals(new Class[] { String.class }, (Class[]) nestedAnno.get("classArray"));
assertArrayEquals(new Class<?>[] {String.class}, (Class<?>[]) nestedAnno.get("classArray"));
AnnotationAttributes[] nestedAnnoArray = specialAttrs.getAnnotationArray("nestedAnnoArray");
assertThat(nestedAnnoArray.length, is(2));
assertThat(nestedAnnoArray[0].getString("value"), is("default"));
assertTrue(nestedAnnoArray[0].getEnum("anEnum").equals(SomeEnum.DEFAULT));
assertArrayEquals(new Class[] { Void.class }, (Class[]) nestedAnnoArray[0].get("classArray"));
assertArrayEquals(new Class<?>[] {Void.class}, (Class<?>[]) nestedAnnoArray[0].get("classArray"));
assertThat(nestedAnnoArray[1].getString("value"), is("na1"));
assertTrue(nestedAnnoArray[1].getEnum("anEnum").equals(SomeEnum.LABEL2));
assertArrayEquals(new Class[] { Number.class }, (Class[]) nestedAnnoArray[1].get("classArray"));
assertArrayEquals(new Class[] { Number.class }, nestedAnnoArray[1].getClassArray("classArray"));
assertArrayEquals(new Class<?>[] {Number.class}, (Class<?>[]) nestedAnnoArray[1].get("classArray"));
assertArrayEquals(new Class<?>[] {Number.class}, nestedAnnoArray[1].getClassArray("classArray"));
AnnotationAttributes optional = specialAttrs.getAnnotation("optional");
assertThat(optional.getString("value"), is("optional"));
assertTrue(optional.getEnum("anEnum").equals(SomeEnum.DEFAULT));
assertArrayEquals(new Class[] { Void.class }, (Class[]) optional.get("classArray"));
assertArrayEquals(new Class[] { Void.class }, optional.getClassArray("classArray"));
assertArrayEquals(new Class<?>[] {Void.class}, (Class<?>[]) optional.get("classArray"));
assertArrayEquals(new Class<?>[] {Void.class}, optional.getClassArray("classArray"));
AnnotationAttributes[] optionalArray = specialAttrs.getAnnotationArray("optionalArray");
assertThat(optionalArray.length, is(1));
assertThat(optionalArray[0].getString("value"), is("optional"));
assertTrue(optionalArray[0].getEnum("anEnum").equals(SomeEnum.DEFAULT));
assertArrayEquals(new Class[] { Void.class }, (Class[]) optionalArray[0].get("classArray"));
assertArrayEquals(new Class[] { Void.class }, optionalArray[0].getClassArray("classArray"));
assertArrayEquals(new Class<?>[] {Void.class}, (Class<?>[]) optionalArray[0].get("classArray"));
assertArrayEquals(new Class<?>[] {Void.class}, optionalArray[0].getClassArray("classArray"));
assertEquals("direct", metadata.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value"));
allMeta = metadata.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.type;
import java.lang.reflect.Method;
@@ -30,9 +31,9 @@ abstract class ClassloadingAssertions {
private static boolean isClassLoaded(String className) {
ClassLoader cl = ClassUtils.getDefaultClassLoader();
Method findLoadeClassMethod = ReflectionUtils.findMethod(cl.getClass(), "findLoadedClass", new Class[] { String.class });
ReflectionUtils.makeAccessible(findLoadeClassMethod);
Class<?> loadedClass = (Class<?>) ReflectionUtils.invokeMethod(findLoadeClassMethod, cl, new Object[] { className });
Method findLoadedClassMethod = ReflectionUtils.findMethod(cl.getClass(), "findLoadedClass", String.class);
ReflectionUtils.makeAccessible(findLoadedClassMethod);
Class<?> loadedClass = (Class<?>) ReflectionUtils.invokeMethod(findLoadedClassMethod, cl, className);
return loadedClass != null;
}
@@ -40,4 +41,4 @@ abstract class ClassloadingAssertions {
assertFalse("Class [" + className + "] should not have been loaded", isClassLoaded(className));
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 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.
@@ -212,7 +212,7 @@ public class ClassUtilsTests {
assertNotNull(method);
assertEquals("size", method.getName());
method = ClassUtils.getMethodIfAvailable(Collection.class, "remove", new Class[] {Object.class});
method = ClassUtils.getMethodIfAvailable(Collection.class, "remove", Object.class);
assertNotNull(method);
assertEquals("remove", method.getName());
@@ -239,7 +239,7 @@ public class ClassUtilsTests {
@Test
public void testNoArgsStaticMethod() throws IllegalAccessException, InvocationTargetException {
Method method = ClassUtils.getStaticMethod(InnerClass.class, "staticMethod", (Class[]) null);
Method method = ClassUtils.getStaticMethod(InnerClass.class, "staticMethod");
method.invoke(null, (Object[]) null);
assertTrue("no argument method was not invoked.",
InnerClass.noArgCalled);
@@ -247,19 +247,16 @@ public class ClassUtilsTests {
@Test
public void testArgsStaticMethod() throws IllegalAccessException, InvocationTargetException {
Method method = ClassUtils.getStaticMethod(InnerClass.class, "argStaticMethod",
new Class[] {String.class});
method.invoke(null, new Object[] {"test"});
Method method = ClassUtils.getStaticMethod(InnerClass.class, "argStaticMethod", String.class);
method.invoke(null, "test");
assertTrue("argument method was not invoked.", InnerClass.argCalled);
}
@Test
public void testOverloadedStaticMethod() throws IllegalAccessException, InvocationTargetException {
Method method = ClassUtils.getStaticMethod(InnerClass.class, "staticMethod",
new Class[] {String.class});
method.invoke(null, new Object[] {"test"});
assertTrue("argument method was not invoked.",
InnerClass.overloadedCalled);
Method method = ClassUtils.getStaticMethod(InnerClass.class, "staticMethod", String.class);
method.invoke(null, "test");
assertTrue("argument method was not invoked.", InnerClass.overloadedCalled);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -60,10 +60,10 @@ public class ObjectUtilsTests {
@Test
public void isCompatibleWithThrowsClause() {
Class<?>[] empty = new Class[0];
Class<?>[] exception = new Class[] {Exception.class};
Class<?>[] sqlAndIO = new Class[] {SQLException.class, IOException.class};
Class<?>[] throwable = new Class[] {Throwable.class};
Class<?>[] empty = new Class<?>[0];
Class<?>[] exception = new Class<?>[] {Exception.class};
Class<?>[] sqlAndIO = new Class<?>[] {SQLException.class, IOException.class};
Class<?>[] throwable = new Class<?>[] {Throwable.class};
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException()));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), empty));

View File

@@ -86,7 +86,7 @@ public class ReflectionUtilsTests {
TestObject bean = new TestObject();
bean.setName(rob);
Method getName = TestObject.class.getMethod("getName", (Class[]) null);
Method getName = TestObject.class.getMethod("getName");
Method setName = TestObject.class.getMethod("setName", String.class);
Object name = ReflectionUtils.invokeMethod(getName, bean);