mardi 13 août 2019

Custom annotation processing while coding

The purpose of the project I'm working on is to handle annotation at compile time, it is not focused on what exactly I'm developing.

I took a simple subject for this and I'm writing a custom collection that will store elements and provide methods to manage them.

What I wanna do is to create an annotation @Contains, for example, to generate itemsContains method that could be processed while coding (instead of writing code manually).

public class Array {

    private List<String> items;

    public Array() {
        items = Arrays.asList("abc", "def", "xyz");
    }

    public boolean itemsContains(String expected) {
        return items.contains(expected);
    }
}

Generally, I want my class to look something like:

public class Array {

    @Contains
    private List<String> items;

    public Array() {
        items = Arrays.asList("abc", "def", "111");
    }
}

The important thing I want to reach is to have itemsContains method show up once the annotation is applied to a field. This is how it should look like:

https://imgur.com/QmSs0uw

Alternate existing examples are Lombok's @Getter/@Setter.

So what functionality or configurations should I implement to get the expected result? Would be grateful for some real implementations or guides how to perform it step by step.





Aucun commentaire:

Enregistrer un commentaire