Skip to content

Commit

Permalink
Add aggregating data for roots. Also add tests to verify illegal comb…
Browse files Browse the repository at this point in the history
…inations.

This CL adds AggregatedRoot for each root found and AggregatedRootSentinel for each root processed. We can then use this information to determine if the roots from past and current compilation units are valid.

RELNOTES=N/A
PiperOrigin-RevId: 369316983
  • Loading branch information
bcorso authored and Dagger Team committed Apr 19, 2021
1 parent fa49bdf commit e33bb84
Show file tree
Hide file tree
Showing 22 changed files with 938 additions and 33 deletions.
4 changes: 4 additions & 0 deletions java/dagger/hilt/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ android_library(
"//java/dagger/hilt/internal:component_manager",
"//java/dagger/hilt/internal:generated_component",
"//java/dagger/hilt/internal:generated_entry_point",
"//java/dagger/hilt/internal/aggregatedroot",
"//java/dagger/hilt/internal/processedrootsentinel",
"//java/dagger/hilt/migration:disable_install_in_check",
"@maven//:androidx_activity_activity",
"@maven//:androidx_annotation_annotation",
Expand Down Expand Up @@ -194,6 +196,8 @@ gen_maven_artifact(
"//java/dagger/hilt/internal:component_entry_point",
"//java/dagger/hilt/internal:generated_entry_point",
"//java/dagger/hilt/internal:test_singleton_component_manager",
"//java/dagger/hilt/internal/aggregatedroot:aggregatedroot",
"//java/dagger/hilt/internal/processedrootsentinel:processedrootsentinel",
],
artifact_target_maven_deps = [
"androidx.activity:activity",
Expand Down
2 changes: 2 additions & 0 deletions java/dagger/hilt/android/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ android_library(
"//java/dagger/hilt/internal:generated_entry_point",
"//java/dagger/hilt/internal:preconditions",
"//java/dagger/hilt/internal:test_singleton_component",
"//java/dagger/hilt/internal/aggregatedroot",
"//java/dagger/hilt/internal/processedrootsentinel",
"//java/dagger/hilt/migration:disable_install_in_check",
"@maven//:androidx_annotation_annotation",
"@maven//:androidx_multidex_multidex",
Expand Down
34 changes: 34 additions & 0 deletions java/dagger/hilt/internal/aggregatedroot/AggregatedRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2021 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dagger.hilt.internal.aggregatedroot;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* An annotation used to aggregate {@link dagger.hilt.android.HiltAndroidApp} and {@link
* dagger.hilt.android.testing.HiltAndroidTest} roots.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface AggregatedRoot {
String root();

Class<?> rootAnnotation();
}
28 changes: 28 additions & 0 deletions java/dagger/hilt/internal/aggregatedroot/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (C) 2021 The Dagger Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Description:
# The annotation for aggregating information about Hilt roots.

package(default_visibility = ["//:src"])

java_library(
name = "aggregatedroot",
srcs = ["AggregatedRoot.java"],
)

filegroup(
name = "srcs_filegroup",
srcs = glob(["*"]),
)
28 changes: 28 additions & 0 deletions java/dagger/hilt/internal/processedrootsentinel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (C) 2021 The Dagger Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Description:
# The annotation for aggregating information about processed Hilt roots.

package(default_visibility = ["//:src"])

java_library(
name = "processedrootsentinel",
srcs = ["ProcessedRootSentinel.java"],
)

filegroup(
name = "srcs_filegroup",
srcs = glob(["*"]),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2021 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dagger.hilt.internal.processedrootsentinel;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/** An annotation used to aggregate sentinels for processed roots. */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface ProcessedRootSentinel {
/** Returns the set of roots processed in a previous build. */
String[] roots();
}
25 changes: 25 additions & 0 deletions java/dagger/hilt/processor/internal/AnnotationValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@

import static com.google.auto.common.AnnotationMirrors.getAnnotationValue;
import static com.google.auto.common.AnnotationMirrors.getAnnotationValuesWithDefaults;
import static com.google.auto.common.MoreTypes.asTypeElement;
import static com.google.common.base.Preconditions.checkNotNull;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;

import com.google.auto.common.MoreTypes;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.List;
import java.util.Optional;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.AnnotationValueVisitor;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
Expand Down Expand Up @@ -87,6 +91,18 @@ public VariableElement visitEnumConstant(VariableElement value, Void unused) {
}
}

/** Returns a class array value as a set of {@link TypeElement}. */
public static ImmutableSet<TypeElement> getTypeElements(AnnotationValue value) {
return getAnnotationValues(value).stream()
.map(AnnotationValues::getTypeElement)
.collect(toImmutableSet());
}

/** Returns a class value as a {@link TypeElement}. */
public static TypeElement getTypeElement(AnnotationValue value) {
return asTypeElement(getTypeMirror(value));
}

/**
* Returns the value as a VariableElement.
*
Expand All @@ -105,6 +121,15 @@ public static String getString(AnnotationValue value) {
return valueOfType(value, String.class);
}

/**
* Returns the value as a boolean.
*
* @throws IllegalArgumentException if the value is not a boolean.
*/
public static boolean getBoolean(AnnotationValue value) {
return valueOfType(value, Boolean.class);
}

private static <T> T valueOfType(AnnotationValue annotationValue, Class<T> type) {
Object value = annotationValue.getValue();
if (!type.isInstance(value)) {
Expand Down
9 changes: 9 additions & 0 deletions java/dagger/hilt/processor/internal/ClassNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@

/** Holder for commonly used class names. */
public final class ClassNames {
public static final String AGGREGATED_ROOT_PACKAGE =
"dagger.hilt.internal.aggregatedroot.codegen";
public static final ClassName AGGREGATED_ROOT =
get("dagger.hilt.internal.aggregatedroot", "AggregatedRoot");
public static final String PROCESSED_ROOT_SENTINEL_PACKAGE =
"dagger.hilt.internal.processedrootsentinel.codegen";
public static final ClassName PROCESSED_ROOT_SENTINEL =
get("dagger.hilt.internal.processedrootsentinel", "ProcessedRootSentinel");

public static final String AGGREGATED_EARLY_ENTRY_POINT_PACKAGE =
"dagger.hilt.android.internal.earlyentrypoint.codegen";
public static final ClassName AGGREGATED_EARLY_ENTRY_POINT =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2021 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dagger.hilt.processor.internal.root;

import com.squareup.javapoet.AnnotationSpec;
import dagger.hilt.processor.internal.ClassNames;
import dagger.hilt.processor.internal.Processors;
import java.io.IOException;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;

/** Generates an {@link dagger.hilt.internal.aggregatedroot.AggregatedRoot}. */
final class AggregatedRootGenerator {
private final TypeElement rootElement;
private final TypeElement rootAnnotation;
private final ProcessingEnvironment processingEnv;

AggregatedRootGenerator(
TypeElement rootElement, TypeElement rootAnnotation, ProcessingEnvironment processingEnv) {
this.rootElement = rootElement;
this.rootAnnotation = rootAnnotation;
this.processingEnv = processingEnv;
}

void generate() throws IOException {
Processors.generateAggregatingClass(
ClassNames.AGGREGATED_ROOT_PACKAGE,
AnnotationSpec.builder(ClassNames.AGGREGATED_ROOT)
.addMember("root", "$S", rootElement.getQualifiedName())
.addMember("rootAnnotation", "$T.class", rootAnnotation)
.build(),
rootElement,
getClass(),
processingEnv);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2021 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dagger.hilt.processor.internal.root;

import static com.google.auto.common.MoreElements.asType;
import static com.google.auto.common.MoreElements.isType;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import dagger.hilt.processor.internal.AnnotationValues;
import dagger.hilt.processor.internal.ClassNames;
import dagger.hilt.processor.internal.ProcessorErrors;
import dagger.hilt.processor.internal.Processors;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

/**
* Represents the values stored in an {@link dagger.hilt.internal.aggregatedroot.AggregatedRoot}.
*/
@AutoValue
abstract class AggregatedRootMetadata {

/** Returns the element that was annotated with the root annotation. */
abstract TypeElement rootElement();

/** Returns the root annotation as an element. */
abstract TypeElement rootAnnotation();

static ImmutableSet<AggregatedRootMetadata> from(Elements elements) {
PackageElement packageElement = elements.getPackageElement(ClassNames.AGGREGATED_ROOT_PACKAGE);

if (packageElement == null) {
return ImmutableSet.of();
}

ImmutableSet.Builder<AggregatedRootMetadata> builder = ImmutableSet.builder();
for (Element element : packageElement.getEnclosedElements()) {
ProcessorErrors.checkState(
isType(element),
element,
"Only types may be in package %s. Did you add custom code in the package?",
ClassNames.AGGREGATED_ROOT_PACKAGE);

builder.add(create(asType(element), elements));
}

return builder.build();
}

private static AggregatedRootMetadata create(TypeElement element, Elements elements) {
AnnotationMirror annotationMirror =
Processors.getAnnotationMirror(element, ClassNames.AGGREGATED_ROOT);

ProcessorErrors.checkState(
annotationMirror != null,
element,
"Classes in package %s must be annotated with @%s: %s. Found: %s.",
ClassNames.AGGREGATED_ROOT_PACKAGE,
ClassNames.AGGREGATED_ROOT,
element.getSimpleName(),
element.getAnnotationMirrors());

ImmutableMap<String, AnnotationValue> values =
Processors.getAnnotationValues(elements, annotationMirror);

return new AutoValue_AggregatedRootMetadata(
elements.getTypeElement(AnnotationValues.getString(values.get("root"))),
AnnotationValues.getTypeElement(values.get("rootAnnotation")));
}
}
5 changes: 4 additions & 1 deletion java/dagger/hilt/processor/internal/root/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ java_plugin(
java_library(
name = "processor_lib",
srcs = [
"AggregatedRootGenerator.java",
"ComponentGenerator.java",
"EarlySingletonComponentCreatorGenerator.java",
"ProcessedRootSentinelGenerator.java",
"RootFileFormatter.java",
"RootGenerator.java",
"RootProcessor.java",
Expand Down Expand Up @@ -64,7 +66,9 @@ java_library(
java_library(
name = "root_metadata",
srcs = [
"AggregatedRootMetadata.java",
"ComponentTree.java",
"ProcessedRootSentinelMetadata.java",
"Root.java",
"RootMetadata.java",
"TestRootMetadata.java",
Expand Down Expand Up @@ -95,7 +99,6 @@ java_library(
srcs = ["RootType.java"],
deps = [
"//java/dagger/hilt/processor/internal:classnames",
"//java/dagger/hilt/processor/internal:processor_errors",
"//java/dagger/hilt/processor/internal:processors",
"@google_bazel_common//third_party/java/javapoet",
],
Expand Down
Loading

0 comments on commit e33bb84

Please sign in to comment.