Skip to content

Commit

Permalink
Add DaggerKotlinIssuesDetector
Browse files Browse the repository at this point in the history
This is a set of custom lints along with fixes to help with migration
to improved kotlin support in Dagger.

This covers the following cases:
* Using field: site target in injected properties
* Using @JvmStatic in object modules
* Annotating companion objects with @module

This CL also includes the necessary infrastructure changes to build,
package and deploy lint rules in the Dagger project.

For Kotlin-only projects, a new artifact 'com.google.dagger:dagger-lint'
will be published so that lint checks can be added via the lintChecks
configuration in Gradle:
```
apply plugin: "com.android.lint"

dependencies {
  lintChecks "com.google.dagger:dagger-lint:<version>"
}
```

Meanwhile for users of Hilt and dagger-android, the lint checks will be
applied from a new transitive dependency 'com.google.dagger:dagger-lint-aar'.

Closes #1805

RELNOTES="Add custom lint checks to Dagger"

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=310269729
  • Loading branch information
ZacSweers authored and nick-someone committed May 8, 2020
1 parent 277fbcf commit 2597f5a
Show file tree
Hide file tree
Showing 15 changed files with 897 additions and 26 deletions.
11 changes: 11 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ http_archive(

load("@rules_jvm_external//:defs.bzl", "maven_install")

ANDROID_LINT_VERSION = "26.6.2"

maven_install(
artifacts = [
"androidx.annotation:annotation:1.1.0",
Expand All @@ -73,6 +75,14 @@ maven_install(
"com.android.support:appcompat-v7:25.0.0",
"com.android.support:support-annotations:25.0.0",
"com.android.support:support-fragment:25.0.0",
"com.android.tools.external.org-jetbrains:uast:%s" % ANDROID_LINT_VERSION,
"com.android.tools.external.com-intellij:intellij-core:%s" % ANDROID_LINT_VERSION,
"com.android.tools.external.com-intellij:kotlin-compiler:%s" % ANDROID_LINT_VERSION,
"com.android.tools.lint:lint:%s" % ANDROID_LINT_VERSION,
"com.android.tools.lint:lint-api:%s" % ANDROID_LINT_VERSION,
"com.android.tools.lint:lint-checks:%s" % ANDROID_LINT_VERSION,
"com.android.tools.lint:lint-tests:%s" % ANDROID_LINT_VERSION,
"com.android.tools:testutils:%s" % ANDROID_LINT_VERSION,
"com.google.guava:guava:27.1-android",
"org.jetbrains.kotlin:kotlin-stdlib:1.3.50",
"org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.1.0",
Expand All @@ -81,6 +91,7 @@ maven_install(
repositories = [
"https://repo1.maven.org/maven2",
"https://maven.google.com",
"https://jcenter.bintray.com/", # Lint has one trove4j dependency in jCenter
],
)

Expand Down
3 changes: 3 additions & 0 deletions java/dagger/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ android_library(
"//java/dagger/android/internal/proguard:plugin",
],
tags = ["maven_coordinates=com.google.dagger:dagger-android:" + POM_VERSION],
exports = [
"//java/dagger/lint:lint-android-artifact-lib",
],
deps = [
"//:dagger_with_compiler",
"@google_bazel_common//third_party/java/auto:value",
Expand Down
19 changes: 19 additions & 0 deletions java/dagger/hilt/android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!--
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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dagger.hilt.android">
<uses-sdk android:minSdkVersion="14" />
</manifest>
3 changes: 3 additions & 0 deletions java/dagger/hilt/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ android_library(
"//java/dagger/hilt:generates_root_input",
"//java/dagger/hilt:install_in",
"//java/dagger/hilt/android:android_entry_point",
"//java/dagger/lint:lint-android-artifact-lib",
],
)

Expand All @@ -88,6 +89,8 @@ gen_maven_artifact(
"dagger.hilt",
],
javadoc_srcs = glob(["*.java"]), # TODO(user): Fix these srcs.
manifest = "AndroidManifest.xml",
packaging = "aar",
shaded_deps = ["@com_google_auto_auto_common//jar"],
shaded_rules = ["rule com.google.auto.common.** dagger.shaded.auto.common.@1"],
deps = [
Expand Down
19 changes: 19 additions & 0 deletions java/dagger/hilt/android/testing/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!--
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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dagger.hilt.android.testing">
<uses-sdk android:minSdkVersion="14" />
</manifest>
2 changes: 2 additions & 0 deletions java/dagger/hilt/android/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ gen_maven_artifact(
"dagger.hilt.android.testing",
],
javadoc_srcs = glob(["*.java"]),
manifest = "AndroidManifest.xml",
packaging = "aar",
shaded_deps = ["@com_google_auto_auto_common//jar"],
shaded_rules = ["rule com.google.auto.common.** dagger.shaded.auto.common.@1"],
deps = [
Expand Down
18 changes: 18 additions & 0 deletions java/dagger/lint/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!--
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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dagger.lint">
</manifest>
77 changes: 77 additions & 0 deletions java/dagger/lint/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 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.

# Description:
# Dagger Lint Rules

load("//tools:maven.bzl", "POM_VERSION", "gen_maven_artifact")
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")

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

kt_jvm_library(
name = "lint-artifact-lib",
srcs = glob(["*.kt"]),
tags = ["maven_coordinates=com.google.dagger:dagger-lint:" + POM_VERSION],
deps = [
"@google_bazel_common//third_party/java/auto:service",
"@maven//:com_android_tools_external_com_intellij_intellij_core",
"@maven//:com_android_tools_external_com_intellij_kotlin_compiler",
"@maven//:com_android_tools_external_org_jetbrains_uast",
"@maven//:com_android_tools_lint_lint",
"@maven//:com_android_tools_lint_lint_api",
],
)

# Current `kt_jvm_library` does not output source jars and gen_maven_artifact expects one.
# See: https://github.com/bazelbuild/rules_kotlin/issues/324
genrule(
name = "dagger-lint-sources",
srcs = glob(["*.kt"]),
outs = ["liblint-artifact-lib-src.jar"],
cmd = """
TEMP="$$(mktemp -d)"
for file in $(SRCS); do
filename="$$TEMP/$${file#java/}"
mkdir -p `dirname $$filename` && cp $$file $$filename
done
jar cf $@ -C $$TEMP .
""",
)

gen_maven_artifact(
name = "lint-artifact",
artifact_id = "dagger-lint",
artifact_name = "Dagger Lint Rules",
artifact_target = ":lint-artifact-lib",
pom_name = "lint-pom",
)

# An empty android artifact to distribute and share the Dagger lint rules for
# the Android sub-projects.
android_library(
name = "lint-android-artifact-lib",
tags = ["maven_coordinates=com.google.dagger:dagger-lint-aar:" + POM_VERSION],
)

gen_maven_artifact(
name = "lint-android-artifact",
artifact_id = "dagger-lint-aar",
artifact_name = "Dagger Lint Rules AAR Distribution",
artifact_target = ":lint-android-artifact-lib",
lint_deps = [":lint-artifact-lib"],
manifest = "AndroidManifest.xml",
packaging = "aar",
pom_name = "lint-android-pom",
)
34 changes: 34 additions & 0 deletions java/dagger/lint/DaggerIssueRegistry.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.
*/
package dagger.lint

import com.android.tools.lint.client.api.IssueRegistry
import com.android.tools.lint.detector.api.CURRENT_API
import com.android.tools.lint.detector.api.Issue
import com.google.auto.service.AutoService

/**
* Dagger Lint Issues Registry.
*
* A META-INF/services entry is added for this class that Lint will discover and call into for
* detecting issues.
*/
@AutoService(IssueRegistry::class)
@Suppress("unused", "UnstableApiUsage")
class DaggerIssueRegistry : IssueRegistry() {
override val api: Int = CURRENT_API
override val issues: List<Issue> = DaggerKotlinIssueDetector.issues
}
Loading

0 comments on commit 2597f5a

Please sign in to comment.