/** * Gets the method to invoke trying to minimize the cost of reflection reusing objects cached * in static members. * * @param socket Instance of the socket to use in connection with server. * @return Method to call to set a write timeout in the socket. */ private static Method getMethod(Socket socket) { final Class<?> socketClass = socket.getClass(); final WriteTimeoutEnforcer instance = mSingleInstance.get(); if (instance == null) { return initFrom(socketClass); } else if (instance.mSocketClassRef.get() != socketClass) { // the underlying class changed return initFrom(socketClass); } else if (instance.mSetSoWriteTimeoutMethodRef == null) { // method not supported return null; } else { final Method cachedSetSoWriteTimeoutMethod = instance.mSetSoWriteTimeoutMethodRef.get(); return (cachedSetSoWriteTimeoutMethod == null) ? initFrom(socketClass) : cachedSetSoWriteTimeoutMethod ; } }