Solved

Reflection help

Posted on 2011-02-14
16
330 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java basic valueOf question 1 33
Eclipse Java import and method not resolved 4 77
Eclipse with various Java releases 7 47
Java Inheritance super keyword use 8 36
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

749 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