Skip to content

Commit

Permalink
Make AutoBazelRepositoryProcessor compatible with Java 8
Browse files Browse the repository at this point in the history
The three-argument overload of `Stream#iterate` is only available as of Java 9.

Fixes bazelbuild#17458

Closes bazelbuild#17460.

PiperOrigin-RevId: 509885763
Change-Id: If83d832b3590b8598c0f4163afa3c7054e7c9786
  • Loading branch information
fmeum authored and copybara-github committed Feb 15, 2023
1 parent c45d6cc commit 420659a
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions tools/java/runfiles/AutoBazelRepositoryProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -51,7 +47,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
.flatMap(element -> roundEnv.getElementsAnnotatedWith(element).stream())
.map(element -> (TypeElement) element)
.forEach(this::emitClass);
return true;
return false;
}

private void emitClass(TypeElement annotatedClass) {
Expand Down Expand Up @@ -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<String> 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<String> 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();
Expand Down

0 comments on commit 420659a

Please sign in to comment.