Java: newInstance of class that has no default constructor

前端 未结 6 800
广开言路
广开言路 2020-11-29 02:32

I\'m trying to build an automatic testing framework (based on jUnit, but that\'s no important) for my students\' homework. They will have to create constructors for some cla

6条回答
  •  囚心锁ツ
    2020-11-29 03:19

    I used the following code to create a list of generic objects of any type of class name passed in. It uses all of the set methods within the class to set all the values passed in via the result set. I'm posting this in case anyone was interested in it as well.

    protected List FillObject(ResultSet rs, String className)
        {
            List dList = new ArrayList();
    
            try
            {
                ClassLoader classLoader = GenericModel.class.getClassLoader();
    
                while (rs.next())
                {
                    Class reflectionClass = classLoader.loadClass("models." + className);
    
                    Object objectClass = reflectionClass.newInstance();
    
                    Method[] methods = reflectionClass.getMethods();
    
                    for(Method method: methods)
                    {
                        if (method.getName().indexOf("set") > -1)
                        {
                            Class[] parameterTypes = method.getParameterTypes();
    
                            for(Class pT: parameterTypes)
                            {
                                Method setMethod = reflectionClass.getMethod(method.getName(), pT);
    
                                switch(pT.getName())
                                {
                                    case "int":
                                        int intValue = rs.getInt(method.getName().replace("set", ""));
                                        setMethod.invoke(objectClass, intValue);
                                        break;
    
                                    case "java.util.Date":
                                        Date dateValue = rs.getDate(method.getName().replace("set", ""));
                                        setMethod.invoke(objectClass, dateValue);
                                        break;
    
                                    case "boolean":
                                        boolean boolValue = rs.getBoolean(method.getName().replace("set", ""));
                                        setMethod.invoke(objectClass, boolValue);
                                        break;
    
                                    default:
                                        String stringValue = rs.getString(method.getName().replace("set", ""));
                                        setMethod.invoke(objectClass, stringValue);
                                        break;
                                }
                            }
                        }
                    }
    
                    dList.add(objectClass);
                }
            }
            catch (Exception e)
            {
                this.setConnectionMessage("ERROR: reflection class loading: " + e.getMessage());
            }
    
            return dList;
        }
    
        

    提交回复
    热议问题