• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Type-safe list instantiation w/ reflection

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create a type-safe copy of a list. The following is the method I am having trouble with:

private List<E> listCopy(List<E> list) throws InstantiationException,
IllegalAccessException {
List<E> copy = list.getClass().newInstance();
return copy;
}

This helper method exists in a generic class. The preceding code compiles, but gives me an unchecked conversion warning. I'm not a whiz with either generics or reflection and the research I've done so far has not really cleared up whether this is possible to do without the warning. Is there some other way to achieve this?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way to get rid of that warning is to use a suppressWarnings annotation, I fear...
 
Tom Collins
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, thanks for the reply. My method now looks like this:



However, I am still receiving the following warning from the compiler:

Note: C:\Documents and Settings\Tom Collins\My Documents\snuglesoft\flashcard\src\com\digiquiz\model\AbstractModel.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

After researching @SuppressWarnings I've found that Java 1.5.0 does not support this annotation. Do you know if any later versions of Java 5 support this?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only know that Eclipse supports it, sorry.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to this bug report SuppressWarnings was finally fixed in JDK 1.5.0_06. I can confirm that it works in JDK 1.5.0_09.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't use annotations often but shouldn't that be @SuppressWarnings({"unchecked"}).
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The two forms are equivalent, Ken - though most people do prefer the shorter one, I think.
 
Tom Collins
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tip Jim. I downloaded JDK 1.5.0_10 and @SuppressWarnings works like a charm! Now, on the to-do list: Learn what annotations are and when and how to use them.
 
reply
    Bookmark Topic Watch Topic
  • New Topic