GÜNCELLEME çözüm Java.lang.reflect.Proxy returning another proxy from invocation results in ClassCastException on assignmentkutu vekil bazı sınıfları ama diğerlerini
Testim kodu vekiller java.sql.Connection
olduğunu.
ben şöyle my proxy oluşturmak: Bir H2 DB bağlantısı sararken
log.info("connection is "+connection.getClass().getName()+", "+
(connection instanceof Connection));
Object proxy = java.lang.reflect.Proxy.newProxyInstance(
connection.getClass().getClassLoader(),
connection.getClass().getInterfaces(),
new MockFailureWrapper(connection));
log.info("proxy is "+proxy.getClass().getName()+", "+
(proxy instanceof Connection));
return (Connection)proxy;
, bu mükemmel çalışıyor. Ben sarma olduğum connection
tip Connection
ait olsa bile
ben denemek ve dönüş başarısız bir MySQL bağlantısı, Connection
için proxy döküm sarın
connection is org.h2.jdbc.JdbcConnection, true
proxy is $Proxy9, true
Ve MySQL bağlantısı için
:connection is com.mysql.jdbc.JDBC4Connection, true
proxy is $Proxy11, false
Neler oluyor ve neden olamaz
java.lang.ClassCastException: $Proxy11 cannot be cast to java.sql.Connection
bir H2 bağlantısı için günlük çizgidir MySQL DB bağlantılarını mu? Bu sadece size doğrudan vekil istiyoruz sınıfı tarafından uygulanan arabirimleri verir
connection.getClass().getInterfaces()
:
Bize 'İstisna'yı'/'Hatayı' gösterir lütfen. –
@AdamArold klasik CastCastException; – Will
sorusuna ekledi. Kodunuzu iade ekstresinde hata ayıkladığınızda ne görüyorsunuz? Proxy'nin türü nedir? –