SI-4788 Respect RetentionPolicy of Java classes

Also closes SI-5420 and SI-5948.