@Test public void testMerge() throws Exception { Jar wrapper = newCapsuleJar() .setAttribute("Caplets", "MyCapsule") .setAttribute("System-Properties", "p1=555") .addClass(MyCapsule.class); Jar app = newCapsuleJar() .setAttribute("Application-Class", "com.acme.Foo") .setAttribute("System-Properties", "p1=111") .setListAttribute("App-Class-Path", list("lib/a.jar")) .addClass(Capsule.class) .addEntry("foo.jar", emptyInputStream()) .addEntry("a.class", emptyInputStream()) .addEntry("b.txt", emptyInputStream()) .addEntry("lib/a.jar", emptyInputStream()) .addEntry("lib/b.class", emptyInputStream()) .addEntry("META-INF/x.txt", emptyInputStream()); Class<?> capsuleClass = loadCapsule(wrapper); Path fooPath = mockDep(capsuleClass, "com.acme:foo", "jar", "com.acme:foo:1.0").get(0); Files.createDirectories(fooPath.getParent()); app.write(fooPath); props.setProperty("capsule.merge", "out.jar"); int exit = main0(capsuleClass, "com.acme:foo"); assertEquals(0, exit); assertTrue(Files.isRegularFile(path("out.jar"))); Jar out = new Jar(path("out.jar")); assert_().that(out.getAttribute("Main-Class")).isEqualTo("TestCapsule"); assert_().that(out.getAttribute("Premain-Class")).isEqualTo("TestCapsule"); assert_().that(out.getListAttribute("Caplets")).isEqualTo(list("MyCapsule")); assert_().that(out.getMapAttribute("System-Properties", "")).isEqualTo(map("p1", "111")); FileSystem jar = ZipFS.newZipFileSystem(path("out.jar")); assertTrue(Files.isRegularFile(jar.getPath("Capsule.class"))); assertTrue(Files.isRegularFile(jar.getPath("TestCapsule.class"))); assertTrue(Files.isRegularFile(jar.getPath("MyCapsule.class"))); assertTrue(Files.isRegularFile(jar.getPath("foo.jar"))); assertTrue(Files.isRegularFile(jar.getPath("a.class"))); assertTrue(Files.isRegularFile(jar.getPath("b.txt"))); assertTrue(Files.isRegularFile(jar.getPath("lib/a.jar"))); assertTrue(Files.isRegularFile(jar.getPath("lib/b.class"))); assertTrue(Files.isRegularFile(jar.getPath("META-INF/x.txt"))); }
@SuppressWarnings("unchecked") private static <T> T merge(T v1, T v2) { if (v2 == null) return v1; if (v1 instanceof Collection) { final Collection<Object> c1 = (Collection<Object>) v1; final Collection<Object> c2 = (Collection<Object>) v2; final Collection<Object> cm; if (v1 instanceof List) cm = new ArrayList<>(c1.size() + c2.size()); else if (v1 instanceof Set) cm = new HashSet<>(c1.size() + c2.size()); else throw new RuntimeException("Unhandled type: " + v1.getClass().getName()); cm.addAll(c1); addAllIfAbsent(cm, c2); return (T) cm; } else if (v1 instanceof Map) { final Map<Object, Object> mm = new HashMap<>(); mm.putAll((Map<Object, Object>) v1); mm.putAll((Map<Object, Object>) v2); return (T) mm; } else return v2; }
Capsule implements Runnable, InvocationHandler { @SuppressWarnings("unchecked") private static <T> T merge(T v1, T v2) { if (v2 == null) return v1; if (v1 instanceof Collection) { final Collection<Object> c1 = (Collection<Object>) v1; final Collection<Object> c2 = (Collection<Object>) v2; final Collection<Object> cm; if (v1 instanceof List) cm = new ArrayList<>(c1.size() + c2.size()); else if (v1 instanceof Set) cm = new HashSet<>(c1.size() + c2.size()); else throw new RuntimeException("Unhandled type: " + v1.getClass().getName()); cm.addAll(c1); addAllIfAbsent(cm, c2); return (T) cm; } else if (v1 instanceof Map) { final Map<Object, Object> mm = new HashMap<>(); mm.putAll((Map<Object, Object>) v1); mm.putAll((Map<Object, Object>) v2); return (T) mm; } else return v2; } }
Capsule implements Runnable, InvocationHandler { @SuppressWarnings("unchecked") private static <T> T merge(T v1, T v2) { if (v2 == null) return v1; if (v1 instanceof Collection) { final Collection<Object> c1 = (Collection<Object>) v1; final Collection<Object> c2 = (Collection<Object>) v2; final Collection<Object> cm; if (v1 instanceof List) cm = new ArrayList<>(c1.size() + c2.size()); else if (v1 instanceof Set) cm = new HashSet<>(c1.size() + c2.size()); else throw new RuntimeException("Unhandled type: " + v1.getClass().getName()); cm.addAll(c1); addAllIfAbsent(cm, c2); return (T) cm; } else if (v1 instanceof Map) { final Map<Object, Object> mm = new HashMap<>(); mm.putAll((Map<Object, Object>) v1); mm.putAll((Map<Object, Object>) v2); return (T) mm; } else return v2; } @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) protected  Capsule(Path jarFile); @SuppressWarnings("LeakingThisInConstructor") protected  Capsule(Capsule pred); }
Capsule implements Runnable, InvocationHandler { @SuppressWarnings("unchecked") private static <T> T merge(T v1, T v2) { if (v2 == null) return v1; if (v1 instanceof Collection) { final Collection<Object> c1 = (Collection<Object>) v1; final Collection<Object> c2 = (Collection<Object>) v2; final Collection<Object> cm; if (v1 instanceof List) cm = new ArrayList<>(c1.size() + c2.size()); else if (v1 instanceof Set) cm = new HashSet<>(c1.size() + c2.size()); else throw new RuntimeException("Unhandled type: " + v1.getClass().getName()); cm.addAll(c1); addAllIfAbsent(cm, c2); return (T) cm; } else if (v1 instanceof Map) { final Map<Object, Object> mm = new HashMap<>(); mm.putAll((Map<Object, Object>) v1); mm.putAll((Map<Object, Object>) v2); return (T) mm; } else return v2; } @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) protected  Capsule(Path jarFile); @SuppressWarnings("LeakingThisInConstructor") protected  Capsule(Capsule pred); static final void main(String[] args); @SuppressWarnings({"BroadCatchBlock", "UnusedAssignment"}) static int main0(String[] args0); static void premain(String agentArgs, Instrumentation inst); static Object getCapsule(String capletClassName); boolean isAgent(); @Override final void run(); @Override Object invoke(Object proxy, Method method, Object[] args); @Override final int hashCode(); @Override final boolean equals(Object obj); @Override String toString(); }
Capsule implements Runnable, InvocationHandler { @SuppressWarnings("unchecked") private static <T> T merge(T v1, T v2) { if (v2 == null) return v1; if (v1 instanceof Collection) { final Collection<Object> c1 = (Collection<Object>) v1; final Collection<Object> c2 = (Collection<Object>) v2; final Collection<Object> cm; if (v1 instanceof List) cm = new ArrayList<>(c1.size() + c2.size()); else if (v1 instanceof Set) cm = new HashSet<>(c1.size() + c2.size()); else throw new RuntimeException("Unhandled type: " + v1.getClass().getName()); cm.addAll(c1); addAllIfAbsent(cm, c2); return (T) cm; } else if (v1 instanceof Map) { final Map<Object, Object> mm = new HashMap<>(); mm.putAll((Map<Object, Object>) v1); mm.putAll((Map<Object, Object>) v2); return (T) mm; } else return v2; } @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) protected  Capsule(Path jarFile); @SuppressWarnings("LeakingThisInConstructor") protected  Capsule(Capsule pred); static final void main(String[] args); @SuppressWarnings({"BroadCatchBlock", "UnusedAssignment"}) static int main0(String[] args0); static void premain(String agentArgs, Instrumentation inst); static Object getCapsule(String capletClassName); boolean isAgent(); @Override final void run(); @Override Object invoke(Object proxy, Method method, Object[] args); @Override final int hashCode(); @Override final boolean equals(Object obj); @Override String toString(); static final String VERSION; }