org.powermock.reflect.internal.WhiteboxImpl.getAllConstructors()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(4.2k)|赞(0)|评价(0)|浏览(137)

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

WhiteboxImpl.getAllConstructors介绍

[英]Get all declared constructors in the class and set accessible to true.
[中]获取类中所有声明的构造函数,并将其设置为可访问true

代码示例

代码示例来源:origin: org.powermock.api/powermock-api-support

/**
 * Get all constructors in the supplied class(es).
 * 
 * @param cls
 *            The class whose constructors to get.
 * @param additionalClasses
 *            Additional classes whose constructors to get.
 * @return All constructors declared in this class.
 */
public static Constructor<?>[] constructorsDeclaredIn(final Class<?> cls, final Class<?>... additionalClasses) {
  if (cls == null) {
    throw new IllegalArgumentException("You need to supply at least one class.");
  }
  Set<Constructor<?>> constructors = new HashSet<Constructor<?>>();
  constructors.addAll(asList(WhiteboxImpl.getAllConstructors(cls)));
  for (Class<?> klass : additionalClasses) {
    constructors.addAll(asList(WhiteboxImpl.getAllConstructors(klass)));
  }
  return constructors.toArray(new Constructor[constructors.size()]);
}

代码示例来源:origin: org.powermock/powermock-api-support

/**
 * Get all constructors in the supplied class(es).
 * 
 * @param cls
 *            The class whose constructors to get.
 * @param additionalClasses
 *            Additional classes whose constructors to get.
 * @return All constructors declared in this class.
 */
public static Constructor<?>[] constructorsDeclaredIn(final Class<?> cls, final Class<?>... additionalClasses) {
  if (cls == null) {
    throw new IllegalArgumentException("You need to supply at least one class.");
  }
  Set<Constructor<?>> constructors = new HashSet<Constructor<?>>();
  constructors.addAll(asList(WhiteboxImpl.getAllConstructors(cls)));
  for (Class<?> klass : additionalClasses) {
    constructors.addAll(asList(WhiteboxImpl.getAllConstructors(klass)));
  }
  return constructors.toArray(new Constructor[constructors.size()]);
}

代码示例来源:origin: org.powermock/powermock-api-mockito2

@Override
public OngoingStubbing<T> withAnyArguments() throws Exception {
  if (mockType == null) {
    throw new IllegalArgumentException("Class to expected cannot be null");
  }
  final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getOriginalUnmockedType(mockType);
  final Constructor<?>[] allConstructors = WhiteboxImpl.getAllConstructors(unmockedType);
  final Constructor<?> constructor = allConstructors[0];
  final Class<?>[] parameterTypes = constructor.getParameterTypes();
  Object[] paramArgs = new Object[parameterTypes.length];
  for (int i = 0; i < parameterTypes.length; i++) {
    Class<?> paramType = parameterTypes[i];
    paramArgs[i] = createParamArgMatcher(paramType);
  }
  Constructor<?>[] otherCtors = new Constructor<?>[allConstructors.length - 1];
  System.arraycopy(allConstructors, 1, otherCtors, 0, allConstructors.length - 1);
  
  final OngoingStubbing<T> ongoingStubbing = createNewSubstituteMock(mockType, parameterTypes, paramArgs);
  return new DelegatingToConstructorsOngoingStubbing<T>(otherCtors, ongoingStubbing);
}

代码示例来源:origin: org.powermock/powermock-api-mockito-common

@Override
public OngoingStubbing<T> withAnyArguments() throws Exception {
  if (mockType == null) {
    throw new IllegalArgumentException("Class to expected cannot be null");
  }
  final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getOriginalUnmockedType(mockType);
  final Constructor<?>[] allConstructors = WhiteboxImpl.getAllConstructors(unmockedType);
  final Constructor<?> constructor = allConstructors[0];
  final Class<?>[] parameterTypes = constructor.getParameterTypes();
  Object[] paramArgs = new Object[parameterTypes.length];
  for (int i = 0; i < parameterTypes.length; i++) {
    Class<?> paramType = parameterTypes[i];
    paramArgs[i] = createParamArgMatcher(paramType);
  }
  final OngoingStubbing<T> ongoingStubbing = createNewSubstituteMock(mockType, parameterTypes, paramArgs);
  Constructor<?>[] otherCtors = new Constructor<?>[allConstructors.length - 1];
  System.arraycopy(allConstructors, 1, otherCtors, 0, allConstructors.length - 1);
  return new DelegatingToConstructorsOngoingStubbing<T>(otherCtors, ongoingStubbing);
}

相关文章

WhiteboxImpl类方法