You're still casting.  You're just postponing it a little.
DefaultServiceManager.lookup does a cast to type T.  You specified what type
T is in MyClass.main.  It is perhaps possible that DefaultServiceManager
could be implemented without casting (using further generics), but off the
top of my head I can't see how.

<snip>

    interface ServiceManager {
        public <T> T lookup (String key) throws Exception;
        public void release (Object o);
    }

    class DefaultServiceManager implements ServiceManager {
        public <T> T lookup (String key) throws Exception {
            Object o = doGet (key);
            try {
                return (T) o; // unchecked cast to type T here, should be
fine.
            } catch (Throwable t) {
                return null;
            }
        }

        private Object doGet (String key) {
            if (key.equals ("A")) return new A();
            if (key.equals ("B")) return new B();
            return null;
        }

        public void release (Object o) {};
    }

    class A {}
    class B {}

    public class MyClass {

        public static void main(String[] args) throws Exception {
            ServiceManager sm = new DefaultServiceManager();
            A a = sm.<A>lookup ("A");
            B b = sm.<B>lookup ("B");
            System.out.println (a);
            System.out.println (b);
        }
    }

</snip>

----- Original Message ----- 
From: "Leo Sutic" <[EMAIL PROTECTED]>
To: "'Avalon Developers List'" <[EMAIL PROTECTED]>
Sent: Wednesday, January 28, 2004 11:18 AM
Subject: RE: Java 1.5, fun with lookups and generics


>
>
> > From: Jonathan Hawkes [mailto:[EMAIL PROTECTED]
> >
> > It just looks like a belayed cast to me.
>
> What do you mean by that?
>
> /LS
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to