Skip to content

Commit

Permalink
Split codegen code into a few distinct java_libraries to speed up com…
Browse files Browse the repository at this point in the history
…pilation.

We still ship one jar to Maven Central, as this is purely a build time implementation detail.

This is a rollforward of b9326a1.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=174861897

Signed-off-by: Ron Shapiro <[email protected]>
  • Loading branch information
ronshapiro committed Nov 8, 2017
1 parent 258295a commit 6863e7e
Show file tree
Hide file tree
Showing 17 changed files with 642 additions and 377 deletions.
18 changes: 17 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,27 @@ jarjar_library(
name = "shaded_compiler",
rules_file = "shade_rules.txt",
deps = [
"//java/dagger/internal/codegen",
"//java/dagger/internal/codegen:base",
"//java/dagger/internal/codegen:binding",
"//java/dagger/internal/codegen:processor",
"//java/dagger/internal/codegen:validation",
"//java/dagger/internal/codegen:writing",
"@com_google_auto_auto_common//jar",
],
)

jarjar_library(
name = "shaded_compiler_src",
rules_file = "merge_all_rules.txt",
deps = [
"//java/dagger/internal/codegen:libbase-src.jar",
"//java/dagger/internal/codegen:libbinding-src.jar",
"//java/dagger/internal/codegen:libprocessor-src.jar",
"//java/dagger/internal/codegen:libvalidation-src.jar",
"//java/dagger/internal/codegen:libwriting-src.jar",
],
)

