@Test public void verify_spring_can_find_files() throws Exception { Random random = new Random(); final String packageName = "package" + random.nextLong(); final String classname = packageName + ".Test"; final File baseDir = new File("target/testClasspathWithSpring/" + System.currentTimeMillis()); baseDir.mkdirs(); final ClassPool pool = ClassPool.getDefault(); final CtClass cc = pool.makeClass(classname); final ClassFile classFile = cc.getClassFile(); ConstPool cp = classFile.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); attr.setAnnotation(new Annotation("org.springframework.stereotype.Component", cp)); classFile.addAttribute(attr); cc.writeFile(baseDir.toString()); final byte[] jarAsBytes = ClasspathUtils.jarFiles(baseDir.toString()); final File testJar = new File(baseDir, "test.jar"); FileOutputStream fos = new FileOutputStream(testJar); fos.write(jarAsBytes); fos.close(); final URLClassLoader classLoader = (URLClassLoader) this.getClass().getClassLoader(); final Method addURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); addURL.setAccessible(true); addURL.invoke(classLoader, testJar.toURI().toURL()); @SuppressWarnings("resource") ApplicationContext context = new AnnotationConfigApplicationContext(packageName); final Map<String, Object> beansWithAnnotation = context.getBeansWithAnnotation(Component.class); assertEquals(1, beansWithAnnotation.size()); assertTrue(beansWithAnnotation.containsKey("test")); assertEquals(classname, beansWithAnnotation.get("test").getClass().getName()); }
public static byte[] jarFiles(String path) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); JarOutputStream jarOut = new JarOutputStream(bos); int size = addFiles(jarOut, "", new File(path)); if (size == 0) { return null; } jarOut.close(); return bos.toByteArray(); }
ClasspathUtils { public static byte[] jarFiles(String path) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); JarOutputStream jarOut = new JarOutputStream(bos); int size = addFiles(jarOut, "", new File(path)); if (size == 0) { return null; } jarOut.close(); return bos.toByteArray(); } }
ClasspathUtils { public static byte[] jarFiles(String path) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); JarOutputStream jarOut = new JarOutputStream(bos); int size = addFiles(jarOut, "", new File(path)); if (size == 0) { return null; } jarOut.close(); return bos.toByteArray(); }  }
ClasspathUtils { public static byte[] jarFiles(String path) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); JarOutputStream jarOut = new JarOutputStream(bos); int size = addFiles(jarOut, "", new File(path)); if (size == 0) { return null; } jarOut.close(); return bos.toByteArray(); }  static List<String> getStartupClasspath(); static Collection<URL> listCurrentClasspath(); static ClassLoader getNearestSystemClassloader(ClassLoader cl); static Collection<URL> listCurrentClasspath(URLClassLoader classLoader); static byte[] createManifestJar(Manifest manifest); static byte[] createBootstrapperJar(Manifest manifest, Class<?> bootstrapper); static byte[] jarFiles(String path); }
ClasspathUtils { public static byte[] jarFiles(String path) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); JarOutputStream jarOut = new JarOutputStream(bos); int size = addFiles(jarOut, "", new File(path)); if (size == 0) { return null; } jarOut.close(); return bos.toByteArray(); }  static List<String> getStartupClasspath(); static Collection<URL> listCurrentClasspath(); static ClassLoader getNearestSystemClassloader(ClassLoader cl); static Collection<URL> listCurrentClasspath(URLClassLoader classLoader); static byte[] createManifestJar(Manifest manifest); static byte[] createBootstrapperJar(Manifest manifest, Class<?> bootstrapper); static byte[] jarFiles(String path);  }