The moose likes OO, Patterns, UML and Refactoring and the fly likes a Factory for creating LinkedLists of Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "a Factory for creating LinkedLists of Objects" Watch "a Factory for creating LinkedLists of Objects" New topic
Author

a Factory for creating LinkedLists of Objects

Stuart Rogers
Ranch Hand

Joined: Oct 02, 2008
Posts: 108
Greetings all,

I'm developing using Eclipse, which prods incessantly to not use raw types.

I'm trying to create LinkedLists that set their parameter type at runtime.
I'd also like to have these lists have some useful methods associated with them.

I thought up class ExpandedLinkedListObject, which contains two elements:
public Class<?> typpe;
public LinkedList<?> lisst;

where typpe will contain the Class information of the objects I want to put in lisst;
This get around erasure - by refering to typpe the program can always know what
is the correct type of object to put in lisst. It also gives me access to the correct
constructors for the objects.

Goal: read several cvs's into the .lisst of several ExpandedLinkedListObjects. There
is a class defined to match each cvs, of course.

Here's what I've got so far


I could use some help getting my <T>s <E>s <?>s in order to make this work.

Or has a simpler/better/faster/cheaper/std way to accomplish this already been invented? And if so send the link ;-)

TIA,

Still-learning Stuart

This message was edited 1 time. Last update was at by Stuart Rogers

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Stuart Rogers wrote:I'm developing using Eclipse, which prods incessantly to not use raw types.
Can be turned off

Why are you using generics when you're only adding Strings to the list?
And why are you using reflection for that?
Close your streams.
Stuart Rogers
Ranch Hand

Joined: Oct 02, 2008
Posts: 108
But I'm not adding strings to the lists, I'm trying to instantiate objects with string arrays and add the objects to the list.
Thanks for your reply though.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 12865

Using reflection isn't going to make those messages go away. Reflection happens at run time, but generics are a compile-time feature. So if you don't know what type of object is going to be in the list at compile time, then just use the annotation which tells the compiler to ignore the fact that you are using a raw type.
Stuart Rogers
Ranch Hand

Joined: Oct 02, 2008
Posts: 108
Updating these lines with wildcard type parameters solved the raw messages


leaving only the line

now causing me grief, in the form of the message:
the method add(capture#12-of ?) in the type LinkedList(capture#12-of ?) is not applicable for the arguments (capture#13-of ?)

Now what?

Still-learning Stuart


Stuart Rogers
Ranch Hand

Joined: Oct 02, 2008
Posts: 108
I solved my problem by refactoring my code to use a combination of Generics, reflection and patterns.

class ExpandedLinkedListObject
has transmorgified into

where the .populate method uses a .make( Class<?> typpe, String[] inn_str_arry) method from the AssetFactory,
which in turn uses reflection on typpe to pull the correct constructor.

the .writeOut method uses reflection on typpe to retreive and invoke the correct matching getAsArray method
prior to formatting and writing to the file.

Now my main() is this simple:


Thanks to all who replied.

CASE CLOSED

Still-learning Stuart

This message was edited 1 time. Last update was at by Stuart Rogers

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12612
All that misspelling-to-avoid-confusion is really going to cause confusion in the real world. I mean, come on:

this.lisst.add(strCtor.newInstance(innline_arry));

Three in a single line?!

Also, using underscored properties (theApp.asset_HP_list) is really counter to any Java coding convention.

It's a lot easier for other people to understand your Java code if conventions are adhered to, and things aren't unnecessarily obfuscated.

This message was edited 1 time. Last update was at by David Newton

 
 
subject: a Factory for creating LinkedLists of Objects
 
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture