Skip to content

Commit

Permalink
Consistently register CGLIB hints for lazy resolution proxy classes
Browse files Browse the repository at this point in the history
Core JDK/CGLIB proxy registration code extracted to ClassHintUtils.

Closes gh-29584
  • Loading branch information
jhoeller committed Nov 26, 2022
1 parent 902f8dd commit 8e5eb84
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -46,6 +45,7 @@
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.support.ClassHintUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
Expand Down Expand Up @@ -1018,10 +1018,10 @@ private void registerHints(RuntimeHints runtimeHints) {

private void registerProxyIfNecessary(RuntimeHints runtimeHints, DependencyDescriptor dependencyDescriptor) {
if (this.candidateResolver != null) {
Class<?> proxyType =
Class<?> proxyClass =
this.candidateResolver.getLazyResolutionProxyClass(dependencyDescriptor, null);
if (proxyType != null && Proxy.isProxyClass(proxyType)) {
runtimeHints.proxies().registerJdkProxy(proxyType.getInterfaces());
if (proxyClass != null) {
ClassHintUtils.registerProxyIfNecessary(proxyClass, runtimeHints);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.context.aot;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.springframework.aot.generate.GeneratedFiles;
Expand All @@ -26,6 +25,7 @@
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.support.ClassHintUtils;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.core.io.ByteArrayResource;

Expand All @@ -34,8 +34,10 @@
* and register the necessary hints so that they can be instantiated.
*
* @author Stephane Nicoll
* @see ReflectUtils#setGeneratedClassHandler(BiConsumer)
* @see ReflectUtils#setLoadedClassHandler(Consumer)
* @since 6.0
* @see ReflectUtils#setGeneratedClassHandler
* @see ReflectUtils#setLoadedClassHandler
* @see ClassHintUtils#registerProxyIfNecessary
*/
class CglibClassHandler {

Expand All @@ -46,11 +48,13 @@ class CglibClassHandler {

private final GeneratedFiles generatedFiles;


CglibClassHandler(GenerationContext generationContext) {
this.runtimeHints = generationContext.getRuntimeHints();
this.generatedFiles = generationContext.getGeneratedFiles();
}


/**
* Handle the specified generated CGLIB class.
* @param cglibClassName the name of the generated class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.aot.hint.support.ClassHintUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
Expand All @@ -49,7 +46,6 @@
import org.springframework.core.metrics.ApplicationStartup;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* Generic ApplicationContext implementation that holds a single internal
Expand All @@ -72,7 +68,7 @@
* definitions on it. {@link #refresh()} may only be called once.
*
* <p>This ApplicationContext implementation is suitable for Ahead of Time
* processing, using {@link #refreshForAotProcessing()} as an alternative to the
* processing, using {@link #refreshForAotProcessing} as an alternative to the
* regular {@link #refresh()}.
*
* <p>Usage example:
Expand All @@ -88,16 +84,13 @@
* MyBean myBean = (MyBean) ctx.getBean("myBean");
* ...</pre>
*
* For the typical case of XML bean definitions, simply use
* For the typical case of XML bean definitions, you may also use
* {@link ClassPathXmlApplicationContext} or {@link FileSystemXmlApplicationContext},
* which are easier to set up - but less flexible, since you can just use standard
* resource locations for XML bean definitions, rather than mixing arbitrary bean
* definition formats. The equivalent in a web environment is
* {@link org.springframework.web.context.support.XmlWebApplicationContext}.
*
* <p>For custom application context implementations that are supposed to read
* special bean definition formats in a refreshable manner, consider deriving
* from the {@link AbstractRefreshableApplicationContext} base class.
* definition formats. For a custom application context implementation supposed to
* read a specific bean definition format in a refreshable manner, consider
* deriving from the {@link AbstractRefreshableApplicationContext} base class.
*
* @author Juergen Hoeller
* @author Chris Beams
Expand All @@ -111,15 +104,6 @@
*/
public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {

private static final Consumer<Builder> asClassBasedProxy = hint ->
hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_DECLARED_METHODS,
MemberCategory.DECLARED_FIELDS);

private static final Consumer<Builder> asProxiedUserClass = hint ->
hint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS);


private final DefaultListableBeanFactory beanFactory;

@Nullable
Expand Down Expand Up @@ -442,34 +426,18 @@ private void preDetermineBeanTypes(RuntimeHints runtimeHints) {
for (String beanName : this.beanFactory.getBeanDefinitionNames()) {
Class<?> beanType = this.beanFactory.getType(beanName);
if (beanType != null) {
registerProxyHintIfNecessary(beanType, runtimeHints);
ClassHintUtils.registerProxyIfNecessary(beanType, runtimeHints);
for (SmartInstantiationAwareBeanPostProcessor bpp : bpps) {
Class<?> newBeanType = bpp.determineBeanType(beanType, beanName);
if (newBeanType != beanType) {
registerProxyHintIfNecessary(newBeanType, runtimeHints);
ClassHintUtils.registerProxyIfNecessary(newBeanType, runtimeHints);
beanType = newBeanType;
}
}
}
}
}

private void registerProxyHintIfNecessary(Class<?> beanType, RuntimeHints runtimeHints) {
if (Proxy.isProxyClass(beanType)) {
// A JDK proxy class needs an explicit hint
runtimeHints.proxies().registerJdkProxy(beanType.getInterfaces());
}
else {
// Potentially a CGLIB-generated subclass with reflection hints
Class<?> userClass = ClassUtils.getUserClass(beanType);
if (userClass != beanType) {
runtimeHints.reflection()
.registerType(beanType, asClassBasedProxy)
.registerType(userClass, asProxiedUserClass);
}
}
}


//---------------------------------------------------------------------
// Convenient methods for registering individual beans
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2002-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.aot.hint.support;

import java.lang.reflect.Proxy;
import java.util.function.Consumer;

import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeHint;
import org.springframework.util.ClassUtils;

/**
* Utilities for core hint inference on Spring-managed classes,
* specifically for proxy types such as interface-based JDK proxies
* and CGLIB-generated subclasses which need proxy/reflection hints.
*
* <p>Note that this class does not take specifics of Spring AOP or
* any other framework arrangement into account. It just operates
* on the JDK and CGLIB proxy facilities and their core conventions.
*
* @author Juergen Hoeller
* @since 6.0.3
* @see org.springframework.aot.hint.ProxyHints
* @see org.springframework.aot.hint.ReflectionHints
*/
public abstract class ClassHintUtils {

private static final Consumer<TypeHint.Builder> asClassBasedProxy = hint ->
hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_DECLARED_METHODS,
MemberCategory.DECLARED_FIELDS);

private static final Consumer<TypeHint.Builder> asProxiedUserClass = hint ->
hint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS);


/**
* Register a proxy hint for a JDK proxy or corresponding reflection hints
* for a CGLIB-generated subclass, if necessary.
* @param candidateClass the class to introspect
* @param runtimeHints the RuntimeHints instance to register the hints on
* @see Proxy#isProxyClass(Class)
* @see ClassUtils#getUserClass(Class)
*/
public static void registerProxyIfNecessary(Class<?> candidateClass, RuntimeHints runtimeHints) {
if (Proxy.isProxyClass(candidateClass)) {
// A JDK proxy class needs an explicit hint
runtimeHints.proxies().registerJdkProxy(candidateClass.getInterfaces());
}
else {
// Potentially a CGLIB-generated subclass with reflection hints
Class<?> userClass = ClassUtils.getUserClass(candidateClass);
if (userClass != candidateClass) {
runtimeHints.reflection()
.registerType(candidateClass, asClassBasedProxy)
.registerType(userClass, asProxiedUserClass);
}
}
}

}

0 comments on commit 8e5eb84

Please sign in to comment.