Reflections: `reflections.get(SubTypes.of(…).asClass())` does not work when resources are from an external jar

I’m currently running Spring Boot 3.2.1 with Java 17, with Gradle 8.4 as a build tool and using Reflections 0.10.2 (latest version as of writing). Our project is a multi-module project, but for simplicity, assume we have 2 modules, with one module being the main, bootable module (boot jar) while the other module is something like a plugin and is not bootable. Gradle will end up building 2 different JARs, one being a Spring Boot bootable jar while the other will be a plain non-bootable jar.

We shall call the main bootable module as module A while the non-bootable module as module B.

Background

In module A, there’s an interface called IFoo. Module B will use IFoo to all the classes within, hence all given classes in module B have the same implemented methods. All given classes in module B are inside the same package, say xx.yy.zz. Module A will then scan module B for these classes via Reflections and attempt to execute them by looking at the same methods per IFoo.

Before that, module A and B were once a single module. While being a single module, creating a ConfigurationBuilder and targeting the package via forPackage() were done easily and Reflections were able to obtain all the classes successfully, as such:

var testPackage = "xx.yy.zz";
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
                      .forPackage(testPackage)
                      .filterInputsBy(s -> s.contains(testPackage));
Reflections reflections = new Reflections(configurationBuilder);
Set<Class<?>> subTypes = reflections.get(SubTypes.of(IFoo.class).asClass());

Imagine if under xx.yy.zz, there’s FooA and FooB, both implementing IFoo, the subTypes set will contain both these classes.

We then decided to split them out for modularity AS A REQUIREMENT (and hence ending up with 2 different jars as stated in the preface).

After splitting module A and B, the dependency between A and B can be described as one-way dependency, such that B is dependent on A because B imports IFoo from A to implement it in its classes under xx.yy.zz. For clarification, note that module A owns IFoo and module B doesn’t (as B merely imports it from A). A is NOT dependent on B simply because A does not use B, but if module B (as a plain jar) is present, A will fire up Reflections to scan the classes from module B’s jar, as such:

ClassLoader fooLoader = IFoo.class.getClassLoader();
URL url = new URL("jar:file:/<path to our plain plugin jar file>.jar!/";
URLClassLoader pluginLoader = URLClassLoader.newInstance(new URL[]{url}, fooLoader) // fooLoader as parent loader so that pluginLoader is aware of IFoo;
// test
var bool = scriptClassLoader.loadClass(IFoo.class.getName()).equals(IFoo.class); // returns true

… with some changes over in ConfigurationBuilder to add the plugin’s class loader:

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
                      .forPackage(testPackage, pluginLoader)
                      .filterInputsBy(s -> s.contains(testPackage));

… and calls the same subTypes = reflections.get(SubTypes.of(IFoo.class).asClass()) to obtain the resources.

The problem

If we call reflections.get(SubTypes.of(IFoo.class)), it is able to obtain FooA and FooB (.get() will return a set of String as .asClass() is currently not called) successfully. However, reflections.get(SubTypes.of(IFoo.class).asClass()) does not return anything at all. When module A and B were once a single module, this line would succeed but now when A and B are both different modules, it no longer works.

The class loader pluginLoader is fully aware of IFoo as its parent is fooLoader as per the code snippet above, hence the line scriptClassLoader.loadClass(IFoo.class.getName()).equals(IFoo.class) will return true. But regardless, asClass() will not work properly, seemingly not returning anything at all (subTypes will have a size of 0).

Help is much appreciated. Thanks in advance.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật