samedi 2 janvier 2021

Implement parameter validation through Proxy, InvocationHandler and custom Annotations

I am trying to imitate Spings validated and custom constraints annotations so that I can validate parameters when they reach an endpoint on my server. I got the basics of Proxy and InvocationHandler down, but I am stuck on how a single annotation would execute some code. Take this example:

I want parameters annotated with

@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface PostId {}

to be validated automatically in the background. So ideally for any method with a parameter and this annotation I could create validation tests.

public class Main {

    public void doSomething(@PostId String id) {
        // id should be evaluated in the background and not explicitly
    }

    public static void main(String[] args) {
        String test = "test";
        doSomething(test);
    }
}

EDIT: I explicitly said I want to imitate the behavior of Spring because I want to implement this myself, but I don't quite know how to.





Aucun commentaire:

Enregistrer un commentaire