Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes How to check given field whether primitive or user defined Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to check given field whether primitive or user defined " Watch "How to check given field whether primitive or user defined " New topic
Author

How to check given field whether primitive or user defined

Priyatham Anisetty
Greenhorn

Joined: Apr 21, 2011
Posts: 15
Hi I am using reflection to get all the declared fields in a given class. using

I have two java files
the first one is


The user defined class is as follows



No my question is when i use Field[] fieldsInClass = sample.getDeclaredFields();

It is returning all the declared fields in sample class. I want to check every field whether it is primitive type or user defined .

and if the type of a field is user defined then I want to get all the fileds declared in that class(in the above example it is UTC class).

Any suggestions will be really helpful .

Thanks in advance .
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Use the method
Priyatham Anisetty
Greenhorn

Joined: Apr 21, 2011
Posts: 15
E Armitage wrote:Use the method


Thanks for the reply .
Now how can I get all the fileds names which are declared in that User defined class.
In detail I am trying to create a tree structure for all the fields which are declared in a class . If the declared field is user defined (like UTC class) then I want to create a expandable node which displays all the fileds declared in that class .

I tried using

But it is not giving the fileds in UTC class.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You seem to be going all around the houses in that code. You already called Field.getType() to find out the class of a field. That should give you UTC.class for one of the fields. So you don't need to get the name of that class and load the class, you've already got the class. Just start from there.
Siva Vulchi
Ranch Hand

Joined: Nov 17, 2011
Posts: 30
If you are still unable to get UTC object fields then try this. It may help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Siva Vulchi wrote:If you are still unable to get UTC object fields then try this. It may help.

That should be field.getType() == UTC.class, or if the UTC class is not available during compile time at least compare the full name. Also, do not ignore the case. Class UTC is not the same as class Utc.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Siva Vulchi
Ranch Hand

Joined: Nov 17, 2011
Posts: 30
Rob Spoor wrote:
That should be field.getType() == UTC.class, or if the UTC class is not available during compile time at least compare the full name. Also, do not ignore the case. Class UTC is not the same as class Utc.


You are right. Thanks!!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Note that for example String is neither primitive nor user-defined.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Priyatham Anisetty
Greenhorn

Joined: Apr 21, 2011
Posts: 15
Siva Vulchi wrote:If you are still unable to get UTC object fields then try this. It may help.



Thanks for reply . I tried the following but it is giving me error.




I think there is some problem in processHierarchy method which i was using to create the tree nodes. Please correct me .

Thanks in adavance
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Priyatham Anisetty wrote:I think there is some problem in processHierarchy method which i was using to create the tree nodes. Please correct me .

I think it might be better if you explained what you're trying to do, because all we're doing here is answering direct questions about Java reflection; which is slow, verbose, error-prone and arcane (and, as far as I'm concerned, to be avoided at almost any cost).

Suffice to say, you've probably written more on this project than I have in 11 years of Java programming - and not because I don't know how.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Priyatham Anisetty
Greenhorn

Joined: Apr 21, 2011
Posts: 15
Winston Gutkowski wrote:
Priyatham Anisetty wrote:I think there is some problem in processHierarchy method which i was using to create the tree nodes. Please correct me .

I think it might be better if you explained what you're trying to do, because all we're doing here is answering direct questions about Java reflection; which is slow, verbose, error-prone and arcane (and, as far as I'm concerned, to be avoided at almost any cost).

Suffice to say, you've probably written more on this project than I have in 11 years of Java programming - and not because I don't know how.

Winston





I am trying create a tree structure of all the fields which are declared in a given class. If one of the defined field type is user defined for example




Then that particular(user defined type) node should be expandable . And it should show all the fields in the UTC class.

I was using reflection both the sample class and the UTC class will be created during runtime . I found reflection to access .class files.
Please let me know if there is any other way to complete this.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Priyatham Anisetty wrote:if(name.getType().toString() != "class")[/code]

That's going to return true regardless of the type. You should use the equals method, not == or !=, to check for String content equality. Right now you're checking for reference equality.

However, after you change that to if(!name.getType().toString().equals("class")) it's going to return false regardless of the type. That's because class is a reserved word, and therefore no type will be called class. Why did you stop using isPrimitive()?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't get any of this. You already know how to get the fields from an object (not from a class), your code already shows that. And there's no such thing as a "user defined" class, there are just classes and primitives. You seem to be avoiding the obvious -- once you have an object which is one of the values of the fields in your "root" object, you use exactly the same logic to get its fields as you did to get the fields of the original object. A recursive method would be suitable here.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Paul Clapham wrote:I don't get any of this....A recursive method would be suitable here.

Absolutely. My question is: Why would anyone need to do it in the first place?

If you're writing some sort of GUI "inspection" tool, then I could see the possibility; but beyond that...

Needless to say, further questions (if they be just) are probably beyond my ken, so I bid you, Priyatham, and everyone else, a good night.

Winston
Priyatham Anisetty
Greenhorn

Joined: Apr 21, 2011
Posts: 15
Winston Gutkowski wrote:
Paul Clapham wrote:

If you're writing some sort of GUI "inspection" tool, then I could see the possibility;

Winston


Thanks for your reply . Yes I am writing an GUI application which digs into file directory to load .class files .
Priyatham Anisetty
Greenhorn

Joined: Apr 21, 2011
Posts: 15
Paul Clapham wrote:I don't get any of this. You already know how to get the fields from an object (not from a class), your code already shows that. And there's no such thing as a "user defined" class, there are just classes and primitives. You seem to be avoiding the obvious -- once you have an object which is one of the values of the fields in your "root" object, you use exactly the same logic to get its fields as you did to get the fields of the original object. A recursive method would be suitable here.



Thanks for your suggestion I got it to working .

my final version of the code is
>
 
wood burning stoves
 
subject: How to check given field whether primitive or user defined
 
Similar Threads
Calling object methods using reference to variables
Using reflection to get ALL fields, not just public ones
Sun Cirtification
Need help on JTree
Auto toString of Variables