Re: Class<T> question
Patrick Schneider <[email protected]> Wed, 5 Dec 2007 10:36:22 -0600
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <[email protected]> |
In order to cut down on the type specifiers, I was shown this trick earlier
in the week...
Map<String, Object> map = createMap();
...
public static <K, V> Map<K, V> createMap() {
return new HashMap<K, V>();
}
...which is essentially the same idea as what's going on in the
implementation of Collections.emptyMap(). Since the method is generic, you
only need to specify the types of the Map on the left hand side, saving you
from typing two type specifiers.
Patrick
On Dec 5, 2007 9:48 AM, Attila Szegedi <[email protected]> wrote:
> In my recent wrestling with "genericizing" a codebase that was
> originally pre-Java 5, I'd suggest you try replacing "Class" with
> "Class<?>".
>
> Alternatively, disable compiler warning for generics. No, seriously.
> Honestly, my feeling is that programming with generics causes more
> noise in source code than it eliminates. Yeah, it eliminates casts,
> but previously I would write:
>
> Map map = new HashMap();
> ...
> String x = (String)map.get(blah);
>
> Now I write monstrosities such as:
>
> Map<? extends Object, String> map = new HashMap<Object, String>();
> ...
> String x = map.get(blah);
>
> So, I need to write one cast less, and four type specifiers more. Net
> loss in code conciseness.
> I guess I'm just getting old and resistant to change.
>
> Attila.
>
> On 2007.12.05., at 14:12, Gart, Mitchell wrote:
>
> > I am trying to get rid of some compiler warnings in our code. We
> > have a
> > method:
> >
> > public static Logger getLogger(Class c) {
> > return new Log4jLogger(c.getName());
> > }
> >
> > and the compiler produces this warning:
> >
> > Class is a raw type. References to generic type Class<T> should be
> > parameterized
> >
> > It is because Class is a generic type but I don't quite understand
> > how I
> > would change the declaration of the method, and the calls to the
> > method,
> > to make it parameterized and get rid of the warning. Can somebody
> > explain this a little bit? Thanks.
> >
> > - Mitch Gart
>
> ===================================
> This list is hosted by DevelopMentor(R) http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com