简体   繁体   中英

Get all public variables names & values (including of inherited object) in Java

  • edit - Here I gave a specific obj. as an example but I'm asking for any obj. I am given *

I'm looking for a way to get all public attributes in the class and all subclasses of an object (name of the attribute and its value). Let say we have a People object:

i

mport java.util.ArrayList;

    public class People {
        public ArrayList<Person> ppl= new ArrayList<Person>();
        int count=2;
        public People() {
            ppl.add(new Person(55, "Daddy", "Long Legs"));
            ppl.add(new Person(20, "Jhon", "Snow"));
        }
        public class Person{
            public int age;
            public Name name;
            public Person(int age, String first, String last){
                this.name = new Name(first, last);
                this.age = age;
            }
            public class Name{
                String first;
                String last;
                public Name(String first, String last) {
                    this.first = first;
                    this.last = last;
                }
            }
        }

    }

I saw a reference here (I can't comment on there bc I don't have enough points): Java reflection get sub class variable values / get object instance from member field

and tried to implement it also but then my output is

ppl [People$Person@4aa298b7, People$Person@7d4991ad]

whereas I need it needs to go into each Person and extract its variables(and their values). I searched for a information that could help me but I couldn't find anything..any advice?

code a toString() method

What you are getting People$Person@4aa298b7 is the Object.toString representation....

getClass().getName() + '@' + Integer.toHexString(hashCode())

IMHO You need to override the toString() method in both classes: Person and Name .

For example:

public class Name{
    String first;
    String last;
    public Name(String first, String last) {
        this.first = first;
        this.last = last;
    }

    @Override
    public String toString() {
        return this.first + " " + this.last;
    }
} 

get fields and values based on known Person class

If this does not fit, you can get fields names and values using reflection like this:

public static void main(String[] args) throws IllegalAccessException
{
    People pe = new People();
    Field[] allFields = People.Person.class.getDeclaredFields();

    for (Field field : allFields)
    {
      for (People.Person p : pe.ppl)
        System.out.println("Name: " + field.getName() + ". Value: " + field.get(p));
    }
}

OUTPUT:

Name: age. Value: 55
Name: age. Value: 20
Name: name. Value: Daddy Long Legs
Name: name. Value: Jhon Snow
Name: this$0. Value: People@677327b6
Name: this$0. Value: People@677327b6

NOTE: if you don't want this 2 final values representing the People with ugly results you can:

  • Split Person and Name and make them 2 independent classes
  • Make a toString() method in People class

dynamically get fields from inner classes

If you want to dynamically get fields from inner classes:

public static void main(String[] args) throws IllegalAccessException
{
    Class[] allClasses = People.class.getClasses();

    for (Class clazz : allClasses) {
      Field[] allFields = clazz.getFields();
      for (Field field : allFields) {
        String className = clazz.getName();
        String fieldName = field.getName();
        System.out.println("Class name: " + className + " - Field name: " + fieldName + ".");
      }
    }
}

OUTPUT:

Class name: People$Name - Field name: first.
Class name: People$Name - Field name: last.
Class name: People$Person - Field name: age.
Class name: People$Person - Field name: name.

But not sure how you can get values from inside the ArrayList<Person> ....

public People() {
        ppl.add(new Person(55, "Daddy", "Long Legs"));
        ppl.add(new Person(20, "Jhon", "Snow"));

        for (Person person : ppl) {
            System.out.println(person.name.last);
            System.out.println(person.name.first);
            System.out.println(person.age);
        }

        System.out.println("Size of list: " + ppl.size());
    }

Example without toString() method.

I believe the closest you can get is related to this question java: get all variable names in a class .

Using Field[] fields = YourClassName.class.getFields(); returns all class fields as java.lang.reflect.Field .

You can check if field is public using Field.getModifiers() and Modifier.isPublic(Modifier) .

You can get the field value using Object Field.get() .

Hope that helps.

I Agree with @Jordi Castilla , you need to override toString method properly to get correct output.

For Example :

import java.util.ArrayList;
class People {

    public ArrayList<Person> ppl= new ArrayList<Person>();
    int count=2;
    public People() {
        ppl.add(new Person(55, "Daddy", "Long Legs"));
        ppl.add(new Person(20, "Jhon", "Snow"));
    }

    @Override
    public String toString() {
        return "{ Count: "+this.count + " , People:" + this.ppl+" }";
    }

    public class Person{
        public int age;
        public Name name;
        public Person(int age, String first, String last){
            this.name = new Name(first, last);
            this.age = age;
        }


        @Override
        public String toString() {
            return "{ Name: "+this.name + " , Age:" + this.age+" }";
        }

        public class Name{
            String first;
            String last;
            public Name(String first, String last) {
                this.first = first;
                this.last = last;
            }

            @Override
            public String toString() {
                return "{ FirstName: "+this.first + ", LastName: " + this.last+ " }";
            }
        }
    }


  public static void main(String[] args) {
    People ppl = new People();
    System.out.println("OUTPUT => "+ ppl.toString());

  }
}


//Output

OUTPUT => { 
    Count: 2 , 
    People:[
        { Name: { FirstName: Daddy, LastName: Long Legs } , Age:55 }, 
        { Name: { FirstName: Jhon, LastName: Snow } , Age:20 }
    ] 
}

here is a recursive method I did (after I added a toString method to Name class). Here it is. However, it is still doesn't prints the variable names inside the ppl list:

private static String getAllFields(Object obj){
    Class<?> objClass = obj.getClass();
    StringBuilder res = new StringBuilder();
    Field[] fields = objClass.getFields();
    res.append(objClass+"\n");

    for(Field field : fields) {
        Class<?> type = field.getType();
        String name = field.getName();
        res.append(" name: "+name+ " ");

        try {
            Object value = field.get(obj);
            res.append("value: "+value+ "\n");
            if (!type.isPrimitive() && !name.contains("java.lang"))
            {
                res.append(getAllFields(value));
            }
        } catch (IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return res.toString();
}

here is the output: class People name: ppl value: [Daddy Long Legs 55 , Jhon Snow 20 ] class java.util.ArrayList name: count value: 2

notice that there isn't the Person class name there in the output or the names of the variable names of the variables there. I don't really understand why

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM