Reflex - Reflection API for Kotlin

Overview

Reflex

repositories {
    maven { url = uri("https://repo.tabooproject.org/repository/releases/") }
    mavenCentral()
}

dependencies {
    // 本体
    implementation("org.tabooproject.reflex:analyser:{version}")
    implementation("org.tabooproject.reflex:fast-instance-getter:{version}")
    implementation("org.tabooproject.reflex:reflex:{version}") // 需要 analyser 模块
    // 本体依赖
    implementation("org.ow2.asm:asm:9.2")
    implementation("org.ow2.asm:asm-util:9.2")
    implementation("org.ow2.asm:asm-commons:9.2")
    implementation(kotlin("stdlib"))
}

analyser & reflex

Reflex 为基于 Kotlin 语言开发的反射工具,其与 Java 原生反射 API 及 kotlin-reflect 间最大区别在于其可无视软兼容反射目标类中的字段或方法。

如下方代码所示,此类现象在 Bukkit 插件开发中较为常见:

public class AnyPlugin extends JavaPlugin {
    
    private PlayerPointsAPI api; // PlayerPoints
    private AnyField target;
}

习惯于将其他插件内的接口缓存于主类的 Bukkit 开发者不在少数,因即便此时 PlayerPoints 并未安装,类的运行和插件的启动也不会受到影响。但此做法造成的结果是开发者无法通过反射获取该类中的任何字段:

Field field = Main.class.getDeclaredField("target"); // NoClassDefFoundError: PlayerPointsAPI

同类问题也存在于 Bukkit 监听器的注册之上。若插件运行于 1.12 版本以下的环境时,下述 PlayerJoinEvent 监听器将无法正常注册。

public class AnyListener extends Listener {
    
    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        // 全版本
    }
    
    @EventHandler
    public void onSwap(PlayerSwapItemEvent event) {
        // 1.12 新增
    }
}

因此, Reflex 脱身于 TabooLib 并发展为独立类库。通过此类库,下述操作将成为可能:

fast-instance-getter

基于 Java 且不依赖反射的高性能 Kotlin 单例/伴生类实例获取工具,此工具同样解决了上述问题。

val getter = FastInstGetter(ObjectTarget::class.java.name) // 初始化损耗较高,复用时需手动缓存该实例
getter.instance // 获取单例实例
getter.companion // 获取伴生类实例
You might also like...
This Kotlin Multiplatform library is for accessing the TMDB API to get movie and TV show content. Using for Android, iOS, and JS projects.

Website | Forum | Documentation | TMDb 3 API Get movie and TV show content from TMDb in a fast and simple way. TMDb API This library gives access to T

A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations.
A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations.

kotlin-ktor-rest-api A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations. F

An Android app built with Kotlin, consuming StarWars API to display characters of the popular StarWars Movie. It is built with the MVVM pattern and the latest Jetpack components.
An Android app built with Kotlin, consuming StarWars API to display characters of the popular StarWars Movie. It is built with the MVVM pattern and the latest Jetpack components.

StarWars An Android app built with Kotlin, consuming StarWars API to display characters of the popular StarWars Movie. It is built with the MVVM patte

Kotlin API for MiniKeyValue server

minikeyvaluekt Kotlin API for MiniKeyValue. Example val mkv = MiniKeyValue("http://localhost:3000/", MKVOkHttp) assert(mkv.put("wehave", "bigswag")) a

API for a library using Kotlin, Spring-boot and covered by test

Library API This API is to create Books and be able to borrow from them I'm using: Spring Boot version 2.5.6 Kotlin 1.5.31 Java 11 I'm implementing us

Blog Backend Api built with Kotlin, Springboot and AWS

Kotlin, Spring Boot, MySQL, JPA, Hibernate Rest API for Blogs A Restful CRUD API using Kotlin, Spring Boot, Mysql, JPA and Hibernate hosted on AWS Ela

Desenvolvimento de uma API utilizando SpringBoot + Kotlin com o intuito de cadastro de Vingadores.

Avengers API - Curso de introdução á Arquitetura Hexagonal com Spring Boot e Kotlin Desenvolvimento de uma API utilizando SpringBoot + Kotlin com o in

API Rest With Kotlin And Spring Boot

##API REST WITH KOTLIN AND SPRING BOOT GET Url: http://localhost:8080/customers Response (Status Code: 200 Ok) { "_embedded": { "customer

Api Rest Card Game made in Kotlin with Ktor

ApiRest-CardGame "Card Game API" is a project made in Kotlin with Ktor. The API allows you to manage a simple card game deck (shuffle, take a card, pu

Comments
Owner
TabooLib Project
老坏黑的🦠库
TabooLib Project
intera.kt is a Kotlin library for interacting with the Discord Interactions API through a gateway service or a REST API.

?? Overview ⚠️ WARNING: intera.kt is a work in progress. It is not yet ready for use. You may encounter bugs and other issues, but please report if yo

Pedro Henrique 1 Nov 30, 2021
intera.kt is a Kotlin library for interacting with the Discord Interactions API through a gateway service or a REST API.

?? Overview ⚠️ WARNING: intera.kt is a work in progress. It is not yet ready for use. You may encounter bugs and other issues, but please report if yo

Pedro Henrique 1 Nov 30, 2021
Postman-API-Fest-22 - Project for Postman API Fest 22

Project for Postman API Fest 22 Team Moon With only two members on the board, we

Pradumna Saraf 9 Nov 27, 2022
Mocking for Kotlin/Native and Kotlin Multiplatform using the Kotlin Symbol Processing API (KSP)

Mockative Mocking for Kotlin/Native and Kotlin Multiplatform using the Kotlin Symbol Processing API (KSP). Installation Mockative uses KSP to generate

Mockative 121 Dec 26, 2022
Kotlin-GraphQL-Apollo - Sencillo cliente para consumir una API GraphQL con Apollo usando Kotlin

Kotlin GraphQL Apollo Sencillo cliente para consumir una API GraphQL con Apollo

José Luis González Sánchez 2 Jan 25, 2022
Spring-kotlin - Learning API Rest with Kotlin, Spring and PostgreSQL

Kotlin, Spring, PostgreSQL and Liquibase Database Migrations Learning Kotlin for

Andre L S Ferreira 2 Feb 14, 2022
MMKV for Kotlin Multiplatform is a wrapper for MMKV using Kotlin API

MMKV for Kotlin Multiplatform is a wrapper for MMKV using Kotlin API

Ctrip, Inc. 65 Dec 29, 2022
TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer

KataTODOApiClient for Kotlin We are here to practice integration testsing using HTTP stubbing. We are going to use MockWebServer to simulate a HTTP se

Karumi 61 Nov 20, 2022
Kotlin app that shows a list of characters using the api SWAPI

StarWarsCharacters Kotlin app that shows a list of characters using the api SWAPI (https://swapi.dev) This is a clean architecture app example built w

Lorenzo 3 Jun 2, 2021