1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
import org .springframework .objenesis .ObjenesisStd ;
27
27
28
28
/**
29
- * Objenesis based extension of {@link CglibAopProxy} to create proxy instances without
30
- * invoking the constructor of the class.
29
+ * Objenesis- based extension of {@link CglibAopProxy} to create proxy instances
30
+ * without invoking the constructor of the class.
31
31
*
32
32
* @author Oliver Gierke
33
+ * @author Juergen Hoeller
33
34
* @since 4.0
34
35
*/
35
36
@ SuppressWarnings ("serial" )
@@ -41,8 +42,8 @@ class ObjenesisCglibAopProxy extends CglibAopProxy {
41
42
42
43
43
44
/**
44
- * Creates a new {@link ObjenesisCglibAopProxy} using the given {@link AdvisedSupport} .
45
- * @param config must not be {@literal null}.
45
+ * Create a new ObjenesisCglibAopProxy for the given AOP configuration .
46
+ * @param config the AOP configuration as AdvisedSupport object
46
47
*/
47
48
public ObjenesisCglibAopProxy (AdvisedSupport config ) {
48
49
super (config );
@@ -53,18 +54,32 @@ public ObjenesisCglibAopProxy(AdvisedSupport config) {
53
54
@ Override
54
55
@ SuppressWarnings ("unchecked" )
55
56
protected Object createProxyClassAndInstance (Enhancer enhancer , Callback [] callbacks ) {
57
+ Class <?> proxyClass = enhancer .createClass ();
58
+ Object proxyInstance = null ;
59
+
56
60
try {
57
- Factory factory = (Factory ) this .objenesis .newInstance (enhancer .createClass ());
58
- factory .setCallbacks (callbacks );
59
- return factory ;
61
+ proxyInstance = this .objenesis .newInstance (proxyClass );
60
62
}
61
63
catch (ObjenesisException ex ) {
62
- // Fallback to regular proxy construction on unsupported JVMs
63
- if (logger .isDebugEnabled ()) {
64
- logger .debug ("Unable to instantiate proxy using Objenesis, falling back to regular proxy construction" , ex );
64
+ logger .debug ("Unable to instantiate proxy using Objenesis, " +
65
+ "falling back to regular proxy construction" , ex );
66
+ }
67
+
68
+ if (proxyInstance == null ) {
69
+ // Regular instantiation via default constructor...
70
+ try {
71
+ proxyInstance = (this .constructorArgs != null ?
72
+ proxyClass .getConstructor (this .constructorArgTypes ).newInstance (this .constructorArgs ) :
73
+ proxyClass .newInstance ());
74
+ }
75
+ catch (Exception ex ) {
76
+ throw new AopConfigException ("Unable to instantiate proxy using Objenesis, " +
77
+ "and regular proxy instantiation via default constructor fails as well" , ex );
65
78
}
66
- return super .createProxyClassAndInstance (enhancer , callbacks );
67
79
}
80
+
81
+ ((Factory ) proxyInstance ).setCallbacks (callbacks );
82
+ return proxyInstance ;
68
83
}
69
84
70
85
}
0 commit comments