https://github.com/eric2788/reflectionproxy
using java reflection elegantly with metaprogramming
https://github.com/eric2788/reflectionproxy
java java-proxy metaprogramming reflection reflection-library
Last synced: 3 months ago
JSON representation
using java reflection elegantly with metaprogramming
- Host: GitHub
- URL: https://github.com/eric2788/reflectionproxy
- Owner: eric2788
- Created: 2024-12-03T09:43:31.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-12-13T09:51:31.000Z (about 1 year ago)
- Last Synced: 2025-02-01T15:09:25.105Z (11 months ago)
- Topics: java, java-proxy, metaprogramming, reflection, reflection-library
- Language: Java
- Homepage:
- Size: 94.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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
- [Maven Central](https://central.sonatype.com/artifact/xyz.ericlamm.toolkits/reflection-proxy)
- [Github Packages](https://github.com/eric2788/ReflectionProxy/packages/2334237)
## Example
Assume you would like to change a private field of an object from a library:
```java
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:
```java
@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
```java
public static void main(String[] args) {
var proxy = ReflectionProxy
.builder()
.prepare(SecretManagerProxy.class)
.build();
var ins = SecretManager.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](/src/test/java/xyz/ericlamm/toolkits/reflectionproxy/TestReflectionProxy.java)