Solved

Reflection help

Posted on 2011-02-14
16
323 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
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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

747 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

7 Experts available now in Live!

Get 1:1 Help Now