Refine reflection hints handling for anonymous class

Before this commit, anonymous classes could throw an
unexpected NullPointerException in
ReflectionsHint#registerType and lambdas entries could
be created in the related generated reflect-config.json.

This commit refines how anonymous classes are handled by
explicitly checking for null class and canonical name in
ReflectionTypeReference#of, while skipping such class in
ReflectionHints#registerType in order to keep a lenient
behavior.

Closes gh-29774
This commit is contained in:
Sébastien Deleuze
2023-03-01 10:47:58 +01:00
parent fe73c630da
commit dbbebf541d
7 changed files with 57 additions and 8 deletions

View File

@@ -34,6 +34,7 @@ import static org.mockito.Mockito.verifyNoInteractions;
* Tests for {@link ReflectionHints}.
*
* @author Stephane Nicoll
* @author Sebastien Deleuze
*/
class ReflectionHintsTests {
@@ -132,6 +133,16 @@ class ReflectionHintsTests {
assertThat(fieldHint.getName()).isEqualTo("field"));
}
@Test
void registerTypeIgnoresLambda() {
Runnable lambda = () -> { };
Consumer<TypeHint.Builder> hintBuilder = mock();
this.reflectionHints.registerType(lambda.getClass());
this.reflectionHints.registerType(lambda.getClass(), hintBuilder);
assertThat(this.reflectionHints.typeHints()).isEmpty();
verifyNoInteractions(hintBuilder);
}
private void assertTestTypeFieldHint(Consumer<FieldHint> fieldHint) {
assertThat(this.reflectionHints.typeHints()).singleElement().satisfies(typeHint -> {
assertThat(typeHint.getType().getCanonicalName()).isEqualTo(TestType.class.getCanonicalName());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,11 +18,13 @@ package org.springframework.aot.hint;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.jupiter.params.provider.Arguments.arguments;
/**
@@ -30,9 +32,21 @@ import static org.junit.jupiter.params.provider.Arguments.arguments;
*
* @author Stephane Nicoll
* @author Moritz Halbritter
* @author Sebastien Deleuze
*/
class ReflectionTypeReferenceTests {
@Test
void typeReferenceWithNullClass() {
assertThatIllegalArgumentException().isThrownBy(() -> ReflectionTypeReference.of(null));
}
@Test
void typeReferenceWithLambda() {
Runnable lambda = () -> { };
assertThatIllegalArgumentException().isThrownBy(() -> ReflectionTypeReference.of(lambda.getClass()));
}
@ParameterizedTest
@MethodSource("reflectionTargetNames")
void typeReferenceFromClassHasSuitableReflectionTargetName(Class<?> clazz, String binaryName) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -201,6 +201,14 @@ public class ReflectionHintsWriterTests {
""", hints);
}
@Test
void ignoreLambda() throws JSONException {
Runnable anonymousRunnable = () -> { };
ReflectionHints hints = new ReflectionHints();
hints.registerType(anonymousRunnable.getClass());
assertEquals("[]", hints);
}
private void assertEquals(String expectedString, ReflectionHints hints) throws JSONException {
StringWriter out = new StringWriter();
BasicJsonWriter writer = new BasicJsonWriter(out, "\t");