aspose file tools*
The moose likes Beginning Java and the fly likes Method to retrieve all same instance properties of classes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method to retrieve all same instance properties of classes." Watch "Method to retrieve all same instance properties of classes." New topic
Author

Method to retrieve all same instance properties of classes.

Justin Filmer
Greenhorn

Joined: Jul 04, 2011
Posts: 27
Hey guys,

Let's say I have a class MyAnimal that looks like this:


Now let's say that I was able to get a list of instances of MyAnimal.


I want to create a method that when passed in the property as a string, will return an array of each instance's specific property. The thing is, I'm used to dot notation, so I don't know how to handle retreiving data when a string is given.

For example, let's say I have three animals and they have the colors red, green, and blue. When I call this method:

It should return:


I should also be able to call:


Thanks for the help!
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

You can use the reflection API ( http://download.oracle.com/javase/tutorial/reflect/index.html ) but since this is posted in the "Beginning Java" forum I suspect your requirement is the result of a bad design. Why do you think you need this?


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Justin Filmer
Greenhorn

Joined: Jul 04, 2011
Posts: 27
Thanks.

I have an Array of Objects, and I need to quickly print out and log (to a text file) all of the same instance properties for the Objects. I don't want to have to create a new method for each since I know specs are going to change and more instance variables will be added to the Class. I'd much rather call the same method with a different argument.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Justin Filmer wrote:Thanks.

I have an Array of Objects, and I need to quickly print out and log (to a text file) all of the same instance properties for the Objects. I don't want to have to create a new method for each since I know specs are going to change and more instance variables will be added to the Class. I'd much rather call the same method with a different argument.



In my humble opinion, you are not thinking in an object oriented matter. You should split out the Color to a class of its own, maybe even an Enum if there is only to be a fixed number of colors. Also, the Skills/TrainedAreas could be a class of its own. I can bet quite big bucks that the later will have a training level later on...

Regarding using one method to get them all (aka the method of the ring) or multiple methods there are some different ways to go:
1. Live with the fact that you have to implement a new method for each attribute type.
2. Rely on reflection and make a nice bean centric framework.
3. Look into some sort of persistence layer and use that to search by attribute.
4. Delegate to the class holding the attribute to return is based on an interface that allows you to query by attribute name.
5. A combination of the previous four.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method to retrieve all same instance properties of classes.