0

The code

import java.beans.*
for (PropertyDescriptor pd : Introspector.getBeanInfo(Foo.class).getPropertyDescriptors()) {
    if (pd.getReadMethod() != null && !"class".equals(pd.getName()))
        System.out.println(pd.getReadMethod().invoke(foo));
}

This code returns the getters of the class but I am trying to access the getters in the order of attributes where I can set their values to.

How can I access the getters in particualr order?

3
  • What order are you expecting? The JVM does not guarantee any specific ordering. Commented Oct 11, 2016 at 11:27
  • @ChrisK In case if I have id , name , I would like to getid() and then getName(). but the code above doesn't provide any specific order Commented Oct 11, 2016 at 11:30
  • Have you considered sorting, using a custom comparator? eg mkyong.com/java/… Commented Oct 11, 2016 at 11:32

1 Answer 1

0

DEfine the class, then in the instance get all the methods then use a stream to filter the getters only

public class Pint {

    private int x;
    private int y;
    private int z;

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getZ() {
        return z;
    }

and then

 Arrays.stream(p.getClass().getDeclaredMethods()).filter(x -> x.getName().contains("get")).forEach(System.out::println);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.