Solved

Field API

Posted on 2011-02-15
9
359 Views
Last Modified: 2012-05-11
Is there a way to get the values from the getter method of a POJO through Field API java reflection?.
public class Cruft {
	public static void main(String[] args) {
		Student student = new Student();
		student.setAge(12);
		student.setName("Foo");
		Field[] fields = Student.class.getDeclaredFields();
		for (Field field : fields) {
			System.out.println(field.getType().getSimpleName() + field.getName() );
		}
	}
}

Open in new window

0
Comment
Question by:Micheal_Male
9 Comments
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 75 total points
Comment Utility

Are you talking about invocation of methods through reflection, like in this example:
http://www.javadb.com/invoke-methods-of-an-object-using-reflection
0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 75 total points
Comment Utility
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
0
 

Author Comment

by:Micheal_Male
Comment Utility
Thanks. Figured out after reading Field api.
public class Cruft {
	public static void main(String[] args) {
		Student student = new Student();
		student.setAge(12);
		student.setName("Foo");
		Field[] fields = Student.class.getDeclaredFields();
		for (Field field : fields) {
			System.out.println(field.getType().getSimpleName() + field.getName() );
System.out.println(field.get(student));		
}
	}
}

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Micheal_Male
Comment Utility
forgot to mention that i have to add :-

field.setVisible(true);

also in order to get the values
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> from the getter method of a POJO

be aware that what you are doing is not calling the getter methods
0
 

Author Comment

by:Micheal_Male
Comment Utility
you are right objects but i did not find any easier way to get the values from the getter method through reflection when looping through the field name.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
there are two examples of how to above
0
 

Author Comment

by:Micheal_Male
Comment Utility
Used method API which worked just fine
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now