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

Stack Overflow | checketts | 4 months 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

    DummyInvocationUtils doesn't like String return values

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

    Automated exception search integrated into your IDE

  4. 0

    DummyInvocationUtils can't handle String return values

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

    GitHub comment 35#155794417

    GitHub | 1 year ago | clay227
    java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String

    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 java.lang.String

      at org.springframework.cglib.proxy.Enhancer.generateClass()
    2. Spring Core
      AbstractClassGenerator$ClassLoaderData$3.apply
      1. org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:565)
      2. org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
      3. org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:329)
      4. org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492)
      5. org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
      6. org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
      6 frames
    3. org.springframework.cglib
      LoadingCache$2.call
      1. org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
      1 frame
    4. Java RT
      FutureTask.run
      1. java.util.concurrent.FutureTask.run(FutureTask.java:266)
      1 frame
    5. org.springframework.cglib
      LoadingCache.get
      1. org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
      2. org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
      2 frames
    6. Spring Core
      Enhancer.createClass
      1. org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
      2. org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
      3. org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
      4. org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337)
      4 frames
    7. Spring MVC
      MvcUriComponentsBuilder$ControllerMethodInvocationInterceptor.intercept
      1. org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.initProxy(MvcUriComponentsBuilder.java:611)
      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:725)
      3 frames