So, I'm using an API which is a little unfriendly in certain ways. Basically, this API creates a resource which can be fetched later. This resource may or may not still exist when we go to fetch it later.
To fetch the previously created resource, you have to use a result guid like so:
String resultKey = "12345";
PersistedResult r = mFactory.getPersistedResult(resultKey);
Now, the tricky thing here is that getPersistedResult
does NOT throw an exception when called with an invalid guid... PersistedResult
is a lazy loader and will only fail when one of its methods is called (causing the object to load itself).
So, to try and determine whether or not the resource is valid, I'm doing the following:
PersistedResult r = null;
if (!StringUtils.isEmpty(resultKey)) {
try {
r = mFactory.getPersistedResult(resultKey);
r.getResultCount(); // Triggers exception if result key was invalid.
} catch (Exception e) {
// handle exception
}
}
Is my call to getResultCount
at risk of being optimized out because I'm not using the value?
Calling any method on PersistedResult
goes to an external database, just in case that matters.
The compiler can't assume that getResultCount() has no side-effects -- therefore it can't remove the call.