ダウト

昨日のエントリから。

なお、上記では取得したMXBeanをインタフェースに直接キャストしているが、実際にはMBeanServerConnection#isInstanceOfメソッドを使って、現在接続しているMXBeanの型を前もってチェックすることで、InstanceNotFoundExceptionを捕捉できるようになる。

ObjectName on = new ObjectName("com.kazz.impl:type=ICodeBaseAgent");
MBeanServerConnection connection = connector.getMBeanServerConnection();
if (connection.isInstanceOf(on, "com.kazz.ICodeBaseAgent ")) {
    ICodeBaseAgent agent = (ICodeBaseAgent)MBeanServerInvocationHandler.newProxyInstance(connection, on, ICodeBaseAgent.class, false);
}

MBeanServerInvocationHandler.newProxyInstanceはジェネリクスなメソッドであり、第3パラメタで指定した型に内部でキャスト(Class#cast)されるため、ここではキャストは一切不要だ。

ObjectName on = new ObjectName("com.kazz.impl:type=ICodeBaseAgent");
MBeanServerConnection connection = connector.getMBeanServerConnection();
if (connection.isInstanceOf(on, "com.kazz.ICodeBaseAgent ")) {
    ICodeBaseAgent agent = MBeanServerInvocationHandler.newProxyInstance(connection, on, ICodeBaseAgent.class, false);
}

失礼しました。