Coder Social home page Coder Social logo

ridebmx / mixin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spectral-powered/mixin

0.0 0.0 0.0 225 KB

A Mixin injector framework for modifying bytecode in scalable format

Home Page: https://spectralpowered.org

License: GNU General Public License v3.0

Java 4.41% Kotlin 95.59%

mixin's Introduction

Spectral Powered Mixin contains a Mixin style injector framework in order to modify bytecode in a scalable way. Mixin also provides a simple injector setup solution via a gradle plugin handling the build-time injection and configurations.

Introduction  •  Installation  •  Usage  •  Documentation  •  Issue?

Introduction

This mixin framework library provides> simple APIs/functions/methods to handle a mixin based approach to bytecode modifications.

  • Annotation based raw injections
  • Runtime / Compile Time injectors
  • Simple gradle plugin configuration
  • Easy Api injection
  • Smart multi Mixin class merging

Installation

Gradle

Add to settings.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
        maven(url = "https://maven.spectralpowered.org/")
    }
}

Add to build.gradle

plugins {
    id("org.spectralpowered.mixin.plugin") version "0.1.0"
}

repositories {
    mavenCentral()
    maven(url = "https://maven.spectralpowered.org/")
}

To setup the mixin injector configurations.

dependencies {
    // The project / dependency containing your mixins
    mixin(project(":my-mixin-module"))
    
    // The project / dependency containing your minxin api
    mixinApi(project(":my-api"))
    
    // The project / dependency you want to inject into
    inject(project(":my-injection-target"))
}

Mixin dependencies:

dependencies {
    // Includes Everything Normally Needed
    implementation("org.spectralpowered:mixin:0.1.0")
    
    // Explicit Mixin modules 
    implementation("org.spectralpowered:mixin-injector:0.1.0")
    implementation("org.spectralpowered:mixin-annotations:0.1.0")
    implementation("org.spectralpowered:mixin-asm:0.1.0")
}

Usage

Mixin Class example:

@Mixin(Test.class)
public abstract class TestMixin implements TestApi {
    
    @Shadow
    private abstract void shadow$testMethod();
    
    private void myCustomLogic() {
        System.out.println("Hello from TestMixin!");
    }
    
    @Overwrite
    @Override
    public void testMethod() {
        this.myCustomLogic();
        this.shadow$testMethod();
    }
}

Target Class Example:

public class Test {
    
    public void testMethod() {
        System.out.println("Hello from original Test class.");
    }
}

Api Class Example:

interface TestApi {
    
    void testMethod();
    
}

When you build or run the module/project with the mixin gradle plugin applied. There will be an embedded jar file put in the resources of your sourceSet at compileTime containing the injected target jar.

my-app/
├─ org.myapp.app/
│  ├─ App.class
│  ├─ Other.class
├─ target.injected.jar

This injected jar target.injected.jar can simply be loaded into a classloader. See example below.

object App {
    
    @JvmStatic
    fun main(args: Array<String>) {
        println("Starting app.")
        
        val classLoader = URLClassLoader(arrayOf(App::class.java.getResource("/target.injected.jar")!!.toURI().toURL()))
        val testKlass = classLoader.loadClass("myapp.TestClass") as Class<TestApi>
        val testInstance = testKlass.getDeclaredConstructor().newInstance()
        testInstance.testMethod()
    }
}

The following main function will print the following to console:

Starting app.
Hello from TestMixin!
Hello from original Test class.

mixin's People

Contributors

kyle-escobar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.