/** * Checks if the class has a default constructor. * @param clazz class to check * @return true if the class has a default constructor. */ private static boolean hasDefaultConstructor(Class<?> clazz) { boolean result = false; for (Constructor<?> constructor : clazz.getDeclaredConstructors()) { if (constructor.getParameterCount() == 0) { result = true; break; } } return result; }
/** * 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; } }
@Override public String[] getParameterNames(Constructor<?> ctor) { if (ctor.getParameterCount() == 0) { return new String[0]; } return doGetParameterNames(ctor.getParameterAnnotations()); }
/** * Check if constructor can be used for deserialization. */ private static boolean checkConstructor(Constructor<?> constructor, boolean hasParams) { return Modifier.isPublic(constructor.getModifiers()) && !constructor.isAnnotationPresent(Transient.class) && ((constructor.getParameterCount() > 0) == hasParams); }
/** * 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; } }
/** * Creates a new {@link Service} that decorates this {@link Service} with a new {@link Service} instance * of the specified {@code serviceType}. The specified {@link Class} must have a single-parameter * constructor which accepts this {@link Service}. */ default <R extends Service<?, ?>> R decorate(Class<R> serviceType) { requireNonNull(serviceType, "serviceType"); Constructor<?> constructor = null; for (Constructor<?> c : serviceType.getConstructors()) { if (c.getParameterCount() != 1) { continue; } if (c.getParameterTypes()[0].isAssignableFrom(getClass())) { constructor = c; break; } } if (constructor == null) { throw new IllegalArgumentException("cannot find a matching constructor: " + serviceType.getName()); } try { return (R) constructor.newInstance(this); } catch (Exception e) { throw new IllegalStateException("failed to instantiate: " + serviceType.getName(), e); } }
public static Object newInstance(Class<?> klazz) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (Enum.class.isAssignableFrom(klazz)) { return newEnumInstance(klazz); } Object newInstance; try { // Try it without any funny business. newInstance = klazz.newInstance(); } catch (InstantiationException | IllegalAccessException e) { if (log.isTraceEnabled()) { log.trace("{} has no default constructor, picking another", klazz); } // Alright, we'll try it the hard way. Constructor<?>[] ctors = klazz.getDeclaredConstructors(); Constructor<?> ctor = ctors[0]; ctor.setAccessible(true); // the little games we play, java... Object[] args = new Object[ctor.getParameterCount()]; Class<?>[] parameterTypes = ctor.getParameterTypes(); for (int i = 0; i < args.length; i++) { Class<?> parameterType = parameterTypes[i]; args[i] = Defaults.defaultValue(parameterType); } newInstance = ctor.newInstance(args); } return newInstance; }
private Mono<?> constructAttribute(Constructor<?> ctor, String attributeName, BindingContext context, ServerWebExchange exchange) { if (ctor.getParameterCount() == 0) {
/** * Looks at whether the DefaultInstantiator resolving a valid constructor does not met this case: * - No constructor with @Inject annotation is defined * - NoArgs constructor is defined * - Instantiator ignores JAX-RS valid constructor with multiple params * * @param it injection target containing instantiator with resolved constructor. * @param clazz class which analyzed constructor belongs to. * @param <T> type of the analyzed class. * @return {@code true} if no-arg constructor was selected while multi-params constructor exists. */ private static <T> boolean isNoArgConstructorCase(BasicInjectionTarget<T> it, Class<T> clazz) { if (!(it instanceof NonProducibleInjectionTarget)) { Instantiator<T> instantiator = it.getInstantiator(); Constructor<T> constructor = instantiator.getConstructor(); return constructor.getParameterCount() == 0 && clazz.getConstructors().length > 1; } return false; }
if ( ctor.getParameterTypes() != null && ctor.getParameterCount() == 1 ) { if ( ViolatedConstraintNameExtracter.class.isAssignableFrom( ctor.getParameterTypes()[0] ) ) { try {
private Optional<MethodHandle> getConstructor(Method method, Optional<Constructor<?>> optionalConstructor) { if (isStatic(method.getModifiers())) { return Optional.empty(); } checkArgument(optionalConstructor.isPresent(), "Method [%s] is an instance method. It must be in a class annotated with @ScalarFunction, and the class is required to have a public constructor.", method); Constructor<?> constructor = optionalConstructor.get(); Set<TypeParameter> constructorTypeParameters = Stream.of(constructor.getAnnotationsByType(TypeParameter.class)) .collect(ImmutableSet.toImmutableSet()); checkArgument(constructorTypeParameters.containsAll(typeParameters), "Method [%s] is an instance method and requires a public constructor containing all type parameters: %s", method, typeParameters); for (int i = 0; i < constructor.getParameterCount(); i++) { Annotation[] annotations = constructor.getParameterAnnotations()[i]; checkArgument(containsImplementationDependencyAnnotation(annotations), "Constructors may only have meta parameters [%s]", constructor); checkArgument(annotations.length == 1, "Meta parameters may only have a single annotation [%s]", constructor); Annotation annotation = annotations[0]; if (annotation instanceof TypeParameter) { checkTypeParameters(parseTypeSignature(((TypeParameter) annotation).value()), typeParameterNames, method); } constructorDependencies.add(createDependency(annotation, literalParameters)); } MethodHandle result = constructorMethodHandle(FUNCTION_IMPLEMENTATION_ERROR, constructor); // Change type of return value to Object to make sure callers won't have classloader issues return Optional.of(result.asType(result.type().changeReturnType(Object.class))); }
else if (candidate.getParameterCount() == 0) { defaultConstructor = candidate; else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) { candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
for (final Constructor<T> constructor : constructors) { if (constructor.getParameterCount() == 0 && candidate == null) { candidate = new SimpleImmutableEntry<>(constructor, ImmutableList.of()); continue;
} else if (c.getParameterCount() == 0) { def = c;
Constructor<?> constructor = null; for (Constructor<?> c : clazz.getDeclaredConstructors()) { if (c.getParameterCount() > 0) { constructor = c; break;
int c1pl = c1.getParameterCount(); int c2pl = c2.getParameterCount(); return (c1pl < c2pl ? -1 : (c1pl > c2pl ? 1 : 0)); });
final Object[] params = new Object[ctor.getParameterCount()];
private List<FrameworkMethod> getParametersMethods() throws Exception { List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods( Parameterized.Parameters.class); SortedMap<Integer, FrameworkMethod> sortedMethods = new TreeMap<Integer, FrameworkMethod>(); for (FrameworkMethod each : methods) { if (each.isPublic()) { if (!each.isStatic()) { if (getTestClass().getOnlyConstructor().getParameterCount() != 0) { throw new Exception("Method " + each.getMethod() + " is annotated with @Parameters, it is not static and there is no parameter-less constructor!"); } } Order order = each.getAnnotation(Order.class); int value = order == null ? 0 : order.value(); FrameworkMethod prev = sortedMethods.put(value, each); if (prev != null) { throw new Exception(String.format("There are more methods annotated with @Parameters and @Order(value=%d): %s (%s) and %s (%s)", value, prev.getMethod(), prev.getAnnotation(Order.class), each.getMethod(), order)); } } else { throw new Exception("Method " + each.getMethod() + " is annotated with @Parameters but it is not public!"); } } if (sortedMethods.isEmpty()) { throw new Exception("No public static parameters method on class " + getTestClass().getName()); } return new ArrayList<FrameworkMethod>(sortedMethods.values()); }
if (ctor.getParameterCount() == 0) {
if (uniqueCandidate.getParameterCount() == 0) { synchronized (mbd.constructorArgumentLock) { mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;