24 Mart 2023 Cuma

ClassGraph Kullanımı

Giriş
Şu satırı dahil ederiz
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
Açıklaması şöyle
The cool thing is that Classgraph is fast, as it works on the byte-code level, meaning the inspected classes are not loaded to the JVM, and it doesn't use reflection for processing.
Açıklaması şöyle
ClassGraph has the ability to "invert" the Java class and/or reflection API, or has the ability to index classes and resources. For example, the Java class and reflection API can tell you the superclass of a given class, or the interfaces implemented by a given class, or can give you the list of annotations on a class; ClassGraph can find all classes that extend a given class (all subclasses of a given class), or all classes that implement a given interface, or all classes that are annotated with a given annotation. The Java API can load the content of a resource file with a specific path in a specific ClassLoader, but ClassGraph can find and load all resources in all classloaders with paths matching a given pattern.
Açıklaması şöyle
ClassGraph is a Java library that can be used to scan the classpath and/or module path for classes, annotations, and resources. It is a very fast and lightweight library, and it can be used for a variety of tasks, such as:

- Finding all classes with a particular annotation.
- Finding all classes that extend a specific class.
- Finding all resources that match a specific pattern.
- Creating a graph of the class dependencies.

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>io.github.classgraph</groupId>
  <artifactId>classgraph</artifactId>
  <version>4.8.157</version>
</dependency>
ClassGraph Sınıfı
enableAllInfo
enableAnnotationInfo
enableClassInfo()
ignoreClassVisibility
whitelistPackages
whitelistPaths
gibi ayarlar atandıktan sonra scan() metodu çağrılır. scan() sonucunda bir ScanResult nesnsi elde edilir


ScanResult Sınıfı
Tüm marifet bu sınıfta. Belirtilen paket, dizin ve anotasyonları arayan metodlar sunar

getClassesImplementing metodu
Bir arayüzü gerçekleştiren tüm sınıfları bulur
Örnek
Şöyle yaparız
List<String> classNames = new ClassGraph()
  .enableClassInfo()
  .scan()
  .getClassesImplementing(FooInterface.class)
  .stream()
  .map(ClassInfo::getName)
  .collect(Collectors.toList());
getAllClasses metodu
ClassInfo listesi döner
Örnek
Şöyle yaparız. Burada belirtilen paket veya dizindeki sınıflar listeleniyor.
ClassGraph classGraph = new ClassGraph()
  .whitelistPackages("com.hazelcast.jet.config")
  .whitelistPaths("com/hazelcast/jet/config")
  .ignoreClassVisibility();
try (ScanResult scanResult = classGraph.scan()) {
  scanResult.getAllClasses().stream().map(classInfo -> ...)
  ...
}
getClassesWithAnnotation metodu
Örnek
Belirtilen paketteki tüm sınıfları tarayıp, belirli bir anotasyona sahip sınıfları bulmak için şöyle yaparız
try (ScanResult result = new ClassGraph().enableClassInfo().enableAnnotationInfo()
  .whitelistPackages(...).scan()) {
    
  ClassInfoList classInfos = result
    .getClassesWithAnnotation(FooAnnotation.class.getName());
    
  assertThat(classInfos).extracting(ClassInfo::getName)
    .contains(FooWithAnnotation.class.getName());
}
getClassesWithFieldAnnotation metodu
Örnek
Belirtilen paketteki tüm sınıfları tarayıp, belirli bir anotasyona sahip üye alanları bulmak için şöyle yaparız
try (ScanResult result = new ClassGraph().enableAllInfo()
  .whitelistPackages(...).scan()) {
    
  ClassInfoList classInfos = result
    .getClassesWithFieldAnnotation(FooAnnotation.class.getName());
    
  assertThat(classInfos).extracting(ClassInfo::getName)
    .contains(FooWithAnnotation.class.getName());
}
getClassesWithMethodAnnotation metodu
Örnek
Belirtilen paketteki tüm sınıfları tarayıp, belirli bir anotasyona sahip metodları bulmak için şöyle yaparız
try (ScanResult result = new ClassGraph().enableAllInfo()
  .whitelistPackages(...).scan()) {
    
  ClassInfoList classInfos = result
    .getClassesWithMethodAnnotation(FooAnnotation.class.getName());
    
  assertThat(classInfos).extracting(ClassInfo::getName)
    .contains(FooWithAnnotation.class.getName());
}
getAllResources metodu
Örnek
Şöyle yaparız
ClassGraph classGraph = new ClassGraph()
  .whitelistPackages("com.hazelcast.jet.config")
  .whitelistPaths("com/hazelcast/jet/config")
  .ignoreClassVisibility();
try (ScanResult scanResult = classGraph.scan()) {
  ...
  //file:/hazelcast/target/test-classes/com/hazelcast/jet/config/package.properties
  Collection<URL> nonClasses = scanResult.getAllResources().nonClassFilesOnly().getURLs();
  ...
}
getResourcesWithExtension metodu
Örnek
src/test/resources/classgraph/my.config dosyası yaratalım ve içeriği my data olsun
şöyle yaparız
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");
}

Hiç yorum yok:

Yorum Gönder