Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Obtain class of Generic type? (how can I remove a Class<T> parameter)

 
 
mortoray@ecircle-ag.com
Guest
Posts: n/a
 
      01-18-2005
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
 
 
 
 
moongate
Guest
Posts: n/a
 
      01-18-2005
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

asString(obj)

becomes basically the same as
(String)obj

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

MC

 
Reply With Quote
 
 
 
 
moongate
Guest
Posts: n/a
 
      01-18-2005
My fault. It looks like Java now has generics. Sorry for the mispost.

 
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
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 nramnath@gmail.com 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



Advertisments