Git Product home page Git Product logo

jeflect's Introduction

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

jeflect's People

Contributors

dependabot[bot] avatar romanqed avatar

Stargazers

 avatar

Watchers

 avatar

jeflect's Issues

Migrate library to java 11

It is necessary to write an integration with project-jigsaw, adapt the syntax and rewrite the reflection hacks.

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.