All Projects → Foso → Mpapt

Foso / Mpapt

Licence: apache-2.0
🔧 Kotlin Native/JS/JVM Annotation Processor library for Kotlin compiler plugins

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Mpapt

Gsonpath
A Java annotation processor library which generates gson type adapters using basic JsonPath style annotations
Stars: ✭ 54 (-74.65%)
Mutual labels:  annotation-processor
Kson
Gson TypeAdapter & Factory generator for Kotlin data classes
Stars: ✭ 90 (-57.75%)
Mutual labels:  annotation-processor
Androidannotations
Fast Android Development. Easy maintainance.
Stars: ✭ 11,167 (+5142.72%)
Mutual labels:  annotation-processor
Kpoet
An expressive DSL built on top of JavaPoet to make writing code almost as easy as writing the code yourself.
Stars: ✭ 58 (-72.77%)
Mutual labels:  annotation-processor
Dart
Extras binding and intent builders for Android apps.
Stars: ✭ 1,203 (+464.79%)
Mutual labels:  annotation-processor
Annotation Processing Example
It is the example project for the annotation processing tutorial.
Stars: ✭ 116 (-45.54%)
Mutual labels:  annotation-processor
Zerocell
Simple, efficient Excel to POJO library for Java
Stars: ✭ 53 (-75.12%)
Mutual labels:  annotation-processor
Placeholderview
This library provides advance views for lists and stacks. Some of the views are build on top of RecyclerView and others are written in their own. Annotations are compiled by annotation processor to generate bind classes. DOCS -->
Stars: ✭ 2,104 (+887.79%)
Mutual labels:  annotation-processor
Mapstruct Kotlin
Using mapstruct with kotlin data classes.
Stars: ✭ 84 (-60.56%)
Mutual labels:  annotation-processor
Bulldog
Android library to simplify reading and writing to SharedPreferences, never write code like this anymore prefs.edit().putString("someKey","someString").apply()
Stars: ✭ 133 (-37.56%)
Mutual labels:  annotation-processor
Inspector
A tiny class validation library.
Stars: ✭ 64 (-69.95%)
Mutual labels:  annotation-processor
Gencycler
Gencycler is the fastest way to write RecyclerView adapters
Stars: ✭ 73 (-65.73%)
Mutual labels:  annotation-processor
Crumb
An annotation processor for breadcrumbing metadata across compilation boundaries.
Stars: ✭ 115 (-46.01%)
Mutual labels:  annotation-processor
Auto Value Map
AutoValue Extension to add Map generation support
Stars: ✭ 56 (-73.71%)
Mutual labels:  annotation-processor
Shortbread
Android library that creates app shortcuts from annotations
Stars: ✭ 1,803 (+746.48%)
Mutual labels:  annotation-processor
Jenny
JNI glue code generator
Stars: ✭ 53 (-75.12%)
Mutual labels:  annotation-processor
Kripton
A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.
Stars: ✭ 110 (-48.36%)
Mutual labels:  annotation-processor
Awesome Annotation Processing
A curated list of resources related to the Java annotation processing API (JSR 269)
Stars: ✭ 193 (-9.39%)
Mutual labels:  annotation-processor
Beanknife
An annotation processor library to automatically generate the data transfer objects (DTO).
Stars: ✭ 163 (-23.47%)
Mutual labels:  annotation-processor
Flownav
Annotation processor that provides better navigation on android multi-modules projects 🛳.
Stars: ✭ 122 (-42.72%)
Mutual labels:  annotation-processor

MpApt - Kotlin (Native/JS/JVM) Annotation Processor library

jCenterjCenter All Contribtors PRs Welcome All Contributors Tweet Kotlinlang slack

Introduction 🙋‍♂️ 🙋‍

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.3.71,1.4.0

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:

Show some ❤️ and star the repo to support the project

GitHub stars GitHub forks GitHub watchers Twitter Follow

Projects that use MpApt:

Usage

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'
}
  1. Create a class that extends de.jensklingenberg.mpapt.model.AbstractProcessor
class MpAptTestProcessor() : AbstractProcessor() {

  1. Add the names of your annotations that you want to detect:
override fun getSupportedAnnotationTypes(): Set<String> = setOf(TestClass::class.java.name, TestFunction::class.java.name)
  1. Do something with detected annotations:
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)
                }
            }
        }
}
  1. Init MpApt inside your ComponentRegistrar:
  • Pass an instance of your processor and the CompilerConfiguration into MpAptProject
  • Then add an instance of MpAptProject to the following extension classes:

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)
    }
  1. That's it

Choose supported target platforms

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

✍️ Feedback

Feel free to send feedback on Twitter or file an issue or join the Kotlin Slack and the Kotlinlang slack channel. Feature requests are always welcome. If you wish to contribute, please take a quick look at How to develop?

👷 Development Project Structure

  • demoProject - An example project that is using MpApt+KotlinPoet to generate code on KotlinJS
  • annotations - A Kotlin Multiplatform project which contains test annotations
  • example - A Kotlin Multiplatform project which applies a gradle plugin(de.jensklingenberg.mpapt) whichs triggers the compiler plugin.
  • buildSrc - This module contains the gradle plugin which trigger the compiler plugin
  • kotlin-plugin - This module contains the Kotlin Compiler Plugin for JVM/JS targets, it implements the kotlin-plugin-shared-module
  • kotlin-compiler-native-plugin - This module contains the Kotlin Compiler Plugin for Native targets, it implements the kotlin-plugin-shared-module
  • kotlin-plugin-shared Contains an implementation of MpApt

Testing

The CompileTest shows you, how you can use Kotlin Compile Testing to test your Processor/Compiler Plugin

See also

📜 License

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].