forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Objenesis proxy creation.
Extended DefaultAopProxyFactory to create Objenesis based proxies if the library is on the classpath. This allows classes without a default constructor being CGLib proxied. We're now falling back to original CGLib based behavior in case the proxy creation using Objenesis fails. Objenesis 2.0 is now inlined into spring-core to avoid interfering with other Objenesis versions on the classpath. Issue: SPR-10594
- Loading branch information
Showing
10 changed files
with
289 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
spring-aop/src/main/java/org/springframework/aop/framework/ObjenesisCglibAopProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2002-2013 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 | ||
* | ||
* http://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.aop.framework; | ||
|
||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.cglib.proxy.Callback; | ||
import org.springframework.cglib.proxy.Enhancer; | ||
import org.springframework.cglib.proxy.Factory; | ||
import org.springframework.objenesis.ObjenesisException; | ||
import org.springframework.objenesis.ObjenesisStd; | ||
|
||
/** | ||
* Objenesis based extension of {@link CglibAopProxy} to create proxy instances without | ||
* invoking the constructor of the class. | ||
* | ||
* @author Oliver Gierke | ||
* @since 4.0 | ||
*/ | ||
class ObjenesisCglibAopProxy extends CglibAopProxy { | ||
|
||
private static final Log logger = LogFactory.getLog(ObjenesisCglibAopProxy.class); | ||
|
||
private final ObjenesisStd objenesis; | ||
|
||
|
||
/** | ||
* Creates a new {@link ObjenesisCglibAopProxy} using the given {@link AdvisedSupport}. | ||
* @param config must not be {@literal null}. | ||
*/ | ||
public ObjenesisCglibAopProxy(AdvisedSupport config) { | ||
super(config); | ||
this.objenesis = new ObjenesisStd(true); | ||
} | ||
|
||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) { | ||
try { | ||
Factory factory = (Factory) objenesis.newInstance(enhancer.createClass()); | ||
factory.setCallbacks(callbacks); | ||
return factory; | ||
} | ||
catch (ObjenesisException ex) { | ||
// Fallback to Cglib on unsupported JVMs | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("Unable to instantiate proxy using Objenesis, falling back " | ||
+ "to regular proxy construction", ex); | ||
} | ||
return super.createProxyClassAndInstance(enhancer, callbacks); | ||
} | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
spring-aop/src/test/java/org/springframework/aop/framework/ClassWithConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2002-2013 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 | ||
* | ||
* http://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.aop.framework; | ||
|
||
public class ClassWithConstructor { | ||
|
||
public ClassWithConstructor(Object object) { | ||
|
||
} | ||
|
||
public void method() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...-context/src/test/java/org/springframework/aop/framework/ClassWithComplexConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2002-2013 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 | ||
* | ||
* http://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.aop.framework; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* @author Oliver Gierke | ||
*/ | ||
@Component | ||
public class ClassWithComplexConstructor { | ||
|
||
private final Dependency dependency; | ||
|
||
@Autowired | ||
public ClassWithComplexConstructor(Dependency dependency) { | ||
Assert.notNull(dependency); | ||
this.dependency = dependency; | ||
} | ||
|
||
public Dependency getDependency() { | ||
return dependency; | ||
} | ||
|
||
public void method() { | ||
this.dependency.method(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
spring-context/src/test/java/org/springframework/aop/framework/Dependency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2002-2013 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 | ||
* | ||
* http://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.aop.framework; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component class Dependency { | ||
|
||
private int value = 0; | ||
|
||
public void method() { | ||
value++; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
} |
Oops, something went wrong.