• This repository has been archived on 20/Dec/2022
  • Stars
    star
    238
  • Rank 169,306 (Top 4 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

(Deprecated) ๐Ÿ”ง Kotlin Native/JS/JVM Annotation Processor library for Kotlin compiler plugins

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

Introduction ๐Ÿ™‹โ€โ™‚๏ธ ๐Ÿ™‹โ€

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:

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.

More Repositories

1

Jetpack-Compose-Playground

Community-driven collection of Jetpack Compose example code and tutorials ๐Ÿš€ https://foso.github.io/compose
Kotlin
3,132
star
2

Ktorfit

HTTP client generator / KSP plugin for Kotlin Multiplatform (Android, iOS, Js, Jvm, Native) using KSP and Ktor clients inspired by Retrofit https://foso.github.io/Ktorfit
Kotlin
1,545
star
3

Cabret-Log

โœ๏ธ Method call logging for Kotlin Multiplatform
Kotlin
196
star
4

KotlinCompilerPluginExample

This is an example project that shows how to create a Kotlin Compiler Plugin. The plugin will print "Hello from" and the name of the file that is being compiled, as a compiler warning to the terminal log.
Kotlin
107
star
5

HtmlToComposeWebConverter

IntelliJ Idea Plugin that can convert HTML to Compose HTML code. https://plugins.jetbrains.com/plugin/18261-html-to-compose-web-converter
Kotlin
96
star
6

Showdown

Showdown is a selfhosted open source web app/server, you can use for remote planning pokerยฎ with scrum teams. Try at http://showdown.fly.dev/#/
Kotlin
48
star
7

KotlinReactNativeMpp

A ReactNative App written with Kotlin JS
Kotlin
35
star
8

Sheasy

This an Android App that helps you share/manage your files on your Android Device through a WebInterface in the Browser - Built with Ktor and Kotlin-React
Kotlin
35
star
9

Folders2kt

An interpreter/transpiler, written in Kotlin, for the esoteric programming language Folders, a language with no code and just folders
Kotlin
27
star
10

ExoPlayer-with-MediaControls

This is a small project i created to learn how to use the Exoplayer from Google. It plays an video from an URL and you can control the playback. It can now play HLS,Dash and mp4 streams
Java
20
star
11

gtvmonkey-scripts

This a template project that helps you write Greasemonkey/Tampermonkey/ViolentMonkey scripts with KotlinJs
Kotlin
18
star
12

compose-snake-web

This is a Compose HTML port of CompoSnake
Kotlin
12
star
13

JKAndroidWebserver

This is an example project that uses NanoHTTPd to run a Webserver on Android
Java
9
star
14

BitriseArtifactDownloader

This is a flutter app which uses the Bitrise Api(https://api-docs.bitrise.io/) to show the bitrise projects and builds and lets you download your artifacts.
Dart
9
star
15

JKManageSpaceActivity

This is an example project for my post about ManageSpaceActivity on Android
Kotlin
8
star
16

C-Crit_Generator

A generator for your critical secret keys
Kotlin
6
star
17

Android-Deeplink-Starter

IntelliJ/Android Studio plugin that adds an alternative UI to start Android deeplinks
Kotlin
3
star
18

compose-html-hello-world

Kotlin
3
star
19

ReverseMe

The app is part of my post about reserve engineering android apps
Java
3
star
20

ComposeReact

Just for fun experiment with a React like API for Jetpack Compose
Kotlin
2
star
21

MealApp

Kotlin Multiplatform project using Jetpack Compose and SwiftUI
Kotlin
2
star
22

JKLiveDataExample

This is an example project for my article about LiveData http://jensklingenberg.de/learn-how-to-use-livedata/
Java
2
star
23

KmdcExample

Example project with Compose HTML and Kmdc
Kotlin
2
star
24

JKWebsocket

Example Project that shows how to use NanoHTTP/NanoWSD to create a Websocket Server. After you connect to it, it will post "Hello World" plus the systemdate to the client every second.
Kotlin
2
star
25

KSP-Example

Kotlin
1
star
26

BasicKmm

Kotlin
1
star
27

dukat-idea

Intellij Plugin that adds a menu entry to start Dukat to generate KotlinJs Wrappers
Kotlin
1
star
28

ExtensionGenerator

Work in Progress
Kotlin
1
star
29

BananiaMapConverter

Tiled map files and converter for the game Banania
Kotlin
1
star
30

Foso

1
star
31

compose-web-nes-css

Compose Wrapper for Nes Css (WIP)
Kotlin
1
star
32

kt_dart_builder

A fluent Kotlin API for generating valid Dart source code
Kotlin
1
star
33

Experimental

Nothing to see here
Kotlin
1
star
34

kotlin-react-Hello-World

A simple Kotlin React Project with gradle, that shows Hello World and a reactstrap button
Kotlin
1
star
35

kotlinforeverything

WIP
1
star
36

compose

This is only used as an url shortener for https://github.com/Foso/Jetpack-Compose-Playground/
HTML
1
star
37

JK_Launcher

This is a very simple launcher app.
Java
1
star