Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.1.0]Make AutoBazelRepositoryProcessor compatible with Java 8 #17504

Merged
merged 2 commits into from
Feb 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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