private static Constructor<?> getEndpointConstructor() { for (Constructor<?> current : TyrusEndpointWrapper.class.getConstructors()) { Class<?>[] types = current.getParameterTypes(); if (Endpoint.class == types[0] && EndpointConfig.class == types[1]) { return current; } } throw new IllegalStateException("No compatible Tyrus version found"); }
public MapDeserializer(Class type) { if (type == null) type = HashMap.class; _type = type; Constructor[] ctors = type.getConstructors(); for (int i = 0; i < ctors.length; i++) { if (ctors[i].getParameterTypes().length == 0) _ctor = ctors[i]; } if (_ctor == null) { try { _ctor = HashMap.class.getConstructor(new Class[0]); } catch (Exception e) { throw new IllegalStateException(e); } } }
public static Optional<Constructor<?>> findConstructor(Class<?> clazz) { Constructor<?>[] constructors = clazz.getConstructors(); checkArgument(constructors.length <= 1, "Class [%s] must have no more than 1 public constructor"); if (constructors.length == 0) { return Optional.empty(); } return Optional.of(constructors[0]); }
private ParameterSupplier buildParameterSupplierFromClass( Class<? extends ParameterSupplier> cls) throws Exception { Constructor<?>[] supplierConstructors = cls.getConstructors(); for (Constructor<?> constructor : supplierConstructors) { Class<?>[] parameterTypes = constructor.getParameterTypes(); if (parameterTypes.length == 1 && parameterTypes[0].equals(TestClass.class)) { return (ParameterSupplier) constructor.newInstance(clazz); } } return cls.newInstance(); }
public static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType) throws NoSuchMethodException { Constructor<?> targetConstructor; try { targetConstructor = clazz.getConstructor(new Class<?>[]{paramType}); } catch (NoSuchMethodException e) { targetConstructor = null; Constructor<?>[] constructors = clazz.getConstructors(); for (Constructor<?> constructor : constructors) { if (Modifier.isPublic(constructor.getModifiers()) && constructor.getParameterTypes().length == 1 && constructor.getParameterTypes()[0].isAssignableFrom(paramType)) { targetConstructor = constructor; break; } } if (targetConstructor == null) { throw e; } } return targetConstructor; }
/** Returns an array of {@link Constructor} containing the public constructors of the class represented by the supplied Class. */ static public Constructor[] getConstructors (Class c) { java.lang.reflect.Constructor[] constructors = c.getConstructors(); Constructor[] result = new Constructor[constructors.length]; for (int i = 0, j = constructors.length; i < j; i++) { result[i] = new Constructor(constructors[i]); } return result; }
/** Returns an array of {@link Constructor} containing the public constructors of the class represented by the supplied Class. */ static public Constructor[] getConstructors (Class c) { java.lang.reflect.Constructor[] constructors = c.getConstructors(); Constructor[] result = new Constructor[constructors.length]; for (int i = 0, j = constructors.length; i < j; i++) { result[i] = new Constructor(constructors[i]); } return result; }
public static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType) throws NoSuchMethodException { Constructor<?> targetConstructor; try { targetConstructor = clazz.getConstructor(new Class<?>[]{paramType}); } catch (NoSuchMethodException e) { targetConstructor = null; Constructor<?>[] constructors = clazz.getConstructors(); for (Constructor<?> constructor : constructors) { if (Modifier.isPublic(constructor.getModifiers()) && constructor.getParameterTypes().length == 1 && constructor.getParameterTypes()[0].isAssignableFrom(paramType)) { targetConstructor = constructor; break; } } if (targetConstructor == null) { throw e; } } return targetConstructor; }
private void validateParameterSupplier(Class<? extends ParameterSupplier> supplierClass, List<Throwable> errors) { Constructor<?>[] constructors = supplierClass.getConstructors(); if (constructors.length != 1) { errors.add(new Error("ParameterSupplier " + supplierClass.getName() + " must have only one constructor (either empty or taking only a TestClass)")); } else { Class<?>[] paramTypes = constructors[0].getParameterTypes(); if (!(paramTypes.length == 0) && !paramTypes[0].equals(TestClass.class)) { errors.add(new Error("ParameterSupplier " + supplierClass.getName() + " constructor must take either nothing or a single TestClass instance")); } } }
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { javaClass = getClassObject(in.readUTF()); constructors = javaClass.getConstructors(); methods = null; }
/** * Returns the only public constructor in the class, or throws an {@code * AssertionError} if there are more or less than one. */ public Constructor<?> getOnlyConstructor() { Constructor<?>[] constructors = clazz.getConstructors(); Assert.assertEquals(1, constructors.length); return constructors[0]; }
public TestRunner(Context context, TestParams testParams, EventBus controlBus) { this.controlBus = controlBus; tests = new ArrayList<Test>(); for (Class<? extends Test> testClazz : testParams.getTestClasses()) { try { Constructor<?>[] constructors = testClazz.getConstructors(); Constructor<? extends Test> constructor = testClazz.getConstructor(Context.class, TestParams.class); Test test = constructor.newInstance(context, testParams); tests.add(test); } catch (Exception e) { throw new RuntimeException(e); } } }
/** * Returns the only public constructor in the class, or throws an {@code * AssertionError} if there are more or less than one. */ public Constructor<?> getOnlyConstructor() { Constructor<?>[] constructors = fClass.getConstructors(); Assert.assertEquals(1, constructors.length); return constructors[0]; }
public static AssertionError createArgumentsAreDifferentException(String message, String wanted, String actual) { try { Class<?> clazz = Class.forName("org.mockito.exceptions.verification.junit.ArgumentsAreDifferent"); AssertionError throwable = (AssertionError) clazz.getConstructors()[0].newInstance(message, wanted, actual); return throwable; } catch (Throwable t) { // throw the default exception in case of problems return new ArgumentsAreDifferent(message); } } }
@Override @Nullable public Constructor<?>[] getPreferredConstructors() { Class<?> clazz = getBeanClass(); Constructor<?> primaryCtor = BeanUtils.findPrimaryConstructor(clazz); if (primaryCtor != null) { return new Constructor<?>[] {primaryCtor}; } Constructor<?>[] publicCtors = clazz.getConstructors(); if (publicCtors.length > 0) { return publicCtors; } return null; }
private Mono<?> createAttribute( String attributeName, Class<?> clazz, BindingContext context, ServerWebExchange exchange) { Constructor<?> ctor = BeanUtils.findPrimaryConstructor(clazz); if (ctor == null) { Constructor<?>[] ctors = clazz.getConstructors(); if (ctors.length == 1) { ctor = ctors[0]; } else { try { ctor = clazz.getDeclaredConstructor(); } catch (NoSuchMethodException ex) { throw new IllegalStateException("No primary or default constructor found for " + clazz, ex); } } } return constructAttribute(ctor, attributeName, context, exchange); }
private static <X extends Exception> X newWithCause(Class<X> exceptionClass, Throwable cause) { // getConstructors() guarantees this as long as we don't modify the array. @SuppressWarnings({"unchecked", "rawtypes"}) List<Constructor<X>> constructors = (List) Arrays.asList(exceptionClass.getConstructors()); for (Constructor<X> constructor : preferringStrings(constructors)) { @Nullable X instance = newFromConstructor(constructor, cause); if (instance != null) { if (instance.getCause() == null) { instance.initCause(cause); } return instance; } } throw new IllegalArgumentException( "No appropriate constructor for exception of type " + exceptionClass + " in response to chained exception", cause); }
private void checkConstructorPublic(Class<T> clz) { Constructor<?>[] constructors = clz.getConstructors(); if (constructors == null || constructors.length == 0) { failThrows(clz, "Error has no public no-args constructor"); } for (Constructor<?> constructor : constructors) { if (Modifier.isPublic(constructor.getModifiers()) && constructor.getParameterTypes().length == 0) { return; } } failThrows(clz, "Error has no public no-args constructor"); }
public BeanDeserializer(Class cl) { _type = cl; _methodMap = getMethodMap(cl); _readResolve = getReadResolve(cl); Constructor[] constructors = cl.getConstructors(); int bestLength = Integer.MAX_VALUE; for (int i = 0; i < constructors.length; i++) { if (constructors[i].getParameterTypes().length < bestLength) { _constructor = constructors[i]; bestLength = _constructor.getParameterTypes().length; } } if (_constructor != null) { _constructor.setAccessible(true); Class[] params = _constructor.getParameterTypes(); _constructorArgs = new Object[params.length]; for (int i = 0; i < params.length; i++) { _constructorArgs[i] = getParamArg(params[i]); } } }
/** * Return the resolved autowire code, * (resolving AUTOWIRE_AUTODETECT to AUTOWIRE_CONSTRUCTOR or AUTOWIRE_BY_TYPE). * @see #AUTOWIRE_AUTODETECT * @see #AUTOWIRE_CONSTRUCTOR * @see #AUTOWIRE_BY_TYPE */ public int getResolvedAutowireMode() { if (this.autowireMode == AUTOWIRE_AUTODETECT) { // Work out whether to apply setter autowiring or constructor autowiring. // If it has a no-arg constructor it's deemed to be setter autowiring, // otherwise we'll try constructor autowiring. Constructor<?>[] constructors = getBeanClass().getConstructors(); for (Constructor<?> constructor : constructors) { if (constructor.getParameterCount() == 0) { return AUTOWIRE_BY_TYPE; } } return AUTOWIRE_CONSTRUCTOR; } else { return this.autowireMode; } }