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

Discussion in 'Java' started by mortoray@ecircle-ag.com, Jan 18, 2005.

  1. Guest

    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?
    , Jan 18, 2005
    #1
    1. Advertising

  2. moongate Guest

    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
    moongate, Jan 18, 2005
    #2
    1. Advertising

  3. moongate Guest

    My fault. It looks like Java now has generics. Sorry for the mispost.
    moongate, Jan 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    433
  2. Eric Smith
    Replies:
    7
    Views:
    11,437
  3. Sébastien de Mapias
    Replies:
    7
    Views:
    858
    Daniel Pitts
    May 29, 2008
  4. minlearn
    Replies:
    2
    Views:
    453
    red floyd
    Mar 13, 2009
  5. jcpc
    Replies:
    2
    Views:
    1,466
Loading...

Share This Page