List of utility methods to do Reflection Constructor Get
Constructor> | getConstructor(Class> clazz, Class>... args) get Constructor for (Constructor<?> c : clazz.getDeclaredConstructors()) if (args.length == 0 && c.getParameterTypes().length == 0 || ClassListEqual(args, c.getParameterTypes())) { c.setAccessible(true); return c; for (Constructor<?> c : clazz.getConstructors()) if (args.length == 0 && c.getParameterTypes().length == 0 ... |
MethodHandle | getConstructor(Class> clazz, Class>... args) Gets a constructor that has the specified types of arguments. try { Constructor<?> ctor = clazz.getDeclaredConstructor(args); ctor.setAccessible(true); return MethodHandles.lookup().unreflectConstructor(ctor); } catch (NoSuchMethodException e) { throw new IllegalStateException( clazz.getName() + " has no constructor with args " + Arrays.toString(args), e); } catch (IllegalAccessException e) { ... |
Constructor> | getConstructor(Class> clazz, Class>... parameterTypes) get Constructor try { return clazz.getDeclaredConstructor(parameterTypes); } catch (Exception e) { return null; |
Constructor> | getConstructor(Class> clazz, Class>... parameterTypes) Fail-safe. try { return clazz.getConstructor(parameterTypes); } catch (NoSuchMethodException e) { } catch (SecurityException e) { return null; |
Constructor> | getConstructor(Class> clazz, Class>... params) Returns the constructor try { return clazz.getConstructor(params); } catch (NoSuchMethodException e) { return null; |
Constructor> | getConstructor(Class> clazz, Class>... params) Attempts to get a constructor from the specified class if (clazz == null) { System.err.println("No class specified."); return null; Constructor<?> constructor = null; try { constructor = clazz.getDeclaredConstructor(params); } catch (NoSuchMethodException nsme) { ... |
Constructor> | getConstructor(Class> clazz, Object... args) get Constructor Constructor<?> constructor = null; int length = args.length; Class<?>[] classes = new Class<?>[length]; for (int i = 0; i < length; i++) { Object object = args[i]; classes[i] = object.getClass(); constructor = clazz.getConstructor(classes); ... |
Constructor> | getConstructor(Class> clazz, Object[] parameters) get Constructor Class<?>[] parameterClasses = new Class<?>[parameters.length]; for (int i = 0; i < parameters.length; i++) { parameterClasses[i] = parameters[i] == null ? null : parameters[i].getClass(); return clazz.getConstructor(parameterClasses); |
Constructor> | getConstructor(Class> cls, Class>[] signature) get Constructor Constructor<?>[] consarr = cls.getDeclaredConstructors(); for (int i = 0; i < consarr.length; i++) { Class<?>[] paramtypes = consarr[i].getParameterTypes(); if (java.util.Arrays.equals(signature, paramtypes)) return consarr[i]; return null; |
Constructor> | getConstructor(Class> cls, Object... parameters) get Constructor Constructor<?> constructor = null; for (Constructor<?> classMethod : cls.getConstructors()) { Class<?>[] parametersType = classMethod.getParameterTypes(); boolean match = false; if (parametersType.length == parameters.length) { match = true; for (int i = 0, length = parametersType.length; i < length; i++) { if (parameters[i] != null) { ... |