简体   繁体   中英

How can I instantiate a class instance in java with generics?

How can I instantiate a class instance in java with generics?

I am trying to read data from XML file and then instantiate an object so that I can add the other properties of the object (read from the XML file) into the object. I thought the easiest way (so that I don't have to read the class method names and find the setters) would be to instantiate the object with all the values in the constructor.

I'm trying to do something like this so I need to have something like: T obj = new Object() but get the objects class

    private static final boolean string_field = true;

    private static <T> T getObject(Element e, String[] fieldNames, boolean[] fieldTypes) {
        Object[] values = new Object[fieldNames.length];
        for (int i=0; i<fieldNames.length; i++) {
            values[i] = (fieldTypes[i]==string_field)? getStringValue(e, fieldNames[i])
                    : getIntegerValue(e, fieldNames[i]);
        }
        return new T(values);
    }

Thanks for your advice.

EDIT:

This is my updated code (untested):

    public static <T> List<T> populateObjectList(Document xmlDoc, String tagName,
            Class clazz, String[] fieldNames, Class[] fieldTypes) {
        List<T> objList = new ArrayList<T>();
        NodeList nl = xmlDoc.getElementsByTagName(tagName);
        if (nl!=null && nl.getLength()>0) {
            for (int i=0; i<nl.getLength(); i++) {
                Element e = (Element) nl.item(i);
                T t;
                try {
                    t = getObject(e, clazz, fieldNames, fieldTypes);
                    objList.add(t);
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (IllegalArgumentException ex) {
                } catch (InvocationTargetException ex) {
                } catch (NoSuchMethodException ex) {
                }
            }
        }
        return objList;
    }

    private static <T> T getObject(Element e, Class clazz, String[] fieldNames, Class[] fieldTypes)
            throws InstantiationException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, NoSuchMethodException {
        Object[] initargs = new Object[fieldNames.length];
        for (int i=0; i<fieldNames.length; i++) {
            initargs[i] = (fieldTypes[i].getName().equals("int"))?
                getIntegerValue(e, fieldNames[i])
                : getStringValue(e, fieldNames[i]);
        }
        return (T) clazz.getConstructor(fieldTypes).newInstance(initargs);
    }

I posted this so that some of you would understand what I'm trying to do.

Thanks again everyone for their advice.

You cannot instantiate a generic type. Java generics have an annoying feature called type erasure , which basically means that the JVM doesn't know what type was used for a generic class or method at runtime. This is for backwards compatibility with pre-generic versions of generic classes, such as collections.

What you can do is add a Class<T> clazz to your parameter list, then replace the end of the method with this:

Class<?>[] paramTypes = new Class[values.length];
for (int i = 0; i < paramTypes.length; i++) {
    paramTypes[i] = values[i].getClass();
}
return clazz.getConstructor(paramTypes).newInstance(values);

This will throw an exception if the class doesn't have a constructor that takes the correct type and number of arguments in the correct order.

CAVEAT: This only works if the constructor's parameter types are exactly the same as the object types. For instance, if values consists of { "Hello", new Integer(2) } , then this will only find a constructor with signature SomeClass(String, Integer) , not one with signature SomeClass(Object, Object) .

How would you even know that T contained a constructor that took a single Object[] argument? The way to fix this is to pass in an abstract factory.

interface ThingFactory { // Choose an appropriate name.
    T create(Object[] values);
}

private static <T> T getObject(
    ThingFactory<T> factory, Element e, String[] fieldNames, boolean[] fieldTypes
) { 
    ...
    return factory.create(values);
}

Now T might not even have a rather strange Object[] constructor. T might be an abstract type. The factory might return a different type dependent upon the arguments. Perhaps sometimes these products are immutable objects that can be cached.

You can't do new T() because at runtime, T is effectively just Object . The compiler uses generics for type-checking (eg you can't put an Integer into a List<String> ), but then throws the generics information away. There's one compiled copy of the method for all types of arguments, so there's no way for it to know which type you expect it to instantiate.

Read about type erasure for more information.

我真的看不到您的问题,无法使用obj.getClass吗?

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