From 420659a9ad2a98f57e057d8c22eb621e3b12803e Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Wed, 15 Feb 2023 11:17:03 -0800 Subject: [PATCH] Make AutoBazelRepositoryProcessor compatible with Java 8 The three-argument overload of `Stream#iterate` is only available as of Java 9. Fixes #17458 Closes #17460. PiperOrigin-RevId: 509885763 Change-Id: If83d832b3590b8598c0f4163afa3c7054e7c9786 --- .../AutoBazelRepositoryProcessor.java | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/tools/java/runfiles/AutoBazelRepositoryProcessor.java b/tools/java/runfiles/AutoBazelRepositoryProcessor.java index ae356d800f6d03..f323cf211ccfc5 100644 --- a/tools/java/runfiles/AutoBazelRepositoryProcessor.java +++ b/tools/java/runfiles/AutoBazelRepositoryProcessor.java @@ -14,22 +14,18 @@ package com.google.devtools.build.runfiles; -import static java.util.stream.Collectors.joining; -import static java.util.stream.Collectors.toList; import java.io.IOException; import java.io.PrintWriter; -import java.util.Collections; -import java.util.List; +import java.util.ArrayDeque; +import java.util.Deque; import java.util.Set; -import java.util.stream.Stream; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedOptions; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; -import javax.lang.model.element.Name; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic.Kind; @@ -51,7 +47,7 @@ public boolean process(Set annotations, RoundEnvironment .flatMap(element -> roundEnv.getElementsAnnotatedWith(element).stream()) .map(element -> (TypeElement) element) .forEach(this::emitClass); - return true; + return false; } private void emitClass(TypeElement annotatedClass) { @@ -80,18 +76,14 @@ private void emitClass(TypeElement annotatedClass) { // AutoBazelRepository_Outer_Middle_Inner. // Note: There can be collisions when local classes are involved, but since the definition of a // class depends only on the containing Bazel target, this does not result in ambiguity. - List nestedClassNames = - Stream.iterate( - annotatedClass, - element -> element instanceof TypeElement, - Element::getEnclosingElement) - .map(Element::getSimpleName) - .map(Name::toString) - .collect(toList()); - Collections.reverse(nestedClassNames); - String generatedClassSimpleName = - Stream.concat(Stream.of("AutoBazelRepository"), nestedClassNames.stream()) - .collect(joining("_")); + Deque classNameSegments = new ArrayDeque<>(); + Element element = annotatedClass; + while (element instanceof TypeElement) { + classNameSegments.addFirst(element.getSimpleName().toString()); + element = element.getEnclosingElement(); + } + classNameSegments.addFirst("AutoBazelRepository"); + String generatedClassSimpleName = String.join("_", classNameSegments); String generatedClassPackage = processingEnv.getElementUtils().getPackageOf(annotatedClass).getQualifiedName().toString();