Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > unchecked conversion problem

Reply
Thread Tools

unchecked conversion problem

 
 
rschmid-google@raptor.net
Guest
Posts: n/a
 
      10-25-2006
I have finally been allowed to upgrade to Java5 and I just finished
fixing some 1200 parameterization warnings which has really improved
the code. I have one, ONE, left and I'm not sure it can be fixed.

I have the following object heierarchy;

SourceMatch
CMatch extends SourceMatch
DMatch extends SourceMatch

SMatchService<T extends SourceMatch>
CMatchService extends SMatchService<CMatch>
DMatchService extends SMatchService<DMatch>

in SMatchService is the following method

public abstract List<T> getMatchingSources(args) {}

which is implemented in the subclass services thus;

public List<CMatch> getMatchingSources(args) {}

and

public List<DMatch> getMatchingSources(args) {}

yet the following code throws an unchecked conversion warning;

SMatchService sourceMatchService = null;
if (c) sourceMatchService = new CMatchService();
else if (d) sourceMatchService = new DMatchService();

List<SourceMatch> sourceList = =
sourceMatchService.getMatchingSources(args);

I have tried using wildcard parameters but my understanding is
imperfect at best.

I suspect that the problem is really architectural but I wanted to find
out if there is a way to make this work.

Any help is appreciated.

 
Reply With Quote
 
 
 
 
Niklas Matthies
Guest
Posts: n/a
 
      10-25-2006
On 2006-10-25 19:05, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
:
> yet the following code throws an unchecked conversion warning;
>
> SMatchService sourceMatchService = null;
> if (c) sourceMatchService = new CMatchService();
> else if (d) sourceMatchService = new DMatchService();
>
> List<SourceMatch> sourceList = =
> sourceMatchService.getMatchingSources(args);


Try this:

SMatchService<?> sourceMatchService = null;
if (c) sourceMatchService = new CMatchService();
else if (d) sourceMatchService = new DMatchService();

List<? extends SourceMatch> sourceList =
sourceMatchService.getMatchingSources(args);

-- Niklas Matthies
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      10-25-2006
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> yet the following code throws an unchecked conversion warning;
>
> SMatchService sourceMatchService = null;


Here SMatchService lacks a type parameter. Try:
SMatchService<? extends SourceMatch> sourceMatchService = null;

> if (c) sourceMatchService = new CMatchService();
> else if (d) sourceMatchService = new DMatchService();
>
> List<SourceMatch> sourceList = =
> sourceMatchService.getMatchingSources(args);


And here the list you get will be:
List<? extends SourceMatch> sourceList =
sourceMatchService.getMatchingSources(args);

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
rschmid-google@raptor.net
Guest
Posts: n/a
 
      10-25-2006

Niklas Matthies wrote:
> On 2006-10-25 19:05, (E-Mail Removed) wrote:
> :
> > yet the following code throws an unchecked conversion warning;
> >
> > SMatchService sourceMatchService = null;
> > if (c) sourceMatchService = new CMatchService();
> > else if (d) sourceMatchService = new DMatchService();
> >
> > List<SourceMatch> sourceList = =
> > sourceMatchService.getMatchingSources(args);

>
> Try this:
>
> SMatchService<?> sourceMatchService = null;
> if (c) sourceMatchService = new CMatchService();
> else if (d) sourceMatchService = new DMatchService();
>
> List<? extends SourceMatch> sourceList =
> sourceMatchService.getMatchingSources(args);
>
> -- Niklas Matthies


Great! Except now;

sourceList = new ArrayList<SourceMatch>();
sourceList.add(new SourceMatch());

throws an error.

I can work around this by creating a new list and copying one into the
other but that is wasteful. What's the right syntax?

 
Reply With Quote
 
Niklas Matthies
Guest
Posts: n/a
 
      10-25-2006
On 2006-10-25 21:18, (E-Mail Removed) wrote:
> Niklas Matthies wrote:

:
>> List<? extends SourceMatch> sourceList =
>> sourceMatchService.getMatchingSources(args);

:
> Great! Except now;
>
> sourceList = new ArrayList<SourceMatch>();
> sourceList.add(new SourceMatch());
>
> throws an error.
>
> I can work around this by creating a new list and copying one into
> the other but that is wasteful. What's the right syntax?


