1

我们知道 Java 使用擦除,所以任何 Generic 类都做不到

T t = new T();

所以我尝试使用 Java 反射来创建一个带有静态方法的类,用任何构造函数实例化任何类。这是代码。

import java.lang.reflect.*;

public class GenericNewInstance {
    public static <T> T createInstance(Class<T> cObj) {
        try {
            return cObj.newInstance();
        } catch (InstantiationException e) {
            System.out.println("Instantiation Exception");
            return null;
        } catch (IllegalAccessException e) {
            System.out.println("Illegal Access Exception");
            return null;
        }
    }

    public static <T> T createInstanceUsingRelection(Class<T> c, Object... initArgs) {
        Constructor<T> cTor = null;
        Class<?>[] cObjs = new Class<?>[initArgs.length];
        int i = 0;

        for(Object o : initArgs) {
            cObjs[i++] = o.getClass();
        }

        try {
            cTor = c.getConstructor(cObjs);
        } catch (SecurityException e) {
            System.out.println("security exception. Cannot get Constructor");
            return null;
        } catch (NoSuchMethodException e) {
            System.out.println("NoSuchMethodException Cannot get constructor");
            return null;
        }

        try {
            return cTor.newInstance(initArgs);
        } catch (IllegalArgumentException e) {
            System.out.println("Illegal Argument Exception");
            return null;
        } catch (InstantiationException e) {
            System.out.println("Instantiation Exception");
            return null;
        } catch (IllegalAccessException e) {
            System.out.println("Illegal Access Exception");
            return null;
        } catch (InvocationTargetException e) {
            System.out.println("Invocation Target Exception");
            return null;
        }
    }
}

使用此示例。

Integer i = GenericNewInstance.createInstanceUsingRelection(Integer.class, "0");

所以我的问题:

  1. 这是实施它的正确方法吗?(或者它很冗长?)
  2. 这样做的典型用例是什么?
  3. 我们可以/应该在使用泛型时避免使用反射吗?
4

1 回答 1

6

您的代码将失败,c.getConstructor(cObjs)因为这没有考虑类型层次结构。如果任何参数是构造函数声明的参数类型的子类型,则此调用将不会返回它。您将需要更多的类型杂耍才能使其正常工作。我建议你看看已经解决了这个问题的代码。也许您甚至可以选择按原样使用该库。它是 Clojure 的实现代码,一种基于 JVM 的动态语言,正是需要这些东西。该库可从 Maven 中央存储库获得。

顺便说一句,您的异常处理是多余的。要么只是声明throws Exception或捕获任何Exception并将其包装在RuntimeException. 当某事失败时,原始异常是最好的诊断。

于 2012-07-14T19:57:09.960 回答