Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it acceptable to pass a Class<T> argument to a generic method?

Tags:

java

generics

Methods that are generic using the T parameter can for sure be handy. However, I am curious what the use of a generic method would be if you pass an argument such as Class<T> clazz to the method. I've come up with a case that maybe could be an possible use. Perhaps you only want to run a part of the method based on the type of class. For example:

/** load(File, Collection<T>, Class<T>)
* Creates an object T from an xml.  It also prints the contents of the collection if T is a House object.
* @return T
* Throws Exception
*/
private static <T> T void load(File xml, Collection<T> t, Class<T> clazz) throws Exception{
    T type = (T) Jaxb.unmarshalFile(xml.getAbsolutePath(), clazz);  // This method accepts a class argument.  Is there an alternative to passing the class here without "clazz"?  How can I put "T" in replace of "clazz" here?
    if (clazz == House.class) {
         System.out.println(t.toString());
    } else {
         t.clear();
    }
    return T;
}

Is this an accepted practice? When is the Class<T> clazz argument useful with generic methods?

like image 245
Stephen D Avatar asked Aug 22 '13 12:08

Stephen D


People also ask

When an argument is passed to a method Java?

Arguments in Java are always passed-by-value. During method invocation, a copy of each argument, whether its a value or reference, is created in stack memory which is then passed to the method.

How do you define a class which accepts a generic type parameter?

A Generic class simply means that the items or functions in that class can be generalized with the parameter(example T) to specify that we can add any type as a parameter in place of T like Integer, Character, String, Double or any other user-defined type.

Which types can be used as arguments of a generic type?

The actual type arguments of a generic type are. reference types, wildcards, or. parameterized types (i.e. instantiations of other generic types).

Can I pass a class as an argument Java?

We cannot pass a class inside the function as an argument. This is because the class is just a human defined data-type that encapsulates all the other datatypes like variables, functions, methods.


1 Answers

Is this an accepted practice?

Well, to me.. no not really. To me, it seems somewhat pointless when you can simply define some boundaries on the type of T. For example:

private static <T extends House> void load(Collection<T> t)

This will guarantee that either the object is of type House or of a subclass of House, but then again if you only want an instance of type House or it's subclasses, it should really just be:

private static void load(Collection<House> houses)

The idea of generics is to make a method or a class more malleable and extensible, so to me it seems counter-intuitive to start comparing class types in the method body, when the very notion of generics is to abstract away from such details.

like image 193
christopher Avatar answered Nov 15 '22 12:11

christopher