Skip to content

Experimental Golang style method impl/Monkey patch in Java via annotation processing

License

Notifications You must be signed in to change notification settings

cuzfrog/java-typeclass-experimental

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typeclass experimental impl in Java

Goland style method impl/Monkey patch experiments in Java.

How to run:

// 1. define types-class:
public interface Jsonable<T> {
    String toJson(T t);
}

// 2. write your POJO:
public final class ObjectB {
    private String value;
    //getter/setter
}

// 3. implement a method somewhere:
@Implementation(Jsonable.class)
public static String toJson(ObjectB self) {
    return String.format("{\"value\": \"%s\"}", self.getValue());
}

// 4. directly call it:
ObjectB b = new ObjectB();
b.setValue("abc")
System.out.println(b.toJson());
mvn install
mvn compile -pl clientapp exec:java
# {"value": "abc"}

See demo.App in clientapp executed with method toJson added to ObjectB

How to debug:

mvndebug + your IDE's remote JVM debug.

How does it work?

Similar to Project Lombok

Author:

Cause Chung cuzfrog@gmail.com

About

Experimental Golang style method impl/Monkey patch in Java via annotation processing

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published