Skip to content

Ghidra is a software reverse engineering (SRE) framework

associate inner class files with their parent java class

Jason P. Leasure99c46911cc6d
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java
index ff00d5a023..c239da4bfa 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java
@@ -82,12 +82,12 @@ public class JavaScriptProvider extends GhidraScriptProvider {
List<ResourceFile> newSource = new ArrayList<>();
List<Path> oldBin = new ArrayList<>();
- BundleHost.visitUpdatedClassFiles(bi.sourceDir, bi.binDir, (sf, bf) -> {
+ BundleHost.visitUpdatedClassFiles(bi.sourceDir, bi.binDir, (sf, bfs) -> {
if (sf != null) {
newSource.add(sf);
}
- if (bf != null) {
- oldBin.add(bf);
+ if (bfs != null) {
+ oldBin.addAll(bfs);
}
});
@@ -299,6 +299,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return null;
}
+ @Deprecated
protected boolean compile(ResourceFile sourceFile, final PrintWriter writer)
throws ClassNotFoundException {
@@ -320,6 +321,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return true;
}
+ @Deprecated
private boolean doCompile(ResourceFile sourceFile, final PrintWriter writer) {
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
@@ -357,6 +359,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return task.call();
}
+ @Deprecated
private List<Class<?>> getParentClasses(ResourceFile scriptSourceFile) {
Class<?> scriptClass = getScriptClass(scriptSourceFile);
@@ -379,6 +382,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return parentClasses;
}
+ @Deprecated
private Class<?> getScriptClass(ResourceFile scriptSourceFile) {
String clazzName = GhidraScriptUtil.getBaseName(scriptSourceFile);
try {
@@ -397,6 +401,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return null;
}
+ @Deprecated
private void compileParentClasses(ResourceFile sourceFile, PrintWriter writer) {
List<Class<?>> parentClasses = getParentClasses(sourceFile);
@@ -441,6 +446,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
}
}
+ @Deprecated
private ResourceFile getSourceFile(Class<?> c) {
// check all script paths for a dir named
String classname = c.getName();
@@ -457,11 +463,13 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return null;
}
+ @Deprecated
private String getSourcePath() {
return GhidraScriptUtil.getScriptSourceDirectories().stream().map(
f -> f.getAbsolutePath()).collect(Collectors.joining(File.pathSeparator));
}
+ @Deprecated
private String getClassPath() {
String scriptBinDirs = GhidraScriptUtil.getScriptBinDirectories().stream().map(
f -> f.getAbsolutePath()).collect(Collectors.joining(File.pathSeparator));
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleCompiler.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleCompiler.java
index e475eed794..4ce39231b4 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleCompiler.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleCompiler.java
@@ -16,15 +16,9 @@
package ghidra.app.script.osgi;
-import java.io.File;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.PrintWriter;
-import java.io.Writer;
+import java.io.*;
import java.nio.charset.Charset;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardCopyOption;
+import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
@@ -32,28 +26,15 @@ import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.Stream;
-import javax.tools.Diagnostic;
-import javax.tools.DiagnosticCollector;
-import javax.tools.JavaCompiler;
-import javax.tools.JavaFileManager;
-import javax.tools.JavaFileObject;
+import javax.tools.*;
import javax.tools.JavaFileObject.Kind;
-import javax.tools.StandardJavaFileManager;
-import javax.tools.ToolProvider;
import org.phidias.compile.BundleJavaManager;
-import aQute.bnd.osgi.Analyzer;
-import aQute.bnd.osgi.Clazz;
+import aQute.bnd.osgi.*;
import aQute.bnd.osgi.Clazz.QUERY;
-import aQute.bnd.osgi.Constants;
-import aQute.bnd.osgi.Instruction;
-import aQute.bnd.osgi.Jar;
import generic.jar.ResourceFile;
-import ghidra.app.script.GhidraScriptUtil;
-import ghidra.app.script.JavaScriptProvider;
-import ghidra.app.script.ResourceFileJavaFileManager;
-import ghidra.app.script.ResourceFileJavaFileObject;
+import ghidra.app.script.*;
public class BundleCompiler {
@@ -64,7 +45,8 @@ public class BundleCompiler {
}
/** compile a source directory to an exploded bundle */
- public void compileToExplodedBundle(ResourceFile srcdir, Path bindir, Writer output) throws IOException {
+ public void compileToExplodedBundle(ResourceFile srcdir, Path bindir, Writer output)
+ throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
List<String> options = new ArrayList<>();
options.add("-g");
@@ -75,17 +57,18 @@ public class BundleCompiler {
options.add("-classpath");
options.add(System.getProperty("java.class.path") + File.pathSeparator + bindir.toString());
options.add("-proc:none");
- final JavaFileManager fm0 = new ResourceFileJavaFileManager(GhidraScriptUtil.getScriptSourceDirectories());
+ final JavaFileManager fm0 =
+ new ResourceFileJavaFileManager(GhidraScriptUtil.getScriptSourceDirectories());
BundleJavaManager fm = new BundleJavaManager(bh.getHostFramework(), fm0, options);
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
final List<ResourceFileJavaFileObject> compilationUnits = new ArrayList<>();
BundleHost.visitUpdatedClassFiles(srcdir, bindir, (sf, bf) -> {
- if (sf != null)
- compilationUnits.add(new ResourceFileJavaFileObject(sf.getParentFile(), sf, Kind.SOURCE));
- if (bf != null)
- Files.delete(bf);
+ if (sf != null) {
+ compilationUnits.add(
+ new ResourceFileJavaFileObject(sf.getParentFile(), sf, Kind.SOURCE));
+ }
});
Path dmf = bindir.resolve("META-INF").resolve("MANIFEST.MF");
@@ -93,7 +76,8 @@ public class BundleCompiler {
Files.delete(dmf);
}
- JavaCompiler.CompilationTask task = compiler.getTask(output, fm, diagnostics, options, null, compilationUnits);
+ JavaCompiler.CompilationTask task =
+ compiler.getTask(output, fm, diagnostics, options, null, compilationUnits);
// task.setProcessors // for annotation processing / code generation
Boolean successfulCompilation = task.call();
@@ -101,7 +85,8 @@ public class BundleCompiler {
System.err.printf("%s: %s\n", successfulCompilation ? "Success" : "Fail", output);
if (successfulCompilation) {
- ResourceFile smf = new ResourceFile(srcdir, "META-INF" + File.separator + "MANIFEST.MF");
+ ResourceFile smf =
+ new ResourceFile(srcdir, "META-INF" + File.separator + "MANIFEST.MF");
if (smf.exists()) {
System.err.printf("Found manifest, not generating one\n");
Files.createFile(dmf);
@@ -112,18 +97,20 @@ public class BundleCompiler {
// no manifest, so create one with bndtools
Analyzer analyzer = new Analyzer();
analyzer.setJar(new Jar(bindir.toFile())); // give bnd the contents
- Stream<Object> bjars = Files.list(bh.getCompiledBundlesDir()).filter(f -> f.toString().endsWith(".jar"))
- .map(f -> {
- try {
- return new Jar(f.toFile());
- } catch (IOException e1) {
- e1.printStackTrace();
- return null;
- }
- });
+ Stream<Object> bjars = Files.list(bh.getCompiledBundlesDir()).filter(
+ f -> f.toString().endsWith(".jar")).map(f -> {
+ try {
+ return new Jar(f.toFile());
+ }
+ catch (IOException e1) {
+ e1.printStackTrace();
+ return null;
+ }
+ });
analyzer.addClasspath(bjars.collect(Collectors.toUnmodifiableList()));
- analyzer.setProperty("Bundle-SymbolicName", JavaScriptProvider.getSymbolicNameFromSourceDir(srcdir));
+ analyzer.setProperty("Bundle-SymbolicName",
+ JavaScriptProvider.getSymbolicNameFromSourceDir(srcdir));
analyzer.setProperty("Bundle-Version", "1.0");
analyzer.setProperty("Import-Package", "*");
// analyzer.setBundleActivator(s);
@@ -134,7 +121,8 @@ public class BundleCompiler {
String activator_classname = null;
for (Clazz clazz : analyzer.getClassspace().values()) {
- if (clazz.is(QUERY.IMPLEMENTS, new Instruction("org.osgi.framework.BundleActivator"), analyzer)) {
+ if (clazz.is(QUERY.IMPLEMENTS,
+ new Instruction("org.osgi.framework.BundleActivator"), analyzer)) {
System.err.printf("found BundleActivator class %s\n", clazz);
activator_classname = clazz.toString();
}
@@ -142,11 +130,12 @@ public class BundleCompiler {
if (activator_classname == null) {
Path activator_dest = bindir.resolve("GeneratedActivator.java");
try (PrintWriter writer = new PrintWriter(
- Files.newBufferedWriter(activator_dest, Charset.forName("UTF-8")))) {
+ Files.newBufferedWriter(activator_dest, Charset.forName("UTF-8")))) {
writer.println("import ghidra.app.script.osgi.GhidraBundleActivator;");
writer.println("import org.osgi.framework.BundleActivator;");
writer.println("import org.osgi.framework.BundleContext;");
- writer.println("public class GeneratedActivator extends GhidraBundleActivator {");
+ writer.println(
+ "public class GeneratedActivator extends GhidraBundleActivator {");
writer.println(" protected void start(BundleContext bc, Object api) {");
writer.println(" // TODO: stuff to do on bundle start");
writer.println(" }");
@@ -155,7 +144,8 @@ public class BundleCompiler {
writer.println(" }");
writer.println();
writer.println("}");
- } catch (IOException ex) {
+ }
+ catch (IOException ex) {
ex.printStackTrace();
return;
}
@@ -171,15 +161,17 @@ public class BundleCompiler {
options.add(System.getProperty("java.class.path"));
options.add("-proc:none");
- StandardJavaFileManager fm02 = compiler.getStandardFileManager(null, null, null);
+ StandardJavaFileManager fm02 =
+ compiler.getStandardFileManager(null, null, null);
fm = new BundleJavaManager(bh.getHostFramework(), fm02, options);
- Iterable<? extends JavaFileObject> compilationUnits2 = fm02
- .getJavaFileObjectsFromPaths(List.of(activator_dest));
+ Iterable<? extends JavaFileObject> compilationUnits2 =
+ fm02.getJavaFileObjectsFromPaths(List.of(activator_dest));
- JavaCompiler.CompilationTask task2 = compiler.getTask(output, fm, diagnostics, options, null,
- compilationUnits2);
- if (!task2.call())
+ JavaCompiler.CompilationTask task2 =
+ compiler.getTask(output, fm, diagnostics, options, null, compilationUnits2);
+ if (!task2.call()) {
return;
+ }
// since we add the activator after bndtools built the imports, we should add its imports too
String imps = ma.getValue(Constants.IMPORT_PACKAGE);
ma.putValue(Constants.IMPORT_PACKAGE, imps + ",ghidra.app.script.osgi");
@@ -190,11 +182,13 @@ public class BundleCompiler {
try (OutputStream out = Files.newOutputStream(dmf)) {
manifest.write(out);
}
- } catch (Exception e) {
+ }
+ catch (Exception e) {
e.printStackTrace();
}
analyzer.close();
- } else {
+ }
+ else {
for (Diagnostic<? extends JavaFileObject> dm : diagnostics.getDiagnostics()) {
System.err.printf("COMPILE ERROR: %s\n", dm);
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleHost.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleHost.java
index b265141e7b..afc5eca12e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleHost.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleHost.java
@@ -16,20 +16,12 @@
package ghidra.app.script.osgi;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
+import static java.util.stream.Collectors.*;
+
+import java.io.*;
import java.net.URL;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.ArrayDeque;
-import java.util.Collections;
-import java.util.Deque;
-import java.util.Map;
-import java.util.Properties;
-import java.util.function.Function;
+import java.nio.file.*;
+import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -38,21 +30,9 @@ import org.apache.felix.fileinstall.internal.FileInstall;
import org.apache.felix.framework.FrameworkFactory;
import org.apache.felix.framework.util.FelixConstants;
import org.apache.felix.main.AutoProcessor;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.BundleListener;
-import org.osgi.framework.Constants;
-import org.osgi.framework.FrameworkEvent;
-import org.osgi.framework.FrameworkListener;
-import org.osgi.framework.ServiceEvent;
-import org.osgi.framework.ServiceListener;
-import org.osgi.framework.ServiceReference;
+import org.osgi.framework.*;
import org.osgi.framework.launch.Framework;
-import org.osgi.service.log.LogEntry;
-import org.osgi.service.log.LogListener;
-import org.osgi.service.log.LogReaderService;
+import org.osgi.service.log.*;
import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ClassInfo;
@@ -60,27 +40,34 @@ import com.google.common.reflect.ClassPath.ClassInfo;
import generic.jar.ResourceFile;
import ghidra.framework.Application;
import ghidra.util.exception.CancelledException;
-import ghidra.util.task.Task;
-import ghidra.util.task.TaskLauncher;
-import ghidra.util.task.TaskMonitor;
+import ghidra.util.task.*;
public class BundleHost {
void dispose() {
- if (felix != null)
+ if (felix != null) {
stop_felix();
+ }
+ }
+
+ String buildExtraPackages0() {
+ Arrays.stream(Package.getPackages()).map(x -> x.getName()).sorted().distinct().forEach(
+ s -> System.err.printf("%s\n", s));
+ return Arrays.stream(Package.getPackages()).map(
+ x -> x.getName()).sorted().distinct().collect(Collectors.joining(","));
}
String buildExtraPackages() {
try {
-
- //ClassPath.from(BundleHost.class.getClassLoader()).getTopLevelClasses().stream()
- // .map(ClassInfo::getPackageName).sorted().distinct().forEach(x->{
- // System.err.printf("%s\n", x);
- // });
- return ClassPath.from(BundleHost.class.getClassLoader()).getTopLevelClasses().stream()
- .map(ClassInfo::getPackageName).sorted().distinct().collect(Collectors.joining(","));
- } catch (IOException e1) {
+ // return Arrays.stream(BundleHost.class.getClassLoader().getDefinedPackages()).map(x -> x.getName()).sorted().distinct().collect(Collectors.joining(","));
+ ClassPath.from(BundleHost.class.getClassLoader()).getTopLevelClasses().stream().map(
+ ClassInfo::getPackageName).sorted().distinct().forEach(
+ s -> System.err.printf("%s\n", s));
+ return ClassPath.from(
+ BundleHost.class.getClassLoader()).getTopLevelClasses().stream().map(
+ ClassInfo::getPackageName).sorted().distinct().collect(Collectors.joining(","));
+ }
+ catch (IOException e1) {
e1.printStackTrace();
}
return null;
@@ -104,8 +91,8 @@ public class BundleHost {
void dumpLoadedBundles() {
System.err.printf("=== Bundles ===\n");
for (Bundle bundle : bc.getBundles()) {
- System.err.printf("%s: %s: %s: %s\n", bundle.getBundleId(), bundle.getSymbolicName(), bundle.getState(),
- bundle.getVersion());
+ System.err.printf("%s: %s: %s: %s\n", bundle.getBundleId(), bundle.getSymbolicName(),
+ bundle.getState(), bundle.getVersion());
}
}
@@ -122,8 +109,8 @@ public class BundleHost {
@SuppressWarnings("rawtypes")
@Override
- protected void doLog(final Bundle bundle, final ServiceReference sr, final int level, final String msg,
- final Throwable throwable) {
+ protected void doLog(final Bundle bundle, final ServiceReference sr, final int level,
+ final String msg, final Throwable throwable) {
// plugin.printf("felixlogger: %s %s %s\n", bundle, msg, throwable);
}
@@ -134,14 +121,16 @@ public class BundleHost {
Properties config = new Properties();
config.setProperty(Constants.FRAMEWORK_BSNVERSION, Constants.FRAMEWORK_BSNVERSION_MULTIPLE);
- config.setProperty(Constants.FRAMEWORK_SYSTEMCAPABILITIES, "osgi.ee; osgi.ee=\"JavaSE\";version:List=\"11\"");
+ config.setProperty(Constants.FRAMEWORK_SYSTEMCAPABILITIES,
+ "osgi.ee; osgi.ee=\"JavaSE\";version:List=\"11\"");
config.setProperty(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, buildExtraPackages());
// extra packages have lower precedence than imports, so a Bundle-Import will
// prevent "living off the land"
config.setProperty("org.osgi.service.http.port", "8080");
- config.setProperty(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
+ config.setProperty(Constants.FRAMEWORK_STORAGE_CLEAN,
+ Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
config.setProperty(Constants.FRAMEWORK_STORAGE, makeCacheDir());
// more properties available at:
@@ -173,7 +162,8 @@ public class BundleHost {
// plugin.printf("%s: %s\n", entry.getBundle(), entry.getMessage());
}
});
- } else {
+ }
+ else {
// plugin.printf("no logreaderservice in felix!\n");
}
@@ -192,12 +182,13 @@ public class BundleHost {
String type = "?";
if (event.getType() == ServiceEvent.REGISTERED) {
type = "registered";
- } else if (event.getType() == ServiceEvent.UNREGISTERING) {
+ }
+ else if (event.getType() == ServiceEvent.UNREGISTERING) {
type = "unregistering";
}
System.err.printf("%s %s from %s\n", event.getSource(), type,
- event.getServiceReference().getBundle().getLocation());
+ event.getServiceReference().getBundle().getLocation());
}
});
@@ -207,18 +198,18 @@ public class BundleHost {
public void bundleChanged(BundleEvent event) {
// System.err.printf("%s %s\n", event.getBundle(), event);
switch (event.getType()) {
- case BundleEvent.INSTALLED:
- System.err.printf("INSTALLED %s\n", event.getBundle().getSymbolicName());
- break;
- case BundleEvent.UNINSTALLED:
- System.err.printf("UNINSTALLED %s\n", event.getBundle().getSymbolicName());
- break;
- case BundleEvent.STARTED:
- System.err.printf("STARTED %s\n", event.getBundle().getSymbolicName());
- break;
- case BundleEvent.STOPPED:
- System.err.printf("STOPPED %s\n", event.getBundle().getSymbolicName());
- break;
+ case BundleEvent.INSTALLED:
+ System.err.printf("INSTALLED %s\n", event.getBundle().getSymbolicName());
+ break;
+ case BundleEvent.UNINSTALLED:
+ System.err.printf("UNINSTALLED %s\n", event.getBundle().getSymbolicName());
+ break;
+ case BundleEvent.STARTED:
+ System.err.printf("STARTED %s\n", event.getBundle().getSymbolicName());
+ break;
+ case BundleEvent.STOPPED:
+ System.err.printf("STOPPED %s\n", event.getBundle().getSymbolicName());
+ break;
}
}
});
@@ -261,56 +252,60 @@ public class BundleHost {
public boolean synchronousStop(Bundle b) throws InterruptedException, BundleException {
if (b != null) {
switch (b.getState()) {
- case Bundle.STARTING:
- case Bundle.ACTIVE:
- b.stop();
- case Bundle.STOPPING:
- while (true) {
- switch (b.getState()) {
- case Bundle.ACTIVE:
- case Bundle.STOPPING:
- Thread.sleep(500);
- default:
- return true;
+ case Bundle.STARTING:
+ case Bundle.ACTIVE:
+ b.stop();
+ case Bundle.STOPPING:
+ while (true) {
+ switch (b.getState()) {
+ case Bundle.ACTIVE:
+ case Bundle.STOPPING:
+ Thread.sleep(500);
+ default:
+ return true;
+ }
}
- }
- case Bundle.INSTALLED:
- case Bundle.RESOLVED:
- case Bundle.UNINSTALLED:
+ case Bundle.INSTALLED:
+ case Bundle.RESOLVED:
+ case Bundle.UNINSTALLED:
}
}
return false;
}
public boolean synchronousUninstall(Bundle b) throws InterruptedException, BundleException {
- if (b != null)
+ if (b != null) {
if (b.getState() != Bundle.UNINSTALLED) {
b.uninstall();
while (true) {
- if (b.getState() == Bundle.UNINSTALLED)
+ if (b.getState() == Bundle.UNINSTALLED) {
return true;
+ }
Thread.sleep(500);
}
}
+ }
return false;
}
- public boolean waitForBundleStart(String location) throws InterruptedException, BundleException {
+ public boolean waitForBundleStart(String location)
+ throws InterruptedException, BundleException {
while (true) {
Bundle b = bc.getBundle(location);
- if (b != null)
+ if (b != null) {
switch (b.getState()) {
- case Bundle.ACTIVE:
- return true;
- case Bundle.UNINSTALLED:
- case Bundle.STOPPING:
- return false;
- case Bundle.INSTALLED:
- case Bundle.RESOLVED:
- b.start();
- continue;
- case Bundle.STARTING:
+ case Bundle.ACTIVE:
+ return true;
+ case Bundle.UNINSTALLED:
+ case Bundle.STOPPING:
+ return false;
+ case Bundle.INSTALLED:
+ case Bundle.RESOLVED:
+ b.start();
+ continue;
+ case Bundle.STARTING:
}
+ }
Thread.sleep(500);
}
}
@@ -325,7 +320,8 @@ public class BundleHost {
FrameworkEvent x = felix.waitForStop(5000);
System.err.printf("killed felix with %s", x.toString());
felix = null;
- } catch (BundleException | InterruptedException e) {
+ }
+ catch (BundleException | InterruptedException e) {
System.err.printf("failed to kill felix: %s", e);
}
}
@@ -343,7 +339,8 @@ public class BundleHost {
try {
fileinstall_bundle.stop();
return true;
- } catch (BundleException e) {
+ }
+ catch (BundleException e) {
e.printStackTrace();
}
}
@@ -355,7 +352,8 @@ public class BundleHost {
try {
fileinstall_bundle.start();
return true;
- } catch (BundleException e) {
+ }
+ catch (BundleException e) {
e.printStackTrace();
}
}
@@ -363,10 +361,11 @@ public class BundleHost {
}
public interface NewSourceCallback {
- void found(ResourceFile source_file, Path class_file) throws Throwable;
+ void found(ResourceFile source_file, Collection<Path> class_files) throws Throwable;
}
- public static void visitUpdatedClassFiles(ResourceFile srcdir, Path bindir, NewSourceCallback new_source_cb) {
+ public static void visitUpdatedClassFiles(ResourceFile srcdir, Path bindir,
+ NewSourceCallback new_source_cb) {
try {
// delete class files for which java is either newer, or no longer exists
Deque<ResourceFile> stack = new ArrayDeque<>();
@@ -377,36 +376,42 @@ public class BundleHost {
Path bd = bindir.resolve(relpath);
// index the class files in the corresponding directory by basename
- Map<String, Path> binfiles = Files.exists(bd) ? Files.list(bd)
- .filter(x -> Files.isRegularFile(x) && x.getFileName().toString().endsWith(".class"))
- .collect(Collectors.toMap(x -> {
+ Map<String, List<Path>> binfiles =
+ Files.exists(bd) ? Files.list(bd).filter(x -> Files.isRegularFile(x) &&
+ x.getFileName().toString().endsWith(".class")).collect(groupingBy(x -> {
String s = x.getFileName().toString();
- return s.substring(0, s.length() - 6);// drop ".class"
- }, Function.identity())) : Collections.emptyMap();
+ int money = s.indexOf('$');
+ if (money >= 0) {
+ return s.substring(0, money);
+ }
+ return s.substring(0, s.length() - 6);
+ })) : Collections.emptyMap();
for (ResourceFile sf : sd.listFiles()) {
- if (sf.isDirectory())
+ if (sf.isDirectory()) {
stack.push(sf);
+ }
else {
String n = sf.getName();
if (n.endsWith(".java")) {
- Path bf = binfiles.remove(n.substring(0, n.length() - 5)); // drop ".java"
- long bfl = -1;
long sfl = sf.lastModified();
- if (bf != null)
- bfl = bf.toFile().lastModified();
+ List<Path> bfs = binfiles.remove(n.substring(0, n.length() - 5));
+ long bfl = (bfs == null || bfs.isEmpty()) ? -1
+ : bfs.stream().mapToLong(
+ bf -> bf.toFile().lastModified()).min().getAsLong();
if (sfl > bfl) {
- new_source_cb.found(sf, bf);
+ new_source_cb.found(sf, bfs);
}
}
}
}
// any remaining .class files are missing .java files
- for (Path bf : binfiles.values()) {
- new_source_cb.found(null, bf);
- }
+ new_source_cb.found(null,
+ binfiles.values().stream().flatMap(l -> l.stream()).collect(
+ Collectors.toList()));
}
- } catch (Throwable t) {
+ }
+ catch (Throwable t) {
t.printStackTrace();
}
}
@@ -418,18 +423,23 @@ public class BundleHost {
final ClassLoader loader = c.getClassLoader();
if (loader == null) {
location = ClassLoader.getSystemResource(classLocation);
- } else {
+ }
+ else {
location = loader.getResource(classLocation);
}
if (location != null) {
Pattern p = Pattern.compile("^.*:(.*)!.*$");
Matcher m = p.matcher(location.toString());
- if (m.find())
+ if (m.find()) {
return m.group(1);
- else
+ }
+ else {
return null; // not loaded from jar?
- } else
+ }
+ }
+ else {
return null;
+ }
}
}