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
49 changes: 49 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,39 @@ 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) {
}
}

// Marker type a caller might validate a resolved Class against before touching it.
public interface Plugin {
}

// A Plugin whose static initializer has an observable side effect.
public static class PluginImpl implements Plugin {

static {
pluginInitializerRan = true;
}
}

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

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

private static Stream<Arguments> numberTestParameters() {
final List<Arguments> lst = new ArrayList<>();
lst.add(Arguments.of("123", Long.valueOf("123")));
Expand All @@ -72,6 +100,27 @@ 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 testClassValidatedBeforeInitialization() throws Exception {
// The caller pattern this enables: resolve the option, gate it with isAssignableFrom, then instantiate.
// The assignability check must not run the class's static initializer; only newInstance() should.
final Class<?> cls = Converter.CLASS.apply(PluginImpl.class.getName());
assertTrue(Plugin.class.isAssignableFrom(cls));
assertFalse(pluginInitializerRan);
final Object instance = cls.getConstructor().newInstance();
assertTrue(instance instanceof Plugin);
assertTrue(pluginInitializerRan);
}

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