From 70cdac4c8d6bc43601f72a2d0f4cb2d296b284b1 Mon Sep 17 00:00:00 2001 From: Evan Tatarka Date: Wed, 2 Jul 2014 19:41:14 -0400 Subject: [PATCH] Added argument to tell only which files have changed This is useful for incremental compiling, where you only want to process some of the files in the dir. --- README.md | 5 +++ .../net/orfjackal/retrolambda/Config.java | 20 ++++++++++- .../java/net/orfjackal/retrolambda/Main.java | 34 +++++++++++++++---- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e69bf281..f7326363 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,11 @@ Optional system properties: retrolambda.outputDir Output directory into where the generated class files are written. Defaults to same as retrolambda.inputDir + + retrolambda.changed + A list of all the files that have changed since last run. + This is useful for a build tool to support incremental compilation. + ``` diff --git a/retrolambda/src/main/java/net/orfjackal/retrolambda/Config.java b/retrolambda/src/main/java/net/orfjackal/retrolambda/Config.java index 7d40c53c..5cfdd9d6 100644 --- a/retrolambda/src/main/java/net/orfjackal/retrolambda/Config.java +++ b/retrolambda/src/main/java/net/orfjackal/retrolambda/Config.java @@ -6,8 +6,11 @@ import org.objectweb.asm.Opcodes; -import java.nio.file.*; +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.*; +import java.util.stream.Collectors; public class Config { @@ -16,6 +19,7 @@ public class Config { private static final String INPUT_DIR = PREFIX + "inputDir"; private static final String OUTPUT_DIR = PREFIX + "outputDir"; private static final String CLASSPATH = PREFIX + "classpath"; + private static final String CHANGED = PREFIX + "changed"; private static final List requiredProperties = new ArrayList<>(); private static final List requiredProperitesHelp = new ArrayList<>(); @@ -119,6 +123,20 @@ private String getRequiredProperty(String key) { return value; } + // incremental files + + static { + optionalParameterHelp(CHANGED, + "A list of all the files that have changed since last run.", + "This is useful for a build tool to support incremental compilation."); + } + + public List getChangedFiles() { + String files = p.getProperty(CHANGED); + if (files == null) return null; + return Arrays.asList(files.split(File.pathSeparator)).stream() + .map(Paths::get).collect(Collectors.toList()); + } // help diff --git a/retrolambda/src/main/java/net/orfjackal/retrolambda/Main.java b/retrolambda/src/main/java/net/orfjackal/retrolambda/Main.java index 7ae38508..08204853 100644 --- a/retrolambda/src/main/java/net/orfjackal/retrolambda/Main.java +++ b/retrolambda/src/main/java/net/orfjackal/retrolambda/Main.java @@ -4,10 +4,19 @@ package net.orfjackal.retrolambda; -import java.io.*; -import java.net.*; -import java.nio.file.*; -import java.util.*; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; public class Main { @@ -23,10 +32,14 @@ public static void main(String[] args) { Path inputDir = config.getInputDir(); Path outputDir = config.getOutputDir(); String classpath = config.getClasspath(); + List changedFiles = config.getChangedFiles(); System.out.println("Bytecode version: " + bytecodeVersion + " (" + config.getJavaVersion() + ")"); System.out.println("Input directory: " + inputDir); System.out.println("Output directory: " + outputDir); System.out.println("Classpath: " + classpath); + if (changedFiles != null) { + System.out.println("Changed: " + changedFiles.size() + " files"); + } if (!Files.isDirectory(inputDir)) { System.out.println("Nothing to do; not a directory: " + inputDir); @@ -35,12 +48,21 @@ public static void main(String[] args) { try { Thread.currentThread().setContextClassLoader(new URLClassLoader(asUrls(classpath))); - Files.walkFileTree(inputDir, new BytecodeTransformingFileVisitor(inputDir, outputDir) { + + BytecodeTransformingFileVisitor visitor = new BytecodeTransformingFileVisitor(inputDir, outputDir) { + @Override protected byte[] transform(byte[] bytecode) { return LambdaUsageBackporter.transform(bytecode, bytecodeVersion); } - }); + }; + if (changedFiles == null) { + Files.walkFileTree(inputDir, visitor); + } else { + for (Path inputFile : changedFiles) { + visitor.visitFile(inputFile, Files.readAttributes(inputFile, BasicFileAttributes.class)); + } + } } catch (Throwable t) { System.out.println("Error! Failed to transform some classes"); t.printStackTrace(System.out);