/** * Invoke the supplied constructor with dynamic parameter resolution. * * @param constructor the constructor to invoke and resolve parameters for * @param extensionContext the current {@code ExtensionContext} * @param extensionRegistry the {@code ExtensionRegistry} to retrieve * {@code ParameterResolvers} from */ public <T> T invoke(Constructor<T> constructor, ExtensionContext extensionContext, ExtensionRegistry extensionRegistry) { return ReflectionUtils.newInstance(constructor, resolveParameters(constructor, Optional.empty(), extensionContext, extensionRegistry)); }
@SuppressWarnings("ConstantConditions") private ArgumentsProvider instantiateArgumentsProvider(Class<? extends ArgumentsProvider> clazz) { try { return ReflectionUtils.newInstance(clazz); } catch (Exception ex) { if (ex instanceof NoSuchMethodException) { String message = String.format("Failed to find a no-argument constructor for ArgumentsProvider [%s]. " + "Please ensure that a no-argument constructor exists and " + "that the class is either a top-level class or a static nested class", clazz.getName()); throw new JUnitException(message, ex); } throw ex; } }
/** * Invoke the supplied constructor with the supplied outer instance and * dynamic parameter resolution. * * <p>This method should only be used to invoke the constructor for * an inner class. * * @param constructor the constructor to invoke and resolve parameters for * @param outerInstance the outer instance to supply as the first argument * to the constructor * @param extensionContext the current {@code ExtensionContext} * @param extensionRegistry the {@code ExtensionRegistry} to retrieve * {@code ParameterResolvers} from */ public <T> T invoke(Constructor<T> constructor, Object outerInstance, ExtensionContext extensionContext, ExtensionRegistry extensionRegistry) { return ReflectionUtils.newInstance(constructor, resolveParameters(constructor, Optional.empty(), outerInstance, extensionContext, extensionRegistry)); }
/** * Instantiate an extension of the given type using its default constructor * and register it in this registry. * * <p>A new {@link Extension} will not be registered if an extension of the * given type already exists in this registry or a parent registry. * * @param extensionType the type of extension to register */ void registerExtension(Class<? extends Extension> extensionType) { if (!isAlreadyRegistered(extensionType)) { registerExtension(ReflectionUtils.newInstance(extensionType)); this.registeredExtensionTypes.add(extensionType); } }
@Override Resolver createResolver(ParameterContext parameterContext) { try { // @formatter:off return AnnotationUtils.findAnnotation(parameterContext.getParameter(), ConvertWith.class) .map(ConvertWith::value) .map(clazz -> (ArgumentConverter) ReflectionUtils.newInstance(clazz)) .map(converter -> AnnotationConsumerInitializer.initialize(parameterContext.getParameter(), converter)) .map(Converter::new) .orElse(Converter.DEFAULT); } // @formatter:on catch (Exception ex) { throw parameterResolutionException("Error creating ArgumentConverter", ex, parameterContext); } } },
private static Function<String, Object> findFactoryExecutable(Class<?> targetType) { return factoryExecutableCache.computeIfAbsent(targetType, type -> { Method factoryMethod = findFactoryMethod(type); if (factoryMethod != null) { return source -> invokeMethod(factoryMethod, null, source); } Constructor<?> constructor = findFactoryConstructor(type); if (constructor != null) { return source -> newInstance(constructor, source); } return NULL_EXECUTABLE; }); }
private static DisplayNameGenerator getDisplayNameGenerator(Class<?> testClass) { Preconditions.notNull(testClass, "Test class must not be null"); DisplayNameGeneration generation = getDisplayNameGeneration(testClass).orElse(null); // trivial case: no user-defined generation annotation present, return default generator if (generation == null) { return standardGenerator; } // check for pre-defined generators and return matching singleton Class<? extends DisplayNameGenerator> displayNameGeneratorClass = generation.value(); if (displayNameGeneratorClass == Standard.class) { return standardGenerator; } if (displayNameGeneratorClass == ReplaceUnderscores.class) { return replaceUnderscoresGenerator; } // else: create an instance of the supplied generator implementation class and return it return ReflectionUtils.newInstance(displayNameGeneratorClass); }
@Override public String getDisplayName(int invocationIndex) { Class<? extends DataProviderTestNameFormatter> formatter = displayNameContext.getFormatter(); if (formatter == null || DataProviderPlaceholderFormatter.class.equals(formatter)) { return new DataProviderPlaceholderFormatter(displayNameContext.getFormat(), displayNameContext.getPlaceholders()).format(testMethod, invocationIndex, arguments); } return ReflectionUtils.newInstance(formatter).format(testMethod, invocationIndex, arguments); }
@Override public ParallelExecutionConfiguration createConfiguration(ConfigurationParameters configurationParameters) { String className = configurationParameters.get(CONFIG_CUSTOM_CLASS_PROPERTY_NAME).orElseThrow( () -> new JUnitException(CONFIG_CUSTOM_CLASS_PROPERTY_NAME + " must be set")); return ReflectionUtils.tryToLoadClass(className) // .andThenTry(strategyClass -> { Preconditions.condition( ParallelExecutionConfigurationStrategy.class.isAssignableFrom(strategyClass), CONFIG_CUSTOM_CLASS_PROPERTY_NAME + " does not implement " + ParallelExecutionConfigurationStrategy.class); return (ParallelExecutionConfigurationStrategy) ReflectionUtils.newInstance(strategyClass); }) // .andThenTry(strategy -> strategy.createConfiguration(configurationParameters)) // .getOrThrow(cause -> new JUnitException( "Could not create configuration for strategy class: " + className, cause)); } };