jeudi 14 mai 2020

Java reflection: Is the constructor parameter order guaranteed

I created my own annotation to annotate constructors:

@MyConstructorAnnotation
public Constructor(int param1, int param2) {
    this.param1 = param1;
    this.param2 = param2;
}

I wrote an annotation processor which reads the parameter names of each annotated constructor. This is inside the process() method:

for (Element element : annotatedElements)) {
    Field paramsField = element.getClass().getField("params");
    Iterable parameters = (Iterable) paramsField.get(element);
        for (Object parameter : parameters) {
            String name = parameter.toString();
            // do something with the name
        }
}

This may not be very clean, but it gets me the names "param1" and "param2". I tested that with 10+ parameters and the order was still correct. My question is: Is it guaranteed that the parameters will be in the correct order?





Aucun commentaire:

Enregistrer un commentaire