From 53dbcdf35c9fa6a0db7bf29516008e1e08bcc12b Mon Sep 17 00:00:00 2001 From: Marc Prud'hommeaux Date: Fri, 15 Sep 2006 22:12:05 +0000 Subject: [PATCH] Add a better error message when casting an instance to PersistenceCapable fails due to the PersistenceCapable interface being loaded by two separate ClassLoaders git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@446769 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/openjpa/kernel/BrokerImpl.java | 12 ++++++++++++ .../org/apache/openjpa/kernel/localizer.properties | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java index 9b47499bf..e7ce0fdeb 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java @@ -4140,6 +4140,18 @@ public class BrokerImpl if (obj instanceof PersistenceCapable) return (PersistenceCapable) obj; + // check for difference instances of the PersistenceCapable interface + // and throw a better error that mentions the class loaders + Class[] intfs = obj.getClass().getInterfaces(); + for (int i = 0; intfs != null && i < intfs.length; i++) { + if (intfs[i].getName().equals(PersistenceCapable.class.getName())) { + throw new UserException(_loc.get("pc-loader-different", + Exceptions.toString(obj), + PersistenceCapable.class.getClassLoader(), + intfs[i].getClassLoader())).setFailedObject(obj); + } + } + // not enhanced throw new UserException(_loc.get("pc-cast", Exceptions.toString(obj))).setFailedObject(obj); diff --git a/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties b/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties index e817c643e..eedb15dc7 100644 --- a/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties +++ b/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties @@ -48,6 +48,10 @@ marked-rollback: The transaction cannot be committed, because it was already \ marked for rollback only. The transaction will be rolled back instead. refresh-flushed: You cannot refresh an instance that has been flushed to the \ data store. +pc-loader-different: Attempt to cast instance "{0}" to PersistenceCapable failed. \ + The object implemented org.apache.openjpa.enhance.PersistenceCapable, \ + but the instance of that interface was loaded by two different ClassLoaders: \ + "{1}" and "{2}". pc-cast: Attempt to cast instance "{0}" to PersistenceCapable failed. Ensure \ that it has been enhanced. del-instance: The instance of type "{0}" with oid "{1}" no longer exists in \