java.lang.reflect.Constructor.getParameterCount()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(93)

本文整理了Java中java.lang.reflect.Constructor.getParameterCount()方法的一些代码示例,展示了Constructor.getParameterCount()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Constructor.getParameterCount()方法的具体详情如下:
包路径:java.lang.reflect.Constructor
类名称:Constructor
方法名:getParameterCount

Constructor.getParameterCount介绍

暂无

代码示例

代码示例来源:origin: checkstyle/checkstyle

/**
 * 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;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * 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;
  }
}

代码示例来源:origin: lettuce-io/lettuce-core

@Override
public String[] getParameterNames(Constructor<?> ctor) {
  if (ctor.getParameterCount() == 0) {
    return new String[0];
  }
  return doGetParameterNames(ctor.getParameterAnnotations());
}

代码示例来源:origin: oracle/helidon

/**
 * 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);
}

代码示例来源:origin: org.springframework/spring-beans

/**
 * 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;
  }
}

代码示例来源:origin: line/armeria

/**
 * 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);
  }
}

代码示例来源:origin: CalebFenton/simplify

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;
}

代码示例来源:origin: spring-projects/spring-framework

private Mono<?> constructAttribute(Constructor<?> ctor, String attributeName,
    BindingContext context, ServerWebExchange exchange) {
  if (ctor.getParameterCount() == 0) {

代码示例来源:origin: jersey/jersey

/**
 * 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;
}

代码示例来源:origin: hibernate/hibernate-orm

if ( ctor.getParameterTypes() != null && ctor.getParameterCount() == 1 ) {
  if ( ViolatedConstraintNameExtracter.class.isAssignableFrom( ctor.getParameterTypes()[0] ) ) {
    try {

代码示例来源:origin: prestodb/presto

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)));
}

代码示例来源:origin: spring-projects/spring-framework

else if (candidate.getParameterCount() == 0) {
    defaultConstructor = candidate;
else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
  candidateConstructors = new Constructor<?>[] {rawCandidates[0]};

代码示例来源:origin: line/armeria

for (final Constructor<T> constructor : constructors) {
  if (constructor.getParameterCount() == 0 && candidate == null) {
    candidate = new SimpleImmutableEntry<>(constructor, ImmutableList.of());
    continue;

代码示例来源:origin: jooby-project/jooby

} else if (c.getParameterCount() == 0) {
 def = c;

代码示例来源:origin: apache/hbase

Constructor<?> constructor = null;
for (Constructor<?> c : clazz.getDeclaredConstructors()) {
 if (c.getParameterCount() > 0) {
  constructor = c;
  break;

代码示例来源:origin: spring-projects/spring-framework

int c1pl = c1.getParameterCount();
  int c2pl = c2.getParameterCount();
  return (c1pl < c2pl ? -1 : (c1pl > c2pl ? 1 : 0));
});

代码示例来源:origin: robolectric/robolectric

final Object[] params = new Object[ctor.getParameterCount()];

代码示例来源:origin: hibernate/hibernate-orm

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());
}

代码示例来源:origin: spring-projects/spring-framework

if (ctor.getParameterCount() == 0) {

代码示例来源:origin: spring-projects/spring-framework

if (uniqueCandidate.getParameterCount() == 0) {
  synchronized (mbd.constructorArgumentLock) {
    mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;

相关文章

微信公众号

最新文章

更多