samedi 26 mars 2016

Working with custom annotations

So today I tried to create my custom annotation to represent a set of attributes but I need a void to execute some kind of functionality with the set of attributes provided.

I'm looking for something similar to

Public @interface CommandSignature {

String[] alias();

Rights[] rights();

String syntax();

void execute();

}

and a sub-class would look like

@CommandSignature(
    alias = { "hello" }, 
    rights = { Rights.USER}, 
    syntax = "Use this command as ::hello"
)
public final class Test {

    @Override
    public void execute() {
        System.out.println("this is not possible since custom annotations cannot hold void methods");
    }
}

As u can see that's not possible since a custom annotation class cannot hold void methods.

So the solution would be defining a new interface which holds an #execute method.

public interface Command {

void execute();

}

But how would I retrieve the execute and the command signature for each sub-class in a specific directory? I'm sure reflection is the way to go but I can't wrap my mind around it.

tl;dr how can I call execute and retrieve the annotations of that sub-class.

Keep in mind, I might end up with 20-30 classes so defining each value in a list is not prefered unless done with reflection on startup.





Aucun commentaire:

Enregistrer un commentaire