diff options
author | Jan Kuehle <jankuehle@google.com> | 2023-03-09 09:32:22 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-03-09 09:32:54 -0800 |
commit | 6c074b6ce60b1ccfdc17bcf5e0566f497a869790 (patch) | |
tree | c562c4b8abb303385cf015c610de974eed0d6be6 | |
parent | 7c4232cdb27bee38265e180d9d7749d776cd29a8 (diff) | |
download | bazelbuild-kotlin-rules-6c074b6ce60b1ccfdc17bcf5e0566f497a869790.tar.gz |
Automated rollback of commit 7c4232cdb27bee38265e180d9d7749d776cd29a8.
PiperOrigin-RevId: 515358464
Change-Id: Id64dd1d95e7e9cc7e4807b99a6797bc466a7bbd9
30 files changed, 1377 insertions, 27 deletions
diff --git a/kotlin/common.bzl b/kotlin/common.bzl index 7faae74..76bb57b 100644 --- a/kotlin/common.bzl +++ b/kotlin/common.bzl @@ -317,9 +317,7 @@ def _run_turbine( def _kt_plugins_map( android_lint_singlejar_plugins = depset(), android_lint_libjar_plugin_infos = [], - java_plugin_datas = depset(), - direct_java_plugin_infos = [], - kt_codegen_plugin_infos = depset(), + java_plugin_infos = [], kt_compiler_plugin_infos = []): """A struct containing all the plugin types understood by rules_kotlin. @@ -328,17 +326,13 @@ def _kt_plugins_map( Each JAR is self-contained and should be loaded in an isolated classloader. android_lint_libjar_plugin_infos: (list[JavaInfo]) Android Lint checkers. All infos share transitive dependencies and should be loaded in a combined classloader. - java_plugin_datas: depset([JavaPluginData]) for KtCodegenProcessing. - direct_java_plugin_infos: (list[JavaPluginInfo]) - kt_codegen_plugin_infos: depset([KtCodegenPluginInfo]) for KtCodegenProcessing. + java_plugin_infos: (list[JavaPluginInfo]) kt_compiler_plugin_infos: (list[KtCompilerPluginInfo]) """ return struct( android_lint_singlejar_plugins = android_lint_singlejar_plugins, android_lint_libjar_plugin_infos = android_lint_libjar_plugin_infos, - java_plugin_datas = java_plugin_datas, - direct_java_plugin_infos = direct_java_plugin_infos, - kt_codegen_plugin_infos = kt_codegen_plugin_infos, + java_plugin_infos = java_plugin_infos, kt_compiler_plugin_infos = kt_compiler_plugin_infos, ) @@ -735,14 +729,11 @@ def _kt_jvm_library( static_deps = list(deps) # Defensive copy kt_codegen_processing_env = dict( - processors_for_kt_codegen_processing = depset(), - processors_for_legacy_kapt = [], + pre_processed_processors = depset(), codegen_output_java_infos = [], - java_plugin_datas_legacy = [], - legacy_java_plugin_classpaths = depset(), ) - kt_codegen_processors = kt_codegen_processing_env["processors_for_kt_codegen_processing"].to_list() + pre_processed_processors = kt_codegen_processing_env["pre_processed_processors"] generative_deps = kt_codegen_processing_env["codegen_output_java_infos"] java_syncer = kt_srcjars.DirSrcjarSyncer(ctx, kt_toolchain, file_factory) @@ -768,9 +759,14 @@ def _kt_jvm_library( # Collect all plugin data, including processors to run and all plugin classpaths, # whether they have processors or not (b/120995492). # This may include go/errorprone plugin classpaths that kapt will ignore. - java_plugin_datas_legacy = kt_codegen_processing_env["java_plugin_datas_legacy"] - legacy_kapt_processors = kt_codegen_processing_env["processors_for_legacy_kapt"] - legacy_java_plugin_classpaths = kt_codegen_processing_env["legacy_java_plugin_classpaths"] + java_plugin_datas = [info.plugins for info in plugins.java_plugin_infos] + [dep.plugins for dep in static_deps] + plugin_processors = [ + cls + for p in java_plugin_datas + for cls in p.processor_classes.to_list() + if cls not in pre_processed_processors.to_list() + ] + plugin_classpaths = depset(transitive = [p.processor_jars for p in java_plugin_datas]) out_jars = [] out_srcjars = [] @@ -796,16 +792,16 @@ def _kt_jvm_library( # Skip kapt if no plugins have processors (can happen with only # go/errorprone plugins, # b/110540324) kapt_outputs = _EMPTY_KAPT_OUTPUTS - if kt_srcs and legacy_kapt_processors: + if kt_srcs and plugin_processors: kapt_outputs = _kapt( ctx, file_factory = file_factory, kt_srcs = kt_hdrs, common_srcs = common_hdrs, java_srcs = java_srcs, - plugin_processors = legacy_kapt_processors, - plugin_classpaths = legacy_java_plugin_classpaths, - plugin_data = depset(transitive = [p.processor_data for p in java_plugin_datas_legacy]), + plugin_processors = plugin_processors, + plugin_classpaths = plugin_classpaths, + plugin_data = depset(transitive = [p.processor_data for p in java_plugin_datas]), # Put contents of Bazel flag --javacopt before given javacopts as is Java rules. # This still ignores package configurations, which aren't exposed to Starlark. javacopts = (java_common.default_javac_opts(java_toolchain = java_toolchain) + @@ -875,7 +871,7 @@ def _kt_jvm_library( javac_out = output if is_android_library_without_kt_srcs else file_factory.declare_file("-java.jar") - annotation_plugins = list(plugins.direct_java_plugin_infos) + annotation_plugins = list(plugins.java_plugin_infos) # Enable annotation processing for java-only sources to enable data binding enable_annotation_processing = not kt_srcs @@ -919,7 +915,7 @@ def _kt_jvm_library( java_gensrcjar = None java_genjar = None - if kt_codegen_processors: + if pre_processed_processors: java_gen_srcjars = kt_codegen_processing_env["java_gen_srcjar"] kt_gen_srcjars = kt_codegen_processing_env["kt_gen_srcjar"] java_gensrcjar = file_factory.declare_file("-java_info_generated_source_jar.srcjar") @@ -978,14 +974,14 @@ def _kt_jvm_library( config = kt_toolchain.android_lint_config, android_lint_plugins_depset = depset( order = "preorder", - transitive = [legacy_java_plugin_classpaths] + [ + transitive = [plugin_classpaths] + [ dep.transitive_runtime_jars for dep in plugins.android_lint_libjar_plugin_infos ], ), android_lint_rules = plugins.android_lint_singlejar_plugins, lint_flags = lint_flags, - extra_input_depsets = [p.processor_data for p in java_plugin_datas_legacy] + [depset([java_genjar] if java_genjar else [])], + extra_input_depsets = [p.processor_data for p in java_plugin_datas] + [depset([java_genjar] if java_genjar else [])], testonly = testonly, android_java8_libs = kt_toolchain.android_java8_apis_desugared, mnemonic = "KtAndroidLint", # so LSA extractor can distinguish Kotlin (b/189442586) diff --git a/kotlin/jvm_compile.bzl b/kotlin/jvm_compile.bzl index d045f0a..9b195b4 100644 --- a/kotlin/jvm_compile.bzl +++ b/kotlin/jvm_compile.bzl @@ -168,10 +168,10 @@ def kt_jvm_compile( plugins = common.kt_plugins_map( android_lint_singlejar_plugins = android_lint_rules_jars, android_lint_libjar_plugin_infos = [p[JavaInfo] for p in android_lint_plugins], - direct_java_plugin_infos = [ + java_plugin_infos = [ plugin[JavaPluginInfo] for plugin in plugins - if JavaPluginInfo in plugin + if (JavaPluginInfo in plugin) ], kt_compiler_plugin_infos = kt_traverse_exports.expand_compiler_plugins(deps).to_list() + [ diff --git a/tests/jvm/java/beer/BUILD b/tests/jvm/java/beer/BUILD new file mode 100644 index 0000000..9a50474 --- /dev/null +++ b/tests/jvm/java/beer/BUILD @@ -0,0 +1,45 @@ +# Copyright 2022 Google LLC. All rights reserved. +# +# 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. + +load("//kotlin:rules.bzl", "kt_jvm_library", "kt_jvm_test") + +licenses(["notice"]) + +kt_jvm_library( + name = "beer_lib", + srcs = ["Beer.kt"], + deps = [ + "@dagger", + "@maven//:javax_inject_jsr330_api", + ], +) + +kt_jvm_test( + name = "beer", + main_class = "beer.BeerKt", + tags = ["darwin_x86_64_compatible"], + runtime_deps = [":beer_lib"], +) + +# Regression test for b/123767247 +sh_test( + name = "build_data_test", + srcs = ["build_data_test.sh"], + data = [ + ":libbeer_lib.jar", + ], + env = { + "TEST_JAR": "$(location :libbeer_lib.jar)", + }, +) diff --git a/tests/jvm/java/beer/Beer.kt b/tests/jvm/java/beer/Beer.kt new file mode 100644 index 0000000..5bac323 --- /dev/null +++ b/tests/jvm/java/beer/Beer.kt @@ -0,0 +1,186 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 beer + +import dagger.Binds +import dagger.Component +import dagger.Lazy +import dagger.Module +import dagger.Provides +import javax.inject.Inject +import javax.inject.Singleton + +interface Grains { + val name: String +} + +interface Hops { + val name: String +} + +interface Yeast { + val name: String +} + +interface Kettle { + fun steep() + fun heat() + fun boil() + fun cool() + fun ferment() + fun addGrains(grains: Grains) + fun addHops(hops: Hops) + fun addYeast(yeast: Yeast) + val isBoiling: Boolean + val hasCooled: Boolean + val isDone: Boolean +} + +interface Storage { + fun store() +} + +class PilsnerMalt +@Inject constructor() : Grains { + override val name get() = "Pilsner Malt" +} + +class Spalt +@Inject constructor() : Hops { + override val name get() = "Spalt" +} + +class Wyeast +@Inject constructor() : Yeast { + override val name get() = "Wyeast 2565 Kölsch" +} + +class BrewPot : Kettle { + var boiling: Boolean = false + var cool: Boolean = true + var done: Boolean = false + + override fun addGrains(grains: Grains) { + println("Adding grains: " + grains.name) + } + + override fun steep() { + println("=> Steeping") + } + + override fun heat() { + println("=> Heating") + this.cool = false + } + + override fun boil() { + println("=> Boiling") + this.boiling = true + } + + override fun cool() { + println("=> Cooling") + this.boiling = false + this.cool = true + } + + override fun addHops(hops: Hops) { + println("Adding hops: " + hops.name) + } + + override fun ferment() { + println("=> Fermenting") + this.done = true + } + + override fun addYeast(yeast: Yeast) { + println("Adding yeast: " + yeast.name) + } + + override val isBoiling get() = boiling + override val hasCooled get() = cool + override val isDone get() = done +} + +class Bottler +@Inject constructor( + private val kettle: Kettle +) : Storage { + override fun store() { + if (kettle.isDone) { + println("=> bottling") + } + } +} + +class BeerBrewer +@Inject constructor( + private val kettle: Lazy<Kettle>, + private val bottler: Bottler, + private val grains: Grains, + private val hops: Hops, + private val yeast: Yeast +) { + fun brew() { + kettle.get().apply { + addGrains(grains) + steep() + heat() + boil() + if (isBoiling) addHops(hops) + cool() + if (hasCooled) addYeast(yeast) + ferment() + } + bottler.store() + } +} + +@Module +abstract class CommercialEquipmentModule { + @Binds + abstract fun provideStorage(storage: Bottler): Storage +} + +@Module(includes = [CommercialEquipmentModule::class]) +class BrewingEquipmentModule { + @Provides @Singleton + fun provideKettle(): Kettle = BrewPot() +} + +@Module +interface KolschRecipeModule { + @Binds + fun bindGrains(grains: PilsnerMalt): Grains + + @Binds + fun bindHops(hops: Spalt): Hops + + @Binds + fun bindYeast(yeast: Wyeast): Yeast +} + +@Singleton +@Component(modules = [BrewingEquipmentModule::class, KolschRecipeModule::class]) +interface Brewery { + fun brewery(): BeerBrewer +} + +fun main(args: Array<String>) { + val beer = DaggerBrewery.builder().build() + beer.brewery().brew() +} diff --git a/tests/jvm/java/beer/build_data_test.sh b/tests/jvm/java/beer/build_data_test.sh new file mode 100755 index 0000000..c0c9084 --- /dev/null +++ b/tests/jvm/java/beer/build_data_test.sh @@ -0,0 +1,25 @@ +#!/bin/bash -eu +# Copyright 2022 Google LLC. All rights reserved. +# +# 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. + + +# Assert expected files exist +if ! unzip -l "$TEST_JAR" | grep "Kettle.class"; then + exit 1 +fi + +# Assert build data is stripped +if unzip -l "$TEST_JAR" | grep "build-data.properties"; then + exit 1 +fi diff --git a/tests/jvm/java/kapt/AP.java b/tests/jvm/java/kapt/AP.java new file mode 100644 index 0000000..d8b6d9f --- /dev/null +++ b/tests/jvm/java/kapt/AP.java @@ -0,0 +1,75 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import com.google.auto.service.AutoService; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UncheckedIOException; +import java.util.Set; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.ProcessingEnvironment; +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.TypeElement; + +/** Simple test processor that numbered classes in rounds as specified in {@link Count}. */ +@AutoService(Processor.class) +@SupportedAnnotationTypes("*") +public class AP extends AbstractProcessor { + + private volatile String prefix = ""; + + @Override + public synchronized void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + prefix = processingEnv.getOptions().getOrDefault("kapt.AP.indexPrefix", ""); + } + + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + + @Override + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + for (Element element : roundEnv.getElementsAnnotatedWith(Count.class)) { + Count c = element.getAnnotation(Count.class); + if (c.value() > 0) { + int next = c.value() - 1; + String simpleName = element.getSimpleName() + "_" + prefix + next; + try (OutputStream os = + processingEnv + .getFiler() + .createSourceFile("kapt." + simpleName, element) + .openOutputStream()) { + os.write( + String.format("package kapt; @Count(%d) public class %s {}", next, simpleName) + .getBytes(UTF_8)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + } + return false; + } +} diff --git a/tests/jvm/java/kapt/APTest.kt b/tests/jvm/java/kapt/APTest.kt new file mode 100644 index 0000000..cd1dfa6 --- /dev/null +++ b/tests/jvm/java/kapt/APTest.kt @@ -0,0 +1,60 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +import com.google.testing.compile.Compilation +import com.google.testing.compile.CompilationSubject.assertThat +import com.google.testing.compile.Compiler.javac +import com.google.testing.compile.JavaFileObjects +import javax.tools.JavaFileObject +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +/** Sample test for [AP] that uses `com.google.testing.compile` (b/199411692). */ +@RunWith(JUnit4::class) +class APTest { + @Test + fun testKTest() { + // This is the stub file kapt generates for KTest.kt as of kotlinc 1.5.31, with the original + // Kotlin module name shortened. + // Alternatively our test could run kapt to get a fresh stub file, but on the other hand we + // can test with a particular stub file this way, which may be useful for some regression tests. + val testStub = JavaFileObjects.forSourceString( + /* fullyQualifiedName= */ "kapt.KTest", + """ + |package kapt; + + |import java.lang.System; + + |@kotlin.Metadata(mv = {1, 5, 1}, k = 1, d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\b\u0007\u0018\u00002\u00020\u0001B\u0005\u00a2\u0006\u0002\u0010\u0002\u00a8\u0006\u0003"}, d2 = {"Lkapt/KTest;", "", "()V", "shortened.java.kapt_test_kapt"}) + |@Count(value = 3, clazz = KTest_2_1_0.class) + |public final class KTest { + | + | public KTest() { + | super(); + | } + |} + """.trimMargin() + ) + val compilation: Compilation = javac().withProcessors(AP()).compile(testStub) + assertThat(compilation).succeededWithoutWarnings() + assertThat(compilation).generatedSourceFile("kapt.KTest_2") + assertThat(compilation).generatedSourceFile("kapt.KTest_2_1") + assertThat(compilation).generatedSourceFile("kapt.KTest_2_1_0") + } +} diff --git a/tests/jvm/java/kapt/BUILD b/tests/jvm/java/kapt/BUILD new file mode 100644 index 0000000..ea6ad66 --- /dev/null +++ b/tests/jvm/java/kapt/BUILD @@ -0,0 +1,331 @@ +# Copyright 2022 Google LLC. All rights reserved. +# +# 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. + +load( + "//kotlin:rules.bzl", + "kt_jvm_library", + "kt_jvm_test", +) +load("@bazel_skylib//rules:build_test.bzl", "build_test") + +licenses(["notice"]) + +package(default_visibility = [ + "//tests/android/java/com/google/local_test:__subpackages__", +]) + +build_test( + name = "kapt_regressions", + tags = ["darwin_x86_64_compatible"], + targets = [ + ":test_interface_with_default_impls", + ":test_javac", + ":test_kapt", + ":test_javac", + ":test_javac_with_exported_plugin", + ":test_kapt_with_exported_plugin", + ":test_no_processors", + ":test_noop", + ":test_noop_processors_ignored", + ":test_processor_flags", + ":test_processor_with_data", + ":test_reference", + ":test_stubs", + ":test_direct_classpath", + ], +) + +# Regression test for no processor outputs (b/79878966) +# and running non-service processors (b/110540324) +kt_jvm_library( + name = "test_noop", + srcs = ["Noop.kt"], + plugins = [ + ":non_service_processor", + ], +) + +kt_jvm_library( + name = "test_interface_with_default_impls", + srcs = [ + "InterfaceWithDefaultImpls.kt", + ], + plugins = [":AP"], # trigger kapt for regression test (b/121222399) +) + +kt_jvm_library( + name = "test_kapt", + srcs = [ + "KTest.kt", + ], + plugins = [":AP"], + deps = [ + ":anno", + ], +) + +# Test support for -A processor flags (b/134963914). +kt_jvm_library( + name = "test_processor_flags", + srcs = ["FlagTest.kt"], + javacopts = ["-Akapt.AP.indexPrefix=Gen"], + plugins = [":AP"], + deps = [":anno"], +) + +# Skip kapt altogether when no plugin defines a processor (b/110540324). +kt_jvm_library( + name = "test_no_processors", + srcs = [ + "Noop.kt", + ], + plugins = [ + ":ignored_failing_processor", # would fail if run + ], + deps = [ + ":anno", + ], +) + +# Skip plugins without processors, for consistency with how Blaze runs javac (b/110540324). +kt_jvm_library( + name = "test_noop_processors_ignored", + srcs = [ + "KTest.kt", + ], + plugins = [ + ":AP", + ":ignored_failing_processor", # would fail if run + ], + deps = [ + ":anno", + ], +) + +# Use kapt to annotation-process a .java file +kt_jvm_library( + name = "test_kapt_java", + srcs = [ + "Noop.kt", # needed so kapt is used + "Test.java", + ], + plugins = [":AP"], + deps = [ + ":anno", + ], +) + +# No .kt sources: javac is used for annotation processing +kt_jvm_library( + name = "test_javac", + srcs = ["Test.java"], + plugins = [":AP"], + deps = [ + ":anno", + ], +) + +# Test for java_library picking up exported_plugin from kt_jvm_library +java_library( + name = "test_javac_with_exported_plugin", + srcs = ["Test.java"], + deps = [ + ":anno_with_plugin", + ], +) + +# Reference build with java_library +java_library( + name = "test_reference", + srcs = ["Test.java"], + plugins = [":AP"], + deps = [ + ":anno", + ], +) + +# This target compiles repro code for b/118338417 and b/110373008. Since these are issues with the +# kapt-generated .java stub files, which aren't currently used by the Kotlin build rules, this +# target compiles, but we can manually inspect the stubs to see if issues were fixed. +# TODO: Turn this into a dump test asserting the content of the generated stubs +kt_jvm_library( + name = "test_stubs", + srcs = [ + "InnerEnumImport.kt", # repro for https://youtrack.jetbrains.net/issue/KT-28220 + "SealedClasses.kt", # repro for https://youtrack.jetbrains.net/issue/KT-29924 + "StaticImport.kt", # Static* repro https://youtrack.jetbrains.net/issue/KT-25071 (fixed) + "StaticMethod.java", + ], + plugins = [":AP"], +) + +java_plugin( + name = "AP", + generates_api = 1, + processor_class = "kapt.AP", + deps = [":AP_lib"], +) + +java_library( + name = "AP_lib", + srcs = ["AP.java"], + deps = [ + ":anno", + "//bazel:auto_service", + ], +) + +# Processor that doesn't advertise itself to j.u.ServiceLoader (regression for b/110540324) +java_plugin( + name = "non_service_processor", + srcs = ["NonServiceProcessor.java"], + processor_class = "kapt.NonServiceProcessor", +) + +# Processor that would fail but doesn't declare processor_class and hence shouldn't run. +java_plugin( + name = "ignored_failing_processor", + srcs = ["FailingProcessor.java"], + deps = [ + "//bazel:auto_service", + ], +) + +java_library( + name = "anno", + srcs = ["Count.java"], +) + +java_library( + name = "anno-android", + srcs = ["Count.java"], +) + +kt_jvm_library( + name = "anno_with_plugin", + srcs = ["Count.java"], + exported_plugins = [":AP"], +) + +kt_jvm_library( + name = "test_kapt_with_exported_plugin", + srcs = ["KTest.kt"], + deps = [ + ":anno_with_plugin", + ], +) + +# Regression test for b/199932860: processor with data dependency +java_plugin( + name = "ProcessorWithData", + data = ["MakeHelperClass.java.template"], + processor_class = "kapt.ProcessorWithData", + deps = [":processor_with_data"], +) + +kt_jvm_library( + name = "processor_with_data", + srcs = [ + "MakeHelper.kt", + "ProcessorWithData.kt", + ], + deps = [ + "//bazel:auto_service", + ], +) + +kt_jvm_library( + name = "make_helper", + srcs = ["MakeHelper.kt"], + exported_plugins = [":ProcessorWithData"], +) + +kt_jvm_library( + name = "test_processor_with_data", + srcs = ["TriggerHelper.kt"], + deps = [":make_helper"], +) + +kt_jvm_library( + name = "generates_services", + srcs = ["GeneratesServices.kt"], + deps = [ + "//bazel:auto_service", + ], +) + +kt_jvm_test( + name = "GeneratesServicesTest", + srcs = ["GeneratesServicesTest.kt"], + tags = ["darwin_x86_64_compatible"], + deps = [ + ":generates_services", + "@maven//:com_google_truth_truth", + "@maven//:junit_junit", + ], +) + +kt_jvm_library( + name = "dagger_c", + testonly = 1, + srcs = ["DaggerC.kt"], + deps = [ + "@maven//:javax_inject_jsr330_api", + ], +) + +kt_jvm_library( + name = "dagger_b", + testonly = 1, + srcs = ["DaggerB.kt"], + deps = [ + ":dagger_c", + "@maven//:javax_inject_jsr330_api", + ], +) + +kt_jvm_library( + name = "dagger_a", + testonly = 1, + srcs = ["DaggerA.kt"], + deps = [ + ":dagger_b", + ":dagger_c", + "@dagger", + ], +) + +# Tests a scenario where Dagger generates code referencing indirect dependencies, which can confuse +# Blaze's direct classpath optimization for running Turbine. +kt_jvm_library( + name = "test_direct_classpath", + testonly = 1, + srcs = ["DaggerTopLevel.kt"], + deps = [ + ":dagger_a", + ], +) + +# Demonstrates a simple compile test that uses Kotlin kapt stubs (b/199411692) +kt_jvm_test( + name = "APTest", + srcs = ["APTest.kt"], + tags = ["darwin_x86_64_compatible"], + deps = [ + ":AP_lib", + "@maven//:com_google_testing_compile_compile_testing", + "@maven//:com_google_truth_truth", + "@maven//:junit_junit", + ], +) diff --git a/tests/jvm/java/kapt/Count.java b/tests/jvm/java/kapt/Count.java new file mode 100644 index 0000000..17332db --- /dev/null +++ b/tests/jvm/java/kapt/Count.java @@ -0,0 +1,23 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt; + +/** Annotation used by {@link AP}. */ +public @interface Count { + int value() default 0; + Class<?> clazz() default Object.class; +} diff --git a/tests/jvm/java/kapt/DaggerA.kt b/tests/jvm/java/kapt/DaggerA.kt new file mode 100644 index 0000000..ee319bd --- /dev/null +++ b/tests/jvm/java/kapt/DaggerA.kt @@ -0,0 +1,32 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +import dagger.Component +import javax.inject.Singleton + +class DaggerA { + @Singleton + @Component + public interface Thing { + fun b(): DaggerB + } + + fun main() { + DaggerDaggerA_Thing.builder().build().b() + } +} diff --git a/tests/jvm/java/kapt/DaggerB.kt b/tests/jvm/java/kapt/DaggerB.kt new file mode 100644 index 0000000..f487b8a --- /dev/null +++ b/tests/jvm/java/kapt/DaggerB.kt @@ -0,0 +1,21 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +import javax.inject.Inject + +public class DaggerB @Inject public constructor(c: DaggerC) {} diff --git a/tests/jvm/java/kapt/DaggerC.kt b/tests/jvm/java/kapt/DaggerC.kt new file mode 100644 index 0000000..6a9dd3d --- /dev/null +++ b/tests/jvm/java/kapt/DaggerC.kt @@ -0,0 +1,22 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +import javax.inject.Inject + +public class DaggerC @Inject public constructor() {} + diff --git a/tests/jvm/java/kapt/DaggerTopLevel.kt b/tests/jvm/java/kapt/DaggerTopLevel.kt new file mode 100644 index 0000000..27adb00 --- /dev/null +++ b/tests/jvm/java/kapt/DaggerTopLevel.kt @@ -0,0 +1,23 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +class DaggerTopLevel { + fun main() { + DaggerA().main() + } +} diff --git a/tests/jvm/java/kapt/FailingProcessor.java b/tests/jvm/java/kapt/FailingProcessor.java new file mode 100644 index 0000000..1e3e703 --- /dev/null +++ b/tests/jvm/java/kapt/FailingProcessor.java @@ -0,0 +1,49 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt; + + +import com.google.auto.service.AutoService; +import java.util.Set; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.ProcessingEnvironment; +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; + +/** Annotation processor that fails when invoked. */ +@AutoService(Processor.class) +@SupportedAnnotationTypes("*") +public class FailingProcessor extends AbstractProcessor { + + @Override + public synchronized void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + } + + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + + @Override + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + throw new IllegalStateException("Shouldn't get here"); + } +} diff --git a/tests/jvm/java/kapt/FlagTest.kt b/tests/jvm/java/kapt/FlagTest.kt new file mode 100644 index 0000000..acf6145 --- /dev/null +++ b/tests/jvm/java/kapt/FlagTest.kt @@ -0,0 +1,22 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +/** Requires -Akapt.AP.indexPrefix=Gen to generate expected class name. */ +@Count(value = 3, clazz = FlagTest_Gen2_Gen1_Gen0::class) +class FlagTest { +} diff --git a/tests/jvm/java/kapt/GeneratesServices.kt b/tests/jvm/java/kapt/GeneratesServices.kt new file mode 100644 index 0000000..771743d --- /dev/null +++ b/tests/jvm/java/kapt/GeneratesServices.kt @@ -0,0 +1,28 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +import com.google.auto.service.AutoService + +public interface Regressible { + val regressed: Boolean +} + +@AutoService(Regressible::class) +public class Fixed() : Regressible { + override val regressed = false +} diff --git a/tests/jvm/java/kapt/GeneratesServicesTest.kt b/tests/jvm/java/kapt/GeneratesServicesTest.kt new file mode 100644 index 0000000..c2b4c56 --- /dev/null +++ b/tests/jvm/java/kapt/GeneratesServicesTest.kt @@ -0,0 +1,35 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import java.util.ServiceLoader + +@RunWith(JUnit4::class) +class GeneratesServicesTest { + + @Test + fun servicesDiscoverable() { + val impls = ServiceLoader.load(Regressible::class.java) + assertThat(impls).isNotEmpty() + assertThat(impls.iterator().next().regressed).isFalse() + } +} diff --git a/tests/jvm/java/kapt/InnerEnumImport.kt b/tests/jvm/java/kapt/InnerEnumImport.kt new file mode 100644 index 0000000..ef4b4f1 --- /dev/null +++ b/tests/jvm/java/kapt/InnerEnumImport.kt @@ -0,0 +1,36 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +import kapt.InnerEnumImport.Options.A +import kapt.InnerEnumImport.Options.B +import kapt.InnerEnumImport.Options.C + +class InnerEnumImport { + fun name(o: Options) = + when (o) { + A -> "a" + B -> "b" + C -> "c" + } + + enum class Options { + A, + B, + C + } +} diff --git a/tests/jvm/java/kapt/InterfaceWithDefaultImpls.kt b/tests/jvm/java/kapt/InterfaceWithDefaultImpls.kt new file mode 100644 index 0000000..0bb1f12 --- /dev/null +++ b/tests/jvm/java/kapt/InterfaceWithDefaultImpls.kt @@ -0,0 +1,24 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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. + */ + +// This file contains the example filed upstream for b/121222399. +package kapt + +interface Base<T> + +interface HasDefaultMethod<T> : Base<T> { + fun print(msg: String) = println(msg) +} diff --git a/tests/jvm/java/kapt/KTest.kt b/tests/jvm/java/kapt/KTest.kt new file mode 100644 index 0000000..88efba8 --- /dev/null +++ b/tests/jvm/java/kapt/KTest.kt @@ -0,0 +1,21 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +@Count(value = 3, clazz = KTest_2_1_0::class) +class KTest { +} diff --git a/tests/jvm/java/kapt/MakeHelper.kt b/tests/jvm/java/kapt/MakeHelper.kt new file mode 100644 index 0000000..9809f34 --- /dev/null +++ b/tests/jvm/java/kapt/MakeHelper.kt @@ -0,0 +1,22 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +import kotlin.reflect.KClass + +/** Annotation to specify a file name for `ProcessorWithData`. */ +annotation class MakeHelper(val value: String) diff --git a/tests/jvm/java/kapt/MakeHelperClass.java.template b/tests/jvm/java/kapt/MakeHelperClass.java.template new file mode 100644 index 0000000..db25fc7 --- /dev/null +++ b/tests/jvm/java/kapt/MakeHelperClass.java.template @@ -0,0 +1,20 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt; + +/** Java class copied into output by {@code ProcessorWithData}. */ +class |name| {} diff --git a/tests/jvm/java/kapt/NonServiceProcessor.java b/tests/jvm/java/kapt/NonServiceProcessor.java new file mode 100644 index 0000000..cab01d9 --- /dev/null +++ b/tests/jvm/java/kapt/NonServiceProcessor.java @@ -0,0 +1,45 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt; + +import java.util.Set; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.ProcessingEnvironment; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; + +/** Annotation processor <b>not</b> loadable via service loader. */ +@SupportedAnnotationTypes("*") +public class NonServiceProcessor extends AbstractProcessor { + + @Override + public synchronized void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + } + + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + + @Override + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + return false; + } +} diff --git a/tests/jvm/java/kapt/Noop.kt b/tests/jvm/java/kapt/Noop.kt new file mode 100644 index 0000000..0c3d9c3 --- /dev/null +++ b/tests/jvm/java/kapt/Noop.kt @@ -0,0 +1,20 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +fun main(argv : Array<String>) { +} diff --git a/tests/jvm/java/kapt/ProcessorWithData.kt b/tests/jvm/java/kapt/ProcessorWithData.kt new file mode 100644 index 0000000..0d58205 --- /dev/null +++ b/tests/jvm/java/kapt/ProcessorWithData.kt @@ -0,0 +1,65 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +import com.google.auto.service.AutoService +import java.io.IOException +import java.io.UncheckedIOException +import java.nio.charset.StandardCharsets.UTF_8 +import java.nio.file.Files +import java.nio.file.Paths +import javax.annotation.processing.AbstractProcessor +import javax.annotation.processing.Processor +import javax.annotation.processing.RoundEnvironment +import javax.annotation.processing.SupportedAnnotationTypes +import javax.lang.model.SourceVersion +import javax.lang.model.element.TypeElement + +/** Annotation processor to test `data` dependencies specified with [MakeHelper] annotations. */ +@AutoService(Processor::class) +@SupportedAnnotationTypes("*") +internal class ProcessorWithData : AbstractProcessor() { + + private val template: String = + Files.readString( + Paths.get( + "tests/jvm/java/kapt/MakeHelperClass.java.template" + ), + UTF_8 + ) + + override fun getSupportedSourceVersion(): SourceVersion = SourceVersion.latestSupported() + + override fun process( + annotations: Set<TypeElement>, + roundEnv: RoundEnvironment, + ): Boolean { + for (element in roundEnv.getElementsAnnotatedWith(MakeHelper::class.java)) { + val c = element.getAnnotation(MakeHelper::class.java) + if (c.value.isNotEmpty()) { + try { + processingEnv.filer.createSourceFile(c.value, element).openWriter().use { + it.write(template.replace("|name|", c.value.substring(c.value.lastIndexOf(".") + 1))) + } + } catch (e: IOException) { + throw UncheckedIOException(e) + } + } + } + return false + } +} diff --git a/tests/jvm/java/kapt/SealedClasses.kt b/tests/jvm/java/kapt/SealedClasses.kt new file mode 100644 index 0000000..7727e08 --- /dev/null +++ b/tests/jvm/java/kapt/SealedClasses.kt @@ -0,0 +1,21 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +sealed class Sealed + +class Sub : Sealed() diff --git a/tests/jvm/java/kapt/StaticImport.kt b/tests/jvm/java/kapt/StaticImport.kt new file mode 100644 index 0000000..28d3752 --- /dev/null +++ b/tests/jvm/java/kapt/StaticImport.kt @@ -0,0 +1,25 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +import java.util.Objects.isNull +import kapt.StaticMethod.of + +class StaticImport { + val x = isNull("hello") + val list = of("hello", "world") +} diff --git a/tests/jvm/java/kapt/StaticMethod.java b/tests/jvm/java/kapt/StaticMethod.java new file mode 100644 index 0000000..5c6fc0c --- /dev/null +++ b/tests/jvm/java/kapt/StaticMethod.java @@ -0,0 +1,32 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt; + +public class StaticMethod<T> { + public static <T> StaticMethod<T> of(T t1) { + return new StaticMethod<T>(t1); + } + public static <T> StaticMethod<T> of(T t1, T t2) { + return new StaticMethod<T>(t1, t2); + } + + private final T[] ts; + + private StaticMethod(T... ts) { + this.ts = ts; + } +} diff --git a/tests/jvm/java/kapt/Test.java b/tests/jvm/java/kapt/Test.java new file mode 100644 index 0000000..f3ca1cf --- /dev/null +++ b/tests/jvm/java/kapt/Test.java @@ -0,0 +1,22 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt; + +@Count(value = 3, clazz = Test_2_1_0.class) +class Test { + Test_2_1_0 x; +} diff --git a/tests/jvm/java/kapt/TriggerHelper.kt b/tests/jvm/java/kapt/TriggerHelper.kt new file mode 100644 index 0000000..0ccb204 --- /dev/null +++ b/tests/jvm/java/kapt/TriggerHelper.kt @@ -0,0 +1,24 @@ +/* + * * Copyright 2022 Google LLC. All rights reserved. + * + * 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 kapt + +/** Cause `ProcessorWithData` to generate a class with the specified name. */ +@MakeHelper("kapt.GeneratedHelper") +class TriggerHelper { + /** Property that makes sure the file is successfully copied and compiled into this package. */ + private val helper = GeneratedHelper() +} |