Skip to content

Commit

Permalink
Copy non-class files from input directory to output directory as-is
Browse files Browse the repository at this point in the history
Closes #54
  • Loading branch information
luontola committed Jul 8, 2015
1 parent 6f49e17 commit 46b0d84
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ Version History
- Removes bytecode references to `java.lang.invoke.MethodHandles.Lookup` on
Java 6 and older
([Issue #61](https://github.com/orfjackal/retrolambda/issues/61))
- Copies non-class files from input to output directory
([Issue #54](https://github.com/orfjackal/retrolambda/issues/54))

### Retrolambda 2.0.3 (2015-06-07)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ public static void run(Config config) throws Throwable {

visitFiles(inputDir, includedFiles, new BytecodeFileVisitor() {
@Override
protected void visit(byte[] bytecode) {
protected void visitClass(byte[] bytecode) {
analyzer.analyze(bytecode);
}

@Override
protected void visitResource(Path relativePath, byte[] content) throws IOException {
saver.saveResource(relativePath, content);
}
});

// Because Transformers.backportLambdaClass() analyzes the lambda class,
Expand All @@ -75,7 +80,7 @@ protected void visit(byte[] bytecode) {
// We need to load some of the classes (for calling the lambda metafactory)
// so we need to take care not to modify any bytecode before loading them.
for (byte[] bytecode : transformed) {
saver.save(bytecode);
saver.saveClass(bytecode);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2013-2014 Esko Luontola <www.orfjackal.net>
// Copyright © 2013-2015 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

Expand All @@ -10,15 +10,32 @@

public abstract class BytecodeFileVisitor extends SimpleFileVisitor<Path> {

private Path baseDir;

@Override
public FileVisitResult visitFile(Path inputFile, BasicFileAttributes attrs) throws IOException {
if (isJavaClass(inputFile)) {
visit(Files.readAllBytes(inputFile));
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (baseDir == null) {
baseDir = dir;
}
return super.preVisitDirectory(dir, attrs);
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path relativePath = baseDir.relativize(file);
byte[] content = Files.readAllBytes(file);

if (isJavaClass(relativePath)) {
visitClass(content);
} else {
visitResource(relativePath, content);
}
return FileVisitResult.CONTINUE;
}

protected abstract void visit(byte[] bytecode);
protected abstract void visitClass(byte[] bytecode) throws IOException;

protected abstract void visitResource(Path relativePath, byte[] content) throws IOException;

private static boolean isJavaClass(Path file) {
return file.getFileName().toString().endsWith(".class");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2013-2014 Esko Luontola <www.orfjackal.net>
// Copyright © 2013-2015 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

Expand All @@ -17,13 +17,18 @@ public ClassSaver(Path outputDir) {
this.outputDir = outputDir;
}

public void save(byte[] bytecode) throws IOException {
public void saveClass(byte[] bytecode) throws IOException {
if (bytecode == null) {
return;
}
ClassReader cr = new ClassReader(bytecode);
Path outputFile = outputDir.resolve(cr.getClassName() + ".class");
Path relativePath = Paths.get(cr.getClassName() + ".class");
saveResource(relativePath, bytecode);
}

public void saveResource(Path relativePath, byte[] content) throws IOException {
Path outputFile = outputDir.resolve(relativePath);
Files.createDirectories(outputFile.getParent());
Files.write(outputFile, bytecode);
Files.write(outputFile, content);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2013-2014 Esko Luontola <www.orfjackal.net>
// Copyright © 2013-2015 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

Expand Down Expand Up @@ -27,7 +27,7 @@ public void saveIfLambda(String className, byte[] bytecode) {
private void reifyLambdaClass(String className, byte[] bytecode) {
try {
System.out.println("Saving lambda class: " + className);
saver.save(transformers.backportLambdaClass(new ClassReader(bytecode)));
saver.saveClass(transformers.backportLambdaClass(new ClassReader(bytecode)));

} catch (Throwable t) {
// print to stdout to keep in sync with other log output
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2013-2014 Esko Luontola <www.orfjackal.net>
// Copyright © 2013-2015 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

Expand All @@ -14,13 +14,15 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertTrue;

public class RetrolambdaTest {

@Rule
public final TemporaryFolder tempDir = new TemporaryFolder();

private Path inputDir;
private Path outputDir;

private final List<Path> visitedFiles = new ArrayList<>();
private final FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
Expand All @@ -38,6 +40,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
@Before
public void setup() throws IOException {
inputDir = tempDir.newFolder("inputDir").toPath();
outputDir = tempDir.newFolder("outputDir").toPath();
file1 = Files.createFile(inputDir.resolve("file1.txt"));
file2 = Files.createFile(inputDir.resolve("file2.txt"));
Path subdir = inputDir.resolve("subdir");
Expand Down Expand Up @@ -70,4 +73,21 @@ public void ignores_included_files_that_are_outside_the_input_directory() throws

assertThat(visitedFiles, containsInAnyOrder(file1));
}

@Test
public void copies_resources_to_output_directory() throws Throwable {
Properties p = new Properties();
p.setProperty(Config.INPUT_DIR, inputDir.toString());
p.setProperty(Config.OUTPUT_DIR, outputDir.toString());
p.setProperty(Config.CLASSPATH, "");

Retrolambda.run(new Config(p));

assertIsFile(outputDir.resolve("file1.txt"));
assertIsFile(outputDir.resolve("subdir/file.txt"));
}

private static void assertIsFile(Path path) {
assertTrue("Expected " + path + " to be a file", Files.isRegularFile(path));
}
}

0 comments on commit 46b0d84

Please sign in to comment.