@Test public void testJarTransformation() throws Exception { JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(file)); try { for (Class<?> type : Arrays.asList(Sample.class, Substitution.class, Target.class)) { jarOutputStream.putNextEntry(new JarEntry(type.getName().replace('.', '/') + ".class")); InputStream inputStream = type.getResourceAsStream(type.getSimpleName() + ".class"); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { jarOutputStream.write(buffer, 0, length); } jarOutputStream.closeEntry(); } } finally { jarOutputStream.close(); } new GradleTypeTransformer(Sample.class.getPackage().getName().replace('.', '/'), Substitution.class.getName().replace('.', '/')).transform(file); JarInputStream jarInputStream = new JarInputStream(new FileInputStream(file)); try { JarEntry entry = jarInputStream.getNextJarEntry(); assertThat(entry, notNullValue(JarEntry.class)); assertThat(entry.getName(), is(Target.class.getName().replace('.', '/') + ".class")); new ClassReader(jarInputStream).accept(new ClassVisitor(Opcodes.ASM9) { private boolean found; @Override public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) { assertThat(descriptor, is("Lfoo/Bar;")); found = true; return null; } @Override public void visitEnd() { assertThat(found, is(true)); } }, ClassReader.SKIP_CODE); jarInputStream.closeEntry(); assertThat(jarInputStream.getNextJarEntry(), nullValue(JarEntry.class)); } finally { jarInputStream.close(); } }
public void transform(File jar) throws IOException { Map<String, String> names = GradlePackageVisitor.toGradleTypeNames(api, type, jar); File temporary = File.createTempFile("gradle-byte-buddy-plugin", ".jar"); JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jar)); try { JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(temporary)); try { JarEntry entry; while ((entry = jarInputStream.getNextJarEntry()) != null) { if (!entry.getName().startsWith(api)) { if (entry.getName().endsWith(CLASS_FILE)) { ClassReader reader = new ClassReader(jarInputStream); ClassWriter writer = new ClassWriter(0); reader.accept(new ClassRemapper(writer, new SimpleRemapper(names)), 0); jarOutputStream.putNextEntry(new JarEntry(entry.getName())); jarOutputStream.write(writer.toByteArray()); } else { jarOutputStream.putNextEntry(entry); byte[] buffer = new byte[1024]; int length; while ((length = jarInputStream.read(buffer)) != -1) { jarOutputStream.write(buffer, 0, length); } } jarOutputStream.closeEntry(); } jarInputStream.closeEntry(); } } finally { jarOutputStream.close(); } } finally { jarInputStream.close(); } if (!jar.delete()) { throw new IllegalStateException("Could not delete " + jar); } else if (!temporary.renameTo(jar)) { throw new IllegalStateException("Could not rename " + temporary + " to " + jar); } }
GradleTypeTransformer { public void transform(File jar) throws IOException { Map<String, String> names = GradlePackageVisitor.toGradleTypeNames(api, type, jar); File temporary = File.createTempFile("gradle-byte-buddy-plugin", ".jar"); JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jar)); try { JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(temporary)); try { JarEntry entry; while ((entry = jarInputStream.getNextJarEntry()) != null) { if (!entry.getName().startsWith(api)) { if (entry.getName().endsWith(CLASS_FILE)) { ClassReader reader = new ClassReader(jarInputStream); ClassWriter writer = new ClassWriter(0); reader.accept(new ClassRemapper(writer, new SimpleRemapper(names)), 0); jarOutputStream.putNextEntry(new JarEntry(entry.getName())); jarOutputStream.write(writer.toByteArray()); } else { jarOutputStream.putNextEntry(entry); byte[] buffer = new byte[1024]; int length; while ((length = jarInputStream.read(buffer)) != -1) { jarOutputStream.write(buffer, 0, length); } } jarOutputStream.closeEntry(); } jarInputStream.closeEntry(); } } finally { jarOutputStream.close(); } } finally { jarInputStream.close(); } if (!jar.delete()) { throw new IllegalStateException("Could not delete " + jar); } else if (!temporary.renameTo(jar)) { throw new IllegalStateException("Could not rename " + temporary + " to " + jar); } } }
GradleTypeTransformer { public void transform(File jar) throws IOException { Map<String, String> names = GradlePackageVisitor.toGradleTypeNames(api, type, jar); File temporary = File.createTempFile("gradle-byte-buddy-plugin", ".jar"); JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jar)); try { JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(temporary)); try { JarEntry entry; while ((entry = jarInputStream.getNextJarEntry()) != null) { if (!entry.getName().startsWith(api)) { if (entry.getName().endsWith(CLASS_FILE)) { ClassReader reader = new ClassReader(jarInputStream); ClassWriter writer = new ClassWriter(0); reader.accept(new ClassRemapper(writer, new SimpleRemapper(names)), 0); jarOutputStream.putNextEntry(new JarEntry(entry.getName())); jarOutputStream.write(writer.toByteArray()); } else { jarOutputStream.putNextEntry(entry); byte[] buffer = new byte[1024]; int length; while ((length = jarInputStream.read(buffer)) != -1) { jarOutputStream.write(buffer, 0, length); } } jarOutputStream.closeEntry(); } jarInputStream.closeEntry(); } } finally { jarOutputStream.close(); } } finally { jarInputStream.close(); } if (!jar.delete()) { throw new IllegalStateException("Could not delete " + jar); } else if (!temporary.renameTo(jar)) { throw new IllegalStateException("Could not rename " + temporary + " to " + jar); } } GradleTypeTransformer(); protected  GradleTypeTransformer(String api, String type); }
GradleTypeTransformer { public void transform(File jar) throws IOException { Map<String, String> names = GradlePackageVisitor.toGradleTypeNames(api, type, jar); File temporary = File.createTempFile("gradle-byte-buddy-plugin", ".jar"); JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jar)); try { JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(temporary)); try { JarEntry entry; while ((entry = jarInputStream.getNextJarEntry()) != null) { if (!entry.getName().startsWith(api)) { if (entry.getName().endsWith(CLASS_FILE)) { ClassReader reader = new ClassReader(jarInputStream); ClassWriter writer = new ClassWriter(0); reader.accept(new ClassRemapper(writer, new SimpleRemapper(names)), 0); jarOutputStream.putNextEntry(new JarEntry(entry.getName())); jarOutputStream.write(writer.toByteArray()); } else { jarOutputStream.putNextEntry(entry); byte[] buffer = new byte[1024]; int length; while ((length = jarInputStream.read(buffer)) != -1) { jarOutputStream.write(buffer, 0, length); } } jarOutputStream.closeEntry(); } jarInputStream.closeEntry(); } } finally { jarOutputStream.close(); } } finally { jarInputStream.close(); } if (!jar.delete()) { throw new IllegalStateException("Could not delete " + jar); } else if (!temporary.renameTo(jar)) { throw new IllegalStateException("Could not rename " + temporary + " to " + jar); } } GradleTypeTransformer(); protected  GradleTypeTransformer(String api, String type); void transform(File jar); }
GradleTypeTransformer { public void transform(File jar) throws IOException { Map<String, String> names = GradlePackageVisitor.toGradleTypeNames(api, type, jar); File temporary = File.createTempFile("gradle-byte-buddy-plugin", ".jar"); JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jar)); try { JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(temporary)); try { JarEntry entry; while ((entry = jarInputStream.getNextJarEntry()) != null) { if (!entry.getName().startsWith(api)) { if (entry.getName().endsWith(CLASS_FILE)) { ClassReader reader = new ClassReader(jarInputStream); ClassWriter writer = new ClassWriter(0); reader.accept(new ClassRemapper(writer, new SimpleRemapper(names)), 0); jarOutputStream.putNextEntry(new JarEntry(entry.getName())); jarOutputStream.write(writer.toByteArray()); } else { jarOutputStream.putNextEntry(entry); byte[] buffer = new byte[1024]; int length; while ((length = jarInputStream.read(buffer)) != -1) { jarOutputStream.write(buffer, 0, length); } } jarOutputStream.closeEntry(); } jarInputStream.closeEntry(); } } finally { jarOutputStream.close(); } } finally { jarInputStream.close(); } if (!jar.delete()) { throw new IllegalStateException("Could not delete " + jar); } else if (!temporary.renameTo(jar)) { throw new IllegalStateException("Could not rename " + temporary + " to " + jar); } } GradleTypeTransformer(); protected  GradleTypeTransformer(String api, String type); void transform(File jar);  }