import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotation {
String value() default "";
}
public class MyClass {
@CustomAnnotation("This method does something")
public void myMethod() {
// Code implementation
}
}
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfoList;
import io.github.classgraph.ScanResult;
public class AnnotationDetector {
public static void main(String[] args) {
try (ScanResult scanResult = new ClassGraph()
.enableAllInfo()
.whitelistPackages("com.example")
.scan()) {
ClassInfoList classInfoList = scanResult.getClassesWithAnnotation(CustomAnnotation.class.getName());
for (String className : classInfoList.getNames()) {
System.out.println("Class with CustomAnnotation: " + className);
}
}
}
}
<dependencies>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.88</version>
</dependency>
</dependencies>
groovy
dependencies {
implementation 'io.github.classgraph:classgraph:4.8.88'
}