(Deprecated) 🔧 Kotlin Native/JS/JVM Annotation Processor library for Kotlin compiler plugins
Kotlin
238
146 commits
updated Aug 20, 2022
Note This project is deprecated, when you are looking for a multiplatform annotation processor, please take a look at KSP
I wrote an annotation processing libary that can detect annotations in Kotlin Native/JS and Jvm projects, because Kapt is only working with KotlinJvm. The library can be used in Kotlin Compiler plugins. Tested with Kotlin 1.4.0,1.5.10
It can detect annotations with following targets:
(CLASS,FUNCTION,PROPERTY,VALUE_PARAMETER,PROPERTY_GETTER,PROPERTY_GETTER,CONSTRUCTOR)
(ANNOTATION_CLASS,TYPE_PARAMETER,FIELD,FILE,LocalVariable)
Example output of my example plugin on Kotlin Native:
Your project?
These are the instructions for v0.8.7, check Changelog for changes on the active development branch
Inside your compiler plugin, add the dependency from MavenCentral
repositories {
mavenCentral()
}
dependencies {
compile 'de.jensklingenberg:mpapt-runtime:0.8.7'
}
class MpAptTestProcessor() : AbstractProcessor() {
override fun getSupportedAnnotationTypes(): Set<String> = setOf(TestClass::class.java.name, TestFunction::class.java.name)
override fun process(roundEnvironment: RoundEnvironment) {
roundEnvironment.getElementsAnnotatedWith(TestClass::class.java.name).forEach {
when (it) {
is Element.ClassElement -> {
log("Found Class: " + it.classDescriptor.name + " Module: " + it.classDescriptor.module.simpleName() + " platform " + activeTargetPlatform.first().platformName)
}
}
}
roundEnvironment.getElementsAnnotatedWith(TestFunction::class.java.name).forEach {
when (it) {
is Element.FunctionElement -> {
log("Found Function: " + it.func.name + " Module: " + it.func.module.simpleName() + " platform " + activeTargetPlatform.first().platformName)
}
}
}
}
Inside a Kotlin Native Compiler Plugin:
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
val processor = MpAptTestProcessor()
val mpapt = MpAptProject(processor,configuration)
StorageComponentContainerContributor.registerExtension(project,mpapt)
IrGenerationExtension.registerExtension(project,mpapt)
}
Inside a Kotlin JVM/JS Compiler Plugin:
override fun registerProjectComponents(
project: MockProject,
configuration: CompilerConfiguration
) {
val processor = MpAptTestProcessor()
val mpapt = MpAptProject(processor,configuration)
StorageComponentContainerContributor.registerExtension(project,mpapt)
ClassBuilderInterceptorExtension.registerExtension(project,mpapt)
JsSyntheticTranslateExtension.registerExtension(project,mpapt)
}
By default your processor is enabled for every target. You can override
isTargetPlatformSupported(platform: TargetPlatform): Boolean
and return "true" if you want to support the target or "false" you don't.
override fun isTargetPlatformSupported(platform: TargetPlatform): Boolean {
val targetName = platform.first().platformName
return when (targetName) {
KotlinPlatformValues.JS -> true
KotlinPlatformValues.JVM -> true
KotlinPlatformValues.NATIVE -> {
return when (configuration.nativeTargetPlatformName()) {
KonanTargetValues.LINUX_X64, KonanTargetValues.MACOS_X64 -> {
true
}
else -> {
true
}
}
}
else -> {
log(targetName)
true
}
}
}
You can distinguish between the native target platforms you want to support.
configuration.nativeTargetPlatformName() will get you the names of the Native Targets(macos_x64,linux_x64,etc). The values are defined in KonanTargetValues. It needs to be used only on Kotlin Native otherwise it will return an empty string
Feel free to send feedback on Twitter or file an issue or join the Kotlin Slack and the channel. Feature requests are always welcome. If you wish to contribute, please take a quick look at How to develop?
The CompileTest shows you, how you can use Kotlin Compile Testing to test your Processor/Compiler Plugin
This project is licensed under the Apache License, Version 2.0 - see the LICENSE.md file for details
Copyright 2019 Jens Klingenberg
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.
218 followers · starred Jun 2020
303 followers · starred Sep 2019
453 followers · starred Sep 2019
97 followers · starred Sep 2019
(Deprecated) 🔧 Kotlin Native/JS/JVM Annotation Processor library for Kotlin compiler plugins
Kotlin
238
146 commits
updated Aug 20, 2022
Note This project is deprecated, when you are looking for a multiplatform annotation processor, please take a look at KSP
I wrote an annotation processing libary that can detect annotations in Kotlin Native/JS and Jvm projects, because Kapt is only working with KotlinJvm. The library can be used in Kotlin Compiler plugins. Tested with Kotlin 1.4.0,1.5.10
It can detect annotations with following targets:
(CLASS,FUNCTION,PROPERTY,VALUE_PARAMETER,PROPERTY_GETTER,PROPERTY_GETTER,CONSTRUCTOR)
(ANNOTATION_CLASS,TYPE_PARAMETER,FIELD,FILE,LocalVariable)
Example output of my example plugin on Kotlin Native:
Your project?
These are the instructions for v0.8.7, check Changelog for changes on the active development branch
Inside your compiler plugin, add the dependency from MavenCentral
repositories {
mavenCentral()
}
dependencies {
compile 'de.jensklingenberg:mpapt-runtime:0.8.7'
}
class MpAptTestProcessor() : AbstractProcessor() {
override fun getSupportedAnnotationTypes(): Set<String> = setOf(TestClass::class.java.name, TestFunction::class.java.name)
override fun process(roundEnvironment: RoundEnvironment) {
roundEnvironment.getElementsAnnotatedWith(TestClass::class.java.name).forEach {
when (it) {
is Element.ClassElement -> {
log("Found Class: " + it.classDescriptor.name + " Module: " + it.classDescriptor.module.simpleName() + " platform " + activeTargetPlatform.first().platformName)
}
}
}
roundEnvironment.getElementsAnnotatedWith(TestFunction::class.java.name).forEach {
when (it) {
is Element.FunctionElement -> {
log("Found Function: " + it.func.name + " Module: " + it.func.module.simpleName() + " platform " + activeTargetPlatform.first().platformName)
}
}
}
}
Inside a Kotlin Native Compiler Plugin:
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
val processor = MpAptTestProcessor()
val mpapt = MpAptProject(processor,configuration)
StorageComponentContainerContributor.registerExtension(project,mpapt)
IrGenerationExtension.registerExtension(project,mpapt)
}
Inside a Kotlin JVM/JS Compiler Plugin:
override fun registerProjectComponents(
project: MockProject,
configuration: CompilerConfiguration
) {
val processor = MpAptTestProcessor()
val mpapt = MpAptProject(processor,configuration)
StorageComponentContainerContributor.registerExtension(project,mpapt)
ClassBuilderInterceptorExtension.registerExtension(project,mpapt)
JsSyntheticTranslateExtension.registerExtension(project,mpapt)
}
By default your processor is enabled for every target. You can override
isTargetPlatformSupported(platform: TargetPlatform): Boolean
and return "true" if you want to support the target or "false" you don't.
override fun isTargetPlatformSupported(platform: TargetPlatform): Boolean {
val targetName = platform.first().platformName
return when (targetName) {
KotlinPlatformValues.JS -> true
KotlinPlatformValues.JVM -> true
KotlinPlatformValues.NATIVE -> {
return when (configuration.nativeTargetPlatformName()) {
KonanTargetValues.LINUX_X64, KonanTargetValues.MACOS_X64 -> {
true
}
else -> {
true
}
}
}
else -> {
log(targetName)
true
}
}
}
You can distinguish between the native target platforms you want to support.
configuration.nativeTargetPlatformName() will get you the names of the Native Targets(macos_x64,linux_x64,etc). The values are defined in KonanTargetValues. It needs to be used only on Kotlin Native otherwise it will return an empty string
Feel free to send feedback on Twitter or file an issue or join the Kotlin Slack and the channel. Feature requests are always welcome. If you wish to contribute, please take a quick look at How to develop?
The CompileTest shows you, how you can use Kotlin Compile Testing to test your Processor/Compiler Plugin
This project is licensed under the Apache License, Version 2.0 - see the LICENSE.md file for details
Copyright 2019 Jens Klingenberg
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.
218 followers · starred Jun 2020
303 followers · starred Sep 2019
453 followers · starred Sep 2019
97 followers · starred Sep 2019