Fixed a bug using proxies with the JDK API, found by DanielAdolfsson

Cherry pick from master.