Skip to content

Commit

Permalink
Fixes xprocessing jar to properly handle shading.
Browse files Browse the repository at this point in the history
The issue was due to incorrect handling of shading in jarjar for kotlin metadata. In particular, jarjar does not replace UTF8 usages in the classfile, so the Strings in the kotlin metadata do not get replaced. This breaks when the class is compiled with kotlinc which uses the kotlin metadata during compilation.

To fix this, I've switched to using shadowJar to do the shading (https://imperceptiblethoughts.com/shadow/).

Closes #2765

RELNOTES=Fix #2765: Fixes xprocessing jar to properly handle shading.
PiperOrigin-RevId: 386099651
  • Loading branch information
bcorso authored and Dagger Team committed Jul 22, 2021
1 parent ff62b19 commit 1550d0d
Show file tree
Hide file tree
Showing 25 changed files with 432 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.lang.model.element.Modifier.STATIC;

import androidx.room.compiler.processing.XTypeElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XTypeElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import static com.google.common.base.Preconditions.checkState;
import static dagger.internal.codegen.langmodel.DaggerElements.closestEnclosingTypeElement;

import androidx.room.compiler.processing.XExecutableElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XExecutableElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
Expand Down
4 changes: 2 additions & 2 deletions java/dagger/internal/codegen/AssistedProcessingStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static com.google.auto.common.MoreElements.isAnnotationPresent;
import static dagger.internal.codegen.langmodel.DaggerElements.closestEnclosingTypeElement;

import androidx.room.compiler.processing.XVariableElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XVariableElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import dagger.assisted.AssistedInject;
Expand Down
4 changes: 3 additions & 1 deletion java/dagger/internal/codegen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ gen_maven_artifact(
"//java/dagger/internal/codegen/xprocessing",
],
shaded_rules = [
"rule androidx.room.compiler.processing.** dagger.shaded.androidx.room.compiler.processing.@1",
# Normally we would include shading rules for xprocessing here since it's
# included in shaded_deps (above) but the jar is already shaded. We still
# include it in shaded_deps so that the jarjar knows to add it since it.
"rule com.google.auto.common.** dagger.shaded.auto.common.@1",
],
)
Expand Down
4 changes: 2 additions & 2 deletions java/dagger/internal/codegen/ComponentHjarProcessingStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import static dagger.internal.codegen.binding.ComponentCreatorAnnotation.rootComponentCreatorAnnotations;
import static java.util.Collections.disjoint;

import androidx.room.compiler.processing.XTypeElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XTypeElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import dagger.internal.codegen.base.SourceFileGenerator;
Expand Down
4 changes: 2 additions & 2 deletions java/dagger/internal/codegen/ComponentProcessingStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import static dagger.internal.codegen.binding.ComponentCreatorAnnotation.allCreatorAnnotations;
import static java.util.Collections.disjoint;

import androidx.room.compiler.processing.XTypeElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XTypeElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep;
import com.google.auto.common.MoreElements;
import com.google.common.collect.ImmutableSet;
Expand Down
10 changes: 5 additions & 5 deletions java/dagger/internal/codegen/ComponentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING;

import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XProcessingStep;
import androidx.room.compiler.processing.XRoundEnv;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.compiler.processing.javac.JavacBasicAnnotationProcessor;
import androidx.room.shaded.compiler.processing.XProcessingEnv;
import androidx.room.shaded.compiler.processing.XProcessingStep;
import androidx.room.shaded.compiler.processing.XRoundEnv;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.javac.JavacBasicAnnotationProcessor;
import com.google.auto.service.AutoService;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
Expand Down
4 changes: 2 additions & 2 deletions java/dagger/internal/codegen/InjectProcessingStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package dagger.internal.codegen;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.auto.common.MoreElements;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
Expand Down
4 changes: 2 additions & 2 deletions java/dagger/internal/codegen/MapKeyProcessingStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static dagger.internal.codegen.binding.MapKeys.getUnwrappedMapKeyType;
import static javax.lang.model.element.ElementKind.ANNOTATION_TYPE;

import androidx.room.compiler.processing.XTypeElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XTypeElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.auto.common.MoreTypes;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
Expand Down
8 changes: 4 additions & 4 deletions java/dagger/internal/codegen/ModuleProcessingStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import static dagger.internal.codegen.langmodel.DaggerElements.isAnnotationPresent;
import static javax.lang.model.util.ElementFilter.methodsIn;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XTypeElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XElement;
import androidx.room.shaded.compiler.processing.XProcessingEnv;
import androidx.room.shaded.compiler.processing.XTypeElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep;
import com.google.auto.common.MoreElements;
import com.google.common.collect.ImmutableSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import static com.google.common.base.Preconditions.checkArgument;

import androidx.room.compiler.processing.XExecutableElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XExecutableElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import javax.annotation.processing.Messager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package dagger.internal.codegen.validation;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.auto.common.MoreElements;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package dagger.internal.codegen.validation;

import androidx.room.compiler.processing.XTypeElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XTypeElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.auto.common.MoreElements;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static dagger.internal.codegen.langmodel.DaggerElements.getAnnotationMirror;
import static javax.tools.Diagnostic.Kind.ERROR;

import androidx.room.compiler.processing.XExecutableElement;
import androidx.room.compiler.processing.compat.XConverters;
import androidx.room.shaded.compiler.processing.XExecutableElement;
import androidx.room.shaded.compiler.processing.compat.XConverters;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import dagger.internal.codegen.javapoet.TypeNames;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XProcessingStep;
import androidx.room.shaded.compiler.processing.XElement;
import androidx.room.shaded.compiler.processing.XProcessingEnv;
import androidx.room.shaded.compiler.processing.XProcessingStep;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
Expand Down
Binary file modified java/dagger/internal/codegen/xprocessing/xprocessing.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ interface SimpleComponent {

public static void main(String[] args) {
Foo foo = DaggerSimpleApplication_SimpleComponent.create().foo();

// Execute other classes
AssistedInjects.main(args);
}
}
49 changes: 49 additions & 0 deletions javatests/artifacts/dagger/simpleKotlin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2020 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.
*/

plugins {
id 'application'
id 'org.jetbrains.kotlin.jvm' version '1.4.20'
id "org.jetbrains.kotlin.kapt" version "1.5.0"
}

repositories {
google()
mavenCentral()
mavenLocal()
}

java {
// Make sure the generated source is compatible with Java 7.
sourceCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.0'
implementation 'com.google.dagger:dagger:LOCAL-SNAPSHOT'
kapt 'com.google.dagger:dagger-compiler:LOCAL-SNAPSHOT'

// This is testImplementation rather than kaptTest because we're actually
// testing the reference to ComponentProcessor.
// See https://github.com/google/dagger/issues/2765
testImplementation 'com.google.dagger:dagger-compiler:LOCAL-SNAPSHOT'
testImplementation 'com.google.truth:truth:1.0.1'
testImplementation 'junit:junit:4.13'
}

application {
mainClass = 'dagger.simple.SimpleApplicationKt'
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 1550d0d

Please sign in to comment.