Can you find all classes in a package using reflection?

前端 未结 27 2150
不知归路
不知归路 2020-11-21 05:24

Is it possible to find all classes or interfaces in a given package? (Quickly looking at e.g. Package, it would seem like no.)

27条回答
  •  我在风中等你
    2020-11-21 05:46

    I couldn't find a short working snipped for something so simple. So here it is, I made it myself after screwing around for a while:

        Reflections reflections =
            new Reflections(new ConfigurationBuilder()
                    .filterInputsBy(new FilterBuilder().includePackage(packagePath))
                    .setUrls(ClasspathHelper.forPackage(packagePath))
                    .setScanners(new SubTypesScanner(false)));
    
        Set typeList = reflections.getAllTypes(); 
    

提交回复
热议问题