Java SQL Type newInstance(Class clazz, Object... params)

Here you can find the source of newInstance(Class clazz, Object... params)

Description

new Instance

License

Apache License

Declaration

public static <T> T newInstance(Class<T> clazz, Object... params) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Constructor;
import java.sql.SQLException;

public class Main {
    public static <T> T newInstance(Class<T> clazz, Object... params) throws SQLException {
        try {//w  w w .  j a v  a  2 s .c o m
            if (params == null || params.length == 0) {
                return clazz.newInstance();
            } else {
                for (Constructor<?> ctor : clazz.getConstructors()) {
                    if (ctor.getParameterTypes().length == params.length) {
                        int paramIndex = 0;
                        for (Class<?> paramType : ctor.getParameterTypes()) {
                            if (!paramType.isInstance(params[paramIndex])) {
                                break;
                            }
                            paramIndex++;
                        }
                        if (paramIndex == params.length) {
                            return clazz.cast(ctor.newInstance(params));
                        }
                    }
                }
                throw new SQLException("Constructor not found for " + clazz);
            }
        } catch (ReflectiveOperationException reflectiveOperationException) {
            throw new SQLException(reflectiveOperationException);
        }
    }
}

Related

  1. mapXSDTypeName2SQLtype(String xsdtype)
  2. marshalCurlType(String javaType, boolean isAllowNull, boolean generateGenerics)
  3. matchMethods(String name, Method[] methods, Class[] spt, int mrs, boolean exact)
  4. nativeSQL(String sql, boolean noBackslashEscapes)
  5. newArrayOfType(int typeCode, int size)
  6. parse(String txt, int level)
  7. prettyPrint(Object obj)
  8. readAscii(Clob clob, String defaultValue)
  9. readMapListBySQL(String fileDir, String sql, List headers, List headerTypes)