Skip to content

using java reflection elegantly with metaprogramming

Notifications You must be signed in to change notification settings

eric2788/ReflectionProxy

Repository files navigation

ReflectionProxy

Important

This project is proof of concept and just for fun. Use it at your own risk if you want to use it in production.

ReflectionProxy is a simple library that allows you to use reflection more elegantly with metaprogramming, eliminating ugly reflection codes on your programs.

Hook

Example

Assume you would like to change a private field of an object from a library:

package me.example.secret;

public final class SecretManager {
	private static final SecretManager instance = new SecretManager();

	public static SecretManager getInstance() {
		return instance;
	}

	private String secret = "I am a secret"; // edit this one

	public void printSecret() {
		System.out.println(secret);
	}
}

Create a fake inteface for the class:

@ForClass("me.example.secret.SecretManager")
public interface SecretManagerProxy {
	
	@Declared // declare that this is a declared field
    	@Field // declare that this is a field
    	String getSecret();
	
	@Declared // declare that this is a declared field
	@Field // declare that this is a field
    	void setSecret(String secret);

}

Register the interface and use it

public static void main(String[] args) {
	var proxy = ReflectionProxy
			.builder()
			.prepare(SecretManagerProxy.class)
			.build();
	
	var ins = SecretManagerProxy.getInstance();
    	var secretManager = proxy.createProxyForInstance(SecretManagerProxy.class, ins);
	System.out.println("before");
	ins.printSecret();
	secretManager.setSecret("this is a new secret");
    	System.out.println("after");
	ins.printSecret();
}

or checkout the examples from test package

About

using java reflection elegantly with metaprogramming

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages