ShrinkWrapUtils { public static JavaArchive getJavaArchiveFromClass(Class<?> clazz) { URL url = clazz.getResource(clazz.getSimpleName() + ".class"); try { String file = url.getFile(); if (file.startsWith("file:")) { file = file.substring(file.indexOf(":") + 1); } file = file.substring(0, file.indexOf('!')); JavaArchive jar = ShrinkWrap.create(ZipImporter.class, file + "!").importFrom(new File(file)).as(JavaArchive.class); return jar; } catch (Exception e) { throw new IllegalStateException("Cannot get JAR from class " + clazz.getName() + " from URL " + url, e); } }  static JavaArchive getJavaArchiveFromClass(Class<?> clazz);  }
@Test public void testJavaArchiveFromClass() throws Throwable { JavaArchive archive = ShrinkWrapUtils.getJavaArchiveFromClass(Test.class); assertNotNull(archive.get("/org/junit/Test.class")); assertNotNull(archive.get("/org/junit/Ignore.class")); }
@Test public void testMultipleUse() throws ClassNotFoundException { JavaArchive archive = ShrinkWrapUtils.getJavaArchiveFromClass(Test.class); ShrinkWrapClassLoader classLoader = new ShrinkWrapClassLoader(ClassLoaderUtils.getBootstrapClassLoader(), archive); Class<?> nestedClass = classLoader.loadClass(Test.class.getName()); JavaArchive nestedArchive = ShrinkWrapUtils.getJavaArchiveFromClass(nestedClass); assertNotNull(nestedArchive.get("/org/junit/Test.class")); assertNotNull(nestedArchive.get("/org/junit/Ignore.class")); }