Instead of this loop, you just want:

  Ref r = fetchRes.getAdvertisedRef(remoteBranchName);
  if (r == null)
    r = fetchRes.getAdvertisedRef(R_HEADS + remoteBranchName);

Then you can use r.getObjectId() to tell you what the object is.