Skip to content

A set of utilities designed to interact with reflection and speed up it.

License

Notifications You must be signed in to change notification settings

RomanQed/jeflect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jeflect maven-central

A set of utilities designed to interact with reflection and speed up it.

Getting Started

To install it, you will need:

  • Java 11+
  • Maven/Gradle

Features

  • Getting values from annotations by name
  • Parsing bytecode classes
  • Transforming loaded classes
  • Packaging methods using a universal proxy lambdas
  • Packaging methods with meta-lambdas
  • Packaging fields with proxy accessor

Installing

Gradle dependency

dependencies {
    implementation group: 'com.github.romanqed', name: 'jeflect', version: 'LATEST'
}

Maven dependency

<dependency>
    <groupId>com.github.romanqed</groupId>
    <artifactId>jeflect</artifactId>
    <version>LATEST</version>
</dependency>

Examples

Field accessor

package com.github.romanqed.jeflect;

import com.github.romanqed.jeflect.fields.FieldAccessorFactory;

public class Main {
    public static final String README = "README";

    public static void main(String[] args) throws Exception {
        // So, we need to access field by name
        // How can we do this?
        var field = Main.class.getField("README");
        // Default, very slow, built-in reflection way
        System.out.println(field.get(null)); // <-- Very bad choice to use it during active calculating
        // A wonderful, ultra-fast, shining way with field accessor
        var factory = new FieldAccessorFactory();
        var accessor = factory.packField(field); // <-- This action takes a long time, do this only once
        System.out.println(accessor.get()); // <-- This action is performed as fast as a normal field access
    }
}

Lambdas

package com.github.romanqed.jeflect;

import com.github.romanqed.jeflect.lambdas.LambdaFactory;

public class Main {
    public static void main(String[] args) throws Throwable {
        // So, we need to invoke method by name
        // How can we do this?
        var method = Main.class.getMethod("callMe", int.class, int.class, int.class);
        var params = new Object[]{1, 2, 3};
        // Default, very slow, built-in reflection way
        method.invoke(null, params); // <-- Very bad choice to use it during active calculating
        // A wonderful, ultra-fast, shining way with proxy lambdas (not so fast as meta-lambdas, but more universal)
        var factory = new LambdaFactory();
        var lambda = factory.packMethod(method); // <-- This action takes a long time, do this only once
        lambda.invoke(params); // <-- This action is performed as fast as a normal method call
    }

    public static void callMe(int first, int second, int third) {
        System.out.println("Hello, i am very useful method, i can sum your numbers: " + (first + second + third));
    }
}

Meta lambdas

package com.github.romanqed.jeflect;

import com.github.romanqed.jeflect.meta.LambdaType;
import com.github.romanqed.jeflect.meta.MetaFactory;

import java.lang.invoke.MethodHandles;

public class Main {
    public static void main(String[] args) throws Exception {
        // So, we need to invoke method by name
        // How can we do this?
        var method = Main.class.getMethod("callMe");
        // Default, very slow, built-in reflection way
        method.invoke(null); // <-- Very bad choice to use it during active calculating
        // A wonderful, ultra-fast, shining way with meta-lambdas
        var factory = new MetaFactory(MethodHandles.lookup());
        var type = LambdaType.fromClass(Runnable.class);
        var runnable = factory.packLambdaMethod(type, method); // <-- This action takes a long time, do this only once
        runnable.run(); // <-- This action is performed as fast as a normal method call
    }

    public static void callMe() {
        System.out.println("Hello, i am very useful method");
    }
}

Built With

  • Gradle - Dependency management
  • ASM - Generation of dynamic proxies
  • jfunc - Lazy containers

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details