You need to use a second variable declared with a different type:

List<SourceMatch> sourceList2 = new ArrayList<SourceMatch>();
sourceList2.add(new SourceMatch());

List<SourceMatch> means that the list can contain _any_ instances
of SourceMath. List<? extends SourceMatch> on the other hand means
that the list can contain only instances of _some specific_ (but
unspecified) subtype of SourceMatch. The '?' might actually be
SourceMatch itself, but it also might be CMatch or DMatch. That's
why the compiler doesn't allow you to add an arbitrary SourceMatch
instance to such a list, because the list could for example really be
a List<CMatch>, to which it's only permissible to add CMatch objects.
Consider:

List<CMatch> cList = new ArrayList<CMatch>();
List<? extends SourceMatch> sourceList = cList; // fine
sourceList.add(new SourceMatch()); // (*)
// oops, now cList contains a SourceMatch object although
// it is a List<CMatch>!
CMatch cMatch = cList.get(i); // throws ClassCastException!

That's why the line (*) is rejected by the typechecker.

To get a better understanding of generics I suggest you take a look
at http://www.angelikalanger.com/Generi...nericsFAQ.html .

-- Niklas Matthies
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      10-25-2006
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> Niklas Matthies wrote:
>> List<? extends SourceMatch> sourceList =
>> sourceMatchService.getMatchingSources(args);

>
> Great! Except now;
>
> sourceList = new ArrayList<SourceMatch>();
> sourceList.add(new SourceMatch());
>
> throws an error.


As it should. ArrayList<SourceMatch> is not assignable to
List<? extends SourceMatch>.

You can do
sourceList = new ArrayList<? extends SourceMatch>();
but you can't add a SourceMatch to that (or anything at all).

Remember, a List<SourceMatch> and a List<CMatch> are not assignable
to each other. The former allows you to put SourceMatch'es into it,
the latter doesn't. And the latter guarantees that what you take
out of it is a CMatch, the former doesn't.

If you want a variable to hold both of the above list types, it
needs to be something like List<? extends SourceMatch> (which is
a supertype of both). However, you cannot add elements to that list,
since it might be both a List<CMatch> or a List<DMatch>, which cannot
contain the same elements.

Instead, you could just let the getMatchingSources return a
List<SourceMatch>.

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
rschmid-google@raptor.net
Guest
Posts: n/a
 
      10-26-2006

Lasse Reichstein Nielsen wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
>
> > Niklas Matthies wrote:
> >> List<? extends SourceMatch> sourceList =
> >> sourceMatchService.getMatchingSources(args);

> >
> > Great! Except now;
> >
> > sourceList = new ArrayList<SourceMatch>();
> > sourceList.add(new SourceMatch());
> >
> > throws an error.

>
> As it should. ArrayList<SourceMatch> is not assignable to
> List<? extends SourceMatch>.
>
> You can do
> sourceList = new ArrayList<? extends SourceMatch>();
> but you can't add a SourceMatch to that (or anything at all).
>
> Remember, a List<SourceMatch> and a List<CMatch> are not assignable
> to each other. The former allows you to put SourceMatch'es into it,
> the latter doesn't. And the latter guarantees that what you take
> out of it is a CMatch, the former doesn't.
>
> If you want a variable to hold both of the above list types, it
> needs to be something like List<? extends SourceMatch> (which is
> a supertype of both). However, you cannot add elements to that list,
> since it might be both a List<CMatch> or a List<DMatch>, which cannot
> contain the same elements.
>


Ah, Thank you! That was the point I had forgotten. Once I understood
it became obvious that I could move the actual code into a new
parameterized method in SourceMatch.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unchecked conversion warning. Jens Java 32 06-20-2012 11:28 PM
Xlint " warning: [unchecked] unchecked conversion" RVic Java 19 07-25-2009 08:40 PM
Yet another generics question: Needs unchecked conversion to conform to ... Michel T. Java 14 01-18-2007 10:29 PM
Checkbox problem - checkboxes submitting that are unchecked Claire Java 3 03-02-2005 05:17 PM
Checkbox problem - checkboxes submitting that are unchecked Claire Javascript 4 03-02-2005 10:48 AM



Advertisments