{
  "id": "java/classcastexception-proxy-invocationhandler",
  "signature": "java.lang.ClassCastException: class com.sun.proxy.$Proxy0 cannot be cast to class com.example.MyInterface (com.sun.proxy.$Proxy0 is in module java.base of loader 'app'; com.example.MyInterface is in unnamed module of loader 'app')",
  "signature_zh": "java.lang.ClassCastException：类com.sun.proxy.$Proxy0无法转换为类com.example.MyInterface（com.sun.proxy.$Proxy0在加载器'app'的模块java.base中；com.example.MyInterface在加载器'app'的未命名模块中）",
  "regex": "java\\.lang\\.ClassCastException: class com\\.sun\\.proxy\\.\\$Proxy\\d+ cannot be cast to class \\S+",
  "domain": "java",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "This error occurs when a JDK dynamic proxy (Proxy.newProxyInstance) is created for an interface that is not visible to the proxy's class loader, typically because the interface is loaded by a different class loader or the proxy class is from a different module.",
  "root_cause_type": "generic",
  "root_cause_zh": "当为代理的类加载器不可见的接口创建JDK动态代理（Proxy.newProxyInstance）时发生，通常因为接口由不同的类加载器加载或代理类来自不同模块。",
  "versions": [
    {
      "version": "Java 8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java 11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Java 17",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Spring Framework 5.3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Spring Framework 6.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Casting the proxy to the concrete class instead of the interface",
      "why_fails": "JDK dynamic proxies only implement interfaces, not concrete classes; casting to a concrete class will always fail.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "Adding the interface to the classpath multiple times in different jars",
      "why_fails": "Multiple class loaders loading the same interface cause type identity issues; the proxy implements one version, but the cast expects another.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using CGLIB proxies instead without changing the interface visibility",
      "why_fails": "CGLIB creates subclass proxies, which also require the target class to be accessible; the root cause (class loader mismatch) remains.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure the interface is loaded by the same class loader as the proxy: `ClassLoader commonLoader = Thread.currentThread().getContextClassLoader(); MyInterface proxy = (MyInterface) Proxy.newProxyInstance(commonLoader, new Class[]{MyInterface.class}, handler);`",
      "success_rate": 0.85,
      "how": "Ensure the interface is loaded by the same class loader as the proxy: `ClassLoader commonLoader = Thread.currentThread().getContextClassLoader(); MyInterface proxy = (MyInterface) Proxy.newProxyInstance(commonLoader, new Class[]{MyInterface.class}, handler);`",
      "condition": "",
      "sources": []
    },
    {
      "action": "For Spring applications, configure proxy-target-class=\"true\" to use CGLIB proxies instead of JDK dynamic proxies: `<aop:aspectj-autoproxy proxy-target-class=\"true\"/>` or `@EnableAspectJAutoProxy(proxyTargetClass = true)`.",
      "success_rate": 0.9,
      "how": "For Spring applications, configure proxy-target-class=\"true\" to use CGLIB proxies instead of JDK dynamic proxies: `<aop:aspectj-autoproxy proxy-target-class=\"true\"/>` or `@EnableAspectJAutoProxy(proxyTargetClass = true)`.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Move the interface definition to a shared library jar that is loaded by the common class loader (e.g., the bootstrap or extension class loader) to avoid class loader isolation.",
      "success_rate": 0.8,
      "how": "Move the interface definition to a shared library jar that is loaded by the common class loader (e.g., the bootstrap or extension class loader) to avoid class loader isolation.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Ensure the interface is loaded by the same class loader as the proxy: `ClassLoader commonLoader = Thread.currentThread().getContextClassLoader(); MyInterface proxy = (MyInterface) Proxy.newProxyInstance(commonLoader, new Class[]{MyInterface.class}, handler);`",
    "For Spring applications, configure proxy-target-class=\"true\" to use CGLIB proxies instead of JDK dynamic proxies: `<aop:aspectj-autoproxy proxy-target-class=\"true\"/>` or `@EnableAspectJAutoProxy(proxyTargetClass = true)`.",
    "Move the interface definition to a shared library jar that is loaded by the common class loader (e.g., the bootstrap or extension class loader) to avoid class loader isolation."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/reflect/Proxy.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2023-11-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}