Skip to content

Commit

Permalink
Migrate RootProcessor to XProcessing.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 517148453
  • Loading branch information
kuanyingchou authored and Dagger Team committed Mar 26, 2023
1 parent a7f6b79 commit 51abc9b
Show file tree
Hide file tree
Showing 45 changed files with 764 additions and 759 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static java.util.Comparator.comparing;

import androidx.room.compiler.processing.JavaPoetExtKt;
import androidx.room.compiler.processing.XFiler.Mode;
import androidx.room.compiler.processing.XProcessingEnv;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
Expand All @@ -40,7 +42,6 @@
import dagger.multibindings.IntoMap;
import dagger.multibindings.IntoSet;
import java.io.IOException;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Modifier;

/**
Expand All @@ -49,12 +50,12 @@
final class BindValueGenerator {
private static final String SUFFIX = "_BindValueModule";

private final ProcessingEnvironment env;
private final XProcessingEnv env;
private final BindValueMetadata metadata;
private final ClassName testClassName;
private final ClassName className;

BindValueGenerator(ProcessingEnvironment env, BindValueMetadata metadata) {
BindValueGenerator(XProcessingEnv env, BindValueMetadata metadata) {
this.env = env;
this.metadata = metadata;
testClassName = metadata.testElement().getClassName();
Expand Down Expand Up @@ -83,9 +84,8 @@ void generate() throws IOException {
.sorted(comparing(MethodSpec::toString))
.forEachOrdered(builder::addMethod);

JavaFile.builder(className.packageName(), builder.build())
.build()
.writeTo(env.getFiler());
env.getFiler()
.write(JavaFile.builder(className.packageName(), builder.build()).build(), Mode.Isolating);
}

// @Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void postRoundProcess(XRoundEnv roundEnv) throws Exception {
// Generate a module for each testing class with a @BindValue field.
for (Map.Entry<XTypeElement, Collection<XElement>> e : testRootMap.asMap().entrySet()) {
BindValueMetadata metadata = BindValueMetadata.create(e.getKey(), e.getValue());
new BindValueGenerator(getProcessingEnv(), metadata).generate();
new BindValueGenerator(processingEnv(), metadata).generate();
}
}

Expand Down
36 changes: 23 additions & 13 deletions java/dagger/hilt/processor/internal/AggregatedElements.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package dagger.hilt.processor.internal;

import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static androidx.room.compiler.processing.compat.XConverters.toXProcessing;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static javax.lang.model.element.Modifier.PUBLIC;

import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XTypeElement;
import com.google.auto.common.MoreElements;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
Expand All @@ -31,35 +34,34 @@
public final class AggregatedElements {

/** Returns the class name of the proxy or {@link Optional#empty()} if a proxy is not needed. */
public static Optional<ClassName> aggregatedElementProxyName(TypeElement aggregatedElement) {
if (aggregatedElement.getModifiers().contains(PUBLIC)) {
public static Optional<ClassName> aggregatedElementProxyName(XTypeElement aggregatedElement) {
if (aggregatedElement.isPublic() && !aggregatedElement.isInternal()) {
// Public aggregated elements do not have proxies.
return Optional.empty();
}
ClassName name = ClassName.get(aggregatedElement);
ClassName name = aggregatedElement.getClassName();
// To avoid going over the class name size limit, just prepend a single character.
return Optional.of(name.peerClass("_" + name.simpleName()));
}

/** Returns back the set of input {@code aggregatedElements} with all proxies unwrapped. */
public static ImmutableSet<TypeElement> unwrapProxies(
ImmutableSet<TypeElement> aggregatedElements, Elements elements) {
public static ImmutableSet<XTypeElement> unwrapProxies(
ImmutableSet<XTypeElement> aggregatedElements) {
return aggregatedElements.stream()
.map(aggregatedElement -> unwrapProxy(aggregatedElement, elements))
.map(AggregatedElements::unwrapProxy)
.collect(toImmutableSet());
}

private static TypeElement unwrapProxy(TypeElement element, Elements elements) {
return Processors.hasAnnotation(element, ClassNames.AGGREGATED_ELEMENT_PROXY)
private static XTypeElement unwrapProxy(XTypeElement element) {
return element.hasAnnotation(ClassNames.AGGREGATED_ELEMENT_PROXY)
? Processors.getAnnotationClassValue(
elements,
Processors.getAnnotationMirror(element, ClassNames.AGGREGATED_ELEMENT_PROXY),
"value")
element.getAnnotation(ClassNames.AGGREGATED_ELEMENT_PROXY), "value")
: element;
}

// TODO(kuanyingchou): Migrate this once we have suitable APIs for packages in XProcessing.
/** Returns all aggregated elements in the aggregating package after validating them. */
public static ImmutableSet<TypeElement> from(
private static ImmutableSet<TypeElement> from(
String aggregatingPackage, ClassName aggregatingAnnotation, Elements elements) {
PackageElement packageElement = elements.getPackageElement(aggregatingPackage);

Expand Down Expand Up @@ -95,5 +97,13 @@ public static ImmutableSet<TypeElement> from(
return aggregatedElements;
}

/** Returns all aggregated elements in the aggregating package after validating them. */
public static ImmutableSet<XTypeElement> from(
String aggregatingPackage, ClassName aggregatingAnnotation, XProcessingEnv env) {
return from(aggregatingPackage, aggregatingAnnotation, toJavac(env).getElementUtils()).stream()
.map(element -> toXProcessing(element, env))
.collect(toImmutableSet());
}

private AggregatedElements() {}
}
1 change: 1 addition & 0 deletions java/dagger/hilt/processor/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ java_library(
":processor_errors",
":processors",
"//java/dagger/internal/codegen/extension",
"//java/dagger/internal/codegen/xprocessing",
"//third_party/java/auto:common",
"//third_party/java/guava/collect",
"//third_party/java/javapoet",
Expand Down

0 comments on commit 51abc9b

Please sign in to comment.