Skip to content

Instantly share code, notes, and snippets.

@bric3
Forked from raphw/WeakeningAgent.java
Created October 1, 2016 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bric3/8f810997e1f6a4253a33b42c419b1524 to your computer and use it in GitHub Desktop.
Save bric3/8f810997e1f6a4253a33b42c419b1524 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.
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