Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Class Generics

Thread view: 
Jason Cavett - 07 Aug 2007 17:35 GMT
Class modelClass = Class.forName(modelResult.getNodeValue());
Class viewClass = Class.forName(viewResult.getNodeValue());
factory.put(modelClass, viewClass);

I get the warning with the first two lines:
Class is a raw type. References to generic type Class<T> should be
parameterized

So, I tried this...

Class<?> modelClass = Class.forName(modelResult.getNodeValue());
Class<?> viewClass = Class.forName(viewResult.getNodeValue());
factory.put(modelClass, viewClass);

It removes those warnings, but I then get an error at the third line:
The method put(Class<? extends DataModel>, Class<? extends
PropertiesView>) in the type PropertiesFactory is not applicable for
the arguments (Class<capture#3-of ?>, Class<capture#4-of ?>)

So, I figured maybe there was a way to do something like this:

Class<? extends DataModel> modelClass = (Class<? extends DataModel)
Class.forName(modelResult.getNodeValue());
Class<? extends PropertiesView> viewClass = (Class<? extends
PropertiesView) Class.forName(viewResult.getNodeValue());
factory.put(modelClass, viewClass);

Which results in the two warnings:
Type safety: Unchecked cast from Class<capture#1-of ?> to Class<?
extends DataModel>
and
Type safety: Unchecked cast from Class<capture#1-of ?> to Class<?
extends PropertiesView>

At this point, it just seems easiest to use
@SuppressWarnings("unchecked") and ignore the warnings completely.
But, I was wondering if there was something I'm not understanding or
if there's a better way to do what I am trying to accomplish (no
warnings w/ everything cast correctly).

Thanks
Joshua Cranmer - 07 Aug 2007 21:40 GMT
> Class modelClass = Class.forName(modelResult.getNodeValue());
> Class viewClass = Class.forName(viewResult.getNodeValue());
[quoted text clipped - 3 lines]
> Class is a raw type. References to generic type Class<T> should be
> parameterized

That is correct, but Sun fails to follow that in at least one case
(Enum.class is a Class<Enum> not a Class<Enum<?>>).

> So, I figured maybe there was a way to do something like this:
>
[quoted text clipped - 10 lines]
> Type safety: Unchecked cast from Class<capture#1-of ?> to Class<?
> extends PropertiesView>

Almost, but not quite. What you want is:
Class<? extends DataModel> modelClass = Class.forName(
    modelResult.getNodeValue()).asSubclass(DataModel.class);

As aforementioned, this breaks down when handling something like enums:
Class<?> clazz = <get from elsewhere>;
Class<? extends Enum<?>> enumClass = clazz.asSubclass(Enum.class);
Class<? extends Enum<?>> enumClass2 = clazz.asSubclass(Enum<?>.class);

Neither of the last two clauses works, although hopefully generics will
be reified in Java 7 and the last one will work (or they could get off
their butts and fix the first syntax).

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Jason Cavett - 08 Aug 2007 16:35 GMT
> > Class modelClass = Class.forName(modelResult.getNodeValue());
> > Class viewClass = Class.forName(viewResult.getNodeValue());
[quoted text clipped - 38 lines]
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth

Awesome, thank you for that help.

I was reading this guide: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
on generics and it does clear up a lot for me.  I'm still confused,
though, whether arrays can be made generic.

As far as I can tell (and please correct me if I'm wrong), the entire
point of generics is to provide compile-time type safety.  But, from
what I can also tell, arrays ([] not Arrays) types are not known until
runtime.  Thus, this defeats the entire purpose of Arrays.

A case in which I'm seeing this problem is:

Class[] classes = { Class1.class, Class2.class, Class3.class };

That statement would consist of the warning: Class is a raw type.
References to generic type Class<T> should be parameterized

So, it's an easy fix if "type unknown" is used:

Class<?>[] classes = { Class1.class, Class2.class, Class3.class };

That makes sense based on my understanding before.

But, it seems...

Class<? extends Whatever>[] classes = { Class1.class, Class2.class,
Class3.class };

gives a compile time error for each class listed in the array.
Is there any way around this?  Should I use Collections instead?

Thanks
Thomas Hawtin - 08 Aug 2007 19:27 GMT
> Class<? extends Whatever>[] classes = { Class1.class, Class2.class,
> Class3.class };
>
> gives a compile time error for each class listed in the array.
> Is there any way around this?  Should I use Collections instead?

Arrays are broken.

Given the above line, you could then write:

Object class = NotWhatever.class;
Object[] classesObj = classes;
classesObj[0] = class;
Class<? extends Whatever> oops = classes[0];

So, yes, use proper collections. Keep arrays for primitives and dirty.
low-level optimisations.

Tom Hawtin
Oliver Wong - 07 Aug 2007 23:30 GMT
> Class modelClass = Class.forName(modelResult.getNodeValue());
> Class viewClass = Class.forName(viewResult.getNodeValue());
[quoted text clipped - 35 lines]
> if there's a better way to do what I am trying to accomplish (no
> warnings w/ everything cast correctly).

   Probably not. Basically, you're telling the compiler that you're
guaranteeing whatever class is named by the expression
modelResult.getNodeValue() will be a subtype of DataModel. There is no way
for the compiler to verify whether or not this is a true statement. The
warning is the compiler's way of telling you it has no way of verifying
the truth-value of that claim, and there is no source code which you can
write which will prove the claim to the compiler.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.