Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/org/apache/commons/cli/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ public interface Converter<T, E extends Exception> {
Converter<?, RuntimeException> DEFAULT = s -> s;

/**
* Converts a String to a {@link Class}. Calls {@link Class#forName(String)}.
* Converts a String to a {@link Class}. Calls {@link Class#forName(String, boolean, ClassLoader)} with {@code initialize} set to {@code false} so that
* naming a class does not run its static initializer.
*/
Converter<Class<?>, ClassNotFoundException> CLASS = Class::forName;
Converter<Class<?>, ClassNotFoundException> CLASS = s -> Class.forName(s, false, Converter.class.getClassLoader());

/**
* Converts a String to a {@link File}. Calls {@link File#File(String)}.
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/apache/commons/cli/ConverterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more
package org.apache.commons.cli;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URL;
import java.text.DateFormat;
Expand All @@ -41,13 +43,24 @@ Licensed to the Apache Software Foundation (ASF) under one or more
*/
public class ConverterTests {

// A class whose static initializer has an observable side effect.
public static class AClassWithAStaticInitializer {

static {
classInitializerRan = true;
}
}

// A class without a default constructor.
public class AClassWithoutADefaultConstructor {

public AClassWithoutADefaultConstructor(final int i) {
}
}

// Set by the static initializer of AClassWithAStaticInitializer; readable without initializing that class.
private static boolean classInitializerRan;

private static Stream<Arguments> numberTestParameters() {
final List<Arguments> lst = new ArrayList<>();
lst.add(Arguments.of("123", Long.valueOf("123")));
Expand All @@ -72,6 +85,15 @@ void testClass() throws Exception {
assertNotNull(Converter.CLASS.apply(AClassWithoutADefaultConstructor.class.getName()));
}

@Test
void testClassDoesNotInitialize() throws Exception {
final Class<?> cls = Converter.CLASS.apply(AClassWithAStaticInitializer.class.getName());
assertFalse(classInitializerRan);
assertEquals(AClassWithAStaticInitializer.class, cls);
cls.getConstructor().newInstance();
assertTrue(classInitializerRan);
}

@Test
void testDate() throws Exception {
assertThrows(java.text.ParseException.class, () -> Converter.DATE.apply("whatever"));
Expand Down
Loading