@Target ( ElementType . TYPE)
@Retention ( RetentionPolicy . RUNTIME)
@Documented
@Import ( AspectJAutoProxyRegistrar . class )
public @interface EnableAspectJAutoProxy {
boolean proxyTargetClass ( ) default false ;
boolean exposeProxy ( ) default false ;
}
class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions (
AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AopConfigUtils . registerAspectJAnnotationAutoProxyCreatorIfNecessary ( registry) ;
AnnotationAttributes enableAspectJAutoProxy =
AnnotationConfigUtils . attributesFor ( importingClassMetadata, EnableAspectJAutoProxy . class ) ;
if ( enableAspectJAutoProxy != null ) {
if ( enableAspectJAutoProxy. getBoolean ( "proxyTargetClass" ) ) {
AopConfigUtils . forceAutoProxyCreatorToUseClassProxying ( registry) ;
}
if ( enableAspectJAutoProxy. getBoolean ( "exposeProxy" ) ) {
AopConfigUtils . forceAutoProxyCreatorToExposeProxy ( registry) ;
}
}
}
}
@Nullable
public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary ( BeanDefinitionRegistry registry) {
return registerAspectJAnnotationAutoProxyCreatorIfNecessary ( registry, null ) ;
}
@Nullable
public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary (
BeanDefinitionRegistry registry, @Nullable Object source) {
return registerOrEscalateApcAsRequired ( AnnotationAwareAspectJAutoProxyCreator . class , registry, source) ;
}
private static BeanDefinition registerOrEscalateApcAsRequired (
Class < ? > cls, BeanDefinitionRegistry registry, @Nullable Object source) {
Assert . notNull ( registry, "BeanDefinitionRegistry must not be null" ) ;
if ( registry. containsBeanDefinition ( AUTO_PROXY_CREATOR_BEAN_NAME) ) {
BeanDefinition apcDefinition = registry. getBeanDefinition ( AUTO_PROXY_CREATOR_BEAN_NAME) ;
if ( ! cls. getName ( ) . equals ( apcDefinition. getBeanClassName ( ) ) ) {
int currentPriority = findPriorityForClass ( apcDefinition. getBeanClassName ( ) ) ;
int requiredPriority = findPriorityForClass ( cls) ;
if ( currentPriority < requiredPriority) {
apcDefinition. setBeanClassName ( cls. getName ( ) ) ;
}
}
return null ;
}
RootBeanDefinition beanDefinition = new RootBeanDefinition ( cls) ;
beanDefinition. setSource ( source) ;
beanDefinition. getPropertyValues ( ) . add ( "order" , Ordered . HIGHEST_PRECEDENCE) ;
beanDefinition. setRole ( BeanDefinition . ROLE_INFRASTRUCTURE) ;
registry. registerBeanDefinition ( AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition) ;
return beanDefinition;
}
@Override
public Object postProcessBeforeInstantiation ( Class < ? > beanClass, String beanName) {
Object cacheKey = getCacheKey ( beanClass, beanName) ;
if ( ! StringUtils . hasLength ( beanName) || ! this . targetSourcedBeans. contains ( beanName) ) {
if ( this . advisedBeans. containsKey ( cacheKey) ) {
return null ;
}
if ( isInfrastructureClass ( beanClass) || shouldSkip ( beanClass, beanName) ) {
this . advisedBeans. put ( cacheKey, Boolean . FALSE) ;
return null ;
}
}
TargetSource targetSource = getCustomTargetSource ( beanClass, beanName) ;
if ( targetSource != null ) {
if ( StringUtils . hasLength ( beanName) ) {
this . targetSourcedBeans. add ( beanName) ;
}
Object [ ] specificInterceptors = getAdvicesAndAdvisorsForBean ( beanClass, beanName, targetSource) ;
Object proxy = createProxy ( beanClass, beanName, specificInterceptors, targetSource) ;
this . proxyTypes. put ( cacheKey, proxy. getClass ( ) ) ;
return proxy;
}
return null ;
}
protected boolean shouldSkip ( Class < ? > beanClass, String beanName) {
List < Advisor > candidateAdvisors = findCandidateAdvisors ( ) ;
for ( Advisor advisor : candidateAdvisors) {
if ( advisor instanceof AspectJPointcutAdvisor &&
( ( AspectJPointcutAdvisor ) advisor) . getAspectName ( ) . equals ( beanName) ) {
return true ;
}
}
return super . shouldSkip ( beanClass, beanName) ;
}
protected List < Advisor > findCandidateAdvisors ( ) {
List < Advisor > advisors = super . findCandidateAdvisors ( ) ;
if ( this . aspectJAdvisorsBuilder != null ) {
advisors. addAll ( this . aspectJAdvisorsBuilder. buildAspectJAdvisors ( ) ) ;
}
return advisors;
}
public List < Advisor > findAdvisorBeans ( ) {
String [ ] advisorNames = this . cachedAdvisorBeanNames;
if ( advisorNames == null ) {
advisorNames = BeanFactoryUtils . beanNamesForTypeIncludingAncestors (
this . beanFactory, Advisor . class , true , false ) ;
this . cachedAdvisorBeanNames = advisorNames;
}
if ( advisorNames. length == 0 ) {
return new ArrayList < > ( ) ;
}
List < Advisor > advisors = new ArrayList < > ( ) ;
for ( String name : advisorNames) {
if ( isEligibleBean ( name) ) {
if ( this . beanFactory. isCurrentlyInCreation ( name) ) {
if ( logger. isTraceEnabled ( ) ) {
logger. trace ( "Skipping currently created advisor '" + name + "'" ) ;
}
}
else {
try {
advisors. add ( this . beanFactory. getBean ( name, Advisor . class ) ) ;
}
catch ( BeanCreationException ex) {
Throwable rootCause = ex. getMostSpecificCause ( ) ;
if ( rootCause instanceof BeanCurrentlyInCreationException ) {
BeanCreationException bce = ( BeanCreationException ) rootCause;
String bceBeanName = bce. getBeanName ( ) ;
if ( bceBeanName != null && this . beanFactory. isCurrentlyInCreation ( bceBeanName) ) {
if ( logger. isTraceEnabled ( ) ) {
logger. trace ( "Skipping advisor '" + name +
"' with dependency on currently created bean: " + ex. getMessage ( ) ) ;
}
continue ;
}
}
throw ex;
}
}
}
}
return advisors;
}
public List < Advisor > buildAspectJAdvisors ( ) {
List < String > aspectNames = this . aspectBeanNames;
if ( aspectNames == null ) {
synchronized ( this ) {
aspectNames = this . aspectBeanNames;
if ( aspectNames == null ) {
List < Advisor > advisors = new ArrayList < > ( ) ;
aspectNames = new ArrayList < > ( ) ;
String [ ] beanNames = BeanFactoryUtils . beanNamesForTypeIncludingAncestors (
this . beanFactory, Object . class , true , false ) ;
for ( String beanName : beanNames) {
if ( ! isEligibleBean ( beanName) ) {
continue ;
}
Class < ? > beanType = this . beanFactory. getType ( beanName, false ) ;
if ( beanType == null ) {
continue ;
}
if ( this . advisorFactory. isAspect ( beanType) ) {
aspectNames. add ( beanName) ;
AspectMetadata amd = new AspectMetadata ( beanType, beanName) ;
if ( amd. getAjType ( ) . getPerClause ( ) . getKind ( ) == PerClauseKind . SINGLETON) {
MetadataAwareAspectInstanceFactory factory =
new BeanFactoryAspectInstanceFactory ( this . beanFactory, beanName) ;
List < Advisor > classAdvisors = this . advisorFactory. getAdvisors ( factory) ;
if ( this . beanFactory. isSingleton ( beanName) ) {
this . advisorsCache. put ( beanName, classAdvisors) ;
}
else {
this . aspectFactoryCache. put ( beanName, factory) ;
}
advisors. addAll ( classAdvisors) ;
}
else {
if ( this . beanFactory. isSingleton ( beanName) ) {
throw new IllegalArgumentException ( "Bean with name '" + beanName +
"' is a singleton, but aspect instantiation model is not singleton" ) ;
}
MetadataAwareAspectInstanceFactory factory =
new PrototypeAspectInstanceFactory ( this . beanFactory, beanName) ;
this . aspectFactoryCache. put ( beanName, factory) ;
advisors. addAll ( this . advisorFactory. getAdvisors ( factory) ) ;
}
}
}
this . aspectBeanNames = aspectNames;
return advisors;
}
}
}
if ( aspectNames. isEmpty ( ) ) {
return Collections . emptyList ( ) ;
}
List < Advisor > advisors = new ArrayList < > ( ) ;
for ( String aspectName : aspectNames) {
List < Advisor > cachedAdvisors = this . advisorsCache. get ( aspectName) ;
if ( cachedAdvisors != null ) {
advisors. addAll ( cachedAdvisors) ;
}
else {
MetadataAwareAspectInstanceFactory factory = this . aspectFactoryCache. get ( aspectName) ;
advisors. addAll ( this . advisorFactory. getAdvisors ( factory) ) ;
}
}
return advisors;
}
public List < Advisor > getAdvisors ( MetadataAwareAspectInstanceFactory aspectInstanceFactory) {
Class < ? > aspectClass = aspectInstanceFactory. getAspectMetadata ( ) . getAspectClass ( ) ;
String aspectName = aspectInstanceFactory. getAspectMetadata ( ) . getAspectName ( ) ;
validate ( aspectClass) ;
MetadataAwareAspectInstanceFactory lazySingletonAspectInstanceFactory =
new LazySingletonAspectInstanceFactoryDecorator ( aspectInstanceFactory) ;
List < Advisor > advisors = new ArrayList < > ( ) ;
for ( Method method : getAdvisorMethods ( aspectClass) ) {
Advisor advisor = getAdvisor ( method, lazySingletonAspectInstanceFactory, 0 , aspectName) ;
if ( advisor != null ) {
advisors. add ( advisor) ;
}
}
if ( ! advisors. isEmpty ( ) && lazySingletonAspectInstanceFactory. getAspectMetadata ( ) . isLazilyInstantiated ( ) ) {
Advisor instantiationAdvisor = new SyntheticInstantiationAdvisor ( lazySingletonAspectInstanceFactory) ;
advisors. add ( 0 , instantiationAdvisor) ;
}
for ( Field field : aspectClass. getDeclaredFields ( ) ) {
Advisor advisor = getDeclareParentsAdvisor ( field) ;
if ( advisor != null ) {
advisors. add ( advisor) ;
}
}
return advisors;
}
private List < Method > getAdvisorMethods ( Class < ? > aspectClass) {
List < Method > methods = new ArrayList < > ( ) ;
ReflectionUtils . doWithMethods ( aspectClass, methods:: add , adviceMethodFilter) ;
if ( methods. size ( ) > 1 ) {
methods. sort ( adviceMethodComparator) ;
}
return methods;
}
public Advisor getAdvisor ( Method candidateAdviceMethod, MetadataAwareAspectInstanceFactory aspectInstanceFactory,
int declarationOrderInAspect, String aspectName) {
validate ( aspectInstanceFactory. getAspectMetadata ( ) . getAspectClass ( ) ) ;
AspectJExpressionPointcut expressionPointcut = getPointcut (
candidateAdviceMethod, aspectInstanceFactory. getAspectMetadata ( ) . getAspectClass ( ) ) ;
if ( expressionPointcut == null ) {
return null ;
}
return new InstantiationModelAwarePointcutAdvisorImpl ( expressionPointcut, candidateAdviceMethod,
this , aspectInstanceFactory, declarationOrderInAspect, aspectName) ;
}
private AspectJExpressionPointcut getPointcut ( Method candidateAdviceMethod, Class < ? > candidateAspectClass) {
AspectJAnnotation < ? > aspectJAnnotation =
AbstractAspectJAdvisorFactory . findAspectJAnnotationOnMethod ( candidateAdviceMethod) ;
if ( aspectJAnnotation == null ) {
return null ;
}
AspectJExpressionPointcut ajexp =
new AspectJExpressionPointcut ( candidateAspectClass, new String [ 0 ] , new Class < ? > [ 0 ] ) ;
ajexp. setExpression ( aspectJAnnotation. getPointcutExpression ( ) ) ;
if ( this . beanFactory != null ) {
ajexp. setBeanFactory ( this . beanFactory) ;
}
return ajexp;
}
public InstantiationModelAwarePointcutAdvisorImpl ( AspectJExpressionPointcut declaredPointcut,
Method aspectJAdviceMethod, AspectJAdvisorFactory aspectJAdvisorFactory,
MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {
this . declaredPointcut = declaredPointcut;
this . declaringClass = aspectJAdviceMethod. getDeclaringClass ( ) ;
this . methodName = aspectJAdviceMethod. getName ( ) ;
this . parameterTypes = aspectJAdviceMethod. getParameterTypes ( ) ;
this . aspectJAdviceMethod = aspectJAdviceMethod;
this . aspectJAdvisorFactory = aspectJAdvisorFactory;
this . aspectInstanceFactory = aspectInstanceFactory;
this . declarationOrder = declarationOrder;
this . aspectName = aspectName;
if ( aspectInstanceFactory. getAspectMetadata ( ) . isLazilyInstantiated ( ) ) {
Pointcut preInstantiationPointcut = Pointcuts . union (
aspectInstanceFactory. getAspectMetadata ( ) . getPerClausePointcut ( ) , this . declaredPointcut) ;
this . pointcut = new PerTargetInstantiationModelPointcut (
this . declaredPointcut, preInstantiationPointcut, aspectInstanceFactory) ;
this . lazy = true ;
}
else {
this . pointcut = this . declaredPointcut;
this . lazy = false ;
this . instantiatedAdvice = instantiateAdvice ( this . declaredPointcut) ;
}
}
private Advice instantiateAdvice ( AspectJExpressionPointcut pointcut) {
Advice advice = this . aspectJAdvisorFactory. getAdvice ( this . aspectJAdviceMethod, pointcut,
this . aspectInstanceFactory, this . declarationOrder, this . aspectName) ;
return ( advice != null ? advice : EMPTY_ADVICE) ;
}
public Advice getAdvice ( Method candidateAdviceMethod, AspectJExpressionPointcut expressionPointcut,
MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {
Class < ? > candidateAspectClass = aspectInstanceFactory. getAspectMetadata ( ) . getAspectClass ( ) ;
validate ( candidateAspectClass) ;
AspectJAnnotation < ? > aspectJAnnotation =
AbstractAspectJAdvisorFactory . findAspectJAnnotationOnMethod ( candidateAdviceMethod) ;
if ( aspectJAnnotation == null ) {
return null ;
}
if ( ! isAspect ( candidateAspectClass) ) {
throw new AopConfigException ( "Advice must be declared inside an aspect type: " +
"Offending method '" + candidateAdviceMethod + "' in class [" +
candidateAspectClass. getName ( ) + "]" ) ;
}
if ( logger. isDebugEnabled ( ) ) {
logger. debug ( "Found AspectJ method: " + candidateAdviceMethod) ;
}
AbstractAspectJAdvice springAdvice;
switch ( aspectJAnnotation. getAnnotationType ( ) ) {
case AtPointcut :
if ( logger. isDebugEnabled ( ) ) {
logger. debug ( "Processing pointcut '" + candidateAdviceMethod. getName ( ) + "'" ) ;
}
return null ;
case AtAround :
springAdvice = new AspectJAroundAdvice (
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory) ;
break ;
case AtBefore :
springAdvice = new AspectJMethodBeforeAdvice (
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory) ;
break ;
case AtAfter :
springAdvice = new AspectJAfterAdvice (
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory) ;
break ;
case AtAfterReturning :
springAdvice = new AspectJAfterReturningAdvice (
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory) ;
AfterReturning afterReturningAnnotation = ( AfterReturning ) aspectJAnnotation. getAnnotation ( ) ;
if ( StringUtils . hasText ( afterReturningAnnotation. returning ( ) ) ) {
springAdvice. setReturningName ( afterReturningAnnotation. returning ( ) ) ;
}
break ;
case AtAfterThrowing :
springAdvice = new AspectJAfterThrowingAdvice (
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory) ;
AfterThrowing afterThrowingAnnotation = ( AfterThrowing ) aspectJAnnotation. getAnnotation ( ) ;
if ( StringUtils . hasText ( afterThrowingAnnotation. throwing ( ) ) ) {
springAdvice. setThrowingName ( afterThrowingAnnotation. throwing ( ) ) ;
}
break ;
default :
throw new UnsupportedOperationException (
"Unsupported advice type on method: " + candidateAdviceMethod) ;
}
springAdvice. setAspectName ( aspectName) ;
springAdvice. setDeclarationOrder ( declarationOrder) ;
String [ ] argNames = this . parameterNameDiscoverer. getParameterNames ( candidateAdviceMethod) ;
if ( argNames != null ) {
springAdvice. setArgumentNamesFromStringArray ( argNames) ;
}
springAdvice. calculateArgumentBindings ( ) ;
return springAdvice;
}
@Override
public Object postProcessAfterInitialization ( @Nullable Object bean, String beanName) {
if ( bean != null ) {
Object cacheKey = getCacheKey ( bean. getClass ( ) , beanName) ;
if ( this . earlyProxyReferences. remove ( cacheKey) != bean) {
return wrapIfNecessary ( bean, beanName, cacheKey) ;
}
}
return bean;
}
protected Object wrapIfNecessary ( Object bean, String beanName, Object cacheKey) {
if ( StringUtils . hasLength ( beanName) && this . targetSourcedBeans. contains ( beanName) ) {
return bean;
}
if ( Boolean . FALSE. equals ( this . advisedBeans. get ( cacheKey) ) ) {
return bean;
}
if ( isInfrastructureClass ( bean. getClass ( ) ) || shouldSkip ( bean. getClass ( ) , beanName) ) {
this . advisedBeans. put ( cacheKey, Boolean . FALSE) ;
return bean;
}
Object [ ] specificInterceptors = getAdvicesAndAdvisorsForBean ( bean. getClass ( ) , beanName, null ) ;
if ( specificInterceptors != DO_NOT_PROXY) {
this . advisedBeans. put ( cacheKey, Boolean . TRUE) ;
Object proxy = createProxy (
bean. getClass ( ) , beanName, specificInterceptors, new SingletonTargetSource ( bean) ) ;
this . proxyTypes. put ( cacheKey, proxy. getClass ( ) ) ;
return proxy;
}
this . advisedBeans. put ( cacheKey, Boolean . FALSE) ;
return bean;
}
protected Object [ ] getAdvicesAndAdvisorsForBean (
Class < ? > beanClass, String beanName, @Nullable TargetSource targetSource) {
List < Advisor > advisors = findEligibleAdvisors ( beanClass, beanName) ;
if ( advisors. isEmpty ( ) ) {
return DO_NOT_PROXY;
}
return advisors. toArray ( ) ;
}
protected List < Advisor > findEligibleAdvisors ( Class < ? > beanClass, String beanName) {
List < Advisor > candidateAdvisors = findCandidateAdvisors ( ) ;
List < Advisor > eligibleAdvisors = findAdvisorsThatCanApply ( candidateAdvisors, beanClass, beanName) ;
extendAdvisors ( eligibleAdvisors) ;
if ( ! eligibleAdvisors. isEmpty ( ) ) {
eligibleAdvisors = sortAdvisors ( eligibleAdvisors) ;
}
return eligibleAdvisors;
}
protected Object createProxy ( Class < ? > beanClass, @Nullable String beanName,
@Nullable Object [ ] specificInterceptors, TargetSource targetSource) {
if ( this . beanFactory instanceof ConfigurableListableBeanFactory ) {
AutoProxyUtils . exposeTargetClass ( ( ConfigurableListableBeanFactory ) this . beanFactory, beanName, beanClass) ;
}
ProxyFactory proxyFactory = new ProxyFactory ( ) ;
proxyFactory. copyFrom ( this ) ;
if ( ! proxyFactory. isProxyTargetClass ( ) ) {
if ( shouldProxyTargetClass ( beanClass, beanName) ) {
proxyFactory. setProxyTargetClass ( true ) ;
}
else {
evaluateProxyInterfaces ( beanClass, proxyFactory) ;
}
}
Advisor [ ] advisors = buildAdvisors ( beanName, specificInterceptors) ;
proxyFactory. addAdvisors ( advisors) ;
proxyFactory. setTargetSource ( targetSource) ;
customizeProxyFactory ( proxyFactory) ;
proxyFactory. setFrozen ( this . freezeProxy) ;
if ( advisorsPreFiltered ( ) ) {
proxyFactory. setPreFiltered ( true ) ;
}
ClassLoader classLoader = getProxyClassLoader ( ) ;
if ( classLoader instanceof SmartClassLoader && classLoader != beanClass. getClassLoader ( ) ) {
classLoader = ( ( SmartClassLoader ) classLoader) . getOriginalClassLoader ( ) ;
}
return proxyFactory. getProxy ( classLoader) ;
}
public Object getProxy ( @Nullable ClassLoader classLoader) {
return createAopProxy ( ) . getProxy ( classLoader) ;
}
protected final synchronized AopProxy createAopProxy ( ) {
if ( ! this . active) {
activate ( ) ;
}
return getAopProxyFactory ( ) . createAopProxy ( this ) ;
}
public AopProxy createAopProxy ( AdvisedSupport config) throws AopConfigException {
if ( ! NativeDetector . inNativeImage ( ) &&
( config. isOptimize ( ) || config. isProxyTargetClass ( ) || hasNoUserSuppliedProxyInterfaces ( config) ) ) {
Class < ? > targetClass = config. getTargetClass ( ) ;
if ( targetClass == null ) {
throw new AopConfigException ( "TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation." ) ;
}
if ( targetClass. isInterface ( ) || Proxy . isProxyClass ( targetClass) ) {
return new JdkDynamicAopProxy ( config) ;
}
return new ObjenesisCglibAopProxy ( config) ;
}
else {
return new JdkDynamicAopProxy ( config) ;
}
}
public Object getProxy ( @Nullable ClassLoader classLoader) {
if ( logger. isTraceEnabled ( ) ) {
logger. trace ( "Creating CGLIB proxy: " + this . advised. getTargetSource ( ) ) ;
}
try {
Class < ? > rootClass = this . advised. getTargetClass ( ) ;
Assert . state ( rootClass != null , "Target class must be available for creating a CGLIB proxy" ) ;
Class < ? > proxySuperClass = rootClass;
if ( rootClass. getName ( ) . contains ( ClassUtils . CGLIB_CLASS_SEPARATOR) ) {
proxySuperClass = rootClass. getSuperclass ( ) ;
Class < ? > [ ] additionalInterfaces = rootClass. getInterfaces ( ) ;
for ( Class < ? > additionalInterface : additionalInterfaces) {
this . advised. addInterface ( additionalInterface) ;
}
}
validateClassIfNecessary ( proxySuperClass, classLoader) ;
Enhancer enhancer = createEnhancer ( ) ;
if ( classLoader != null ) {
enhancer. setClassLoader ( classLoader) ;
if ( classLoader instanceof SmartClassLoader &&
( ( SmartClassLoader ) classLoader) . isClassReloadable ( proxySuperClass) ) {
enhancer. setUseCache ( false ) ;
}
}
enhancer. setSuperclass ( proxySuperClass) ;
enhancer. setInterfaces ( AopProxyUtils . completeProxiedInterfaces ( this . advised) ) ;
enhancer. setNamingPolicy ( SpringNamingPolicy . INSTANCE) ;
enhancer. setStrategy ( new ClassLoaderAwareGeneratorStrategy ( classLoader) ) ;
Callback [ ] callbacks = getCallbacks ( rootClass) ;
Class < ? > [ ] types = new Class < ? > [ callbacks. length] ;
for ( int x = 0 ; x < types. length; x++ ) {
types[ x] = callbacks[ x] . getClass ( ) ;
}
enhancer. setCallbackFilter ( new ProxyCallbackFilter (
this . advised. getConfigurationOnlyCopy ( ) , this . fixedInterceptorMap, this . fixedInterceptorOffset) ) ;
enhancer. setCallbackTypes ( types) ;
return createProxyClassAndInstance ( enhancer, callbacks) ;
}
catch ( CodeGenerationException | IllegalArgumentException ex) {
throw new AopConfigException ( "Could not generate CGLIB subclass of " + this . advised. getTargetClass ( ) +
": Common causes of this problem include using a final class or a non-visible class" ,
ex) ;
}
catch ( Throwable ex) {
throw new AopConfigException ( "Unexpected AOP exception" , ex) ;
}
}
protected Object createProxyClassAndInstance ( Enhancer enhancer, Callback [ ] callbacks) {
Class < ? > proxyClass = enhancer. createClass ( ) ;
Object proxyInstance = null ;
if ( objenesis. isWorthTrying ( ) ) {
try {
proxyInstance = objenesis. newInstance ( proxyClass, enhancer. getUseCache ( ) ) ;
}
catch ( Throwable ex) {
logger. debug ( "Unable to instantiate proxy using Objenesis, " +
"falling back to regular proxy construction" , ex) ;
}
}
if ( proxyInstance == null ) {
try {
Constructor < ? > ctor = ( this . constructorArgs != null ?
proxyClass. getDeclaredConstructor ( this . constructorArgTypes) :
proxyClass. getDeclaredConstructor ( ) ) ;
ReflectionUtils . makeAccessible ( ctor) ;
proxyInstance = ( this . constructorArgs != null ?
ctor. newInstance ( this . constructorArgs) : ctor. newInstance ( ) ) ;
}
catch ( Throwable ex) {
throw new AopConfigException ( "Unable to instantiate proxy using Objenesis, " +
"and regular proxy instantiation via default constructor fails as well" , ex) ;
}
}
( ( Factory ) proxyInstance) . setCallbacks ( callbacks) ;
return proxyInstance;
}
后面都是Objenesis相关的
public < T > T newInstance ( Class < T > clazz, boolean useCache) {
if ( ! useCache) {
return newInstantiatorOf ( clazz) . newInstance ( ) ;
}
return getInstantiatorOf ( clazz) . newInstance ( ) ;
}
protected < T > ObjectInstantiator < T > newInstantiatorOf ( Class < T > clazz) {
Boolean currentWorthTrying = this . worthTrying;
try {
ObjectInstantiator < T > instantiator = this . strategy. newInstantiatorOf ( clazz) ;
if ( currentWorthTrying == null ) {
this . worthTrying = Boolean . TRUE;
}
return instantiator;
}
catch ( ObjenesisException ex) {
if ( currentWorthTrying == null ) {
Throwable cause = ex. getCause ( ) ;
if ( cause instanceof ClassNotFoundException || cause instanceof IllegalAccessException ) {
this . worthTrying = Boolean . FALSE;
}
}
throw ex;
}
catch ( NoClassDefFoundError err) {
if ( currentWorthTrying == null ) {
this . worthTrying = Boolean . FALSE;
}
throw new ObjenesisException ( err) ;
}
}
public T newInstance ( ) {
try {
return this . mungedConstructor. newInstance ( ( Object [ ] ) null ) ;
} catch ( Exception var2) {
throw new ObjenesisException ( var2) ;
}
}