jarjar_library(
name = "shaded_android_processor",
rules_file = "shade_rules.txt",
Expand Down
18 changes: 2 additions & 16 deletions java/dagger/internal/codegen/AnnotationCreatorGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import static com.squareup.javapoet.MethodSpec.constructorBuilder;
import static com.squareup.javapoet.MethodSpec.methodBuilder;
import static com.squareup.javapoet.TypeSpec.classBuilder;
import static dagger.internal.codegen.AnnotationExpression.createMethodName;
import static dagger.internal.codegen.AnnotationExpression.getAnnotationCreatorClassName;
import static dagger.internal.codegen.CodeBlocks.makeParametersCodeBlock;
import static dagger.internal.codegen.SourceFiles.classFileName;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PUBLIC;
Expand Down Expand Up @@ -77,17 +78,6 @@
*/
class AnnotationCreatorGenerator extends SourceFileGenerator<TypeElement> {

/**
* Returns the name of the generated class that contains the static {@code create} methods for an
* annotation type.
*/
static ClassName getAnnotationCreatorClassName(TypeElement annotationType) {
ClassName annotationTypeName = ClassName.get(annotationType);
return annotationTypeName
.topLevelClassName()
.peerClass(classFileName(annotationTypeName) + "Creator");
}

AnnotationCreatorGenerator(Filer filer, Elements elements) {
super(filer, elements);
}
Expand Down Expand Up @@ -140,10 +130,6 @@ private MethodSpec buildCreateMethod(ClassName generatedTypeName, TypeElement an
return createMethod.build();
}

static String createMethodName(TypeElement annotationType) {
return "create" + annotationType.getSimpleName();
}

/**
* Returns the annotation types for which {@code @AutoAnnotation static Foo createFoo(…)} methods
* should be written.
Expand Down
21 changes: 19 additions & 2 deletions java/dagger/internal/codegen/AnnotationExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.auto.common.AnnotationMirrors.getAnnotationValuesWithDefaults;
import static dagger.internal.codegen.CodeBlocks.makeParametersCodeBlock;
import static dagger.internal.codegen.SourceFiles.classFileName;
import static java.util.stream.Collectors.toList;

import com.google.auto.common.MoreElements;
Expand All @@ -29,6 +30,7 @@
import java.util.List;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
Expand All @@ -55,7 +57,7 @@ class AnnotationExpression extends SimpleAnnotationValueVisitor6<CodeBlock, Anno
AnnotationExpression(AnnotationMirror annotation) {
this.annotation = annotation;
this.creatorClass =
AnnotationCreatorGenerator.getAnnotationCreatorClassName(
getAnnotationCreatorClassName(
MoreTypes.asTypeElement(annotation.getAnnotationType()));
}

Expand All @@ -71,7 +73,7 @@ private CodeBlock getAnnotationInstanceExpression(AnnotationMirror annotation) {
return CodeBlock.of(
"$T.$L($L)",
creatorClass,
AnnotationCreatorGenerator.createMethodName(
createMethodName(
MoreElements.asType(annotation.getAnnotationType().asElement())),
makeParametersCodeBlock(
getAnnotationValuesWithDefaults(annotation)
Expand All @@ -81,6 +83,21 @@ private CodeBlock getAnnotationInstanceExpression(AnnotationMirror annotation) {
.collect(toList())));
}

/**
* Returns the name of the generated class that contains the static {@code create} methods for an
* annotation type.
*/
static ClassName getAnnotationCreatorClassName(TypeElement annotationType) {
ClassName annotationTypeName = ClassName.get(annotationType);
return annotationTypeName
.topLevelClassName()
.peerClass(classFileName(annotationTypeName) + "Creator");
}

static String createMethodName(TypeElement annotationType) {
return "create" + annotationType.getSimpleName();
}

/**
* Returns an expression that evaluates to a {@code value} of a given type on an {@code
* annotation}.
Expand Down
210 changes: 203 additions & 7 deletions java/dagger/internal/codegen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,201 @@ CODEGEN_SHARED_DEPS = [
"//java/dagger/producers",
]

CODEGEN_DEPS = CODEGEN_SHARED_DEPS + [
"//third_party:guava",
]

# Common types needed across all of the codegen package
java_library(
name = "codegen",
srcs = CODEGEN_SRCS,
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
name = "base",
srcs = [
"Accessibility.java",
"AnnotationSpecs.java",
"CodeBlocks.java",
"CompilerOptions.java",
"ContributionType.java",
"DaggerElements.java",
"DaggerStreams.java",
"DaggerTypes.java",
"Expression.java",
"FeatureStatus.java",
"InjectionAnnotations.java",
"MapKeyAccessibility.java",
"MethodSignature.java",
"MoreAnnotationMirrors.java",
"MoreAnnotationValues.java",
"MultibindingAnnotations.java",
"Optionals.java",
"SimpleAnnotationMirror.java",
"SimpleTypeAnnotationValue.java",
"SourceFileGenerationException.java", # Used in :writing and :processor
"SourceFileGenerator.java", # Needed by InjectBindingRegistry in :binding and also :writing
"TypeNames.java",
"TypeSpecs.java",
"UniqueNameSet.java",
"Util.java",
"ValidationType.java",
"package-info.java",
],
plugins = CODEGEN_PLUGINS,
tags = ["maven:merged"],
deps = CODEGEN_DEPS,
)

# Classes that help to build a model of the binding graph
java_library(
name = "binding",
srcs = [
"AnnotationExpression.java",
"Binding.java",
"BindingDeclaration.java",
"BindingGraph.java",
"BindingKey.java",
"BindingType.java",
"BindingTypeMapper.java",
"BindingVariableNamer.java", # needed by FrameworkField
"BindsTypeChecker.java",
"ComponentDescriptor.java",
"ComponentRequirement.java",
"ComponentTreeTraverser.java",
"ConfigurationAnnotations.java", # Uses ModuleDescriptors
"ContributionBinding.java",
"DelegateDeclaration.java",
"DependencyRequest.java",
"DependencyVariableNamer.java", # Used by SourceFiles
"ErrorMessages.java", # Consider splitting this up as it pulls in too much
"FrameworkDependency.java",
"FrameworkField.java", # Used by SourceFiles
"FrameworkType.java",
"FrameworkTypes.java",
"InjectBindingRegistry.java",
"Key.java",
"MapKeys.java",
"MapType.java",
"MembersInjectionBinding.java",
"ModuleDescriptor.java",
"MultibindingDeclaration.java",
"OptionalBindingDeclaration.java",
"OptionalType.java",
"ProductionBinding.java",
"ProvisionBinding.java",
"ResolvedBindings.java",
"Scope.java",
"SetType.java",
"SourceFiles.java", # Consider splitting this up?
"SubcomponentDeclaration.java",
],
plugins = CODEGEN_PLUGINS,
tags = ["maven:merged"],
deps = CODEGEN_DEPS + [":base"],
)

# Code related to validating the user-written Dagger code
java_library(
name = "validation",
srcs = [
"AnyBindingMethodValidator.java",
"BindingDeclarationFormatter.java",
"BindingGraphValidator.java",
"BindingMethodProcessingStep.java",
"BindingMethodValidator.java",
"BindsInstanceProcessingStep.java",
"BindsMethodValidator.java",
"BindsOptionalOfMethodValidator.java",
"BuilderValidator.java",
"CanReleaseReferencesValidator.java",
"ComponentHierarchyValidator.java",
"ComponentValidator.java",
"DependencyRequestFormatter.java",
"ForReleasableReferencesValidator.java",
"Formatter.java",
"InjectValidator.java",
"MapKeyValidator.java",
"MethodSignatureFormatter.java",
"MissingBindingSuggestions.java",
"ModuleValidator.java",
"MultibindingAnnotationsProcessingStep.java",
"MultibindsMethodValidator.java",
"ProducesMethodValidator.java",
"ProvidesMethodValidator.java",
"ValidationReport.java",
],
plugins = CODEGEN_PLUGINS,
tags = ["maven:merged"],
deps = CODEGEN_DEPS + [
":base",
":binding",
],
)

# Classes that assemble the model of the generated code and write to the Filer
java_library(
name = "writing",
srcs = [
"AbstractComponentWriter.java",
"AnnotationCreatorGenerator.java",
"BindingExpression.java",
"BoundInstanceBindingExpression.java",
"ComponentBindingExpressions.java",
"ComponentBuilder.java",
"ComponentGenerator.java",
"ComponentInstanceBindingExpression.java",
"ComponentProvisionBindingExpression.java",
"ComponentRequirementField.java",
"ComponentRequirementFields.java",
"ComponentWriter.java",
"DelegateBindingExpression.java",
"FactoryGenerator.java",
"FrameworkFieldInitializer.java",
"FrameworkInstanceBindingExpression.java",
"GeneratedComponentModel.java",
"GwtCompatibility.java",
"InjectionMethods.java",
"MapBindingExpression.java",
"MemberSelect.java",
"MembersInjectorGenerator.java",
"MonitoringModuleGenerator.java",
"MonitoringModuleProcessingStep.java",
"OptionalBindingExpression.java",
"OptionalFactories.java",
"PrivateMethodBindingExpression.java",
"ProducerFactoryGenerator.java",
"ProductionExecutorModuleGenerator.java",
"ProviderOrProducerBindingExpression.java",
"SetBindingExpression.java",
"SimpleInvocationBindingExpression.java",
"SimpleMethodBindingExpression.java",
"SubcomponentBuilderBindingExpression.java",
"SubcomponentWriter.java",
"UnwrappedMapKeyGenerator.java",
],
plugins = CODEGEN_PLUGINS,
tags = ["maven:merged"],
deps = CODEGEN_DEPS + [
":base",
":binding",
],
)

# The processor's "main", if you will
java_library(
name = "processor",
srcs = [
"CanReleaseReferencesProcessingStep.java",
"ComponentProcessingStep.java",
"ComponentProcessor.java",
"InjectBindingRegistryImpl.java",
"InjectProcessingStep.java",
"MapKeyProcessingStep.java",
"ModuleProcessingStep.java",
"ProductionExecutorModuleProcessingStep.java",
],
plugins = CODEGEN_PLUGINS,
deps = CODEGEN_SHARED_DEPS + [
"//third_party:guava",
deps = CODEGEN_DEPS + [
":base",
":binding",
":writing",
":validation",
],
)

Expand All @@ -58,12 +246,20 @@ javadoc_library(
name = "codegen-javadoc",
srcs = CODEGEN_SRCS,
root_packages = ["dagger.internal.codegen"],
deps = [":codegen"],
deps = [":processor"],
)

java_library(
name = "check-package-javadoc",
testonly = 1,
srcs = CODEGEN_SRCS,
javacopts = DOCLINT_HTML_AND_SYNTAX + DOCLINT_REFERENCES,
deps = CODEGEN_DEPS,
)

java_plugin(
name = "component-codegen",
generates_api = 1,
processor_class = "dagger.internal.codegen.ComponentProcessor",
deps = [":codegen"],
deps = [":processor"],
)
9 changes: 3 additions & 6 deletions java/dagger/internal/codegen/ComponentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,8 @@ protected Iterable<? extends ProcessingStep> initSteps() {
SubcomponentDeclaration.Factory subcomponentDeclarationFactory =
new SubcomponentDeclaration.Factory(keyFactory);

this.factoryGenerator =
new FactoryGenerator(
filer, elements, types, compilerOptions, injectValidatorWhenGeneratingCode);
this.membersInjectorGenerator =
new MembersInjectorGenerator(filer, elements, types, injectValidatorWhenGeneratingCode);
this.factoryGenerator = new FactoryGenerator(filer, elements, types, compilerOptions);
this.membersInjectorGenerator = new MembersInjectorGenerator(filer, elements, types);
ComponentGenerator componentGenerator =
new ComponentGenerator(filer, elements, types, keyFactory, compilerOptions);
ProducerFactoryGenerator producerFactoryGenerator =
Expand All @@ -136,7 +133,7 @@ protected Iterable<? extends ProcessingStep> initSteps() {
new OptionalBindingDeclaration.Factory(keyFactory);

this.injectBindingRegistry =
new InjectBindingRegistry(
new InjectBindingRegistryImpl(
elements,
types,
messager,
Expand Down
Loading

0 comments on commit 6863e7e

Please sign in to comment.