Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A Java agent for fixing exports for an app that is not yet Java 9 aware.
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