Consistent Class array vs vararg declarations (and related polishing)

(cherry picked from commit 3b810f3)
This commit is contained in:
Juergen Hoeller
2018-02-14 14:44:00 +01:00
parent 5ba37762fe
commit 722cb36e01
40 changed files with 486 additions and 489 deletions

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.
@@ -57,7 +57,7 @@ public class ConventionsTests {
@Test
public void emptyList() {
exception.expect(IllegalArgumentException.class);
this.exception.expect(IllegalArgumentException.class);
Conventions.getVariableName(new ArrayList<>());
}
@@ -67,14 +67,14 @@ public class ConventionsTests {
}
@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-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.
@@ -294,9 +294,9 @@ public class AnnotationMetadataTests {
assertEquals("direct", method.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value"));
assertEquals("direct", method.getAnnotationAttributes(DirectAnnotation.class.getName()).get("myValue"));
List<Object> allMeta = method.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");
assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct", "meta")))));
assertThat(new HashSet<>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct", "meta")))));
allMeta = method.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("additional");
assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct")))));
assertThat(new HashSet<>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct")))));
assertTrue(metadata.isAnnotated(IsAnnotatedAnnotation.class.getName()));
@@ -309,36 +309,36 @@ 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");
assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct", "meta")))));
assertThat(new HashSet<>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct", "meta")))));
allMeta = metadata.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("additional");
assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct")))));
assertThat(new HashSet<>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct")))));
}
{ // perform tests with classValuesAsString = true
AnnotationAttributes specialAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(
@@ -365,9 +365,9 @@ public class AnnotationMetadataTests {
assertArrayEquals(new String[] { Void.class.getName() }, (String[]) optionalArray[0].get("classArray"));
assertArrayEquals(new String[] { Void.class.getName() }, optionalArray[0].getStringArray("classArray"));
assertEquals(metadata.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value"), "direct");
assertEquals("direct", metadata.getAnnotationAttributes(DirectAnnotation.class.getName()).get("value"));
allMeta = metadata.getAllAnnotationAttributes(DirectAnnotation.class.getName()).get("value");
assertThat(new HashSet<Object>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct", "meta")))));
assertThat(new HashSet<>(allMeta), is(equalTo(new HashSet<Object>(Arrays.asList("direct", "meta")))));
}
}

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-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.
@@ -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));
@@ -110,7 +110,7 @@ public class ObjectUtilsTests {
assertTrue(isEmpty(Collections.emptyList()));
assertTrue(isEmpty(Collections.emptySet()));
Set<String> set = new HashSet<String>();
Set<String> set = new HashSet<>();
set.add("foo");
assertFalse(isEmpty(set));
assertFalse(isEmpty(Arrays.asList("foo")));
@@ -120,7 +120,7 @@ public class ObjectUtilsTests {
public void isEmptyMap() {
assertTrue(isEmpty(Collections.emptyMap()));
HashMap<String, Object> map = new HashMap<String, Object>();
HashMap<String, Object> map = new HashMap<>();
map.put("foo", 42L);
assertFalse(isEmpty(map));
}
@@ -239,18 +239,21 @@ public class ObjectUtilsTests {
}
@Test
@Deprecated
public void hashCodeWithBooleanFalse() {
int expected = Boolean.FALSE.hashCode();
assertEquals(expected, ObjectUtils.hashCode(false));
}
@Test
@Deprecated
public void hashCodeWithBooleanTrue() {
int expected = Boolean.TRUE.hashCode();
assertEquals(expected, ObjectUtils.hashCode(true));
}
@Test
@Deprecated
public void hashCodeWithDouble() {
double dbl = 9830.43;
int expected = (new Double(dbl)).hashCode();
@@ -258,6 +261,7 @@ public class ObjectUtilsTests {
}
@Test
@Deprecated
public void hashCodeWithFloat() {
float flt = 34.8f;
int expected = (new Float(flt)).hashCode();
@@ -265,6 +269,7 @@ public class ObjectUtilsTests {
}
@Test
@Deprecated
public void hashCodeWithLong() {
long lng = 883l;
int expected = (new Long(lng)).hashCode();

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);