# HG changeset patch # User Jaroslav Tulach # Date 1399277790 -7200 # Node ID 777bd3ed81ba7676b204915715411545af7c8d80 # Parent ed9f8f93960f9b81fd6dbef2ded911b84e9d214f Export only test classes, classes from exported packages and those referenced in META-INF/services diff -r ed9f8f93960f -r 777bd3ed81ba launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java --- a/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Mon May 05 08:42:33 2014 +0200 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Mon May 05 10:16:30 2014 +0200 @@ -25,9 +25,10 @@ import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashSet; +import java.util.Set; import java.util.jar.JarFile; import java.util.logging.Level; -import org.apidesign.vm4brwsr.Bck2Brwsr; /** * Lightweight server to launch Bck2Brwsr applications and tests. @@ -35,6 +36,7 @@ * execution engine. */ final class Bck2BrwsrLauncher extends BaseHTTPLauncher { + private Set testClasses = new HashSet(); public Bck2BrwsrLauncher(String cmd) { super(cmd); @@ -47,7 +49,13 @@ @Override String compileJar(JarFile jar) throws IOException { - return CompileCP.compileJAR(jar); + return CompileCP.compileJAR(jar, testClasses); + } + + @Override + public InvocationContext createInvocation(Class clazz, String method) { + testClasses.add(clazz.getName().replace('.', '/')); + return super.createInvocation(clazz, method); } @Override String compileFromClassPath(URL f, Res loader) throws IOException { diff -r ed9f8f93960f -r 777bd3ed81ba launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java --- a/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java Mon May 05 08:42:33 2014 +0200 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java Mon May 05 10:16:30 2014 +0200 @@ -17,16 +17,20 @@ */ package org.apidesign.bck2brwsr.launcher; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.StringWriter; import java.net.JarURLConnection; import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.logging.Level; @@ -41,10 +45,26 @@ */ class CompileCP { private static final Logger LOG = Logger.getLogger(CompileCP.class.getName()); - static String compileJAR(final JarFile jar) throws IOException { + static String compileJAR(final JarFile jar, Set testClasses) + throws IOException { List arr = new ArrayList<>(); List classes = new ArrayList<>(); - listJAR(jar, classes, arr); + Set exported = new HashSet(); + Set keep = new HashSet(testClasses); + listJAR(jar, classes, arr, exported, keep); + List root = new ArrayList<>(); + for (String c : classes) { + if (keep.contains(c)) { + root.add(c); + continue; + } + int slash = c.lastIndexOf('/'); + String pkg = c.substring(0, slash + 1); + if (exported.contains(pkg)) { + root.add(c); + } + } + StringWriter w = new StringWriter(); try { class JarRes extends EmulationResources implements Bck2Brwsr.Resources { @@ -57,6 +77,7 @@ Bck2Brwsr.newCompiler() .addClasses(classes.toArray(new String[0])) + .addRootClasses(root.toArray(new String[0])) .addResources(arr.toArray(new String[0])) .library(true) .resources(new JarRes()) @@ -115,7 +136,10 @@ return null; } - private static void listJAR(JarFile j, List classes, List resources) throws IOException { + private static void listJAR( + JarFile j, List classes, + List resources, Set exported, Set keep + ) throws IOException { Enumeration en = j.entries(); while (en.hasMoreElements()) { JarEntry e = en.nextElement(); @@ -135,6 +159,28 @@ classes.add(n.substring(0, n.length() - 6)); } else { resources.add(n); + if (n.startsWith("META-INF/services/") && keep != null) { + BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e))); + for (;;) { + String l = r.readLine(); + if (l == null) { + break; + } + if (l.startsWith("#")) { + continue; + } + keep.add(l.replace('.', '/')); + } + } + } + } + String exp = j.getManifest().getMainAttributes().getValue("Export-Package"); + if (exp != null && exported != null) { + for (String def : exp.split(",")) { + for (String sep : def.split(";")) { + exported.add(sep.replace('.', '/') + "/"); + break; + } } } } @@ -168,7 +214,7 @@ List arr = new ArrayList<>(); List classes = new ArrayList<>(); - listJAR(juc.getJarFile(), classes, arr); + listJAR(juc.getJarFile(), classes, arr, null, null); Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0])) .resources(new Bck2Brwsr.Resources() { diff -r ed9f8f93960f -r 777bd3ed81ba rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Mon May 05 08:42:33 2014 +0200 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Mon May 05 10:16:30 2014 +0200 @@ -43,6 +43,7 @@ private final boolean fail; private final HtmlFragment html; private final Http.Resource[] http; + private final InvocationContext c; Object value; Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) { @@ -52,12 +53,12 @@ this.fail = fail; this.html = html; this.http = http; + this.c = l != null ? l.createInvocation(m.getDeclaringClass(), m.getName()) : null; } @Test(groups = "run") public void executeCode() throws Throwable { if (l != null) { - InvocationContext c = l.createInvocation(m.getDeclaringClass(), m.getName()); if (html != null) { c.setHtmlFragment(html.value()); }