Search code examples
javagenericsservletsgeneric-method

I want to implement generic java method passing argument as class object and defining method parameter as Class<T> or T type


I want to achieve generic java method that it should be pass class object in method parameter

E.g :

My method definition Here in different project class

public String getRtbAd(HttpServletRequest request, T userProfileClass,
            List<String> systemControlDetails, T wurflDataGenericClass,String redisConnectionUrl, String dimension, Short rtbTimeOut) 
{

//My Bussiness logic

}

Here,actully I am calling method from different project class

public String getRtbAd(request, userProfileObject,
            systemControlDetails, wurflDataGenericObject,redisConnectionUrl, dimension, rtbTimeOut) 

Solution

    1. First of all if T userProfileClass and T wurflDataGenericClass are different types then they should be having different literals as their type. They must be mentioned like T1 userProfileClass and T2 wurflDataGenericClass.

    2. The class in which this method is mention should accept these types like below:-

        public class myClass {        
            public String getRtbAd(HttpServletRequest request, T1 userProfileClass, List systemControlDetails, T2 wurflDataGenericClass,String redisConnectionUrl, String dimension, Short rtbTimeOut){     
                }        
            }        
            myClass  a = new myClass();
            String str = a.getRtbAd();

    =========== I am really not sure why this comment window is not displaying my generic code correctly.Following are not shown.

    1. myClass < T1, T2 >

    2. myClass < ProfileClassName, DataGenericClassName > a = new myClass< ProfileClassName, DataGenericClassName >();