Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Class as parameter and return same class object

I want to pass Class as parameter and return the same class as parameter. Parameter can be any of the class. so I want comman method in which I need to pass only object of particular class and it will give me same object as return. I know with Java - Generics or with java collection it is possible but I don't know how?

Code:

 public Class_object_as_paramter(parent) webCall(Class Obj as parameter(parent))
                throws JsonSyntaxException, IOException, XmlPullParserException {
            GsonBuilder gsonBuilder = new GsonBuilder();
            Gson gson = gsonBuilder.create();

            parent = (Collection<?>) gson.fromJson(SoaplCalls
                    .GetUserwiseSalesContracts(GsonExample.this, "55000024"),
                    ParentSalesContract.class (also has to pass same class here);
            return parent;
        }
like image 844
nil Avatar asked Aug 31 '25 22:08

nil


2 Answers

Create generic method like this:

<T> T method(T t) {
    ...
    T tt = ...
    return tt;
}
like image 136
Grzegorz Żur Avatar answered Sep 03 '25 14:09

Grzegorz Żur


public <T> T webCall(T type)
{
   return type;  
 }
like image 38
Siva Avatar answered Sep 03 '25 15:09

Siva