Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Apr 10, 2024
1 parent ca2b3c1 commit 39cd316
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ class CglibAopProxy implements AopProxy, Serializable {
private static final int INVOKE_HASHCODE = 6;


private static final String COROUTINES_FLOW_CLASS_NAME = "kotlinx.coroutines.flow.Flow";

private static final boolean coroutinesReactorPresent = ClassUtils.isPresent(
"kotlinx.coroutines.reactor.MonoKt", CglibAopProxy.class.getClassLoader());

/** Logger available to subclasses; static to optimize serialization. */
protected static final Log logger = LogFactory.getLog(CglibAopProxy.class);

/** Keeps track of the Classes that we have validated for final methods. */
private static final Map<Class<?>, Boolean> validatedClasses = new WeakHashMap<>();

private static final String COROUTINES_FLOW_CLASS_NAME = "kotlinx.coroutines.flow.Flow";

private static final boolean coroutinesReactorPresent = ClassUtils.isPresent("kotlinx.coroutines.reactor.MonoKt",
CglibAopProxy.class.getClassLoader());;


/** The configuration used to configure this proxy. */
protected final AdvisedSupport advised;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializa

private static final String COROUTINES_FLOW_CLASS_NAME = "kotlinx.coroutines.flow.Flow";

private static final boolean coroutinesReactorPresent = ClassUtils.isPresent("kotlinx.coroutines.reactor.MonoKt",
JdkDynamicAopProxy.class.getClassLoader());;
private static final boolean coroutinesReactorPresent = ClassUtils.isPresent(
"kotlinx.coroutines.reactor.MonoKt", JdkDynamicAopProxy.class.getClassLoader());

/** We use a static Log to avoid serialization issues. */
private static final Log logger = LogFactory.getLog(JdkDynamicAopProxy.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@
*/
public abstract class AopUtils {

private static final boolean coroutinesReactorPresent = ClassUtils.isPresent("kotlinx.coroutines.reactor.MonoKt",
AopUtils.class.getClassLoader());;
private static final boolean coroutinesReactorPresent = ClassUtils.isPresent(
"kotlinx.coroutines.reactor.MonoKt", AopUtils.class.getClassLoader());


/**
* Check whether the given object is a JDK dynamic proxy or a CGLIB proxy.
Expand Down Expand Up @@ -349,8 +350,8 @@ public static Object invokeJoinpointUsingReflection(@Nullable Object target, Met
// Use reflection to invoke the method.
try {
ReflectionUtils.makeAccessible(method);
return coroutinesReactorPresent && KotlinDetector.isSuspendingFunction(method) ?
KotlinDelegate.invokeSuspendingFunction(method, target, args) : method.invoke(target, args);
return (coroutinesReactorPresent && KotlinDetector.isSuspendingFunction(method) ?
KotlinDelegate.invokeSuspendingFunction(method, target, args) : method.invoke(target, args));
}
catch (InvocationTargetException ex) {
// Invoked method threw a checked exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
*/
class KotlinReflectionBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {

@Nullable
@Override
@Nullable
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
Class<?> beanClass = registeredBean.getBeanClass();
if (KotlinDetector.isKotlinType(beanClass)) {
Expand All @@ -45,6 +45,7 @@ public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registe
return null;
}


private static class AotContribution implements BeanRegistrationAotContribution {

private final Class<?> beanClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ void getHeaders() {
assertThat(headers).as("No HttpHeaders returned").isNotNull();
assertThat(headers.containsKey(headerName)).as("Invalid headers returned").isTrue();
List<String> headerValues = headers.get(headerName);
assertThat(headerValues).as("No header values returned").isNotNull();
assertThat(headerValues.size()).as("Invalid header values returned").isEqualTo(2);
assertThat(headerValues.contains(headerValue1)).as("Invalid header values returned").isTrue();
assertThat(headerValues.contains(headerValue2)).as("Invalid header values returned").isTrue();
Expand All @@ -150,7 +151,7 @@ void getHeadersWithEmptyContentTypeAndEncoding() {
assertThat(headers.getContentType()).isNull();
}

@Test // gh-27957
@Test // gh-27957
void getHeadersWithWildcardContentType() {
mockRequest.setContentType("*/*");
mockRequest.removeHeader("Content-Type");
Expand All @@ -166,7 +167,7 @@ void getBody() throws IOException {
assertThat(result).as("Invalid content returned").isEqualTo(content);
}

@Test // gh-13318
@Test // gh-13318
void getFormBody() throws IOException {
mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
mockRequest.setMethod("POST");
Expand All @@ -189,7 +190,7 @@ void getEmptyFormBody() throws IOException {
assertThat(result).as("Invalid content returned").isEqualTo(content);
}

@Test // gh-31327
@Test // gh-31327
void getFormBodyWhenQueryParamsAlsoPresent() throws IOException {
mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
mockRequest.setMethod("POST");
Expand All @@ -203,7 +204,7 @@ void getFormBodyWhenQueryParamsAlsoPresent() throws IOException {
assertThat(result).as("Invalid content returned").isEqualTo(content);
}

@Test // gh-32471
@Test // gh-32471
void getFormBodyWhenNotEncodedCharactersPresent() throws IOException {
mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
mockRequest.setMethod("POST");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ void resolveString() throws Exception {

MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof String;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
}

Expand All @@ -162,22 +160,18 @@ void resolveStringArray() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof String[];
assertThat(condition).isTrue();
assertThat((String[]) result).as("Invalid result").isEqualTo(expected);
assertThat(result).as("Invalid result").isEqualTo(expected);
}

@Test // gh-32577
@Test // gh-32577
void resolveStringArrayWithEmptyArraySuffix() throws Exception {
String[] expected = new String[] {"foo", "bar"};
request.addParameter("name[]", expected[0]);
request.addParameter("name[]", expected[1]);

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof String[];
assertThat(condition).isTrue();
assertThat((String[]) result).isEqualTo(expected);
assertThat(result).isEqualTo(expected);
}

@Test
Expand All @@ -189,8 +183,6 @@ void resolveMultipartFile() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof MultipartFile;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
}

Expand All @@ -206,9 +198,6 @@ void resolveMultipartFileList() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof List;
assertThat(condition).isTrue();
assertThat(result).isEqualTo(Arrays.asList(expected1, expected2));
}

Expand All @@ -235,9 +224,7 @@ void resolveMultipartFileArray() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof MultipartFile[];
assertThat(condition).isTrue();
assertThat(result instanceof MultipartFile[]).isTrue();
MultipartFile[] parts = (MultipartFile[]) result;
assertThat(parts).hasSize(2);
assertThat(expected1).isEqualTo(parts[0]);
Expand Down Expand Up @@ -266,9 +253,6 @@ void resolvePart() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof Part;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
}

Expand All @@ -286,9 +270,6 @@ void resolvePartList() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof List;
assertThat(condition).isTrue();
assertThat(result).isEqualTo(Arrays.asList(expected1, expected2));
}

Expand Down Expand Up @@ -319,9 +300,7 @@ void resolvePartArray() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof Part[];
assertThat(condition).isTrue();
assertThat(result instanceof Part[]).isTrue();
Part[] parts = (Part[]) result;
assertThat(parts).hasSize(2);
assertThat(expected1).isEqualTo(parts[0]);
Expand Down Expand Up @@ -350,8 +329,6 @@ void resolveMultipartFileNotAnnot() throws Exception {

MethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof MultipartFile;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
}

Expand All @@ -368,8 +345,6 @@ void resolveMultipartFileListNotannot() throws Exception {
.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);

Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof List;
assertThat(condition).isTrue();
assertThat(result).isEqualTo(Arrays.asList(expected1, expected2));
}

Expand Down Expand Up @@ -424,17 +399,13 @@ void resolvePartNotAnnot() throws Exception {

MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof Part;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
}

@Test
void resolveDefaultValue() throws Exception {
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof String;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo("bar");
}

Expand All @@ -452,7 +423,6 @@ public void missingRequestParamEmptyValueConvertedToNull() throws Exception {

WebDataBinderFactory binderFactory = mock();
given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder);

request.addParameter("stringNotAnnot", "");

MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
Expand All @@ -466,7 +436,6 @@ public void missingRequestParamAfterConversionWithDefaultValue() throws Exceptio

WebDataBinderFactory binderFactory = mock();
given(binderFactory.createBinder(webRequest, null, "booleanParam")).willReturn(binder);

request.addParameter("booleanParam", " ");

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Boolean.class);
Expand All @@ -481,22 +450,20 @@ void missingRequestParamEmptyValueNotRequired() throws Exception {

WebDataBinderFactory binderFactory = mock();
given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder);

request.addParameter("name", "");

MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
assertThat(arg).isNull();
}

@Test // gh-29550
@Test // gh-29550
public void missingRequestParamEmptyValueNotRequiredWithDefaultValue() throws Exception {
WebDataBinder binder = new WebRequestDataBinder(null);
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

WebDataBinderFactory binderFactory = mock();
given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder);

request.addParameter("name", " ");

MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
Expand All @@ -509,9 +476,6 @@ void resolveSimpleTypeParam() throws Exception {
request.setParameter("stringNotAnnot", "plainValue");
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);

boolean condition = result instanceof String;
assertThat(condition).isTrue();
assertThat(result).isEqualTo("plainValue");
}

Expand Down Expand Up @@ -658,9 +622,7 @@ void resolveOptionalMultipartFile() throws Exception {

MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);

boolean condition = result instanceof Optional;
assertThat(condition).isTrue();
assertThat(result instanceof Optional).isTrue();
assertThat(((Optional<?>) result).get()).as("Invalid result").isEqualTo(expected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ private void testException(Throwable exception, String threadName, String expect
TestController controller = context.getBean(TestController.class);
controller.setException(exception);

Object actual = handle(adapter, controller, this.postExchange, Duration.ofMillis(100)
, "threadWithArg", String.class).getReturnValue();
Object actual = handle(adapter, controller, this.postExchange, Duration.ofMillis(1000),
"threadWithArg", String.class).getReturnValue();
assertThat(actual).isEqualTo(expected);
}

Expand Down Expand Up @@ -242,14 +242,14 @@ public SecondControllerAdvice anotherTestExceptionResolver() {
}
}


@Controller
static class TestController {

private Validator validator;

private Throwable exception;


void setValidator(Validator validator) {
this.validator = validator;
}
Expand All @@ -258,7 +258,6 @@ void setException(Throwable exception) {
this.exception = exception;
}


@InitBinder
public void initDataBinder(WebDataBinder dataBinder) {
if (this.validator != null) {
Expand Down Expand Up @@ -291,6 +290,7 @@ public String thread() throws Throwable {
}
}


@ControllerAdvice
@Order(1)
static class OneControllerAdvice {
Expand Down Expand Up @@ -323,6 +323,7 @@ public String handleAssertionError(Error err) {
}
}


@ControllerAdvice
@Order(2)
static class SecondControllerAdvice {
Expand Down
Loading

0 comments on commit 39cd316

Please sign in to comment.