Sensitivity analysis for periodic orbits and quasiperiodic invariant tori using the adjoint method