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
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
posted
0
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.
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
posted
0
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
posted
0
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
posted
0
All that misspelling-to-avoid-confusion is really going to cause confusion in the real world. I mean, come on: