All Projects → bennyhuo → Mixin

bennyhuo / Mixin

Licence: MIT license
An annotation processor to mix Java or Kotlin Classes up into a single Class. Also a sample of X Processing which is an abstract layer of apt and ksp.

Programming Languages

kotlin
9241 projects
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Mixin

KML
Kerbal Markup Lister - A persistence file editor for Kerbal Space Program
Stars: ✭ 26 (+30%)
Mutual labels:  ksp
KSP-Recall
Recall for KSP blunders, screw ups and borks.
Stars: ✭ 19 (-5%)
Mutual labels:  ksp
hangar
This is a plugin for the Kerbal Space Program. It provides several modules and parts to store complete vessels that aren't needed to free CPU of their burden.
Stars: ✭ 16 (-20%)
Mutual labels:  ksp
avaje-inject
Dependency injection via APT (source code generation) ala "Server side Dagger DI"
Stars: ✭ 114 (+470%)
Mutual labels:  kapt
KerbalHealth
KSP mod that adds health management.
Stars: ✭ 13 (-35%)
Mutual labels:  ksp
MechPeste-Java
Mod escrito em Java para fazer pousos no estilo "Suicide Burn" no Kerbal Space Program.
Stars: ✭ 46 (+130%)
Mutual labels:  ksp
grpc-kapt
Annotation driven gRPC clients & servers in Kotlin
Stars: ✭ 25 (+25%)
Mutual labels:  kapt
jvmbuilder
A source code generator for Kotlin data classes to automatically create a Builder class.
Stars: ✭ 16 (-20%)
Mutual labels:  kapt
copydynamic
Prototype of generating `copyDynamic` extension functions for kotlin data classes
Stars: ✭ 57 (+185%)
Mutual labels:  kapt
Kerbalism
Hundreds of Kerbals were killed in the making of this mod.
Stars: ✭ 142 (+610%)
Mutual labels:  ksp
Telemachus-1
No description or website provided.
Stars: ✭ 33 (+65%)
Mutual labels:  ksp
Kotlin-Annotation-Processor
Annotation Processor Sample in Kotlin
Stars: ✭ 19 (-5%)
Mutual labels:  kapt
AndroidStartup
模块化启动框架
Stars: ✭ 126 (+530%)
Mutual labels:  ksp
compose-destinations
Annotation processing library for type-safe Jetpack Compose navigation with no boilerplate.
Stars: ✭ 2,068 (+10240%)
Mutual labels:  ksp
SDHI ServiceModuleSystem
Parts pack for Kerbal Space Program that consists of a stockalike Service Module and accessories inspired by NASA’s Orion MPCV, and designed specifically for use with the stock Mk1-3 Command Pod.
Stars: ✭ 24 (+20%)
Mutual labels:  ksp
Custom-Asteroids
A KSP mod that lets users control where asteroids appear
Stars: ✭ 13 (-35%)
Mutual labels:  ksp
ThrottleControlledAvionics
A mod for KerbalSaceProgram
Stars: ✭ 45 (+125%)
Mutual labels:  ksp
Astrogator
A space-navigational aide for Kerbal Space Program
Stars: ✭ 25 (+25%)
Mutual labels:  ksp
dagger2-kotlin
dagger2,querydsl kotlin 1.5.x annotation processor, gradle 7.x
Stars: ✭ 56 (+180%)
Mutual labels:  kapt
ColdStorage
Lightweight data loading and caching library for android
Stars: ✭ 39 (+95%)
Mutual labels:  kapt

Mixin

This is an annotation processor to mix Java or Kotlin Classes up into a single Class.

This is also a sample of X Processing which is an abstract layer of apt and ksp.

Usage

The design of this project is very straightforward.

Suppose I have two api classes for User:

class UserApi {
    fun getName(id: Long): String = TODO()

    fun getAge(id: Long): Int = TODO()
}
public class GitHubUserApi {
    public String getGitHubUrl(long id) {
        throw new NotImplementedError();
    }

    public int getRepositoryCount(long id) {
        throw new NotImplementedError();
    }
}

and I want to combine them into a single class UserApis, I will annotate these two classes with @Mixin:

@Mixin("com.bennyhuo.kotlin.mixin.sample", "UserApis")
class UserApi { 
    ... 
}
@Mixin(packageName = "com.bennyhuo.kotlin.mixin.sample", name = "UserApis")
public class GitHubUserApi {
    ...
}

with apt or ksp, com.bennyhuo.kotlin.mixin.sample.UserApis will be generated:

package com.bennyhuo.kotlin.mixin.sample;

import com.bennyhuo.kotlin.mixin.sample.library1.UserApi;
import com.bennyhuo.kotlin.mixin.sample.library2.GitHubUserApi;
import java.lang.String;
import org.jetbrains.annotations.NotNull;

public class UserApis {
  private final UserApi userApi;

  private final GitHubUserApi gitHubUserApi;

  public UserApis() {
    userApi = new UserApi();
    gitHubUserApi = new GitHubUserApi();
  }

  public int getRepositoryCount(long id) {
    return gitHubUserApi.getRepositoryCount(id);
  }

  public String getGitHubUrl(long id) {
    return gitHubUserApi.getGitHubUrl(id);
  }

  @NotNull("")
  public String getName(long id) {
    return userApi.getName(id);
  }

  public int getAge(long id) {
    return userApi.getAge(id);
  }
}

That's it.

It is pretty useful in some cases.

For example, the JavaScript bridge for Android WebView. You may want to separate different bridge functions into different modules and add them to the WebView as one bridge object, so you have to write a wrapper class in the main module to assemble all the bridge functions. With this library, you can easily achieve that.

Set up your project

The basic settings is shown as below:

plugins {
    id("com.google.devtools.ksp") version "1.6.0-1.0.1" // ksp
    id("org.jetbrains.kotlin.kapt") // kapt
}

repositories {
    mavenCentral()
    google() 
}

dependencies {
    ksp("com.bennyhuo.kotlin:mixin-compiler:1.6.0.1") // ksp
    kapt("com.bennyhuo.kotlin:mixin-compiler:1.6.0.1") // kapt
    implementation("com.bennyhuo.kotlin:mixin-annotations:1.6.0.1")
}

Multi-modules

If you want to mix class files from different modules, you should configure some of them as 'library' to generate index.

For example, we have

  • an Api0.java in module a
  • an Api1.kt in module b
  • an ApiMain.kt in module main

and want to generate the mixed class Api.java into module main, we should add an argument mixin.library to module a and b in their build.gradle.kts files:

// for kapt
kapt {
    arguments {
        arg("mixin.library", "true")
    }
}

// for ksp
ksp {
    arg("mixin.library", "true")
}

The default value is false, so that mixed classes are generated by default.

Change Logs

1.6.0.1

  • Generate mixed classes by default.
  • Keep the method order stable.

1.6.0.0

  • The first release.

License

MIT License

Copyright (c) 2021 Bennyhuo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
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].