From fd2b562aef6831e9707ee3ef7651d30fcbe9ab0b Mon Sep 17 00:00:00 2001 From: Filip Lindqvist Date: Thu, 10 Dec 2015 21:09:36 +0100 Subject: [PATCH 1/2] Added a file parameter for file inclusion --- .../net/orfjackal/retrolambda/Config.java | 30 +++++++++++- .../orfjackal/retrolambda/Retrolambda.java | 6 ++- .../net/orfjackal/retrolambda/ConfigTest.java | 46 ++++++++++++++++++- 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/retrolambda/src/main/java/net/orfjackal/retrolambda/Config.java b/retrolambda/src/main/java/net/orfjackal/retrolambda/Config.java index 67c51425..015e145d 100644 --- a/retrolambda/src/main/java/net/orfjackal/retrolambda/Config.java +++ b/retrolambda/src/main/java/net/orfjackal/retrolambda/Config.java @@ -4,9 +4,13 @@ package net.orfjackal.retrolambda; +import com.google.common.base.Charsets; +import com.google.common.io.*; +import com.google.common.io.Files; import org.objectweb.asm.Opcodes; -import java.io.File; +import java.io.*; +import java.nio.charset.Charset; import java.nio.file.*; import java.util.*; import java.util.stream.Collectors; @@ -20,6 +24,7 @@ public class Config { public static final String OUTPUT_DIR = PREFIX + "outputDir"; public static final String CLASSPATH = PREFIX + "classpath"; public static final String INCLUDED_FILES = PREFIX + "includedFiles"; + public static final String INCLUDED_FILE = PREFIX + "includedFile"; private static final List requiredProperties = new ArrayList<>(); private static final List requiredPropertiesHelp = new ArrayList<>(); @@ -159,6 +164,29 @@ public List getIncludedFiles() { .collect(Collectors.toList()); } + static { + optionalParameterHelp(INCLUDED_FILE, + "A file listing the files to process (one file per line), instead of processing all files.", + "This is useful for a build tool to support incremental compilation."); + } + + public List getIncludedFileList() { + String filePath = p.getProperty(INCLUDED_FILE); + if (filePath == null) { + return null; + } + File file = Paths.get(filePath).toFile(); + + try { + return Files.readLines(file, Charsets.UTF_8).stream() + .filter(s -> !s.isEmpty()) + .map(Paths::get) + .collect(Collectors.toList()); + } catch (IOException e) { + return Collections.emptyList(); + } + } + // help public String getHelp() { diff --git a/retrolambda/src/main/java/net/orfjackal/retrolambda/Retrolambda.java b/retrolambda/src/main/java/net/orfjackal/retrolambda/Retrolambda.java index 1d3f9b5b..5270cbfe 100644 --- a/retrolambda/src/main/java/net/orfjackal/retrolambda/Retrolambda.java +++ b/retrolambda/src/main/java/net/orfjackal/retrolambda/Retrolambda.java @@ -21,13 +21,15 @@ public static void run(Config config) throws Throwable { Path inputDir = config.getInputDir(); Path outputDir = config.getOutputDir(); String classpath = config.getClasspath(); - List includedFiles = config.getIncludedFiles(); + List includedFiles = new ArrayList<>(); + includedFiles.addAll(config.getIncludedFileList()); + includedFiles.addAll(config.getIncludedFiles()); System.out.println("Bytecode version: " + bytecodeVersion + " (" + config.getJavaVersion() + ")"); System.out.println("Default methods: " + defaultMethodsEnabled); System.out.println("Input directory: " + inputDir); System.out.println("Output directory: " + outputDir); System.out.println("Classpath: " + classpath); - if (includedFiles != null) { + if (!includedFiles.isEmpty()) { System.out.println("Included files: " + includedFiles.size()); } diff --git a/retrolambda/src/test/java/net/orfjackal/retrolambda/ConfigTest.java b/retrolambda/src/test/java/net/orfjackal/retrolambda/ConfigTest.java index 717a5f67..caabf4bb 100644 --- a/retrolambda/src/test/java/net/orfjackal/retrolambda/ConfigTest.java +++ b/retrolambda/src/test/java/net/orfjackal/retrolambda/ConfigTest.java @@ -4,12 +4,17 @@ package net.orfjackal.retrolambda; +import com.google.common.base.*; +import com.google.common.collect.Lists; +import com.google.common.io.Files; import org.junit.*; import org.junit.rules.ExpectedException; -import java.io.File; +import java.io.*; import java.nio.file.Paths; import java.util.*; +import java.util.Optional; +import java.util.stream.Collectors; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -72,4 +77,41 @@ public void included_files() { systemProperties.setProperty(Config.INCLUDED_FILES, "/foo/one.class" + File.pathSeparator + "/foo/two.class"); assertThat("multiple values", config().getIncludedFiles(), is(Arrays.asList(Paths.get("/foo/one.class"), Paths.get("/foo/two.class")))); } -} + + @Test + public void included_file() throws IOException { + assertThat("not set", config().getIncludedFileList(), is(nullValue())); + + systemProperties.setProperty(Config.INCLUDED_FILE, ""); + assertThat("zero values", config().getIncludedFileList(), is(empty())); + + // Single file + File singleTmp = File.createTempFile("test",".list"); + singleTmp.deleteOnExit(); + + List file = Lists.newArrayList("foo.java"); + String delimiter = System.getProperty("line.separator"); + Files.write(file.stream() + .collect(Collectors.joining(delimiter)), singleTmp, Charsets.UTF_8); + + systemProperties.setProperty(Config.INCLUDED_FILE, singleTmp.getAbsolutePath()); + assertThat("one value", config().getIncludedFileList(), is( + file.stream() + .map(f -> Paths.get(f)) + .collect(Collectors.toList()))); + + // Multiple files + File multiTmp = File.createTempFile("test",".list"); + multiTmp.deleteOnExit(); + + List files = Lists.newArrayList("foo.java", "bar.java"); + Files.write(files.stream() + .collect(Collectors.joining(delimiter)), multiTmp, Charsets.UTF_8); + + systemProperties.setProperty(Config.INCLUDED_FILE, multiTmp.getAbsolutePath()); + assertThat("two values", config().getIncludedFileList(), is( + files.stream() + .map(f -> Paths.get(f)) + .collect(Collectors.toList()))); + } +} \ No newline at end of file From 2ee88fb03bdf47b46288637e8545708a2ad5399c Mon Sep 17 00:00:00 2001 From: Filip Lindqvist Date: Thu, 10 Dec 2015 21:19:53 +0100 Subject: [PATCH 2/2] Fixed the null case of include files, and updated docs --- README.md | 4 ++++ .../net/orfjackal/retrolambda/Retrolambda.java | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3a4b1cc0..34f1f79a 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,10 @@ Optional system properties: List of files to process, instead of processing all files. This is useful for a build tool to support incremental compilation. + retrolambda.includedFile + A file listing the files to process (one file per line), instead of processing all files. + This is useful for a build tool to support incremental compilation. + If the Java agent is used, then Retrolambda will use it to capture the lambda classes generated by Java. Otherwise Retrolambda will hook into Java's internal lambda dumping API, which is more susceptible to suddenly diff --git a/retrolambda/src/main/java/net/orfjackal/retrolambda/Retrolambda.java b/retrolambda/src/main/java/net/orfjackal/retrolambda/Retrolambda.java index 5270cbfe..83fdf4fa 100644 --- a/retrolambda/src/main/java/net/orfjackal/retrolambda/Retrolambda.java +++ b/retrolambda/src/main/java/net/orfjackal/retrolambda/Retrolambda.java @@ -4,6 +4,7 @@ package net.orfjackal.retrolambda; +import com.google.common.collect.Lists; import net.orfjackal.retrolambda.files.*; import net.orfjackal.retrolambda.interfaces.*; import net.orfjackal.retrolambda.lambdas.*; @@ -21,14 +22,22 @@ public static void run(Config config) throws Throwable { Path inputDir = config.getInputDir(); Path outputDir = config.getOutputDir(); String classpath = config.getClasspath(); - List includedFiles = new ArrayList<>(); - includedFiles.addAll(config.getIncludedFileList()); - includedFiles.addAll(config.getIncludedFiles()); + List includedFilesByArgument = config.getIncludedFileList(); + List includedFilesByFileList = config.getIncludedFiles(); + System.out.println("Bytecode version: " + bytecodeVersion + " (" + config.getJavaVersion() + ")"); System.out.println("Default methods: " + defaultMethodsEnabled); System.out.println("Input directory: " + inputDir); System.out.println("Output directory: " + outputDir); System.out.println("Classpath: " + classpath); + + List includedFiles = Lists.newArrayList(); + if (includedFilesByArgument != null) { + includedFiles.addAll(includedFilesByArgument); + } + if (includedFilesByFileList != null) { + includedFiles.addAll(includedFilesByFileList); + } if (!includedFiles.isEmpty()) { System.out.println("Included files: " + includedFiles.size()); }