private static Class<?> toClass(String type) { //@formatter:off return ReflectionUtils .tryToLoadClass(type) .getOrThrow(cause -> new ArgumentConversionException( "Failed to convert String \"" + type + "\" to type " + Class.class.getName(), cause)); //@formatter:on }
private Class<?> loadRequiredClass(String className) { return ReflectionUtils.tryToLoadClass(className).getOrThrow( cause -> new JUnitException(format("Could not load class [%s]", className), cause)); }
private void lazyLoadJavaClass() { if (this.javaClass == null) { // @formatter:off this.javaClass = ReflectionUtils.tryToLoadClass(this.className).getOrThrow( cause -> new PreconditionViolationException("Could not load class with name: " + this.className, cause)); // @formatter:on } }
/** * Get the selected {@link Class}. * * <p>If the {@link Class} was not provided, but only the name, this method * attempts to lazily load the {@link Class} based on its name and throws a * {@link PreconditionViolationException} if the class cannot be loaded. */ public Class<?> getJavaClass() { if (this.javaClass == null) { this.javaClass = ReflectionUtils.tryToLoadClass(this.className).getOrThrow( cause -> new PreconditionViolationException("Could not load class with name: " + this.className, cause)); } return this.javaClass; }
/** * Get the {@linkplain Class Java class} of this source. * * <p>If the {@link Class} was not provided, but only the name, this method * attempts to lazily load the {@link Class} based on its name and throws a * {@link PreconditionViolationException} if the class cannot be loaded. * * @see #getClassName() * @see #getPosition() */ public final Class<?> getJavaClass() { if (this.javaClass == null) { this.javaClass = ReflectionUtils.tryToLoadClass(this.className).getOrThrow( cause -> new PreconditionViolationException("Could not load class with name: " + this.className, cause)); } return this.javaClass; }
@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)); } };
private ForkJoinPool createForkJoinPool(ConfigurationParameters configurationParameters) { ParallelExecutionConfigurationStrategy strategy = DefaultParallelExecutionConfigurationStrategy.getStrategy( configurationParameters); ParallelExecutionConfiguration configuration = strategy.createConfiguration(configurationParameters); ForkJoinWorkerThreadFactory threadFactory = new WorkerThreadFactory(); return Try.call(() -> { // Try to use constructor available in Java >= 9 Constructor<ForkJoinPool> constructor = ForkJoinPool.class.getDeclaredConstructor(Integer.TYPE, ForkJoinWorkerThreadFactory.class, UncaughtExceptionHandler.class, Boolean.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Predicate.class, Long.TYPE, TimeUnit.class); return constructor.newInstance(configuration.getParallelism(), threadFactory, null, false, configuration.getCorePoolSize(), configuration.getMaxPoolSize(), configuration.getMinimumRunnable(), null, configuration.getKeepAliveSeconds(), TimeUnit.SECONDS); }).orElseTry(() -> { // Fallback for Java 8 return new ForkJoinPool(configuration.getParallelism(), threadFactory, null, false); }).getOrThrow(cause -> new JUnitException("Failed to create ForkJoinPool", cause)); }