Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reflection help

Posted on 2011-02-14
16
Medium Priority
?
333 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 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

715 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