From 6260734304222ea4c8586c7f983f448eb57f0018 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 29 May 2023 00:24:53 +0800 Subject: [PATCH] fix code --- .../hutool/core/reflect/ClassUtil.java | 16 +-- .../reflect/method/MethodScannerTest.java | 107 +++++++++--------- 2 files changed, 62 insertions(+), 61 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/reflect/ClassUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/reflect/ClassUtil.java index b14309d13..3b70ca2eb 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/reflect/ClassUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/reflect/ClassUtil.java @@ -781,7 +781,7 @@ public class ClassUtil { if (clazz == null) { return Collections.emptyList(); } - List> superclasses = new ArrayList<>(); + final List> superclasses = new ArrayList<>(); traverseTypeHierarchy(clazz, t -> !t.isInterface(), superclasses::add, false); return superclasses; } @@ -797,7 +797,7 @@ public class ClassUtil { if (cls == null) { return Collections.emptyList(); } - List> interfaces = new ArrayList<>(); + final List> interfaces = new ArrayList<>(); traverseTypeHierarchy(cls, t -> true, t -> { if (t.isInterface()) { interfaces.add(t); @@ -813,7 +813,7 @@ public class ClassUtil { * @param terminator 对遍历到的每个类与接口执行的校验,若为{@code false}则立刻中断遍历 */ public static void traverseTypeHierarchyWhile( - final Class root, Predicate> terminator) { + final Class root, final Predicate> terminator) { traverseTypeHierarchyWhile(root, t -> true, terminator); } @@ -847,11 +847,11 @@ public class ClassUtil { * @param includeRoot 是否包括根类 */ public static void traverseTypeHierarchy( - final Class root, final Predicate> filter, final Consumer> consumer, boolean includeRoot) { + final Class root, final Predicate> filter, final Consumer> consumer, final boolean includeRoot) { Objects.requireNonNull(root); Objects.requireNonNull(filter); Objects.requireNonNull(consumer); - Function, Collection>> function = t -> { + final Function, Collection>> function = t -> { if (includeRoot || !root.equals(t)) { consumer.accept(t); } @@ -860,9 +860,9 @@ public class ClassUtil { HierarchyUtil.traverseByBreadthFirst(root, filter, HierarchyIteratorUtil.scan(function)); } - private static Set> getNextTypeHierarchies(Class t) { - Set> next = new LinkedHashSet<>(); - Class superclass = t.getSuperclass(); + private static Set> getNextTypeHierarchies(final Class t) { + final Set> next = new LinkedHashSet<>(); + final Class superclass = t.getSuperclass(); if (Objects.nonNull(superclass)) { next.add(superclass); } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/reflect/method/MethodScannerTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/reflect/method/MethodScannerTest.java index 1a0229ef2..6282a4d02 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/reflect/method/MethodScannerTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/reflect/method/MethodScannerTest.java @@ -1,6 +1,7 @@ package org.dromara.hutool.core.reflect.method; import lombok.SneakyThrows; +import org.dromara.hutool.core.annotation.Alias; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -23,27 +24,27 @@ class MethodScannerTest { @Test void testGetMethods() { Assertions.assertEquals(0, MethodScanner.getMethods(null).length); - Method[] actual = MethodScanner.getMethods(Child.class); + final Method[] actual = MethodScanner.getMethods(Child.class); Assertions.assertSame(actual, MethodScanner.getMethods(Child.class)); - Method[] expected = Child.class.getMethods(); + final Method[] expected = Child.class.getMethods(); Assertions.assertArrayEquals(expected, actual); } @Test void testGetDeclaredMethods() { Assertions.assertEquals(0, MethodScanner.getDeclaredMethods(null).length); - Method[] actual = MethodScanner.getDeclaredMethods(Child.class); + final Method[] actual = MethodScanner.getDeclaredMethods(Child.class); Assertions.assertSame(actual, MethodScanner.getDeclaredMethods(Child.class)); - Method[] expected = Child.class.getDeclaredMethods(); + final Method[] expected = Child.class.getDeclaredMethods(); Assertions.assertArrayEquals(expected, actual); } @Test void testGetAllMethods() { Assertions.assertEquals(0, MethodScanner.getAllMethods(null).length); - Method[] actual = MethodScanner.getAllMethods(Child.class); + final Method[] actual = MethodScanner.getAllMethods(Child.class); // get declared method from child、parent、grandparent - Method[] expected = Stream.of(Child.class, Parent.class, Interface.class, Object.class) + final Method[] expected = Stream.of(Child.class, Parent.class, Interface.class, Object.class) .flatMap(c -> Stream.of(MethodScanner.getDeclaredMethods(c))) .toArray(Method[]::new); Assertions.assertArrayEquals(expected, actual); @@ -51,9 +52,9 @@ class MethodScannerTest { @Test void testClearCaches() { - Method[] declaredMethods = MethodScanner.getDeclaredMethods(Child.class); + final Method[] declaredMethods = MethodScanner.getDeclaredMethods(Child.class); Assertions.assertSame(declaredMethods, MethodScanner.getDeclaredMethods(Child.class)); - Method[] methods = MethodScanner.getMethods(Child.class); + final Method[] methods = MethodScanner.getMethods(Child.class); Assertions.assertSame(methods, MethodScanner.getMethods(Child.class)); // clear method cache @@ -66,28 +67,28 @@ class MethodScannerTest { @Test void testFindWithMetadataFromSpecificMethods() { Assertions.assertTrue(MethodScanner.findWithMetadataFromSpecificMethods(null, m -> m.getAnnotation(Annotation.class)).isEmpty()); - Method[] methods = MethodScanner.getMethods(Child.class); - Map actual = MethodScanner.findWithMetadataFromSpecificMethods(methods, m -> m.getAnnotation(Annotation.class)); + final Method[] methods = MethodScanner.getMethods(Child.class); + final Map actual = MethodScanner.findWithMetadataFromSpecificMethods(methods, m -> m.getAnnotation(Annotation.class)); Assertions.assertEquals(1, actual.size()); // check method - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertTrue(actual.containsKey(expectedMethod)); // check annotation - Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); + final Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); Assertions.assertEquals(expectedAnnotation, actual.get(expectedMethod)); } @SneakyThrows @Test void testFindFromSpecificMethods() { - Method[] methods = MethodScanner.getMethods(Child.class); - Set actual = MethodScanner.findFromSpecificMethods(methods, m -> m.getAnnotation(Annotation.class)); + final Method[] methods = MethodScanner.getMethods(Child.class); + final Set actual = MethodScanner.findFromSpecificMethods(methods, m -> m.getAnnotation(Annotation.class)); Assertions.assertEquals(1, actual.size()); // check method - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertTrue(actual.contains(expectedMethod)); } @@ -95,10 +96,10 @@ class MethodScannerTest { @Test void testGetWithMetadataFromSpecificMethods() { // find first oneArgMethod method - Method[] methods = MethodScanner.getMethods(Child.class); - Map.Entry actual = MethodScanner.getWithMetadataFromSpecificMethods(methods, MethodMatcherUtil.forName("oneArgMethod")); + final Method[] methods = MethodScanner.getMethods(Child.class); + final Map.Entry actual = MethodScanner.getWithMetadataFromSpecificMethods(methods, MethodMatcherUtil.forName("oneArgMethod")); Assertions.assertNotNull(actual); - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertEquals(expectedMethod, actual.getKey()); Assertions.assertTrue(actual.getValue()); } @@ -107,145 +108,145 @@ class MethodScannerTest { @Test void testGetFromSpecificMethods() { // find first oneArgMethod method - Method[] methods = MethodScanner.getMethods(Child.class); - Method actual = MethodScanner.getFromSpecificMethods(methods, MethodMatcherUtil.forName("oneArgMethod")); - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method[] methods = MethodScanner.getMethods(Child.class); + final Method actual = MethodScanner.getFromSpecificMethods(methods, MethodMatcherUtil.forName("oneArgMethod")); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertEquals(expectedMethod, actual); } @SneakyThrows @Test void testFindWithMetadataFromMethods() { - Map actual = MethodScanner.findWithMetadataFromMethods(Child.class, m -> m.getAnnotation(Annotation.class)); + final Map actual = MethodScanner.findWithMetadataFromMethods(Child.class, m -> m.getAnnotation(Annotation.class)); Assertions.assertEquals(1, actual.size()); // check method - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertTrue(actual.containsKey(expectedMethod)); // check annotation - Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); + final Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); Assertions.assertEquals(expectedAnnotation, actual.get(expectedMethod)); } @SneakyThrows @Test void testFindFromMethods() { - Set actual = MethodScanner.findFromMethods(Child.class, m -> m.getAnnotation(Annotation.class)); + final Set actual = MethodScanner.findFromMethods(Child.class, m -> m.getAnnotation(Annotation.class)); Assertions.assertEquals(1, actual.size()); // check method - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertTrue(actual.contains(expectedMethod)); } @SneakyThrows @Test void testGetWithMetadataFromMethods() { - Map.Entry actual = MethodScanner.getWithMetadataFromMethods(Child.class, m -> m.getAnnotation(Annotation.class)); + final Map.Entry actual = MethodScanner.getWithMetadataFromMethods(Child.class, m -> m.getAnnotation(Annotation.class)); Assertions.assertNotNull(actual); - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertEquals(expectedMethod, actual.getKey()); - Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); + final Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); Assertions.assertEquals(expectedAnnotation, actual.getValue()); } @SneakyThrows @Test void testGetFromMethods() { - Method actual = MethodScanner.getFromMethods(Child.class, m -> m.getAnnotation(Annotation.class)); - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method actual = MethodScanner.getFromMethods(Child.class, m -> m.getAnnotation(Annotation.class)); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertEquals(expectedMethod, actual); } @SneakyThrows @Test void testFindWithMetadataFromDeclaredMethods() { - Map actual = MethodScanner.findWithMetadataFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class)); + final Map actual = MethodScanner.findWithMetadataFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class)); Assertions.assertEquals(1, actual.size()); // check method - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertTrue(actual.containsKey(expectedMethod)); // check annotation - Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); + final Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); Assertions.assertEquals(expectedAnnotation, actual.get(expectedMethod)); } @SneakyThrows @Test void testFindFromDeclaredMethods() { - Set actual = MethodScanner.findFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class)); + final Set actual = MethodScanner.findFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class)); Assertions.assertEquals(1, actual.size()); // check method - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertTrue(actual.contains(expectedMethod)); } @SneakyThrows @Test void testGetWithMetadataFromDeclaredMethods() { - Map.Entry actual = MethodScanner.getWithMetadataFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class)); + final Map.Entry actual = MethodScanner.getWithMetadataFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class)); Assertions.assertNotNull(actual); - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertEquals(expectedMethod, actual.getKey()); - Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); + final Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); Assertions.assertEquals(expectedAnnotation, actual.getValue()); } @SneakyThrows @Test void testGetFromDeclaredMethods() { - Method actual = MethodScanner.getFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class)); - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method actual = MethodScanner.getFromDeclaredMethods(Parent.class, m -> m.getAnnotation(Annotation.class)); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertEquals(expectedMethod, actual); } @SneakyThrows @Test void testFindWithMetadataFromAllMethods() { - Map actual = MethodScanner.findWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class)); + final Map actual = MethodScanner.findWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class)); Assertions.assertEquals(1, actual.size()); // check method - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertTrue(actual.containsKey(expectedMethod)); // check annotation - Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); + final Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); Assertions.assertEquals(expectedAnnotation, actual.get(expectedMethod)); } @SneakyThrows @Test void testFindFromAllMethods() { - Set actual = MethodScanner.findFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class)); + final Set actual = MethodScanner.findFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class)); Assertions.assertEquals(1, actual.size()); // check method - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertTrue(actual.contains(expectedMethod)); } @SneakyThrows @Test void testGetWithMetadataFromAllMethods() { - Assertions.assertNull(MethodScanner.getWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Deprecated.class))); - Map.Entry actual = MethodScanner.getWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class)); + Assertions.assertNull(MethodScanner.getWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Alias.class))); + final Map.Entry actual = MethodScanner.getWithMetadataFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class)); Assertions.assertNotNull(actual); - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertEquals(expectedMethod, actual.getKey()); - Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); + final Annotation expectedAnnotation = expectedMethod.getAnnotation(Annotation.class); Assertions.assertEquals(expectedAnnotation, actual.getValue()); } @SneakyThrows @Test void testGetFromAllMethods() { - Method actual = MethodScanner.getFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class)); - Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); + final Method actual = MethodScanner.getFromAllMethods(Child.class, m -> m.getAnnotation(Annotation.class)); + final Method expectedMethod = Parent.class.getDeclaredMethod("oneArgMethod", String.class); Assertions.assertEquals(expectedMethod, actual); } @@ -270,7 +271,7 @@ class MethodScannerTest { @Annotation("oneArgMethod") @Override - public void oneArgMethod(String arg) { } + public void oneArgMethod(final String arg) { } } public static class Child extends Parent implements Interface {