Skip to content

Instantly share code, notes, and snippets.

@raphw
Last active January 23, 2021 11:40
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raphw/91c81b8afdfd76ccfd87508a0af0e8bb to your computer and use it in GitHub Desktop.
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.
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