PDA

View Full Version : Creating mixin fails if target has no default constructor



swisswheel
Jul 1st, 2005, 10:57 AM
Hello,

Is it true that the target object of a mixin has to have a default constructor? I'm using cglib 2.1_2 and spring 1.2.1. I've got following code to create a mixin:



Exception e = ...
String stackTrace = ...
ProxyFactory factory = new ProxyFactory();
factory.setProxyTargetClass(true); // Enforce CGLIB usage
factory.setTarget(e); // Exception which has to be wrapped

MockControl stControl
= MockControl.createControl(ServerSideStackTrace.cla ss);
ServerSideStackTrace st
= (ServerSideStackTrace) stControl.getMock();
st.getServiceSideStackTrace();
stControl.setReturnValue(stackTrace, 0, Integer.MAX_VALUE);
stControl.replay();

factory.addAdvisor(0, new DefaultIntroductionAdvisor(
new DelegatingIntroductionInterceptor(st),
ServerSideStackTrace.class));

Exception wrappedException
= (Exception) factory.getProxy();

stControl.verify();
return wrappedException;


I've got the interface ServerSideStackTrace which has a method getServiceSideStackTrace to return a string. If the given exception e has a default constructor this code works. If not factory.getProxy() throws following exception:



java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhan cer.java:712)
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer .java:499)
at net.sf.cglib.transform.TransformingClassGenerator. generateClass(TransformingClassGenerator.java:33)
at net.sf.cglib.core.DefaultGeneratorStrategy.generat e(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(Ab stractClassGenerator.java:215)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer. java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:2 85)
at org.springframework.aop.framework.Cglib2AopProxy.g etProxy(Cglib2AopProxy.java:194)
at org.springframework.aop.framework.Cglib2AopProxy.g etProxy(Cglib2AopProxy.java:150)
at org.springframework.aop.framework.ProxyFactory.get Proxy(ProxyFactory.java:67)
at ch.elca.leaf3.services.remoting.protocol.soap.axis .faulthandling.DefaultHandler.enrichExceptionWithS tackTrace(DefaultHandler.java:291)
at ch.elca.leaf3.services.remoting.protocol.soap.axis .faulthandling.DefaultHandler.translateToBusinessE xception(DefaultHandler.java:196)
at ch.elca.leaf3.services.remoting.protocol.soap.axis .faulthandling.SoapExceptionManager.translateToBus inessException(SoapExceptionManager.java:179)
at ch.elca.leaf3.services.remoting.protocol.soap.axis .faulthandling.ClientSoapInvocationHandler.findAnd TranslateAxisFault(ClientSoapInvocationHandler.jav a:96)
at ch.elca.leaf3.services.remoting.protocol.soap.axis .faulthandling.ClientSoapInvocationHandler.findAnd TranslateAxisFault(ClientSoapInvocationHandler.jav a:98)
at ch.elca.leaf3.services.remoting.protocol.soap.axis .faulthandling.ClientSoapInvocationHandler.invoke( ClientSoapInvocationHandler.java:75)
at $Proxy2.throwMeASpecialException(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at ch.elca.leaf3.services.remoting.ClientContextInvoc ationHandler.invoke(ClientContextInvocationHandler .java:126)
at $Proxy3.throwMeASpecialException(Unknown Source)
at ch.elca.leaf3.tests.remoting.CalculatorSoapTest.te stSpecialExceptionBehaviour(CalculatorSoapTest.jav a:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154 )
at junit.framework.TestCase.runBare(TestCase.java:127 )
at junit.framework.TestResult$1.protect(TestResult.ja va:106)
at junit.framework.TestResult.runProtected(TestResult .java:124)
at junit.framework.TestResult.run(TestResult.java:109 )
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:436)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:311)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:192)


Does anybody see what I'm doing wrong?
Thanks for your help!

Cheers,
Martin

katentim
Jul 3rd, 2005, 04:46 AM
I think this is a limitation of CGLIB - see this JIRA entry (http://opensource.atlassian.com/projects/spring/browse/SPR-285).

You can try using interfaces + JDK dynamic proxy.