【问题标题】:Is this a valid use of the java ClassGraph api?这是对 java ClassGraph api 的有效使用吗?
【发布时间】:2020-02-25 04:30:14
【问题描述】:

使用编译'io.github.classgraph:classgraph:4.8.65'

https://github.com/classgraph/classgraph/wiki/ClassGraph-API

Java 8

ScanResult scanResult = 
    new ClassGraph().enableAllInfo()
                    .whitelistPackages("abc1")
                    .whitelistPackages("abc2")
                    .whitelistPackages("java")
                    .scan();

当我遇到包 abc1 或 abc2 中的类的 ClassInfo 对象时,它们能够引用 java.util.HashMap 之类的东西,我在 FieldInfo 中看到它们。

但是当我继续执行 scanResult.getClassInfo("java.util.HashMap") 时,它返回 null。 (按照 abc1 或 abc2 包中其他类的 FieldInfos 确实会返回更多 ClassInfo 对象)

我的问题是,认为我可以通过上面所示的 ClassGraph 方法链接将 ClassInfo 对象获取到 java jre 类是否正确?

添加了这个失败的测试,令人惊讶的是它只打印一个类而不是预期的几十个:

package abc;

import io.github.classgraph.ScanResult;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;

import java.util.*;
import java.io.*;

import java.util.function.*;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig
@SpringBootTest(classes = {})
public class ExamplesSpec {

        @org.junit.jupiter.api.Test
        @org.junit.jupiter.api.DisplayName(value="test_for_built_in_java_jre_classes")
        public void test_on_line_42() throws Exception {

            System.out.println("test_for_built_in_java_jre_classes");
            ClassInfo found = null;
            try (
                ScanResult result = new ClassGraph().enableAllInfo().whitelistPackages("java.util").scan()
            ) {
                System.out.println("here all the classes....");
                for( ClassInfo item : result.getAllClasses()) {
                    System.out.println("here classinfo: " + item);
                }
                found = result.getClassInfo("java.util.HashMap");
            }
            assert found != null;
        }
}

找到的唯一类是这样的:

这里的classinfo:公共类java.util.zip.VFSZipFile实现java.util.zip.ZipConstants

【问题讨论】:

    标签: java classgraph


    【解决方案1】:

    找到答案了!

    在 ClassGraph 的设置中,为了扫描 jre 提供的类,您需要将其添加到方法链中:

    .enableSystemJarsAndModules()

    例如:

        new ClassGraph().enableAllInfo()
                        .whitelistPackages("abc1")
                        .whitelistPackages("abc2")
                        .whitelistPackages("java")
                        .enableSystemJarsAndModules()
                        .scan();
    

    这在此处找到的文档中有详细说明:

    https://github.com/classgraph/classgraph/wiki/API:-ClassGraph-Constructor#configuring-the-classgraph-instance

    【讨论】:

      猜你喜欢
      • 2011-03-07
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      相关资源
      最近更新 更多