Skip to content

Commit

Permalink
Change source type to String
Browse files Browse the repository at this point in the history
  • Loading branch information
juliette-derancourt committed Nov 8, 2023
1 parent 1e04b62 commit e2d664c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.platform.commons.support.ConversionException;
import org.junit.platform.commons.support.StringConversionSupport;
import org.junit.platform.commons.util.ClassLoaderUtils;
import org.junit.platform.commons.util.ReflectionUtils;

/**
* {@code DefaultArgumentConverter} is the default implementation of the
Expand Down Expand Up @@ -61,15 +62,32 @@ public final Object convert(Object source, ParameterContext context) {
}

public final Object convert(Object source, Class<?> targetType, ParameterContext context) {
Class<?> declaringClass = context.getDeclaringExecutable().getDeclaringClass();
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(declaringClass);
if (source == null) {
if (targetType.isPrimitive()) {
throw new ArgumentConversionException(
"Cannot convert null to primitive value of type " + targetType.getTypeName());
}
return null;
}

try {
return StringConversionSupport.convert(source, targetType, classLoader);
if (ReflectionUtils.isAssignableTo(source, targetType)) {
return source;
}
catch (ConversionException ex) {
throw new ArgumentConversionException(ex.getMessage(), ex);

if (source instanceof String) {
Class<?> declaringClass = context.getDeclaringExecutable().getDeclaringClass();
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(declaringClass);
try {
return StringConversionSupport.convert((String) source, targetType, classLoader);
}
catch (ConversionException ex) {
throw new ArgumentConversionException(ex.getMessage(), ex);
}
}

throw new ArgumentConversionException(
String.format("No built-in converter for source type %s and target type %s",
source.getClass().getTypeName(), targetType.getTypeName()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import org.apiguardian.api.API;
import org.junit.platform.commons.util.ClassLoaderUtils;
import org.junit.platform.commons.util.ReflectionUtils;

/**
* {@code StringConversionSupport} is able to convert from strings to a number
Expand Down Expand Up @@ -60,7 +59,7 @@ private StringConversionSupport() {
}

@SuppressWarnings("unchecked")
public static <T> T convert(Object source, Class<T> targetType, ClassLoader classLoader) {
public static <T> T convert(String source, Class<T> targetType, ClassLoader classLoader) {
if (source == null) {
if (targetType.isPrimitive()) {
throw new ConversionException(
Expand All @@ -69,33 +68,32 @@ public static <T> T convert(Object source, Class<T> targetType, ClassLoader clas
return null;
}

if (ReflectionUtils.isAssignableTo(source, targetType)) {
if (String.class.equals(targetType)) {
return (T) source;
}

if (source instanceof String) {
Class<?> targetTypeToUse = toWrapperType(targetType);
Optional<StringToObjectConverter> converter = stringToObjectConverters.stream().filter(
candidate -> candidate.canConvert(targetTypeToUse)).findFirst();
if (converter.isPresent()) {
try {
ClassLoader classLoaderToUse = Optional.ofNullable(classLoader) //
.orElseGet(ClassLoaderUtils::getDefaultClassLoader);
return (T) converter.get().convert((String) source, targetTypeToUse, classLoaderToUse);
}
catch (Exception ex) {
if (ex instanceof ConversionException) {
// simply rethrow it
throw (ConversionException) ex;
}
// else
throw new ConversionException(
"Failed to convert String \"" + source + "\" to type " + targetType.getTypeName(), ex);
Class<?> targetTypeToUse = toWrapperType(targetType);
Optional<StringToObjectConverter> converter = stringToObjectConverters.stream().filter(
candidate -> candidate.canConvert(targetTypeToUse)).findFirst();
if (converter.isPresent()) {
try {
ClassLoader classLoaderToUse = Optional.ofNullable(classLoader) //
.orElseGet(ClassLoaderUtils::getDefaultClassLoader);
return (T) converter.get().convert(source, targetTypeToUse, classLoaderToUse);
}
catch (Exception ex) {
if (ex instanceof ConversionException) {
// simply rethrow it
throw (ConversionException) ex;
}
// else
throw new ConversionException(
String.format("Failed to convert String \"%s\" to type %s", source, targetType.getTypeName()), ex);
}
}
throw new ConversionException(String.format("No built-in converter for source type %s and target type %s",
source.getClass().getTypeName(), targetType.getTypeName()));

throw new ConversionException(
"No built-in converter for source type java.lang.String and target type " + targetType.getTypeName());
}

private static Class<?> toWrapperType(Class<?> targetType) {
Expand Down

0 comments on commit e2d664c

Please sign in to comment.