wood burning stoves
The moose likes Java in General and the fly likes Reflection Api Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reflection Api" Watch "Reflection Api" New topic

Reflection Api

Navi Lock
Ranch Hand

Joined: Jul 29, 2003
Posts: 39
Hi All,
I am using "Reflections API" for getting the contents of an Object.
Thats fine.When going through the "getDeclaredFields" i get the list of the fields of the class whose object i am trying to read.

The issue is , one of the fields is "an Object of the class which i have created"..
Now i need to read this object (get the details of the Class it respresents,it's parameters..etc) while at the same time ,i do not want to
read other objects which are of type string ...

so how do we differentiate between the types...

pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547

i'm not 100% sure i got your question right. is the getType() method is what you are looking for ?

just call getType() and compare the Class you receive as return value with what you are looking for (i check if it is a String in my example...)

Navi Lock
Ranch Hand

Joined: Jul 29, 2003
Posts: 39
Hey Pascal,
Thanks for replying..

"I am picking up from the point where u are comparing for String.class"

What i meant was, i need to allow the Use of reflection API only for my
customised class objects and "NOT" any other if present in the list like String or int or long or comparatar.

The Problem being i don't know which of the customised class object i would be having in the list along with the standard objects like String,integer..etc

Navi Lock
Ranch Hand

Joined: Jul 29, 2003
Posts: 39
Anybody Listening.....SOS...SOS...
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
The only way I can see is either having a list of classes you want to inspect, or having a list of classes you want to ignore.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
yes, we are listening. SOS. but we have daytime jobs too...

can your custom objects all extend from the same class ? or can you have them implement a interface (e.g. an empty marker interface ?) ?
then you could check for the interface or parent class.
like this you could avoid having the list.


Navi Lock
Ranch Hand

Joined: Jul 29, 2003
Posts: 39
Agreed Sir,
Ofcourse we all have Jobs..some in the Day,others in the Night...(Actually Day is highly Over-rated...really doen't matter..anyways...)

So if i wish to check for the package level ,is there any method i can
check against...can you post a patch of code..would be great...

Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
FYI, one of this week's book promotions is :

Java Reflection in Action
Ira R. Forman, Nate Forman

"This week, we're delighted to have Ira R. Forman and Nate Forman amongst us, helping to answer questions in the Other Java APIs forum."

Maybe you should mosey on over there (or perhaps some kind bartender can move this discussion over there).
[ January 26, 2005: Message edited by: Carol Enderlin ]
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

What you should do is check the package of the class that you are inspecting. You should be able to put an if statement if your code checking to see if the package is your.allowable.package.names.here or some such.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Navi Lock
Ranch Hand

Joined: Jul 29, 2003
Posts: 39
Yes thats is possible..

But is it possible to differntiate between the Classes / objects
provided by the Sun API and the one's developed by me.

Is it possible to just pass a object,
and get the class details of that object and if that class has any object of any other customised class,i should be able to get the details of the class that this new object it represents..and on and on..

I understand ,i can hard code a bit
(like use -- String.startsWith("package ")..)

but would like to make it as much as generic, so that it can
figure on itself which one's are customized and which are from
SUN api objects..

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Sun's classes will start with java., javax., com.sun., sun, or sunw. Yours, presumably, won't.

You could also look to see if the classes were loaded by the boot class loader, or by an application class loader. This code would have to know something about the environment in which it was running.

But in the most general sense, there's simply no difference between API classes and your classes. A class is a class.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link: http://aspose.com/file-tools
subject: Reflection Api
It's not a secret anymore!