-
Notifications
You must be signed in to change notification settings - Fork 72
#15 better support for compiler plugins #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1dd5f2b
4bacb84
bd20eed
bc48d39
c64972a
b778d26
725bed3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Copyright 2010-2016 JetBrains s.r.o. | ||
| * | ||
| * 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 com.tschuchort.compiletesting | ||
|
|
||
| import org.jetbrains.kotlin.base.kapt3.KaptOptions | ||
| import org.jetbrains.kotlin.com.intellij.mock.MockProject | ||
| import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar | ||
| import org.jetbrains.kotlin.config.CompilerConfiguration | ||
| import org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor | ||
|
|
||
| internal class MainComponentRegistrar : ComponentRegistrar { | ||
|
|
||
| override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) { | ||
| val parameters = threadLocalParameters.get() | ||
| KaptComponentRegistrar(parameters.processors,parameters.kaptOptions).registerProjectComponents(project,configuration) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I read a few days ago in the Kotlin slack that the order of registering compiler plugins matters. Right now we are registering Kapt first. Could there be a problem here? For example if a compiler plugin like arrow-meta introduces some new syntax/type checking that isn't legal in vanilla Kotlin the respective plugin would have to be run before Kapt or Kapt wouldn't know how to deal with that. Any thoughts on this @raulraja? |
||
|
|
||
| parameters.compilerPlugins.forEach { componentRegistrar-> | ||
| componentRegistrar.registerProjectComponents(project,configuration) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| /** This compiler plugin is instantiated by K2JVMCompiler using | ||
| * a service locator. So we can't just pass parameters to it easily. | ||
| * Instead we need to use a thread-local global variable to pass | ||
| * any parameters that change between compilations | ||
| */ | ||
| val threadLocalParameters: ThreadLocal<Parameters> = ThreadLocal() | ||
| } | ||
|
|
||
| data class Parameters( | ||
| val processors: List<IncrementalProcessor>, | ||
| val kaptOptions: KaptOptions.Builder, | ||
| val compilerPlugins: List<ComponentRegistrar> | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| com.tschuchort.compiletesting.KaptComponentRegistrar | ||
| com.tschuchort.compiletesting.MainComponentRegistrar |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package com.tschuchort.compiletesting | ||
|
|
||
| import com.nhaarman.mockitokotlin2.any | ||
| import com.nhaarman.mockitokotlin2.atLeastOnce | ||
| import com.nhaarman.mockitokotlin2.verify | ||
| import org.assertj.core.api.Assertions | ||
| import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar | ||
| import org.junit.Assert | ||
| import org.junit.Test | ||
| import org.mockito.Mockito | ||
| import javax.annotation.processing.AbstractProcessor | ||
| import javax.annotation.processing.RoundEnvironment | ||
| import javax.lang.model.element.TypeElement | ||
|
|
||
| class CompilerPluginsTest { | ||
|
|
||
| @Test | ||
| fun `when compiler plugins are added they get executed`() { | ||
|
|
||
| val mockPlugin = Mockito.mock(ComponentRegistrar::class.java) | ||
|
|
||
| val result = defaultCompilerConfig().apply { | ||
| sources = listOf(SourceFile.new("emptyKotlinFile.kt", "")) | ||
| compilerPlugins = listOf(mockPlugin) | ||
| inheritClassPath = true | ||
| }.compile() | ||
|
|
||
| verify(mockPlugin, atLeastOnce()).registerProjectComponents(any(), any()) | ||
|
|
||
| Assertions.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when compiler plugins and annotation processors are added they get executed`() { | ||
|
|
||
| val annotationProcessor = object : AbstractProcessor() { | ||
| override fun getSupportedAnnotationTypes(): Set<String> = setOf(ProcessElem::class.java.canonicalName) | ||
|
|
||
| override fun process(p0: MutableSet<out TypeElement>?, p1: RoundEnvironment?): Boolean { | ||
| p1?.getElementsAnnotatedWith(ProcessElem::class.java)?.forEach { | ||
| Assert.assertEquals("JSource", it?.simpleName.toString()) | ||
| } | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| val mockPlugin = Mockito.mock(ComponentRegistrar::class.java) | ||
|
|
||
| val jSource = SourceFile.kotlin( | ||
| "JSource.kt", """ | ||
| package com.tschuchort.compiletesting; | ||
|
|
||
| @ProcessElem | ||
| class JSource { | ||
| fun foo() { } | ||
| } | ||
| """ | ||
| ) | ||
|
|
||
| val result = defaultCompilerConfig().apply { | ||
| sources = listOf(jSource) | ||
| annotationProcessors = listOf(annotationProcessor) | ||
| compilerPlugins = listOf(mockPlugin) | ||
| inheritClassPath = true | ||
| }.compile() | ||
|
|
||
| verify(mockPlugin, atLeastOnce()).registerProjectComponents(any(), any()) | ||
|
|
||
| Assertions.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.tschuchort.compiletesting | ||
|
|
||
| import io.github.classgraph.ClassGraph | ||
| import org.assertj.core.api.Assertions | ||
| import java.io.File | ||
|
|
||
| fun defaultCompilerConfig(): KotlinCompilation { | ||
| return KotlinCompilation( ).apply { | ||
| inheritClassPath = false | ||
| skipRuntimeVersionCheck = true | ||
| correctErrorTypes = true | ||
| verbose = true | ||
| reportOutputFiles = false | ||
| messageOutputStream = System.out | ||
| } | ||
| } | ||
|
|
||
|
|
||
| fun assertClassLoadable(compileResult: KotlinCompilation.Result, className: String): Class<*> { | ||
| try { | ||
| val clazz = compileResult.classLoader.loadClass(className) | ||
| Assertions.assertThat(clazz).isNotNull | ||
| return clazz | ||
| } | ||
| catch(e: ClassNotFoundException) { | ||
| return Assertions.fail<Nothing>("Class $className could not be loaded") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the classpath for a dependency (format $name-$version). | ||
| * This is necessary to know the actual location of a dependency | ||
| * which has been included in test runtime (build.gradle). | ||
| */ | ||
| fun classpathOf(dependency: String): File { | ||
| val regex = Regex(".*$dependency\\.jar") | ||
| return ClassGraph().classpathFiles.first { classpath -> classpath.name.matches(regex) } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo