BAEL-2779 Guide to classgraph library

This commit is contained in:
Denis Zhdanov 2019-04-22 08:17:20 +08:00
parent 732a7413f4
commit 541e0cc9d5
9 changed files with 137 additions and 0 deletions

View File

@ -30,6 +30,16 @@
</repositories> </repositories>
<dependencies> <dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>${classgraph.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.jbpm</groupId> <groupId>org.jbpm</groupId>
<artifactId>jbpm-test</artifactId> <artifactId>jbpm-test</artifactId>
@ -38,6 +48,8 @@
</dependencies> </dependencies>
<properties> <properties>
<assertj.version>3.6.2</assertj.version>
<classgraph.version>4.8.22</classgraph.version>
<jbpm.version>6.0.0.Final</jbpm.version> <jbpm.version>6.0.0.Final</jbpm.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,74 @@
package com.baeldung.classgraph;
import io.github.classgraph.*;
import org.junit.Test;
import java.io.IOException;
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);
}
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.classgraph;
@TestAnnotation
public class ClassWithAnnotation {
}

View File

@ -0,0 +1,7 @@
package com.baeldung.classgraph;
public class FieldWithAnnotation {
@TestAnnotation
private String s;
}

View File

@ -0,0 +1,8 @@
package com.baeldung.classgraph;
public class MethodWithAnnotation {
@TestAnnotation
public void service() {
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.classgraph;
public class MethodWithAnnotationParameterDao {
@TestAnnotation("dao")
public void service() {
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.classgraph;
public class MethodWithAnnotationParameterWeb {
@TestAnnotation("web")
public void service() {
}
}

View File

@ -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 "";
}

View File

@ -0,0 +1 @@
my data