Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reflection help

Posted on 2011-02-14
16
Medium Priority
?
336 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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 36

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

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!

Question has a verified solution.

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

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 …
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

971 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