Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sneaky-throws in clone() method #161

Open
drekbour opened this issue Mar 8, 2019 · 0 comments
Open

Use sneaky-throws in clone() method #161

drekbour opened this issue Mar 8, 2019 · 0 comments

Comments

@drekbour
Copy link
Contributor

drekbour commented Mar 8, 2019

Currently PB clone() wraps the (checked) CloneNotSupportedException with InternalError. As of Java 8, the following "sneaky throws" allows the code to propagate the checked exception directly.

  @Override
  @GwtIncompatible
  public Object clone() {
    return _clone();
  }

  private <T extends Throwable> Object _clone() throws T {
    try {
      BookBuilder result = (BookBuilder)super.clone();
      result.self = result;
      return result;
    } catch (CloneNotSupportedException e) {
      throw (T)e; // "Sneaky-throws" CloneNotSupportedException
    }
  }

It's also worth discussing if clone() should be suppressing at all. It's fine for but() to do this but I don't follow why we're trying to reduce the public method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant