List of usage examples for java.lang Class getDeclaredConstructors
@CallerSensitive public Constructor<?>[] getDeclaredConstructors() throws SecurityException
From source file:ReflectionTest.java
public static void printConstructors(Class cl) { Constructor[] constructors = cl.getDeclaredConstructors(); for (int i = 0; i < constructors.length; i++) { Constructor c = constructors[i]; Class[] paramTypes = c.getParameterTypes(); String name = c.getName(); System.out.print(Modifier.toString(c.getModifiers())); System.out.print(" " + name + "("); for (int j = 0; j < paramTypes.length; j++) { if (j > 0) System.out.print(", "); System.out.print(paramTypes[j].getName()); }/*from www .java 2 s. c o m*/ System.out.println(");"); } }
From source file:Main.java
private static Constructor<?> getDefaultConstructor(@NonNull Class<?> cls) { final Constructor[] ctors = cls.getDeclaredConstructors(); Constructor ctor = null;/* w w w .j a v a 2s . co m*/ for (Constructor ct : ctors) { ctor = ct; if (ctor.getGenericParameterTypes().length == 0) break; } if (ctor == null) throw new IllegalStateException("No default constructor found for " + cls.getName()); ctor.setAccessible(true); return ctor; }
From source file:ReflectionTest.java
/** * Prints all constructors of a class/*from www . j a va 2 s .co m*/ * @param cl a class */ public static void printConstructors(Class cl) { Constructor[] constructors = cl.getDeclaredConstructors(); for (Constructor c : constructors) { String name = c.getName(); System.out.print(" "); String modifiers = Modifier.toString(c.getModifiers()); if (modifiers.length() > 0) System.out.print(modifiers + " "); System.out.print(name + "("); // print parameter types Class[] paramTypes = c.getParameterTypes(); for (int j = 0; j < paramTypes.length; j++) { if (j > 0) System.out.print(", "); System.out.print(paramTypes[j].getName()); } System.out.println(");"); } }
From source file:Main.java
public static <T> T newInstance(Class<T> claxx) throws IllegalAccessException, InvocationTargetException, InstantiationException { Constructor<?>[] cons = claxx.getDeclaredConstructors(); for (Constructor<?> c : cons) { Class[] cls = c.getParameterTypes(); if (cls.length == 0) { c.setAccessible(true);/*from w ww. j av a 2s .co m*/ return (T) c.newInstance(); } else { Object[] objs = new Object[cls.length]; for (int i = 0; i < cls.length; i++) { objs[i] = getDefaultPrimiticeValue(cls[i]); } c.setAccessible(true); return (T) c.newInstance(objs); } } return null; }
From source file:com.edmunds.autotest.ClassUtil.java
public static Constructor getDefaultConstructor(Class cls) { final Constructor[] constructors = cls.getDeclaredConstructors(); for (Constructor constructor : constructors) { if (constructor.getParameterTypes().length == 0) { return constructor; }/*from ww w . j a va2 s.c o m*/ } return null; }
From source file:org.apache.bval.util.reflection.Reflection.java
/** * Convenient point for {@link Privilizing} {@link Class#getDeclaredConstructors()}. * @param clazz/*from w w w . j a va2s .com*/ * @return {@link Constructor} array */ public static Constructor<?>[] getDeclaredConstructors(final Class<?> clazz) { return clazz.getDeclaredConstructors(); }
From source file:MethodHashing.java
public static Constructor findConstructorByHash(Class clazz, long hash) throws Exception { Constructor[] cons = clazz.getDeclaredConstructors(); for (int i = 0; i < cons.length; i++) { if (constructorHash(cons[i]) == hash) return cons[i]; }/*w ww .j a va2 s . com*/ if (clazz.getSuperclass() != null) { return findConstructorByHash(clazz.getSuperclass(), hash); } return null; }
From source file:com.edmunds.autotest.ClassUtil.java
public static boolean hasDefaultConstructor(Class cls) { boolean defaultConstructor = false; final Constructor[] constructors = cls.getDeclaredConstructors(); for (Constructor constructor : constructors) { if (constructor.getParameterTypes().length == 0) { defaultConstructor = true;/*from w w w. j a va2s .c o m*/ break; } } return defaultConstructor; }
From source file:org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovyCallSiteSelector.java
public static @CheckForNull Constructor<?> constructor(@Nonnull Class<?> receiver, @Nonnull Object[] args) { for (Constructor<?> c : receiver.getDeclaredConstructors()) { if (matches(c.getParameterTypes(), args)) { return c; }//from w w w . ja v a 2s. c om } return null; }
From source file:com.github.tomakehurst.wiremock.matching.StringValuePatternJsonDeserializer.java
@SuppressWarnings("unchecked") private static Constructor<? extends StringValuePattern> findConstructor( Class<? extends StringValuePattern> clazz) { Optional<Constructor<?>> optionalConstructor = tryFind(asList(clazz.getDeclaredConstructors()), new Predicate<Constructor<?>>() { @Override//from www . ja v a 2 s . c om public boolean apply(Constructor<?> input) { return input.getParameterTypes().length == 1 && input.getGenericParameterTypes()[0].equals(String.class); } }); if (!optionalConstructor.isPresent()) { throw new IllegalStateException( "Constructor for " + clazz.getSimpleName() + " must have a single string argument constructor"); } return (Constructor<? extends StringValuePattern>) optionalConstructor.get(); }