diff options
author | cushon <cushon@google.com> | 2020-01-15 13:12:54 -0800 |
---|---|---|
committer | Colin Decker <cgdecker@gmail.com> | 2020-01-16 15:02:02 -0500 |
commit | 9f5f6a5417c5a4a6cbfca53f7a2b62f9b634232c (patch) | |
tree | 9a0970407d55bf5b2aa191971519c8fe3432dbea | |
parent | df269d24ae91343e5da28437001968c8f4bb3c48 (diff) | |
download | turbine-9f5f6a5417c5a4a6cbfca53f7a2b62f9b634232c.tar.gz |
Include .class resources in --resource_output
This was an oversight in a previous change When using turbine as an annotation
processing tool, and .class files generated directly by annotation processors
need to be saved separately and included in the final output (similar to non-.class
resources).
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=289922152
-rw-r--r-- | java/com/google/turbine/main/Main.java | 4 | ||||
-rw-r--r-- | javatests/com/google/turbine/main/MainTest.java | 70 |
2 files changed, 71 insertions, 3 deletions
diff --git a/java/com/google/turbine/main/Main.java b/java/com/google/turbine/main/Main.java index 0e77c65..1e60ae6 100644 --- a/java/com/google/turbine/main/Main.java +++ b/java/com/google/turbine/main/Main.java @@ -353,9 +353,7 @@ public class Main { BufferedOutputStream bos = new BufferedOutputStream(os, BUFFER_SIZE); JarOutputStream jos = new JarOutputStream(bos)) { for (Map.Entry<String, byte[]> resource : generatedResources.entrySet()) { - if (!resource.getKey().endsWith(".class")) { - addEntry(jos, resource.getKey(), resource.getValue()); - } + addEntry(jos, resource.getKey(), resource.getValue()); } } } diff --git a/javatests/com/google/turbine/main/MainTest.java b/javatests/com/google/turbine/main/MainTest.java index ea6f9a8..5d47632 100644 --- a/javatests/com/google/turbine/main/MainTest.java +++ b/javatests/com/google/turbine/main/MainTest.java @@ -37,6 +37,7 @@ import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.UncheckedIOException; import java.io.Writer; import java.nio.file.Files; @@ -64,6 +65,9 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; @RunWith(JUnit4.class) public class MainTest { @@ -401,4 +405,70 @@ public class MainTest { .containsExactly("META-INF/", "META-INF/MANIFEST.MF"); } } + + @SupportedAnnotationTypes("*") + public static class ClassGeneratingProcessor extends AbstractProcessor { + + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + + private boolean first = true; + + @Override + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + if (first) { + try (OutputStream outputStream = + processingEnv.getFiler().createClassFile("g.Gen").openOutputStream()) { + outputStream.write(dump()); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + first = false; + } + return false; + } + + public static byte[] dump() { + ClassWriter classWriter = new ClassWriter(0); + classWriter.visit( + Opcodes.V1_8, + Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, + "g/Gen", + null, + "java/lang/Object", + null); + { + MethodVisitor methodVisitor = + classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); + methodVisitor.visitCode(); + methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); + methodVisitor.visitMethodInsn( + Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); + methodVisitor.visitInsn(Opcodes.RETURN); + methodVisitor.visitMaxs(1, 1); + methodVisitor.visitEnd(); + } + classWriter.visitEnd(); + return classWriter.toByteArray(); + } + } + + @Test + public void classGeneration() throws IOException { + Path src = temporaryFolder.newFile("package-info.jar").toPath(); + MoreFiles.asCharSink(src, UTF_8).write("@Deprecated package test;"); + File resources = temporaryFolder.newFile("resources.jar"); + Main.compile( + optionsWithBootclasspath() + .setProcessors(ImmutableList.of(ClassGeneratingProcessor.class.getName())) + .setSources(ImmutableList.of(src.toString())) + .setResourceOutput(resources.toString()) + .build()); + try (JarFile jarFile = new JarFile(resources); + Stream<JarEntry> entries = jarFile.stream()) { + assertThat(entries.map(JarEntry::getName)).containsExactly("g/Gen.class"); + } + } } |