Skip to content

Instantly share code, notes, and snippets.

@odrotbohm
Last active August 29, 2015 14:24
Show Gist options
  • Save odrotbohm/10c30a0e39972464db88 to your computer and use it in GitHub Desktop.
Save odrotbohm/10c30a0e39972464db88 to your computer and use it in GitHub Desktop.
class Context<T extends Property> {
Path<T> getPath(String dotPath) { … } // foo.bar.foobar
}
class Path<T> { … }
class ResultException { // ResultException<T> is not allowed
Path<T> getResolvedPath() { … }
}
try
Path<T> path = context.getResult(…);
} catch (ResultException ex) {
Path<T> resolvedPath = ex.getResolvedPath();
}

The idea is to have code to be able to turn a segmented String path (e.g. foo.bar.foobbar) into a Path<T> where T is a generic subtype of a well-known one. If the dot path contains an unresolvable segment I’d like to throw an exception that contains the path that already has been resolved. However it doesn’t seem like I could get the generic type of the Path<T> into the exception and have to resort to the common supertype of T.

I guess I’ll resort to keep the String path that could be resolved in the exception, so that clients can do the following:

try
  Path<T> path = context.getResult(…);
} catch (ResultException ex) {
  Path<T> resolvedPath = context.getPath(ex.getResolvedPath());
}

Slightly more involved but should do the trick.

@odrotbohm
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment