Solved

Reflection help

Posted on 2011-02-14
16
331 Views
Last Modified: 2012-05-11
I have a java class Registration.java which contains following fields :-

public class Registration implements Serializable {

private Date registrationDate;

private Integer registeredClasses;

private Student student;

.
.
.
//getters and setters

}

public class Student {

private String name;

private Integer age;

private boolean isRegistered;

.
.
.
//setter and getters
}


void addStudent(Registration registration) {

}

In addStudent method i wanted to use Reflection to get all the property name in Student.class with all their values also (If included in Registration parameter in addStudent. How can i do that ? I tried spring ReflectionUtils but it gave me all the setters and getters method instead of giving me the property names which would be name, age, isRegistered. Any input ?

0
Comment
Question by:Micheal_Male
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
16 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34893691
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34893721
This one also gives good examples:

http://www.roseindia.net/java/reflect/

in particular items 7 and 12  inths list - example about finding
the fields of class String and then the values of fields in item 12.

However, I'm not sure you'll be able to retrieve information about private fields of your class in this way.
0
 

Author Comment

by:Micheal_Male
ID: 34893737
Thank you.Is there a way to just print the type also excluding the whole class name for the type ?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 47

Expert Comment

by:for_yan
ID: 34893748
In the example 7 which I tried also myself
it prints only the field name, without class

import java.lang.reflect.*;

public class Ffield{
  public static void main(String[] args){
    Class cls = java.lang.String.class;
    Field field = cls.getFields()[0];
    String name;
    name = field.getName();     // It'll show CASE_INSENSITIVE_ORDER
    System.out.println(name);
  }
}

Here is the output of the Example :
C:\roseindia>javac Ffield.java

C:\roseindia>java Ffield
CASE_INSENSITIVE_ORDER  - only one word printed - the frst field of class String

0
 

Author Comment

by:Micheal_Male
ID: 34893765
This is what i get when running the program :-

Field[] fields = Student.class.getDeclaredFields();
            for (Field f : fields) {
                  System.out.println("Type & Name " + f.getType() + " " + f.getName() );
            }

Type & Name class java.lang.String name
Type & Name class java.lang.Integer age
Type & Name boolean isRegistered
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34893773
Yes, I was not printing type, I was printing only name - you mean
you don't want to print 'java.lang.String", but want to print jsut "String"?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34893781
You can then use

String s = f.getType();
if(s.indexOf(".")>-1)s = s.substring(s.lastIndexOf("."));
System.out.println(s);

You'll get only "String" in this way
0
 

Author Comment

by:Micheal_Male
ID: 34893783
Yes
0
 

Author Comment

by:Micheal_Male
ID: 34893816
Thanks yan. I get following out with (.) :-

for (Field field : fields) {
                  String s = field.getType().toString();
                  if(s.indexOf(".") > -1) {
                        s = s.substring(s.lastIndexOf("."));
                  }
                  System.out.println("Type " + s + " name " + field.getName() + " ");
            }

Type .String name name
Type .Integer name age
Type boolean name isRegistered
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34893821
Sorry, this should be fine:

for (Field field : fields) {
                  String s = field.getType().toString();
                  if(s.indexOf(".") > -1) {
                        s = s.substring(s.lastIndexOf(".")+1);
                  }
                  System.out.println("Type " + s + " name " + field.getName() + " ");
            }
0
 

Author Comment

by:Micheal_Male
ID: 34893832
Great. Is it possible to use regex expression for parsing the type instead of substring ?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 34893841
Probably it is possible, but once you are in Java code -
substring will be definitely preferable way in many senses.
0
 

Author Closing Comment

by:Micheal_Male
ID: 34893846
Great
0
 
LVL 35

Expert Comment

by:mccarl
ID: 34893855
Even easier is...

 
for (Field field : fields) 
                  System.out.println("Type " + field.getType().getSimpleName() + " name " + field.getName() + " ");

Open in new window

0
 

Author Comment

by:Micheal_Male
ID: 34893859
Are you serious ?. How in the world i missed getSimpleName() :(. thanks mr carl
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34893870
I see, there is a special method just for that.  
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 68
ejb mdb examples 1 39
Why is enum singleton a better approach than static factory 3 67
Read CLOB data from Oracle using JAVA 3 43
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
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 …
Suggested Courses

751 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