File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reflection Api

 
Navi Lock
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...


Thanks,
Navi.
 
pascal betz
Ranch Hand
Posts: 547
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi


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...)


pascal
 
Navi Lock
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Navi Lock
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody Listening.....SOS...SOS...
 
Steven Bell
Ranch Hand
Posts: 1071
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

cheers


pascal
 
Navi Lock
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

Thanks
-Navi
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI, one of this week's book promotions is :

Java Reflection in Action
Ira R. Forman, Nate Forman
Manning

"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
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Navi Lock
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Basically,
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..

-Navi
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic