Skip to content

Commit

Permalink
Add helper method to rethrow checked as unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorncs committed Dec 2, 2020
1 parent 653a833 commit c047267
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.yahoo.config.provision.security.NodeIdentity;
import com.yahoo.jrt.Request;
import com.yahoo.jrt.SecurityContext;
import java.util.logging.Level;
import com.yahoo.security.tls.MixedMode;
import com.yahoo.security.tls.TransportSecurityUtils;
import com.yahoo.vespa.config.ConfigKey;
Expand All @@ -29,9 +28,11 @@
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.yahoo.vespa.config.server.rpc.security.AuthorizationException.*;
import static com.yahoo.vespa.config.server.rpc.security.AuthorizationException.Type;
import static com.yahoo.yolean.Exceptions.throwUnchecked;


/**
Expand Down Expand Up @@ -206,11 +207,6 @@ private RequestHandler getTenantHandler(TenantName tenantName) {
.orElseThrow(() -> new AuthorizationException(String.format("No handler exists for tenant '%s'", tenantName.value())));
}

@SuppressWarnings("unchecked")
private static <T extends Throwable> void throwUnchecked(Throwable t) throws T {
throw (T)t;
}

private enum JrtErrorCode {
UNAUTHORIZED(1),
AUTHORIZATION_FAILED(2);
Expand Down
6 changes: 6 additions & 0 deletions jdisc_http_service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.yahoo.vespa</groupId>
<artifactId>yolean</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<!-- TEST SCOPE -->
<dependency>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

import static com.yahoo.jdisc.http.HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED;
import static com.yahoo.jdisc.http.core.HttpServletRequestUtils.getConnection;
import static com.yahoo.jdisc.http.server.jetty.Exceptions.throwUnchecked;
import static com.yahoo.jdisc.http.server.jetty.JDiscHttpServlet.getConnector;
import static com.yahoo.yolean.Exceptions.throwUnchecked;

/**
* @author Simon Thoresen Hult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

import static com.yahoo.jdisc.http.server.jetty.Exceptions.throwUnchecked;
import static com.yahoo.jdisc.http.server.jetty.JDiscHttpServlet.getConnector;
import static com.yahoo.yolean.Exceptions.throwUnchecked;

/**
* Runs JDisc security filters for Servlets
Expand Down
3 changes: 2 additions & 1 deletion yolean/abi-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"public static void uncheckAndIgnore(com.yahoo.yolean.Exceptions$RunnableThrowingIOException, java.lang.Class)",
"public static java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException)",
"public static varargs java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.String, java.lang.String[])",
"public static java.lang.Object uncheckAndIgnore(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.Class)"
"public static java.lang.Object uncheckAndIgnore(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.Class)",
"public static java.lang.RuntimeException throwUnchecked(java.lang.Throwable)"
],
"fields": []
},
Expand Down
21 changes: 21 additions & 0 deletions yolean/src/main/java/com/yahoo/yolean/Exceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,25 @@ public static <R, T extends IOException> R uncheckAndIgnore(SupplierThrowingIOEx
public interface SupplierThrowingIOException<T> {
T get() throws IOException;
}

/**
* Allows treating checked exceptions as unchecked.
* Usage:
* throw throwUnchecked(e);
* The reason for the return type is to allow writing throw at the call site
* instead of just calling throwUnchecked. Just calling throwUnchecked
* means that the java compiler won't know that the statement will throw an exception,
* and will therefore complain on things such e.g. missing return value.
*/
public static RuntimeException throwUnchecked(Throwable e) {
throwUncheckedImpl(e);
return new RuntimeException(); // Non-null return value to stop tooling from complaining about potential NPE
}

@SuppressWarnings("unchecked")
private static <T extends Throwable> void throwUncheckedImpl(Throwable t) throws T {
throw (T)t;
}


}

0 comments on commit c047267

Please sign in to comment.