aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/buildjar/java/com
diff options
context:
space:
mode:
authorGravatar Liam Miller-Cushon <cushon@google.com>2016-03-23 00:19:56 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2016-03-23 12:20:02 +0000
commit2d4631c058be532a36729b38dcbdfe5dc0eb7eaa (patch)
tree83ecdb9905f1071bafaf96c0262b1994985a1993 /src/java_tools/buildjar/java/com
parent9ad1a32a8b2c45b28fc45c331b9461f7c8539ec1 (diff)
Record dependencies for exempt annotation processors
Certain annotation processors that generate references to transitive dependencies are exempt from Strict Java Deps. Dagger ensures that all references to transitive types are reachable via a chain of direct dependencies specified in @Component interfaces. We don't record dependencies inside dagger-generated code, since Dagger is already enforcing strict dependencies. By not recording the deps we allow dependency management tools to remove depednencies that are only needed transitively by dagger. This approach doesn't work in general, since processors may generate references to transitive types that are not reachable via a chain of direct deps, and which may be subject to pruning by the reduced classpath optimization. This change causes strict deps to record dependencies (but not emit strict deps errors) inside code generated by exempt annotation processors other than Dagger. -- MOS_MIGRATED_REVID=117882599
Diffstat (limited to 'src/java_tools/buildjar/java/com')
-rw-r--r--src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/StrictJavaDepsPlugin.java47
1 files changed, 27 insertions, 20 deletions
diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/StrictJavaDepsPlugin.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/StrictJavaDepsPlugin.java
index 32590dd081..d00bdd8be0 100644
--- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/StrictJavaDepsPlugin.java
+++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/StrictJavaDepsPlugin.java
@@ -221,6 +221,9 @@ public final class StrictJavaDepsPlugin extends BlazeJavaCompilerPlugin {
/** The set of generators we exempt from this testing. */
private final Set<String> exemptGenerators;
+ /** Was the node being visited generated by an exempt annotation processor? */
+ private boolean isStrictDepsExempt = false;
+
public CheckingTreeScanner(DependencyModule dependencyModule, Log log,
Set<String> missingTargets, Set<String> platformJars, JavaFileManager fileManager) {
this.indirectJarsToTargets = dependencyModule.getIndirectMapping();
@@ -262,7 +265,7 @@ public final class StrictJavaDepsPlugin extends BlazeJavaCompilerPlugin {
* replaced by the more complete Blaze implementation).
*/
private void collectExplicitDependency(String jarName, JCTree node, Symbol.TypeSymbol sym) {
- if (strictJavaDepsMode.isEnabled()) {
+ if (strictJavaDepsMode.isEnabled() && !isStrictDepsExempt) {
// Does it make sense to emit a warning/error for this pair of (type, target)?
// We want to emit only one error/warning per target.
String target = indirectJarsToTargets.get(jarName);
@@ -335,43 +338,47 @@ public final class StrictJavaDepsPlugin extends BlazeJavaCompilerPlugin {
}
}
- private static final String TIKTOK_COMPONENT_PROCESSOR_NAME =
- "com.google.apps.tiktok.inject.processor.ComponentProcessor";
-
private static final String DAGGER_PROCESSOR_PREFIX = "dagger.";
- public boolean generatedByDagger(JCTree.JCClassDecl tree) {
+ enum ProcessorDependencyMode {
+ DEFAULT,
+ EXEMPT_RECORD,
+ EXEMPT_NORECORD;
+ }
+
+ public ProcessorDependencyMode isAnnotationProcessorExempt(JCTree.JCClassDecl tree) {
if (tree.sym == null) {
- return false;
+ return ProcessorDependencyMode.DEFAULT;
}
Generated generated = tree.sym.getAnnotation(Generated.class);
if (generated == null) {
- return false;
+ return ProcessorDependencyMode.DEFAULT;
}
for (String value : generated.value()) {
+ // Relax strict deps for dagger-generated code (b/17979436).
if (value.startsWith(DAGGER_PROCESSOR_PREFIX)) {
- return true;
+ return ProcessorDependencyMode.EXEMPT_NORECORD;
}
- // additional exemption for tiktok (b/21307381)
- if (value.equals(TIKTOK_COMPONENT_PROCESSOR_NAME)) {
- return true;
- }
- for (String exemptGenerator : exemptGenerators) {
- if (value.equals(exemptGenerator)) {
- return true;
- }
+ if (exemptGenerators.contains(value)) {
+ return ProcessorDependencyMode.EXEMPT_RECORD;
}
}
- return false;
+ return ProcessorDependencyMode.DEFAULT;
}
@Override
public void visitClassDef(JCTree.JCClassDecl tree) {
- // Relax strict deps for dagger-generated code (b/17979436).
- if (generatedByDagger(tree)) {
+ ProcessorDependencyMode mode = isAnnotationProcessorExempt(tree);
+ if (mode == ProcessorDependencyMode.EXEMPT_NORECORD) {
return;
}
- super.visitClassDef(tree);
+ boolean previous = isStrictDepsExempt;
+ try {
+ isStrictDepsExempt |= mode == ProcessorDependencyMode.EXEMPT_RECORD;
+ super.visitClassDef(tree);
+ } finally {
+ isStrictDepsExempt = previous;
+ }
}
}