forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add aggregating data for roots. Also add tests to verify illegal comb…
…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
Showing
22 changed files
with
938 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
java/dagger/hilt/internal/aggregatedroot/AggregatedRoot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(["*"]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(["*"]), | ||
) |
30 changes: 30 additions & 0 deletions
30
java/dagger/hilt/internal/processedrootsentinel/ProcessedRootSentinel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
java/dagger/hilt/processor/internal/root/AggregatedRootGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
java/dagger/hilt/processor/internal/root/AggregatedRootMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.