简体   繁体   中英

Pass any Class as a parameter for a method

This has probably been asked before and I've seen a few similar/along the lines, but didn't quite understand it.

In Java, how do make it so a method accepts any class as a parameter?

For example,

public (AnyClass) myMethod(String whatever, (AnyClass) thisClass){

}

to be used like:

myMethod("Hello", ClassA);
myMethod("Hello", ClassB);

Thanks for any help!

Edit:

Usage example was asked for; What I'm doing is mapping a JSON string to a POJO, but trying to abstract this whole thing out so it can be reused. So - I need to be able to pass through any class, and the JSON string (or HttpConn), then build then use Jackson to build that POJO and return the object, of whatever type that may be.

(An idea of what I want to do):

public Class<?> doSomething(String jsonString, Class<?> clazz){
    clazz newInstance = mapper.readValue(jsonString, clazz);
    return clazz;
}

called like:

ClassA test = doSomething("String One", ClassA.class);
ClassB testTwo = doSomething("Different format string", ClassB.class);

Hope that helps understanding of the problem... and my understanding of the solutions!

I assume you need to accept a class of some type T as a parameter and return that class as well. In that case, your method signature will be something like this:

public <T> Class<T> myMethod(String whatever, Class<T> clazz){
    // put the rest of your code here
}

1) Without type information:

public static Class<?> myMethod(String text, Class<?> clazz) {
    // body
    return clazz;
}

Client code:

public static void main(String[] args) {
    Class<?> clss = myMethod("Example", String.class);
}

2) With type information:

public static <T> Class<T> myMethod(String x, Class<T> clazz) {
    // body
    return clazz;
}

Client code:

public static void main(String[] args) {
    Class<String> clsStr = myMethod("Example", String.class);
    Class<?> clsAny = myMethod("Example", String.class);
}

3) Raw type (will work but NOT recommended):

public static Class myMethod(String x, Class clazz) {
    // body
    return clazz;
}

Client code:

public static void main(String[] args) {
    Class<String> clsStr = myMethod("Example", String.class); // warning for Unchecked Assignment 
    Class<?> clsAny = myMethod("Example", String.class);
    Class clsRaw = myMethod("Example", String.class);
}

4) With type information defined at class level:

public class Main<T> { 

    // cannot be static
    public Class<T> myMethod(String x, Class<T> clazz) {
        // body
        return clazz;
    }
}

Client code:

public static void main(String[] args) {
    Main<String> m = new Main<>();
    Class<String> cls = m.myMethod("x", String.class);
}

In your latest edit, your usage example is like this:

ClassA test = doSomething("String One", ClassA.class);

The question shows confusion between a class name, the Class object and an instance of a class. So just to clarify, using the above example:

  • A class name – ClassA – is used to declare a variable.

  • A Class instance – ClassA.class – is a singleton object that holds information about a class and can be passed as an argument to a method.

  • An instance of a class – test – is an object. It's usually created using the new keyword.

You can't use a Class object, such as ClassA.class , directly in a declaration. Instead, you have to call the newInstance() method of the Class class. That method can be used only if your class has a no-args constructor. To create an instance with constructor arguments, use something like this:

public <T> T doSomething(String jsonString, Class<T> clazz) throws ReflectiveOperationException {
    Constructor<T> constructor = clazz.getConstructor(String.class);
    return constructor.newInstance(jsonString);
}

The above method creates an instance of the required class using a constructor that takes a String (the string that was passed in). Change its body to create an instance according to your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM