(注:このブログはもう更新していません)この日記は私的なものであり所属会社の見解とは無関係です。 GitHub: takahashikzn

[クラウド帳票エンジンDocurain]

java.lang.Errorにキャストできない?

JPAのエンティティリスナー実行時にこんなエラーが出ました。
java.lang.Errorにキャストできない?何だそりゃ?

java.lang.ClassCastException: jp.root42.r42fw.service.ServiceException cannot be cast to java.lang.Error
	at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityListener.invokeMethod(EntityListener.java:257) ~[org.eclipse.persistence.jpa-2.3.1.jar:2.3.1.v20111018-r10243]
	at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityListener.invokeMethod(EntityListener.java:272) ~[org.eclipse.persistence.jpa-2.3.1.jar:2.3.1.v20111018-r10243]
	at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityListener.postUpdate(EntityListener.java:358) ~[org.eclipse.persistence.jpa-2.3.1.jar:2.3.1.v20111018-r10243]


で、該当する部分のコードを見てみると…

/**
 * INTERNAL:
 */
void invokeMethod(Method method, Object onObject, Object[] objectList, DescriptorEvent event) {
    if (method != null) {
        try {
            ...(略)
        } catch (IllegalAccessException exception) {
            throw ValidationException.invalidCallbackMethod(onObject.getClass(), method.toString());
        } catch (InvocationTargetException e) {
            Throwable cause = e.getCause();
            
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;    
            } else {
                throw (Error) cause; // ←ココ!!Error決め打ちにしている。
            }
        }
    }
}

... (ノ∀`)アチャー

なんでこんなバグが。。。
って言うか、エンティティリスナーって使ってる人少ないのかな?だからこんなバグが見過ごされているとか。