Skip to content

Commit 4a5e543

Browse files
committed
Implement plain java reflections recurcive classes search in specified package name.
1 parent 107d5d9 commit 4a5e543

File tree

6 files changed

+105
-15
lines changed

6 files changed

+105
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

src/test/java/com/github/daggerok/JupiterTest.java

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.daggerok.plain;
2+
3+
import com.github.daggerok.plain.annotations.MyAggregate;
4+
5+
@MyAggregate
6+
public class MyRootAggregate {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.daggerok.plain.aggregates;
2+
3+
import com.github.daggerok.plain.annotations.MyAggregate;
4+
5+
@MyAggregate
6+
public class MySiblingAggregate {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.daggerok.plain.aggregates.child;
2+
3+
import com.github.daggerok.plain.annotations.MyAggregate;
4+
5+
@MyAggregate
6+
public class MyChildAggregate {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.daggerok.plain.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target(ElementType.TYPE)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface MyAggregate {}

0 commit comments

Comments
 (0)