Last active
January 23, 2021 11:40
-
-
Save raphw/91c81b8afdfd76ccfd87508a0af0e8bb to your computer and use it in GitHub Desktop.
A Java agent for fixing exports for an app that is not yet Java 9 aware.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.instrument.Instrumentation; | |
import java.lang.reflect.Layer; | |
import java.lang.reflect.Module; | |
import java.util.*; | |
public class WeakeningAgent { | |
public static void premain(String argument, Instrumentation instrumentation) { | |
boolean full = argument != null && argument.equals("full"); | |
Set<Module> importing = new HashSet<>(), exporting = new HashSet<>(); | |
Layer.boot().modules().forEach(module -> { | |
if (module.getDescriptor().isAutomatic()) { | |
importing.add(module); | |
} else if (!module.getDescriptor().isWeak()) { | |
exporting.add(module); | |
} | |
}); | |
for (Module module : exporting) { | |
Map<String, Set<Module>> exports = new HashMap<>(); | |
for (String pkg : module.getPackages()) { | |
exports.put(pkg, importing); | |
} | |
instrumentation.redefineModule(module, | |
Collections.emptySet(), | |
full ? exports : Collections.emptyMap(), | |
exports, | |
Collections.emptySet(), | |
Collections.emptyMap()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment