(T) vs. t.cast in p4j5, or the opensource effect

Immediately after opensourcing the first utilities in the Java EE 5 Patterns project, I got not only many requests for developer role, but also some good feedback. In the ElementLocator utility, I wrote the following code:

   public <T> T getElement(String jndiName,Class<T> t){
        Object component = this.getElement(jndiName);
        return (T)PortableRemoteObject.narrow(component, t);
    }

the problem is the cast, which causes warnings, which is in turn sub-optimal :-).

Robert Herschke sent me the following snippet, which was immediately committed to the trunk:

public <T> T getElement(String jndiName,Class<T> t){
       Object component = this.getElement(jndiName);
       return t.cast(PortableRemoteObject.narrow(component, t));
   }

I invited him to get the developer role, but he had to work :-).


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License