ClassFormatError: Duplicate method name&signature from EnhancerBySpringCGLIB

3.7k Views Asked by At

I"m really stumped here. I've upgraded our Spring libraries from 4.0.6 to 4.3.2. One of our tests fail when running with 4.3.2. This is the code in question:

@Bean(name = SCHEDULER_FACTORY)
public SchedulerFactoryBean getSchedulerFactory()
{
    SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
    schedulerFactory.setConfigLocation(schedulerConfig);
    schedulerFactory.setResourceLoader(null);
    schedulerFactory.setDataSource(dataSource);
    schedulerFactory.setJobFactory(getSchedulerJobFactory());
    schedulerFactory.setAutoStartup(false);

    return schedulerFactory;
}

@Bean(name = SCHEDULER)
public Scheduler getScheduler()
{
    return getSchedulerFactory().getScheduler();
}

I'm getting the error java.lang.ClassFormatError: Duplicate method name&signature in class file org/springframework/scheduling/quartz/SchedulerFactoryBean$$EnhancerBySpringCGLIB$$bee87fe8$$EnhancerBySpringCGLIB$$6bb26669.

Running the test using spring 4.0.6 framework works perfectly fine, but with 4.3.2, it's failing. When using 4.0.6, I was using cglib no dependencies library. With 4.3.2, the tests fail regardless of whether or not I use cglib.

Spring embeds cglib and objensis into 4.3.* core library. "Furthermore, Spring Framework 4.3 embeds the updated ASM 5.1, CGLIB 3.2.4, and Objenesis 2.4 in spring-core.jar." SpringDocs

We were using Java 8 and cglib-no-dep 2.2 with Spring framework 4.0.6. We tried running this code with and without the standalone library and see the same results.

Stack trace:

  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.quartz.Scheduler]: Factory method 'getScheduler' threw exception; nested exception is org.springframework.cglib.core.CodeGenerationException:java.lang.reflect.InvocationTargetException  >null
           at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
           at org.springframework.beans.factory.support.ConstructorResolver$3.run(ConstructorResolver.java:582)
           at java.security.AccessController.doPrivileged(Native Method)
           at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579)
           ... 112 more
  Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException  >null
           at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:345)
           at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492)
           at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
  Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 188.486 sec
           at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
           at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
           at java.util.concurrent.FutureTask.run(FutureTask.java:266)
           at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
           at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
           at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
           at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
           at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
           at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337)
           at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.enhanceFactoryBean(ConfigurationClassEnhancer.java:452)
           at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:338)
           at com.example.SpringConfiguration$$EnhancerBySpringCGLIB$$54d3cb35.getSchedulerFactory(<generated>)
           at com.example.SpringConfiguration.getScheduler(SpringConfiguration.java:242)
           at com.example.SpringConfiguration$$EnhancerBySpringCGLIB$$54d3cb35.CGLIB$getScheduler$24(<generated>)
           at com.example.SpringConfiguration$$EnhancerBySpringCGLIB$$54d3cb35$$FastClassBySpringCGLIB$$a2a6e004.invoke(<generated>)
           at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
           at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
           at com.example.SpringConfiguration$$EnhancerBySpringCGLIB$$54d3cb35.getScheduler(<generated>)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
           at java.lang.reflect.Method.invoke(Method.java:498)
           at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
           ... 115 more
  Caused by: java.lang.reflect.InvocationTargetException
           at sun.reflect.GeneratedMethodAccessor26.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
           at java.lang.reflect.Method.invoke(Method.java:498)
           at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:413)
           at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:336)
           ... 140 more
  Caused by: java.lang.ClassFormatError: Duplicate method name&signature in class file org/springframework/scheduling/quartz/SchedulerFactoryBean$$EnhancerBySpringCGLIB$$bee87fe8$$EnhancerBySpringCGLIB$$6bb26669
           at java.lang.ClassLoader.defineClass1(Native Method)
           at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
           ... 145 more
1

There are 1 best solutions below

2
On

This is definetly a bug in cglib. It seems like it does not currectly determines a method in the type hierarchy to be equal to another method. Are you using Java 8 in combination with default methods in interfaces?

Cglib is not really tested for modern Java versions and experiences only little maintenance. Newer features sometimes cause trouble when using the library.