Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Obtain class of Generic type? (how can I remove a Class<T> parameter)

Thread Tools

Obtain class of Generic type? (how can I remove a Class<T> parameter)
Posts: n/a
I have some code (below) that does a custom conversion between types
(namely from an Object type to some specific type). It is a prime
candidate for Generics (to avoid the return value casts by the user),
but there doesn't appear to be any way to get rid fo the Class<T>
parameter -- that is, the type I wish still needs to be a formal
parameter and not just the Generic Type.

/** @param type - the type to conver to
* @param obj - the obejct to convert */
static public <T> T asType( Class<T> type, Object obj ) throws
Exception {
if( type.equals( Number.class ) )
return type.cast( asNumber( obj ) );
if( type.equals( String.class ) )
return type.cast( asString( obj ) );
if( type.equals( OutputStream.class ) )
return type.cast( asOutputStream( obj ) );
if( type.equals( InputStream.class ) )
return type.cast( asInputStream( obj ) );
if( type.equals( Boolean.class ) )
return type.cast( asBoolean( obj ) );
return null;

If I understand generics correctly it woult not be possible to create
an Object of Class<T> type within the routine since the T is actually
not specified at run-time. But is there some other way to get rid of
the formal "type" parameter and make this work?

Reply With Quote
Posts: n/a
AFAIK, Java has no notion of generic programming, methods, or classes.
If I understand correctly, what you might want to do for your code
above is get rid of your if-else' chain and actually write multiple
methods of the form:

public String asString(Object obj) throws ClassCastException {
if(obj instanceOf String)
return (String)obj;
else throw new ClassCastException();

Since the JVM can throw ClassCastException all by itself when you try
an illegal cast, this in turn could be rewritten as follows:

public String asString(Object obj) throws ClassCastException {
return (String)obj; // let JVM throw the exception as needed

But then, this method looks pretty useless to me, i.e., the expression


becomes basically the same as

so it seems the method doesn't buy you much....


Reply With Quote
Posts: n/a
My fault. It looks like Java now has generics. Sorry for the mispost.

Reply With Quote

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
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Generic class in a non generic class Java 2 07-04-2006 07:24 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM