Java indexer: add flag to load plugins registered in a .jar file
Reviewers: zarko, #core_team
Subscribers: #core_team
@@ -34,8 +34,11 @@ import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
+import java.util.ServiceLoader;
/** Binary to run Kythe's Java index over a single .kindex file, emitting entries to STDOUT. */
public class JavaIndexer {
@@ -43,6 +46,19 @@ public class JavaIndexer {
StandaloneConfig config = new StandaloneConfig();
config.parseCommandLine(args);
+ List<Plugin> plugins = new ArrayList<>();
+ if (!Strings.isNullOrEmpty(config.getPlugin())) {
+ URLClassLoader classLoader =
+ new URLClassLoader(new URL[] {new URL("file://" + config.getPlugin())});
+ for (Plugin plugin : ServiceLoader.load(Plugin.class, classLoader)) {
+ System.err.println("Registering plugin: " + plugin.getClass());
+ plugins.add(plugin);
+ }
+ }
+ if (config.getPrintVNames()) {
+ plugins.add(new Plugin.PrintKytheNodes());
+ }
+
MemoryStatisticsCollector statistics = null;
if (config.getPrintStatistics()) {
statistics = new MemoryStatisticsCollector();
@@ -85,9 +101,7 @@ public class JavaIndexer {
new StreamFactEmitter(stream),
statistics == null ? NullStatisticsCollector.getInstance() : statistics,
metadataLoaders);
- if (config.getPrintVNames()) {
- analyzer.registerPlugin(new Plugin.PrintKytheNodes());
- }
+ plugins.forEach(analyzer::registerPlugin);
new JavacAnalysisDriver()
.analyze(analyzer, desc.getCompilationUnit(), new FileDataCache(desc.getFileContents()));
@@ -109,6 +123,12 @@ public class JavaIndexer {
@Parameter(description = "<compilation to analyze>", required = true)
private List<String> compilation = new ArrayList<>();
+ @Parameter(
+ names = "--load_plugin",
+ description = "Load and execute each Kythe Plugin in the given .jar"
+ )
+ private String pluginJar;
+
@Parameter(
names = "--print_vnames",
description = "Print Kythe node VNames associated to each JCTree to stderr"
@@ -137,6 +157,10 @@ public class JavaIndexer {
super("java-indexer");
}
+ public final String getPlugin() {
+ return pluginJar;
+ }
+
public final boolean getPrintVNames() {
return printVNames;
}
