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 / July 2007

Tip: Looking for answers? Try searching our database.

Raw type other than a Class, possible?

Thread view: 
Piotr Kobzda - 24 Jul 2007 19:41 GMT
Can someone give an example of a raw type, which is other than a Class
instance at runtime?

In other words, why is the return type of
java.lang.reflect.ParameterizedType.getRawType() a Type, not a Class?

May I expect the ClassCastException in the following code snippet?

    ParameterizedType type = ...
    Class rawType = (Class)(type.getRawType());

piotr
Daniel Pitts - 25 Jul 2007 02:09 GMT
> Can someone give an example of a raw type, which is other than a Class
> instance at runtime?
[quoted text clipped - 8 lines]
>
> piotr

how 'bout you try it and let us know what happens.
Piotr Kobzda - 25 Jul 2007 14:22 GMT
>> Can someone give an example of a raw type, which is other than a Class
>> instance at runtime?
[quoted text clipped - 6 lines]
>>      ParameterizedType type = ...
>>      Class rawType = (Class)(type.getRawType());

> how 'bout you try it and let us know what happens.

Well, it passes all my tests.  Here is one, if you wanna try it also:

<sscce>
import java.lang.reflect.*;
import java.util.*;

public class RawTypeTest {
    static List<String> list = new ArrayList<String>();

    public static void main(String[] args) throws Exception {
        ParameterizedType type
                = (ParameterizedType)RawTypeTest.class
                        .getDeclaredField("list").getGenericType();
        Class rawType = (Class)type.getRawType();
        System.out.println("raw type of " + type + " is " + rawType);
    }
}
</sscce>

In addition to that, I've even checked Sun's implementation of
ParameterizedType
(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl), and the
getRawType() method's return type is declared as Class<?> there.

The following added to the above example shows that:

    System.out.println("getRawType() return type is "
            + type.getClass()
                    .getMethod("getRawType", (Class<?>[]) null)
                    .getGenericReturnType());

However, all the observations above, doesn't solve my problem.

I'm writing a generic utility for reflective retrieval of actual type
arguments based on a given class type hierarchy.  And I'm not sure if
the above assumption is valid for all possible parameterized types Java
runtime may create?

On the other hand, assuming that there is no other possibility to
represent a raw type in runtime, why getRawType() return type is not
declared as a Class<?> in ParameterizedType interface (as in its
internal implementation)?  It would be easier to use declared that way.

piotr
Daniel Pitts - 25 Jul 2007 15:31 GMT
> >> Can someone give an example of a raw type, which is other than a Class
> >> instance at runtime?
[quoted text clipped - 52 lines]
>
> piotr

Future extendability perhaps.  You might be able to assume its a class
for now, but have to update your code on later Java releases.   You'd
be safest doing an instanceof check on the object you get back, and
reporting something other than class (JOptionPane comes to mind.)

Hope this helps.


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.