Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Aug 22, 2024
1 parent 3d0332b commit 5d39116
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public class BeanMethodUtil {
}


public static Method[] scanSimpleMethod(final Class<?> clazz, boolean allowInit) {
public static Method[] scanSimpleMethod(final Class<?> clazz, boolean allowInit) throws IOException {
return scanSimpleMethod(clazz, "", allowInit);
}

public static Method[] scanSimpleMethod(final Class<?> clazz, String packageName, boolean allowInit) {
public static Method[] scanSimpleMethod(final Class<?> clazz, String packageName, boolean allowInit) throws IOException {

Map<String, Method> foundMethods = new HashMap<>();
Set<Class<?>> methodDeclaredClasses = new HashSet<>();
Expand Down Expand Up @@ -130,8 +130,7 @@ private static boolean isSimpleMethod(String ownerNameDesc, Map<String, LiTuple<
}


private static void visitMethod(Class<?> methodDeclaredClass, Map<String, Method> methodMap, Map<String, LiTuple<Method, MethodNode>> methodVisitors) {
try {
private static void visitMethod(Class<?> methodDeclaredClass, Map<String, Method> methodMap, Map<String, LiTuple<Method, MethodNode>> methodVisitors) throws IOException {
ClassReader classReader = new ClassReader(methodDeclaredClass.getName());
ClassVisitor classVisitor = new ClassVisitor(ASM9) {
@Override
Expand All @@ -149,8 +148,5 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
};
classReader.accept(classVisitor, ClassReader.SKIP_DEBUG);

} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

Expand Down Expand Up @@ -42,11 +43,11 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex

List<TestTemplateInvocationContext> invocationContexts = new ArrayList<>();
// 忽略枚举类
new ClassScanner(scanPackage, cls -> {
Predicate<Class<?>> classPredicate = cls -> {
if (cls.isEnum()) {
return false;
}
// 跳过一些类
// 类名的一部分满足正则,则跳过
if (!cls.getName().equals(cls.getName().replaceAll(skipRegex, ""))) {
return false;
}
Expand All @@ -55,7 +56,12 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex


// 无简单方法的直接跳过
Method[] methods = BeanMethodUtil.scanSimpleMethod(cls, scanPackage, allowInit);
Method[] methods;
try {
methods = BeanMethodUtil.scanSimpleMethod(cls, scanPackage, allowInit);
} catch (Exception ignore) {
return false;
}
if (methods.length == 0) {
return false;
}
Expand All @@ -73,8 +79,8 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
}
return true;

}).scan();

};
new ClassScanner(scanPackage, classPredicate).scan();
return invocationContexts.stream();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

class BeanMethodUtilTest {


@Test
void test() {
void test() throws IOException {
Method[] methods = BeanMethodUtil.scanSimpleMethod(Person.class, false);
Person person = new Person();
for (Method method : methods) {
Expand Down

0 comments on commit 5d39116

Please sign in to comment.