|
| 1 | +package com.github.daggerok; |
| 2 | + |
| 3 | +import io.vavr.control.Try; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import lombok.val; |
| 6 | +import org.junit.jupiter.api.DisplayName; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import javax.tools.FileObject; |
| 10 | +import javax.tools.JavaFileObject; |
| 11 | +import javax.tools.StandardLocation; |
| 12 | +import javax.tools.ToolProvider; |
| 13 | +import java.io.File; |
| 14 | +import java.nio.charset.Charset; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.util.*; |
| 17 | +import java.util.function.Function; |
| 18 | +import java.util.function.Supplier; |
| 19 | +import java.util.stream.Collectors; |
| 20 | +import java.util.stream.StreamSupport; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +@DisplayName("java plain reflection tests") |
| 26 | +class JavaReflectionPlainTest { |
| 27 | + |
| 28 | + private static final Function<Throwable, RuntimeException> asRuntimeException = throwable -> { |
| 29 | + log.error(throwable.getLocalizedMessage()); |
| 30 | + return new RuntimeException(throwable); |
| 31 | + }; |
| 32 | + |
| 33 | + /*private static final Supplier<RuntimeException> andThenJustDie = () -> { |
| 34 | + log.error("just dei..."); |
| 35 | + return new RuntimeException(); |
| 36 | + };*/ |
| 37 | + |
| 38 | + private static final Function<String, Collection<Class<?>>> findAllPackageClasses = basePackageName -> { |
| 39 | + |
| 40 | + Locale locale = Locale.getDefault(); |
| 41 | + Charset charset = StandardCharsets.UTF_8; |
| 42 | + val fileManager = ToolProvider.getSystemJavaCompiler() |
| 43 | + .getStandardFileManager(/* diagnosticListener */ null, locale, charset); |
| 44 | + |
| 45 | + StandardLocation location = StandardLocation.CLASS_PATH; |
| 46 | + JavaFileObject.Kind kind = JavaFileObject.Kind.CLASS; |
| 47 | + Set<JavaFileObject.Kind> kinds = Collections.singleton(kind); |
| 48 | + val javaFileObjects = Try.of(() -> fileManager.list(location, basePackageName, kinds, /* recurse */ true)) |
| 49 | + .getOrElseThrow(asRuntimeException); |
| 50 | + |
| 51 | + String pathToPackageAndClass = basePackageName.replace(".", File.separator); |
| 52 | + Function<String, String> mapToClassName = s -> { |
| 53 | + String prefix = Arrays.stream(s.split(pathToPackageAndClass)) |
| 54 | + .findFirst() |
| 55 | + .orElse(""); |
| 56 | + //.orElseThrow(andThenJustDie); |
| 57 | + return s.replaceFirst(prefix, "") |
| 58 | + .replaceAll(File.separator, "."); |
| 59 | + }; |
| 60 | + |
| 61 | + return StreamSupport.stream(javaFileObjects.spliterator(), /* parallel */ true) |
| 62 | + .filter(javaFileObject -> javaFileObject.getKind().equals(kind)) |
| 63 | + .map(FileObject::getName) |
| 64 | + .map(fileObjectName -> fileObjectName.replace(".class", "")) |
| 65 | + .map(mapToClassName) |
| 66 | + .map(className -> Try.of(() -> Class.forName(className)) |
| 67 | + .getOrElseThrow(asRuntimeException)) |
| 68 | + .collect(Collectors.toList()); |
| 69 | + }; |
| 70 | + |
| 71 | + @Test |
| 72 | + void find_class_annotation() { |
| 73 | + Collection<Class<?>> classes = findAllPackageClasses.apply(getClass().getPackage().getName()); |
| 74 | + assertThat(classes).hasSizeGreaterThan(4); |
| 75 | + classes.stream().map(String::valueOf).forEach(log::info); |
| 76 | + } |
| 77 | +} |
0 commit comments