BAEL-2779 Guide to classgraph library
This commit is contained in:
parent
5754bb3310
commit
af4e221fa8
@ -699,6 +699,12 @@
|
|||||||
<version>${reflections.version}</version>
|
<version>${reflections.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.github.classgraph</groupId>
|
||||||
|
<artifactId>classgraph</artifactId>
|
||||||
|
<version>${classgraph.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
@ -916,6 +922,7 @@
|
|||||||
<mockftpserver.version>2.7.1</mockftpserver.version>
|
<mockftpserver.version>2.7.1</mockftpserver.version>
|
||||||
<commons-net.version>3.6</commons-net.version>
|
<commons-net.version>3.6</commons-net.version>
|
||||||
<reflections.version>0.9.11</reflections.version>
|
<reflections.version>0.9.11</reflections.version>
|
||||||
|
<classgraph.version>4.8.22</classgraph.version>
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
@ -0,0 +1,78 @@
|
|||||||
|
package com.baeldung.classgraph;
|
||||||
|
|
||||||
|
import io.github.classgraph.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class ClassGraphUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenClassAnnotationFilterIsDefined_thenTargetClassesCanBeFound() {
|
||||||
|
doTest(result -> {
|
||||||
|
ClassInfoList classInfos = result.getClassesWithAnnotation(TestAnnotation.class.getName());
|
||||||
|
assertThat(classInfos).extracting(ClassInfo::getName).contains(ClassWithAnnotation.class.getName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMethodAnnotationFilterIsDefined_thenTargetClassesCanBeFound() {
|
||||||
|
doTest(result -> {
|
||||||
|
ClassInfoList classInfos = result.getClassesWithMethodAnnotation(TestAnnotation.class.getName());
|
||||||
|
assertThat(classInfos).extracting(ClassInfo::getName).contains(MethodWithAnnotation.class.getName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMethodAnnotationValueFilterIsDefined_thenTargetClassesCanBeFound() {
|
||||||
|
doTest(result -> {
|
||||||
|
ClassInfoList classInfos = result.getClassesWithMethodAnnotation(TestAnnotation.class.getName());
|
||||||
|
ClassInfoList filteredClassInfos = classInfos.filter(classInfo -> {
|
||||||
|
return classInfo.getMethodInfo().stream().anyMatch(methodInfo -> {
|
||||||
|
AnnotationInfo annotationInfo = methodInfo.getAnnotationInfo(TestAnnotation.class.getName());
|
||||||
|
if (annotationInfo == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return "web".equals(annotationInfo.getParameterValues().getValue("value"));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
assertThat(filteredClassInfos)
|
||||||
|
.extracting(ClassInfo::getName)
|
||||||
|
.contains(MethodWithAnnotationParameterWeb.class.getName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenFieldAnnotationFilterIsDefined_thenTargetClassesCanBeFound() {
|
||||||
|
doTest(result -> {
|
||||||
|
ClassInfoList classInfos = result.getClassesWithFieldAnnotation(TestAnnotation.class.getName());
|
||||||
|
assertThat(classInfos).extracting(ClassInfo::getName).contains(FieldWithAnnotation.class.getName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenResourceIsUsed_thenItCanBeFoundAndLoaded() throws IOException {
|
||||||
|
try (ScanResult result = new ClassGraph().whitelistPaths("classgraph").scan()) {
|
||||||
|
ResourceList resources = result.getResourcesWithExtension("config");
|
||||||
|
assertThat(resources).extracting(Resource::getPath).containsOnly("classgraph/my.config");
|
||||||
|
assertThat(resources.get(0).getContentAsString()).isEqualTo("my data");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doTest(Consumer<ScanResult> checker) {
|
||||||
|
try (ScanResult result = new ClassGraph().enableAllInfo()
|
||||||
|
.whitelistPackages(getClass().getPackage().getName())
|
||||||
|
.scan())
|
||||||
|
{
|
||||||
|
checker.accept(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.classgraph;
|
||||||
|
|
||||||
|
@TestAnnotation
|
||||||
|
public class ClassWithAnnotation {
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.classgraph;
|
||||||
|
|
||||||
|
public class FieldWithAnnotation {
|
||||||
|
|
||||||
|
@TestAnnotation
|
||||||
|
private String s;
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.classgraph;
|
||||||
|
|
||||||
|
public class MethodWithAnnotation {
|
||||||
|
|
||||||
|
@TestAnnotation
|
||||||
|
public void service() {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.classgraph;
|
||||||
|
|
||||||
|
public class MethodWithAnnotationParameterDao {
|
||||||
|
|
||||||
|
@TestAnnotation("dao")
|
||||||
|
public void service() {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.classgraph;
|
||||||
|
|
||||||
|
public class MethodWithAnnotationParameterWeb {
|
||||||
|
|
||||||
|
@TestAnnotation("web")
|
||||||
|
public void service() {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.classgraph;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
|
||||||
|
@Target({TYPE, METHOD, FIELD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface TestAnnotation {
|
||||||
|
|
||||||
|
String value() default "";
|
||||||
|
}
|
1
libraries/src/test/resources/classgraph/my.config
Normal file
1
libraries/src/test/resources/classgraph/my.config
Normal file
@ -0,0 +1 @@
|
|||||||
|
my data
|
Loading…
x
Reference in New Issue
Block a user