Możliwe duplikaty:
Java Reflection: Getting fields and methods in declaration order
Java. Get declared methods in order they apear in source codeCzy możliwe jest getMethods w porządku za pomocą odbić?
Załóżmy, że mam tej klasy
jest możliwe podjęcie metody pobierające w porządku?
public class ClassA {
private String name;
private Integer number;
private Boolean bool;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Boolean getBool() {
return bool;
}
public void setBool(Boolean bool) {
this.bool = bool;
}
}
muszę spróbować ..
for (Method method : ClassA.class.getDeclaredMethods()) {
if (!(method.getReturnType().toString().equals("void"))) {
method.invoke(obj, new Object[0])));
}
}
mam to z dokumentacji
... elementy w tablicy zwróconej nie są sortowane i nie są w dowolnej kolejności ...
Więc ... czy to po prostu? Czy istnieje alternatywa, czy po prostu muszę coś wdrożyć?
Jaki jest twój ostateczny cel w uzyskaniu metod w porządku? – jzd
Ja * zwykle * widzę metody zwracające metody w porządku kodu źródłowego, ale jak zauważyłeś, to ** nie ** gwarantowane przez specyfikację. A jeśli JVM nie da ci go w tej kolejności, nie ma nic, co byś mógł z tym zrobić, poza możliwością ręcznego sparsowania pliku '.class'. –
http://stackoverflow.com/questions/3148274/java-get-declared-methods-in-order-they-apear-in-source-code –