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

@@ -74,7 +74,7 @@ public class BindingReflectionHintsRegistrar {
}
private boolean shouldSkipMembers(Class<?> type) {
return (type.getCanonicalName() != null && type.getCanonicalName().startsWith("java.")) || type.isArray();
return type.getCanonicalName().startsWith("java.") || type.isArray();
}
private void registerReflectionHints(ReflectionHints hints, Set<Type> seen, Type type) {

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.
@@ -28,6 +28,7 @@ import java.util.stream.Stream;
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
@@ -36,6 +37,7 @@ import org.springframework.util.ClassUtils;
* @author Stephane Nicoll
* @author Phillip Webb
* @author Andy Wilkinson
* @author Sebastien Deleuze
* @since 6.0
*/
public class ReflectionHints {
@@ -106,7 +108,11 @@ public class ReflectionHints {
* @see #registerType(Class, MemberCategory...)
*/
public ReflectionHints registerType(Class<?> type, Consumer<TypeHint.Builder> typeHint) {
return registerType(TypeReference.of(type), typeHint);
Assert.notNull(type, "'type' must not be null");
if (type.getCanonicalName() != null) {
registerType(TypeReference.of(type), typeHint);
}
return this;
}
/**
@@ -117,7 +123,11 @@ public class ReflectionHints {
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerType(Class<?> type, MemberCategory... memberCategories) {
return registerType(TypeReference.of(type), memberCategories);
Assert.notNull(type, "'type' must not be null");
if (type.getCanonicalName() != null) {
registerType(TypeReference.of(type), memberCategories);
}
return this;
}
/**

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.
@@ -17,11 +17,13 @@
package org.springframework.aot.hint;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* A {@link TypeReference} based on a {@link Class}.
*
* @author Stephane Nicoll
* @author Sebastien Deleuze
* @since 6.0
*/
final class ReflectionTypeReference extends AbstractTypeReference {
@@ -41,6 +43,8 @@ final class ReflectionTypeReference extends AbstractTypeReference {
}
static ReflectionTypeReference of(Class<?> type) {
Assert.notNull(type, "'type' must not be null");
Assert.notNull(type.getCanonicalName(), "'type.getCanonicalName()' must not be null");
return new ReflectionTypeReference(type);
}

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.
@@ -26,6 +26,7 @@ import org.springframework.lang.Nullable;
* a {@link Class} yet.
*
* @author Stephane Nicoll
* @author Sebastien Deleuze
* @since 6.0
*/
public interface TypeReference {
@@ -68,6 +69,7 @@ public interface TypeReference {
* Create an instance based on the specified type.
* @param type the type to wrap
* @return a type reference for the specified type
* @throws IllegalArgumentException if the specified type {@linkplain Class#getCanonicalName() canonical name} is {@code null}
*/
static TypeReference of(Class<?> type) {
return ReflectionTypeReference.of(type);