geronimo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Vamsavardhana Reddy" <c1vams...@gmail.com>
Subject Re: Anyway to get the generic type from a generic class?
Date Mon, 01 Oct 2007 06:05:15 GMT
Jason,

As per my understanding, an ArrayList<String> will help generating
compilation errors should one try to add a non String object to the list.
It does not mean that you can not add a non String object to the underlying
list.  For e.g. the following code does not result in an error:

    public static void main(String[] args) {
        ArrayList<String> sl = new ArrayList<String>();
        sl.add("Me");
        ((ArrayList)sl).add(new Integer(10));
        System.out.println(sl);
    }

Vamsi

On 10/1/07, Jason Dillon <jason@planet57.com> wrote:
>
> Any of you generics experts out there know if there is any way to get
> the generic type from a generic class... like, say you have:
>
>      Class type = new ArrayList<String>().getClass();
>
> Is there any way to determine that this is an ArrayList containing
> String objects?  I can't seem to figure out how to get this
> information out of the Class instance.  I can figure out what the
> type variable name was, er like T, but that is well, completely
> useless IMO.
>
> Does anyone know if its possible and how to do it?
>
> --jason
>

Mime
View raw message