Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check given field whether primitive or user defined

 
Priyatham Anisetty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the method
 
Priyatham Anisetty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are still unable to get UTC object fields then try this. It may help.
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siva Vulchi
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that for example String is neither primitive nor user-defined.
 
Priyatham Anisetty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Priyatham Anisetty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic