Java 类io.grpc.internal.ServerImpl 实例源码

项目:heroic    文件:GrpcRpcProtocolServer.java   
/**
 * Extract the local address from the current server.
 * <p>
 * Because no api is available to accomplish this, it currently uses a very ugly reflexive
 * approach.
 *
 * @param server Server to extract local address from.
 * @return an InetSocketAddress
 * @throws Exception if something goes wrong (which it should).
 */
private InetSocketAddress extractInetSocketAddress(final Server server) throws Exception {
    final ServerImpl impl = (ServerImpl) server;

    final Field transportServerField = ServerImpl.class.getDeclaredField("transportServer");
    transportServerField.setAccessible(true);
    final Object transportServer = transportServerField.get(impl);

    final Field channelField = transportServer.getClass().getDeclaredField("channel");
    channelField.setAccessible(true);
    final Channel channel = (Channel) channelField.get(transportServer);

    return (InetSocketAddress) channel.localAddress();
}