Skip to content

Commit

Permalink
Add system properties to bypass ServiceLoader mixin service discovery.
Browse files Browse the repository at this point in the history
Workaround for SpongePowered#569
  • Loading branch information
sfPlayer1 committed Apr 1, 2022
1 parent 59d5af0 commit f39ae48
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/org/spongepowered/asm/service/MixinService.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ private MixinService() {
}

private void runBootServices() {
// bypass service loader if the mixin.bootstrapService system property yields the desired IMixinServiceBootstrap implementation directly
String serviceCls = System.getProperty("mixin.bootstrapService");

if (serviceCls != null) {
try {
IMixinServiceBootstrap bootService = (IMixinServiceBootstrap) Class.forName(serviceCls).getConstructor().newInstance();
bootService.bootstrap();
bootedServices.add(bootService.getServiceClassName());

return;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

this.bootstrapServiceLoader = ServiceLoader.<IMixinServiceBootstrap>load(IMixinServiceBootstrap.class, this.getClass().getClassLoader());
Iterator<IMixinServiceBootstrap> iter = this.bootstrapServiceLoader.iterator();
while (iter.hasNext()) {
Expand Down Expand Up @@ -200,6 +215,20 @@ private synchronized IMixinService getServiceInstance() {
}

private IMixinService initService() {
// bypass service loader if the mixin.service system property yields the desired IMixinService implementation directly
String serviceCls = System.getProperty("mixin.service"); // FIXME: there is overlap with bootedServices, may just use that directly?

if (serviceCls != null) {
try {
IMixinService service = (IMixinService) Class.forName(serviceCls).getConstructor().newInstance();
if (!service.isValid()) throw new RuntimeException("invalid service "+serviceCls+" configured via system property");

return service;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

this.serviceLoader = ServiceLoader.<IMixinService>load(IMixinService.class, this.getClass().getClassLoader());
Iterator<IMixinService> iter = this.serviceLoader.iterator();
List<String> badServices = new ArrayList<String>();
Expand Down

0 comments on commit f39ae48

Please sign in to comment.