java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String

Spring JIRA | Kazuki Shimizu | 1 year ago
  1. 0

    When handler method's return type is final, CGLIB error is occurred at the time of calling method of controller instance which generates via {{MvcUriComponentsBuilder#on}}. As follows: {code:java|title=Controller} @RequestMapping("messages") public class MessageRestController { @RequestMapping("{id}") public String getMessage(@PathVariable("id") String id) { return "dummy message by " + id; } @RequestMapping("{id}") public ResponseEntity<String> getMessageWithEntity(@PathVariable("id") String id) { return ResponseEntity.ok("dummy message by " + id); } } {code} {code:java|title=TestCase} private UriComponentsBuilder baseUri = UriComponentsBuilder.fromHttpUrl("http://example.com"); /** * NG Pattern. */ @Test public void testReturnString() { MessageRestController controller = on(MessageRestController.class); controller.getMessage("MSG0001"); URI uri = MvcUriComponentsBuilder.relativeTo(baseUri).withMethodCall(controller).build().toUri(); assertThat(uri, is(URI.create("http://example.com/messages/MSG0001"))); } /** * OK Pattern. */ @Test public void testReturnStringWithEntity() { MessageRestController controller = on(MessageRestController.class); controller.getMessageWithEntity("MSG0001"); URI uri = MvcUriComponentsBuilder.relativeTo(baseUri).withMethodCall(controller).build().toUri(); assertThat(uri, is(URI.create("http://example.com/messages/MSG0001"))); } {code} Error of NG pattern is as follow: {code:console|title=Stack Trace} java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:446) at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216) at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377) at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:317) at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.initProxy(MvcUriComponentsBuilder.java:615) at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.access$000(MvcUriComponentsBuilder.java:89) at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder$ControllerMethodInvocationInterceptor.intercept(MvcUriComponentsBuilder.java:724) at org.springframework.issues.ReproTests$MessageRestController$$EnhancerBySpringCGLIB$$d08e95b6.getMessage(<generated>) at org.springframework.issues.ReproTests.testReturnString(ReproTests.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) {code}

    Spring JIRA | 1 year ago | Kazuki Shimizu
    java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String
  2. 0

    When handler method's return type is final, CGLIB error is occurred at the time of calling method of controller instance which generates via {{MvcUriComponentsBuilder#on}}. As follows: {code:java|title=Controller} @RequestMapping("messages") public class MessageRestController { @RequestMapping("{id}") public String getMessage(@PathVariable("id") String id) { return "dummy message by " + id; } @RequestMapping("{id}") public ResponseEntity<String> getMessageWithEntity(@PathVariable("id") String id) { return ResponseEntity.ok("dummy message by " + id); } } {code} {code:java|title=TestCase} private UriComponentsBuilder baseUri = UriComponentsBuilder.fromHttpUrl("http://example.com"); /** * NG Pattern. */ @Test public void testReturnString() { MessageRestController controller = on(MessageRestController.class); controller.getMessage("MSG0001"); URI uri = MvcUriComponentsBuilder.relativeTo(baseUri).withMethodCall(controller).build().toUri(); assertThat(uri, is(URI.create("http://example.com/messages/MSG0001"))); } /** * OK Pattern. */ @Test public void testReturnStringWithEntity() { MessageRestController controller = on(MessageRestController.class); controller.getMessageWithEntity("MSG0001"); URI uri = MvcUriComponentsBuilder.relativeTo(baseUri).withMethodCall(controller).build().toUri(); assertThat(uri, is(URI.create("http://example.com/messages/MSG0001"))); } {code} Error of NG pattern is as follow: {code:console|title=Stack Trace} java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:446) at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216) at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377) at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:317) at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.initProxy(MvcUriComponentsBuilder.java:615) at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.access$000(MvcUriComponentsBuilder.java:89) at org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder$ControllerMethodInvocationInterceptor.intercept(MvcUriComponentsBuilder.java:724) at org.springframework.issues.ReproTests$MessageRestController$$EnhancerBySpringCGLIB$$d08e95b6.getMessage(<generated>) at org.springframework.issues.ReproTests.testReturnString(ReproTests.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) {code}

    Spring JIRA | 1 year ago | Kazuki Shimizu
    java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String
  3. 0

    DummyInvocationUtils doesn't like String return values

    GitHub | 3 years ago | chrylis
    java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    DummyInvocationUtils can't handle String return values

    GitHub | 3 years ago | chrylis
    java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String
  6. 0

    Spring Hateoas Causing an Exception "Cannot subclass final class void"

    Stack Overflow | 2 years ago | node42
    java.lang.IllegalArgumentException: Cannot subclass final class void

  1. serious2monkeys 2 times, last 4 months ago
3 unregistered visitors
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. java.lang.IllegalArgumentException

    Cannot subclass final class class java.lang.String

    at org.springframework.cglib.proxy.Enhancer.generateClass()
  2. Spring Core
    Enhancer.createClass
    1. org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
    2. org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    3. org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    4. org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    5. org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
    5 frames
  3. Spring MVC
    MvcUriComponentsBuilder$ControllerMethodInvocationInterceptor.intercept
    1. org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.initProxy(MvcUriComponentsBuilder.java:615)
    2. org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.access$000(MvcUriComponentsBuilder.java:89)
    3. org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder$ControllerMethodInvocationInterceptor.intercept(MvcUriComponentsBuilder.java:724)
    3 frames
  4. org.springframework.issues
    ReproTests.testReturnString
    1. org.springframework.issues.ReproTests$MessageRestController$$EnhancerBySpringCGLIB$$d08e95b6.getMessage(<generated>)
    2. org.springframework.issues.ReproTests.testReturnString(ReproTests.java:33)
    2 frames
  5. Java RT
    DelegatingMethodAccessorImpl.invoke
    1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    3 frames
  6. JUnit
    JUnitCore.run
    1. org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    2. org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    3. org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    4. org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    5. org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    6. org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    7. org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    8. org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    9. org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    10. org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    11. org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    12. org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    13. org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    14. org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    14 frames
  7. IntelliJ junit4 module
    JUnit4IdeaTestRunner.startRunnerWithArgs
    1. com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    1 frame
  8. IDEA
    JUnitStarter.main
    1. com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    2. com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    2 frames
  9. Java RT
    NativeMethodAccessorImpl.invoke
    1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    2 frames
  10. IDEA
    AppMain.main
    1. com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
    1 frame