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

Migrate RootProcessor to XProcessing. #3808

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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