Consistently use constructor-based instantiation instead of Class.newInstance / BeanUtils.instantiate
Issue: SPR-14486
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -16,9 +16,12 @@
|
||||
|
||||
package org.springframework.aop.aspectj;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.springframework.aop.framework.AopConfigException;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Implementation of {@link AspectInstanceFactory} that creates a new instance
|
||||
@@ -52,13 +55,23 @@ public class SimpleAspectInstanceFactory implements AspectInstanceFactory {
|
||||
@Override
|
||||
public final Object getAspectInstance() {
|
||||
try {
|
||||
return this.aspectClass.newInstance();
|
||||
return ReflectionUtils.accessibleConstructor(this.aspectClass).newInstance();
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
throw new AopConfigException(
|
||||
"No default constructor on aspect class: " + this.aspectClass.getName(), ex);
|
||||
}
|
||||
catch (InstantiationException ex) {
|
||||
throw new AopConfigException("Unable to instantiate aspect class [" + this.aspectClass.getName() + "]", ex);
|
||||
throw new AopConfigException(
|
||||
"Unable to instantiate aspect class: " + this.aspectClass.getName(), ex);
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
throw new AopConfigException("Cannot access element class [" + this.aspectClass.getName() + "]", ex);
|
||||
throw new AopConfigException(
|
||||
"Could not access aspect constructor: " + this.aspectClass.getName(), ex);
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
throw new AopConfigException(
|
||||
"Failed to invoke aspect constructor: " + this.aspectClass.getName(), ex.getTargetException());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.aspectj.lang.reflect.PerClauseKind;
|
||||
|
||||
import org.springframework.aop.Advisor;
|
||||
import org.springframework.aop.aspectj.AspectJProxyUtils;
|
||||
import org.springframework.aop.framework.AopConfigException;
|
||||
import org.springframework.aop.aspectj.SimpleAspectInstanceFactory;
|
||||
import org.springframework.aop.framework.ProxyCreatorSupport;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
@@ -167,17 +167,9 @@ public class AspectJProxyFactory extends ProxyCreatorSupport {
|
||||
if (instance != null) {
|
||||
return instance;
|
||||
}
|
||||
try {
|
||||
instance = aspectClass.newInstance();
|
||||
aspectCache.put(aspectClass, instance);
|
||||
return instance;
|
||||
}
|
||||
catch (InstantiationException ex) {
|
||||
throw new AopConfigException("Unable to instantiate aspect class [" + aspectClass.getName() + "]", ex);
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
throw new AopConfigException("Cannot access aspect class [" + aspectClass.getName() + "]", ex);
|
||||
}
|
||||
instance = new SimpleAspectInstanceFactory(aspectClass).getAspectInstance();
|
||||
aspectCache.put(aspectClass, instance);
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.aop.framework;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
@@ -23,6 +25,7 @@ import org.springframework.cglib.proxy.Callback;
|
||||
import org.springframework.cglib.proxy.Enhancer;
|
||||
import org.springframework.cglib.proxy.Factory;
|
||||
import org.springframework.objenesis.SpringObjenesis;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Objenesis-based extension of {@link CglibAopProxy} to create proxy instances
|
||||
@@ -68,9 +71,12 @@ class ObjenesisCglibAopProxy extends CglibAopProxy {
|
||||
if (proxyInstance == null) {
|
||||
// Regular instantiation via default constructor...
|
||||
try {
|
||||
Constructor<?> ctor = (this.constructorArgs != null ?
|
||||
proxyClass.getDeclaredConstructor(this.constructorArgTypes) :
|
||||
proxyClass.getDeclaredConstructor());
|
||||
ReflectionUtils.makeAccessible(ctor);
|
||||
proxyInstance = (this.constructorArgs != null ?
|
||||
proxyClass.getConstructor(this.constructorArgTypes).newInstance(this.constructorArgs) :
|
||||
proxyClass.newInstance());
|
||||
ctor.newInstance(this.constructorArgs) : ctor.newInstance());
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new AopConfigException("Unable to instantiate proxy using Objenesis, " +
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.springframework.aop.DynamicIntroductionAdvice;
|
||||
import org.springframework.aop.IntroductionInterceptor;
|
||||
import org.springframework.aop.ProxyMethodInvocation;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Convenient implementation of the
|
||||
@@ -131,7 +132,7 @@ public class DelegatePerTargetObjectIntroductionInterceptor extends Introduction
|
||||
|
||||
private Object createNewDelegate() {
|
||||
try {
|
||||
return this.defaultImplType.newInstance();
|
||||
return ReflectionUtils.accessibleConstructor(this.defaultImplType).newInstance();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new IllegalArgumentException("Cannot create default implementation for '" +
|
||||
|
||||
Reference in New Issue
Block a user