Use system property for lambda dir if set

If system property that triggers writing lambda
classes is already set, use that value for the
lambda dump directory path. Otherwise, generate
a temporary dir path, and set it.

RELNOTES: n/a
PiperOrigin-RevId: 154440327
GitOrigin-RevId: a50a56cf9cd6c0f7c459b265213669b3a2f7ee5e
Change-Id: Ia273792f2f7e89758d962a3dfe051074691c0e59
diff --git a/java/com/google/devtools/build/android/desugar/Desugar.java b/java/com/google/devtools/build/android/desugar/Desugar.java
index c4528ae..cd6b6ab 100644
--- a/java/com/google/devtools/build/android/desugar/Desugar.java
+++ b/java/com/google/devtools/build/android/desugar/Desugar.java
@@ -522,14 +522,20 @@
    * LambdaClassMaker generates lambda classes for us, but it does so by essentially simulating the
    * call to LambdaMetafactory that the JVM would make when encountering an invokedynamic.
    * LambdaMetafactory is in the JDK and its implementation has a property to write out ("dump")
-   * generated classes, which we take advantage of here. Set property before doing anything else
-   * since the property is read in the static initializer; if this breaks we can investigate setting
-   * the property when calling the tool.
+   * generated classes, which we take advantage of here. This property can be set externally, and in
+   * that case the specified directory is used as a temporary dir. Otherwise, it will be set here,
+   * before doing anything else since the property is read in the static initializer.
    */
   private static Path createAndRegisterLambdaDumpDirectory() throws IOException {
-    Path dumpDirectory = Files.createTempDirectory("lambdas");
-    System.setProperty(
-        LambdaClassMaker.LAMBDA_METAFACTORY_DUMPER_PROPERTY, dumpDirectory.toString());
+    String propertyValue = System.getProperty(LambdaClassMaker.LAMBDA_METAFACTORY_DUMPER_PROPERTY);
+    Path dumpDirectory;
+    if (propertyValue != null) {
+      dumpDirectory = Paths.get(propertyValue);
+    } else {
+      dumpDirectory = Files.createTempDirectory("lambdas");
+      System.setProperty(
+          LambdaClassMaker.LAMBDA_METAFACTORY_DUMPER_PROPERTY, dumpDirectory.toString());
+    }
 
     deleteTreeOnExit(dumpDirectory);
     return dumpDirectory;