List of usage examples for java.lang Class getDeclaredConstructor
@CallerSensitive public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:com.swingtech.commons.util.ClassUtil.java
public static Object instantiateObject(final Class inClass, final Object[] args) { Constructor constructor = null; if (inClass == null) { throw new IllegalArgumentException("inClass can not be null."); }/*from w ww.j a v a2 s . c o m*/ try { constructor = inClass.getDeclaredConstructor(getClassArrayFromObjectArray(args)); return constructor.newInstance(args); } catch (final Exception e) { throw new RuntimeException("Error trying to instatiate class, '" + inClass.getName() + "'. Error: " + e.getClass().getName() + ": " + e.getMessage() + "."); } }
From source file:org.apache.tajo.storage.OldStorageManager.java
/** * Returns the proper Tablespace instance according to the dataFormat * * @param tajoConf Tajo system property. * @param uri Key that can identify each storage manager(may be a path) * @param dataFormat Storage type//from w w w . j a v a2s . co m * @return * @throws IOException */ public static synchronized Tablespace getStorageManager(TajoConf tajoConf, URI uri, String dataFormat) throws IOException { Preconditions.checkNotNull(tajoConf); Preconditions.checkNotNull(uri); Preconditions.checkNotNull(dataFormat); String typeName; if (dataFormat.equalsIgnoreCase("HBASE")) { typeName = "hbase"; } else { typeName = "hdfs"; } synchronized (storageManagers) { String storeKey = typeName + "_" + uri.toString(); Tablespace manager = storageManagers.get(storeKey); if (manager == null) { Class<? extends Tablespace> storageManagerClass = tajoConf .getClass(String.format("tajo.storage.manager.%s.class", typeName), null, Tablespace.class); if (storageManagerClass == null) { throw new IOException("Unknown Storage Type: " + typeName); } try { Constructor<? extends Tablespace> constructor = (Constructor<? extends Tablespace>) CONSTRUCTOR_CACHE .get(storageManagerClass); if (constructor == null) { constructor = storageManagerClass .getDeclaredConstructor(TablespaceManager.TABLESPACE_PARAM); constructor.setAccessible(true); CONSTRUCTOR_CACHE.put(storageManagerClass, constructor); } manager = constructor.newInstance(new Object[] { "noname", uri, null }); } catch (Exception e) { throw new RuntimeException(e); } manager.init(tajoConf); storageManagers.put(storeKey, manager); } return manager; } }
From source file:org.bremersee.common.exception.ExceptionRegistry.java
private static Throwable findThrowableByCause(final Class<? extends Throwable> clazz, final String message, final Throwable cause) { try {//www . j a va 2 s . com Constructor<? extends Throwable> constructor = clazz.getDeclaredConstructor(Throwable.class); if (!constructor.isAccessible()) { constructor.setAccessible(true); } Throwable t = constructor.newInstance(cause); putMessage(t, message); return t; } catch (NoSuchMethodException | InstantiationException | IllegalAccessException // NOSONAR | InvocationTargetException e) { // NOSONAR if (LOG.isDebugEnabled()) { LOG.debug("Finding throwable by cause failed. Class [" + clazz.getName() + "] has no suitable constructor: " + e.getMessage() + " (" + e.getClass().getName() + ")."); } return null; } }
From source file:org.talend.core.utils.ReflectionUtils.java
/** * DOC ycbai Comment method "newInstance". * //from w ww . jav a 2 s .c om * Create a new instance of a class. * * @param className * @param initialize * @param loader * @param args * @return * @throws ClassNotFoundException * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalAccessException * @throws InstantiationException * @throws IllegalArgumentException */ public static Object newInstance(String className, ClassLoader loader, Object[] args, Class... argTypes) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object instance = null; Class newClass = null; if (loader != null) { newClass = Class.forName(className, true, loader); } else { newClass = Class.forName(className); } Class[] argsClass = new Class[args.length]; if (argTypes.length > 0 && argTypes.length == args.length) { argsClass = argTypes; } else { for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); } } Constructor cons = newClass.getDeclaredConstructor(argsClass); cons.setAccessible(true); instance = cons.newInstance(args); return instance; }
From source file:org.bremersee.common.exception.ExceptionRegistry.java
private static Throwable findThrowableByMessage(final Class<? extends Throwable> clazz, final String message, final Throwable cause) { try {/*from w ww .ja v a 2s.co m*/ Constructor<? extends Throwable> constructor = clazz.getDeclaredConstructor(String.class); if (!constructor.isAccessible()) { constructor.setAccessible(true); } Throwable t = constructor.newInstance(message); if (cause != null) { t.initCause(cause); } return t; } catch (NoSuchMethodException | InstantiationException | IllegalAccessException // NOSONAR | InvocationTargetException e) { // NOSONAR if (LOG.isDebugEnabled()) { LOG.debug("Finding throwable by message failed. Class [" + clazz.getName() + "] has no suitable constructor: " + e.getMessage() + " (" + e.getClass().getName() + ")."); } return null; } }
From source file:com.github.jinahya.codec.commons.AbstractEncoderProxy.java
/** * Creates a new proxy instance.//from w ww . j a va2 s. c om * * @param <P> proxy type parameter * @param <T> encoder type parameter * @param loader class loader * @param interfaces interfaces * @param proxyType proxy type * @param encoderType encoder type * @param encoder encoder * * @return a new proxy instance. */ protected static <P extends AbstractEncoderProxy<T>, T> Object newInstance(final ClassLoader loader, final Class<?>[] interfaces, final Class<P> proxyType, final Class<T> encoderType, final T encoder) { if (loader == null) { throw new NullPointerException("loader"); } if (interfaces == null) { throw new NullPointerException("interfaces"); } if (proxyType == null) { throw new NullPointerException("proxyType"); } if (encoderType == null) { throw new NullPointerException("encoderType"); } if (encoder == null) { //throw new NullPointerException("encoder"); } try { final Constructor<P> constructor = proxyType.getDeclaredConstructor(encoderType); if (!constructor.isAccessible()) { constructor.setAccessible(true); } try { return Proxy.newProxyInstance(loader, interfaces, constructor.newInstance(encoder)); } catch (final IllegalAccessException iae) { throw new RuntimeException(iae); } catch (final InstantiationException ie) { throw new RuntimeException(ie); } catch (final InvocationTargetException ite) { throw new RuntimeException(ite); } } catch (final NoSuchMethodException nsme) { throw new RuntimeException(nsme); } }
From source file:org.apache.tajo.storage.TablespaceManager.java
private static Tablespace initializeTableSpace(String spaceName, URI uri, JSONObject spaceDesc) { final String scheme = UriUtil.getScheme(uri); Class<? extends Tablespace> clazz = TABLE_SPACE_HANDLERS.get(scheme); if (clazz == null) { throw new TajoRuntimeException(new UndefinedTablespaceHandlerException(scheme)); }//w w w .ja v a 2 s. c o m try { Constructor<? extends Tablespace> constructor = (Constructor<? extends Tablespace>) CONSTRUCTORS .get(clazz); if (constructor == null) { constructor = clazz.getDeclaredConstructor(TABLESPACE_PARAM); constructor.setAccessible(true); CONSTRUCTORS.put(clazz, constructor); } return constructor.newInstance(new Object[] { spaceName, uri, spaceDesc }); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.github.jinahya.codec.commons.AbstractDecoderProxy.java
/** * Creates a new proxy instance.//from w ww . j a va2s . c o m * * @param <P> proxy type parameter * @param <T> decoder type parameter * @param loader class loader * @param interfaces interfaces * @param proxyType proxy type * @param decoderType decoder type * @param decoder decoder * * @return a new proxy instance. */ protected static <P extends AbstractDecoderProxy<T>, T> Object newInstance(final ClassLoader loader, final Class<?>[] interfaces, final Class<P> proxyType, final Class<T> decoderType, final T decoder) { if (loader == null) { throw new NullPointerException("loader"); } if (interfaces == null) { throw new NullPointerException("interfaces"); } if (proxyType == null) { throw new NullPointerException("proxyType"); } if (decoderType == null) { throw new NullPointerException("decoderType"); } if (decoder == null) { //throw new NullPointerException("decoder"); } try { final Constructor<P> constructor = proxyType.getDeclaredConstructor(decoderType); if (!constructor.isAccessible()) { constructor.setAccessible(true); } try { return Proxy.newProxyInstance(loader, interfaces, constructor.newInstance(decoder)); } catch (final InstantiationException ie) { throw new RuntimeException(ie); } catch (final IllegalAccessException iae) { throw new RuntimeException(iae); } catch (final InvocationTargetException ite) { throw new RuntimeException(ite); } } catch (final NoSuchMethodException nsme) { throw new RuntimeException(nsme); } }
From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java
/** * Returns a {@link Constructor} object that reflects the specified constructor of the given type. * <p/>/* w ww .j a v a 2 s .c om*/ * If no parameter types are specified i.e., {@code paramTypes} is {@code null} or empty, the default constructor * is returned.<br/> * If a parameter type is not known i.e., it is {@code null}, all declared constructors are checked whether their * parameter types conform to the known parameter types i.e., if every known type is assignable to the parameter * type of the constructor and if exactly one was found, it is returned.<br/> * Otherwise a {@link NoSuchMethodException} is thrown indicating that no or several constructors were found. * * @param type the class * @param paramTypes the full-qualified class names of the parameters (can be {@code null}) * @param classLoader the class loader to use * @return the accessible constructor resolved * @throws NoSuchMethodException if there are zero or more than one constructor candidates * @throws ClassNotFoundException if a class cannot be located by the specified class loader */ @SuppressWarnings("unchecked") public static <T> Constructor<T> findConstructor(Class<T> type, Class<?>... clazzes) throws NoSuchMethodException, ClassNotFoundException { Constructor<T> constructor = null; // If all parameter types are known, find the constructor that exactly matches the signature if (!ArrayUtils.contains(clazzes, null)) { try { constructor = type.getDeclaredConstructor(clazzes); } catch (NoSuchMethodException e) { // Ignore } } // If no constructor was found, find all possible candidates if (constructor == null) { List<Constructor<T>> candidates = new ArrayList<>(1); for (Constructor<T> declaredConstructor : (Constructor<T>[]) type.getDeclaredConstructors()) { if (ClassUtils.isAssignable(clazzes, declaredConstructor.getParameterTypes())) { // Check if there is already a constructor method with the same signature for (int i = 0; i < candidates.size(); i++) { Constructor<T> candidate = candidates.get(i); /** * If all parameter types of constructor A are assignable to the types of constructor B * (at least one type is a subtype of the corresponding parameter), keep the one whose types * are more concrete and drop the other one. */ if (ClassUtils.isAssignable(declaredConstructor.getParameterTypes(), candidate.getParameterTypes())) { candidates.remove(candidate); i--; } else if (ClassUtils.isAssignable(candidate.getParameterTypes(), declaredConstructor.getParameterTypes())) { declaredConstructor = null; break; } } if (declaredConstructor != null) { candidates.add(declaredConstructor); } } } if (candidates.size() != 1) { throw new NoSuchMethodException( String.format("Cannot find distinct constructor for type '%s' with parameter types %s", type, Arrays.toString(clazzes))); } constructor = candidates.get(0); } //do we really need this dependency? //ReflectionUtils.makeAccessible(constructor); if (constructor != null && !constructor.isAccessible()) constructor.setAccessible(true); return constructor; }
From source file:org.apache.tajo.storage.AbstractStorageManager.java
/** * create a scanner instance.// w w w.ja v a 2 s .c om */ public static <T> T newAppenderInstance(Class<T> theClass, Configuration conf, TableMeta meta, Schema schema, Path path) { T result; try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(DEFAULT_APPENDER_PARAMS); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(new Object[] { conf, schema, meta, path }); } catch (Exception e) { throw new RuntimeException(e); } return result; }