2

Is there any easy way to achieve this.

Let's say I have an array of string with the name of POJO's in there and I"m trying to print all the list of attributes, is there any way to achieve this easily?\

String [] nameofClass;
for(String name:nameofClass)
name.class.getDeclaredFields();

Thanks

2
  • Have you looked into BeanUtils ? Commented Mar 25, 2013 at 16:10
  • By name of POJO do you mean the name of a variable or the class itself? Commented Mar 25, 2013 at 16:23

2 Answers 2

9

Class.forName(name).getDeclaredFields() within your for loop is probably what you're looking for.

Note that name should be the full path of the class, i.e. not only TheClass but the.package.to.TheClass.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes you can. See the example given below:

        String [] nameofClass = {"java.lang.Object","java.lang.Thread"};//Give complete path of the class
        try{
            for(String name : nameofClass)
            {
                Class cl = Class.forName(name);
                System.out.println(cl);
                System.out.println("\t"+java.util.Arrays.toString(cl.getDeclaredFields()));
            }
        }catch(Exception ex){System.out.println(ex);}   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.