3 Replies Latest reply: Feb 15, 2010 11:18 PM by 843793 RSS

    Doubt regarding Generic methods

    843793
      I have 4 generics Methods getEJBHome,getEJBLocalHome ,findEJBLocalHomeAndPopulateCache,findEJBHomeAndPopulateCache
           public <T extends EJBHome> T getEJBHome(final String jndiName,final Class<T> ejbHomeClass) throws NamingException,ClassCastException{
                T ejbHome=null;
                try{
                     if(serviceLocatorCache.containsKey(jndiName)){
                          ejbHome=(T)serviceLocatorCache.get(jndiName);     
                     }
                     else{
                          ejbHome=findEJBHomeAndPopulateCache(jndiName,ejbHomeClass);
                     }
                }
                catch(NamingException namingException ){
                     System.err.println("Exception in getEJBHome ["+namingException.getMessage()+"]");
                     throw namingException;
                }
                catch(ClassCastException classCastException ){
                     System.err.println("Exception in getEJBHome ["+classCastException.getMessage()+"]");
                     throw classCastException;
                }
                return ejbHome;
           }
           private <T extends EJBHome> T findEJBHomeAndPopulateCache(final String jndiName,final Class<T> ejbHomeClass) throws NamingException{
                T ejbHome=null;
                try{
                     ejbHome=(T)javax.rmi.PortableRemoteObject.narrow(context.lookup(jndiName),ejbHomeClass);
                     serviceLocatorCache.put(jndiName,ejbHome);
                }
                catch(NamingException namingException){
                     System.err.println("Exception in findEJBHomeAndPopulateCache ["+namingException.toString()+"]");
                     throw namingException;
                }
                return ejbHome;
           }
      i am calling findEJBHomeAndPopulateCache like normal method call,When i try to call findEJBLocalHomeAndPopulateCache from getEJBLocalHome it shows compile time error .
           public <T extends EJBLocalHome> T getEJBLocalHome(final String jndiName) throws NamingException{
                T ejbLocalHome=null;
                try{
                     if(serviceLocatorCache.containsKey(jndiName)){
                          ejbLocalHome=(T)serviceLocatorCache.get(jndiName);     
                     }
                     else{
                          ejbLocalHome=this.<T>findEJBLocalHomeAndPopulateCache(jndiName);
                          //ejbLocalHome=findEJBLocalHomeAndPopulateCache(jndiName); ERROR
                     }
                }
                catch(NamingException namingException ){
                     System.err.println("Exception in getEJBLocalHome ["+namingException.getMessage()+"]");
                     throw namingException;
                }
                catch(Exception exception ){
                     System.err.println("Exception in getEJBLoacalHome ["+exception.getMessage()+"]");
                     throw new NamingException("Exception in getEJBLoacalHome ["+exception.getMessage()+"]");
                }
                return ejbLocalHome;
           }
      when i try to call method findEJBLocalHomeAndPopulateCache like ejbLocalHome=findEJBLocalHomeAndPopulateCache(jndiName);
      i am getting compile time error
      ServiceLocator.java:133: type parameters of <T>T cannot be determined; no unique maximal instance ex
      ists for type variable T with upper bounds T,javax.ejb.EJBLocalHome
          [javac]                             ejbLocalHome=findEJBLocalHomeAndPopulateCache(jndiName);
          [javac]     
      ^
      when i am calling that method like
      this.<T>findEJBLocalHomeAndPopulateCache(jndiName); it is not showing any error.normally we are invoking generic methods like normal methods ?
      Why i am getting a compile time error for findEJBLocalHomeAndPopulateCache method?
      method findEJBLocalHomeAndPopulateCache is as shown
           private <T extends EJBLocalHome> T findEJBLocalHomeAndPopulateCache(final String jndiName) throws NamingException{
                T ejbLocalHome=null;
                try{
                     ejbLocalHome=(T)context.lookup(jndiName);
                     serviceLocatorCache.put(jndiName,ejbLocalHome);
                }
                catch(NamingException namingException){
                     System.err.println("Exception in findEJBLocalHomeAndPopulateCache ["+namingException.toString()+"]");
                     throw namingException;
                }
                return ejbLocalHome;
           }
      Plz help