├── .editorconfig ├── .gitignore ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── .travis.yml ├── LICENSE ├── README.md ├── documentation └── Roadmap.md ├── ezgif-3-db5218262c.gif ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main └── java │ └── com │ └── rainerhahnekamp │ └── sneakythrow │ ├── Sneaky.java │ └── functional │ ├── SneakyBiConsumer.java │ ├── SneakyBiFunction.java │ ├── SneakyBiPredicate.java │ ├── SneakyBinaryOperator.java │ ├── SneakyConsumer.java │ ├── SneakyFunction.java │ ├── SneakyPredicate.java │ ├── SneakyRunnable.java │ ├── SneakySupplier.java │ └── SneakyUnaryOperator.java └── test └── java └── com └── rainerhahnekamp └── sneakythrow ├── BiPredicateTest.java ├── BinaryOperatorTest.java ├── ConstructorTest.java ├── ConsumerTest.java ├── FunctionTest.java ├── PredicateTest.java ├── RunnableTest.java ├── SneakyBiConsumerTest.java ├── SneakyBiFunctionTest.java ├── SneakyTest.java ├── SupplierTest.java ├── TutorialTest.java └── UnaryOperatorTest.java /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_file = lf 5 | insert_final_newline = true 6 | charset = utf-8 7 | indent_style = space 8 | indent_size = 2 9 | continuation_indent_size = 4 10 | trim_trailing_whitespace = true 11 | 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | /target/ 3 | /.idea/ 4 | /sneakythrow.iml 5 | /settings.xml 6 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rainerhahnekamp/sneakythrow/093a88fb0674ee441c33e31910fe814d5a0542d3/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | language: java 3 | 4 | before_install: 5 | - chmod +x mvnw 6 | 7 | after_success: 8 | - mvn clean test jacoco:report coveralls:report 9 | 10 | jdk: 11 | - oraclejdk8 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018-present, Rainer Hahnekamp 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SneakyThrow 2 | 3 | [![Build Status](https://travis-ci.org/rainerhahnekamp/sneakythrow.svg?branch=master)](https://travis-ci.org/rainerhahnekamp/sneakythrow.svg?branch=master) 4 | [![Coverage Status](https://coveralls.io/repos/github/rainerhahnekamp/sneakythrow/badge.svg?branch=master)](https://coveralls.io/github/rainerhahnekamp/sneakythrow?branch=master) 5 | [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) 6 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.rainerhahnekamp/sneakythrow/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.rainerhahnekamp/sneakythrow) 7 | [![Javadocs](https://www.javadoc.io/badge/com.rainerhahnekamp/sneakythrow.svg)](https://www.javadoc.io/doc/com.rainerhahnekamp/sneakythrow) 8 | 9 | SneakyThrow is a Java library to ignore checked exceptions. You can integrate it using maven: 10 | 11 | ```xml 12 | 13 | com.rainerhahnekamp 14 | sneakythrow 15 | 1.2.0 16 | 17 | ``` 18 | 19 | ![SneakyThrow Usage GIF](https://github.com/rainerhahnekamp/sneakythrow/blob/master/ezgif-3-db5218262c.gif) 20 | 21 | 22 | ## Usage 23 | 24 | Without SneakyThrow: 25 | ```java 26 | URL url; 27 | try { 28 | url = new URL("https://www.hahnekamp.com"); 29 | } catch (MalformedURLException mue) { 30 | throw new RuntimeException(mue); 31 | } 32 | ``` 33 | With SneakyThrow: 34 | ```java 35 | URL url = sneak(() -> new URL("https://www.hahnekamp.com")); 36 | ``` 37 | ## Usage with Java 8 Streams 38 | ```java 39 | private URL createURL(String url) throws MalformedURLException { 40 | return new URL(url); 41 | } 42 | ``` 43 | 44 | The function above used within a Stream without SneakyThrow: 45 | ```java 46 | Stream 47 | .of("https://www.hahnekamp.com", "https://www.austria.info") 48 | .map(url -> { 49 | try { 50 | return this.createURL(url); 51 | } catch (MalformedURLException mue) { 52 | throw new RuntimeException(mue); 53 | } 54 | }) 55 | .collect(Collectors.toList()); 56 | ``` 57 | Again with SneakyThrow: 58 | 59 | ```java 60 | Stream 61 | .of("https://www.hahnekamp.com", "https://www.austria.info") 62 | .map(sneaked(this::createURL)) 63 | .collect(Collectors.toList()); 64 | ``` 65 | The static method `sneaked` wraps each function, that has the same signature as a functional interface (java.util.functional). 66 | 67 | **Please note the difference between `sneak` and `sneaked`.** 68 | ## How it works 69 | 70 | This project is heavily influenced by [ThrowingFunction](https://github.com/pivovarit/ThrowingFunction). 71 | 72 | In SneakyThrow, each functional interface, defined in `java.util.function`, has an equivalent one with the same signature. The only difference is, that these "Sneaky Functional Interfaces" throw exceptions. This gives us the possibility to write lambdas or similar code that also throws exceptions. 73 | 74 | Both `sneak` and `sneaked` wrap the passed "Sneaky Functional Interfaces" into a try/catch clause and return the equivalent `java.util.function` interface. In the case of `sneak`, execution and the return of the result is done immediately. 75 | 76 | 77 | -------------------------------------------------------------------------------- /documentation/Roadmap.md: -------------------------------------------------------------------------------- 1 | # Planned Feature List 2 | * Support for Java 9 modules 3 | * improve how to run code with returning void 4 | 5 | -------------------------------------------------------------------------------- /ezgif-3-db5218262c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rainerhahnekamp/sneakythrow/093a88fb0674ee441c33e31910fe814d5a0542d3/ezgif-3-db5218262c.gif -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 204 | if [ "$MVNW_VERBOSE" = true ]; then 205 | echo $MAVEN_PROJECTBASEDIR 206 | fi 207 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 208 | 209 | # For Cygwin, switch paths to Windows format before running java 210 | if $cygwin; then 211 | [ -n "$M2_HOME" ] && 212 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 213 | [ -n "$JAVA_HOME" ] && 214 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 215 | [ -n "$CLASSPATH" ] && 216 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 217 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 218 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 219 | fi 220 | 221 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 222 | 223 | exec "$JAVACMD" \ 224 | $MAVEN_OPTS \ 225 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 226 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 227 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 228 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | 121 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 122 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 123 | 124 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 125 | if ERRORLEVEL 1 goto error 126 | goto end 127 | 128 | :error 129 | set ERROR_CODE=1 130 | 131 | :end 132 | @endlocal & set ERROR_CODE=%ERROR_CODE% 133 | 134 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 135 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 136 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 137 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 138 | :skipRcPost 139 | 140 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 141 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 142 | 143 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 144 | 145 | exit /B %ERROR_CODE% 146 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.rainerhahnekamp 8 | sneakythrow 9 | 1.2.0 10 | jar 11 | 12 | SneakyThrow 13 | Java library to ignore checked exceptions 14 | https://github.com/rainerhahnekamp/sneakythrow 15 | 16 | 17 | 18 | Rainer Hahnekamp 19 | rainer.hahnekamp@gmail.com 20 | https://www.rainerhahnekamp.com/ 21 | 22 | 23 | 24 | 25 | 26 | MIT License 27 | http://www.opensource.org/licenses/mit-license.php 28 | repo 29 | 30 | 31 | 32 | 33 | https://github.com/rainerhahnekamp/sneakythrow 34 | scm:git:git@github.com:rainerhahnekamp/sneakythrow.git 35 | 36 | 37 | scm:git:git@github.com:rainerhahnekamp/sneakythrhow.git 38 | 39 | 40 | 41 | 42 | https://github.com/rainerhahnekamp/sneakythrow/issues 43 | GitHub Issues 44 | 45 | 46 | 47 | 1.8 48 | UTF-8 49 | UTF-8 50 | 3.0 51 | 52 | 53 | 54 | 55 | org.junit.jupiter 56 | junit-jupiter-engine 57 | 5.2.0 58 | test 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | maven-surefire-plugin 67 | 2.21.0 68 | 69 | 70 | org.junit.platform 71 | junit-platform-surefire-provider 72 | 1.2.0 73 | 74 | 75 | 76 | 77 | 78 | org.apache.maven.plugins 79 | maven-compiler-plugin 80 | 3.7.0 81 | 82 | 1.8 83 | 1.8 84 | 85 | 86 | 87 | 88 | com.theoryinpractise 89 | googleformatter-maven-plugin 90 | 1.6.4 91 | 92 | 93 | reformat-sources 94 | 95 | false 96 | 97 | false 98 | false 99 | false 100 | 101 | 102 | format 103 | 104 | process-sources 105 | 106 | 107 | 108 | 109 | 110 | org.eluder.coveralls 111 | coveralls-maven-plugin 112 | 4.3.0 113 | 114 | 115 | 116 | org.jacoco 117 | jacoco-maven-plugin 118 | 0.8.1 119 | 120 | 121 | 122 | 123 | 124 | prepare-agent 125 | 126 | prepare-agent 127 | 128 | 129 | 130 | post-unit-test 131 | test 132 | 133 | check 134 | report 135 | 136 | 137 | 138 | 139 | 140 | 141 | org.apache.maven.plugins 142 | maven-project-info-reports-plugin 143 | 2.9 144 | 145 | 146 | 147 | 148 | 149 | 150 | release 151 | 152 | 153 | 154 | ossrh 155 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | org.apache.maven.plugins 164 | maven-source-plugin 165 | 3.0.1 166 | 167 | 168 | attach-sources 169 | 170 | jar-no-fork 171 | 172 | 173 | 174 | 175 | 176 | 177 | org.apache.maven.plugins 178 | maven-javadoc-plugin 179 | 3.0.0 180 | 181 | 182 | attach-javadocs 183 | 184 | jar 185 | 186 | 187 | 188 | 189 | 190 | 191 | org.apache.maven.plugins 192 | maven-gpg-plugin 193 | 1.5 194 | 195 | 196 | sign-artifacts 197 | verify 198 | 199 | sign 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/Sneaky.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import com.rainerhahnekamp.sneakythrow.functional.SneakyBiConsumer; 28 | import com.rainerhahnekamp.sneakythrow.functional.SneakyBiFunction; 29 | import com.rainerhahnekamp.sneakythrow.functional.SneakyBiPredicate; 30 | import com.rainerhahnekamp.sneakythrow.functional.SneakyBinaryOperator; 31 | import com.rainerhahnekamp.sneakythrow.functional.SneakyConsumer; 32 | import com.rainerhahnekamp.sneakythrow.functional.SneakyFunction; 33 | import com.rainerhahnekamp.sneakythrow.functional.SneakyPredicate; 34 | import com.rainerhahnekamp.sneakythrow.functional.SneakyRunnable; 35 | import com.rainerhahnekamp.sneakythrow.functional.SneakySupplier; 36 | import com.rainerhahnekamp.sneakythrow.functional.SneakyUnaryOperator; 37 | 38 | import java.util.function.BiConsumer; 39 | import java.util.function.BiFunction; 40 | import java.util.function.BiPredicate; 41 | import java.util.function.BinaryOperator; 42 | import java.util.function.Consumer; 43 | import java.util.function.Function; 44 | import java.util.function.Predicate; 45 | import java.util.function.Supplier; 46 | import java.util.function.UnaryOperator; 47 | 48 | /** 49 | * Code that throws checked exceptions can be executed with the static methods of this class without 50 | * catching or throwing them. 51 | * 52 | *

A function or lambda that throws an exception can be executed via {@link 53 | * #sneak(SneakySupplier)} method. For example: 54 | * 55 | *

 56 |  *     URL austriaInfo = sneak(() -> new URL("https://www.austria.info");}
57 | * 58 | *

If a function should only be wrapped, then the {@link #sneaked(SneakyRunnable)} & 59 | * overloads can be used: 60 | * 61 | *

 62 |  *     public void createUrls() {
 63 |  *         Stream.of("https://www.austria.info")
 64 |  *             .map(sneaked(this::toURL))
 65 |  *             .collect(Collectors.toList());
 66 |  *     }
 67 |  *
 68 |  *     private URL toURL(String url) throws MalformedURLException {
 69 |  *         return new URL(url);
 70 |  *     }
 71 |  * 
72 | * 73 | * @author Rainer Hahnekamp {@literal } 74 | */ 75 | public class Sneaky { 76 | /** 77 | * returns a value from a lambda (Supplier) that can potentially throw an exception. 78 | * 79 | * @param supplier Supplier that can throw an exception 80 | * @param type of supplier's return value 81 | * @return a Supplier as defined in java.util.function 82 | */ 83 | public static T sneak(SneakySupplier supplier) { 84 | return sneaked(supplier).get(); 85 | } 86 | 87 | /** 88 | * Sneaky throws a BiConsumer lambda. 89 | * 90 | * @param biConsumer BiConsumer that can throw an exception 91 | * @param type of first argument 92 | * @param type of the second argument 93 | * @return a BiConsumer as defined in java.util.function 94 | */ 95 | public static BiConsumer sneaked( 96 | SneakyBiConsumer biConsumer) { 97 | return (t, u) -> { 98 | @SuppressWarnings("unchecked") 99 | SneakyBiConsumer castedBiConsumer = 100 | (SneakyBiConsumer) biConsumer; 101 | castedBiConsumer.accept(t, u); 102 | }; 103 | } 104 | 105 | /** 106 | * Sneaky throws a BiFunction lambda. 107 | * 108 | * @param biFunction BiFunction that can throw an exception 109 | * @param type of first argument 110 | * @param type of second argument 111 | * @param return type of biFunction 112 | * @return a BiFunction as defined in java.util.function 113 | */ 114 | public static BiFunction sneaked( 115 | SneakyBiFunction biFunction) { 116 | return (t, u) -> { 117 | @SuppressWarnings("unchecked") 118 | SneakyBiFunction castedBiFunction = 119 | (SneakyBiFunction) biFunction; 120 | return castedBiFunction.apply(t, u); 121 | }; 122 | } 123 | 124 | /** 125 | * Sneaky throws a BinaryOperator lambda. 126 | * 127 | * @param binaryOperator BinaryOperator that can throw an exception 128 | * @param type of the two arguments and the return type of the binaryOperator 129 | * @return a BinaryOperator as defined in java.util.function 130 | */ 131 | public static BinaryOperator sneaked( 132 | SneakyBinaryOperator binaryOperator) { 133 | return (t1, t2) -> { 134 | @SuppressWarnings("unchecked") 135 | SneakyBinaryOperator castedBinaryOperator = 136 | (SneakyBinaryOperator) binaryOperator; 137 | return castedBinaryOperator.apply(t1, t2); 138 | }; 139 | } 140 | 141 | /** 142 | * Sneaky throws a BiPredicate lambda. 143 | * 144 | * @param biPredicate BiPredicate that can throw an exception 145 | * @param type of first argument 146 | * @param type of second argument 147 | * @return a BiPredicate as defined in java.util.function 148 | */ 149 | public static BiPredicate sneaked( 150 | SneakyBiPredicate biPredicate) { 151 | return (t, u) -> { 152 | @SuppressWarnings("unchecked") 153 | SneakyBiPredicate castedBiPredicate = 154 | (SneakyBiPredicate) biPredicate; 155 | return castedBiPredicate.test(t, u); 156 | }; 157 | } 158 | 159 | /** 160 | * Sneaky throws a Consumer lambda. 161 | * 162 | * @param consumer Consumer that can throw an exception 163 | * @param type of first argument 164 | * @return a Consumer as defined in java.util.function 165 | */ 166 | public static Consumer sneaked(SneakyConsumer consumer) { 167 | return t -> { 168 | @SuppressWarnings("unchecked") 169 | SneakyConsumer casedConsumer = 170 | (SneakyConsumer) consumer; 171 | casedConsumer.accept(t); 172 | }; 173 | } 174 | 175 | /** 176 | * Sneaky throws a Function lambda. 177 | * 178 | * @param function Function that can throw an exception 179 | * @param type of first argument 180 | * @param type of the second argument 181 | * @return a Function as defined in java.util.function 182 | */ 183 | public static Function sneaked( 184 | SneakyFunction function) { 185 | return t -> { 186 | @SuppressWarnings("unchecked") 187 | SneakyFunction f1 = (SneakyFunction) function; 188 | return f1.apply(t); 189 | }; 190 | } 191 | 192 | /** 193 | * Sneaky throws a Predicate lambda. 194 | * 195 | * @param predicate Predicate that can throw an exception 196 | * @param type of first argument 197 | * @return a Predicate as defined in java.util.function 198 | */ 199 | public static Predicate sneaked(SneakyPredicate predicate) { 200 | return t -> { 201 | @SuppressWarnings("unchecked") 202 | SneakyPredicate castedSneakyPredicate = 203 | (SneakyPredicate) predicate; 204 | return castedSneakyPredicate.test(t); 205 | }; 206 | } 207 | 208 | /** 209 | * Sneaky throws a Runnable lambda. 210 | * 211 | * @param runnable Runnable that can throw an exception 212 | * @return a Runnable as defined in java.util.function 213 | */ 214 | public static Runnable sneaked(SneakyRunnable runnable) { 215 | return () -> { 216 | @SuppressWarnings("unchecked") 217 | SneakyRunnable castedRunnable = (SneakyRunnable) runnable; 218 | castedRunnable.run(); 219 | }; 220 | } 221 | 222 | /** 223 | * Sneaky throws a Supplier lambda. 224 | * 225 | * @param supplier Supplier that can throw an exception 226 | * @param type of supplier's return value 227 | * @return a Supplier as defined in java.util.function 228 | */ 229 | public static Supplier sneaked(SneakySupplier supplier) { 230 | return () -> { 231 | @SuppressWarnings("unchecked") 232 | SneakySupplier castedSupplier = 233 | (SneakySupplier) supplier; 234 | return castedSupplier.get(); 235 | }; 236 | } 237 | 238 | /** 239 | * Sneaky throws a UnaryOperator lambda. 240 | * 241 | * @param unaryOperator UnaryOperator that can throw an exception 242 | * @param type of unaryOperator's argument and returned value 243 | * @return a UnaryOperator as defined in java.util.function 244 | */ 245 | public static UnaryOperator sneaked( 246 | SneakyUnaryOperator unaryOperator) { 247 | return t -> { 248 | @SuppressWarnings("unchecked") 249 | SneakyUnaryOperator castedUnaryOperator = 250 | (SneakyUnaryOperator) unaryOperator; 251 | return castedUnaryOperator.apply(t); 252 | }; 253 | } 254 | } 255 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyBiConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow.functional; 26 | 27 | @FunctionalInterface 28 | public interface SneakyBiConsumer { 29 | void accept(T t, U u) throws E; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyBiFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow.functional; 26 | 27 | @FunctionalInterface 28 | public interface SneakyBiFunction { 29 | R apply(T t, U u) throws E; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyBiPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow.functional; 26 | 27 | @FunctionalInterface 28 | public interface SneakyBiPredicate { 29 | boolean test(T t, U u) throws E; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyBinaryOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow.functional; 26 | 27 | @FunctionalInterface 28 | public interface SneakyBinaryOperator 29 | extends SneakyBiFunction {} 30 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow.functional; 26 | 27 | @FunctionalInterface 28 | public interface SneakyConsumer { 29 | void accept(T t) throws E; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow.functional; 26 | 27 | @FunctionalInterface 28 | public interface SneakyFunction { 29 | R apply(T t) throws E; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyPredicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow.functional; 26 | 27 | @FunctionalInterface 28 | public interface SneakyPredicate { 29 | boolean test(T t) throws E; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyRunnable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow.functional; 26 | 27 | @FunctionalInterface 28 | public interface SneakyRunnable { 29 | void run() throws E; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakySupplier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow.functional; 26 | 27 | @FunctionalInterface 28 | public interface SneakySupplier { 29 | T get() throws E; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyUnaryOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow.functional; 26 | 27 | @FunctionalInterface 28 | public interface SneakyUnaryOperator extends SneakyFunction {} 29 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/BiPredicateTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 28 | import static java.lang.Integer.parseInt; 29 | import static org.junit.jupiter.api.Assertions.assertThrows; 30 | import static org.junit.jupiter.api.Assertions.assertTrue; 31 | 32 | import java.util.function.BiPredicate; 33 | 34 | import org.junit.jupiter.api.Test; 35 | 36 | public class BiPredicateTest { 37 | @Test 38 | public void withoutException() { 39 | assertTrue(execute(sneaked((Integer a, String b) -> a == parseInt(b)))); 40 | } 41 | 42 | @Test 43 | public void withException() { 44 | assertThrows( 45 | ArithmeticException.class, 46 | () -> execute(sneaked((Integer a, String b) -> (a / 0) == parseInt(b)))); 47 | } 48 | 49 | private boolean execute(BiPredicate biPredicate) { 50 | return biPredicate.test(2, "2"); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/BinaryOperatorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 28 | import static org.junit.jupiter.api.Assertions.assertEquals; 29 | import static org.junit.jupiter.api.Assertions.assertThrows; 30 | 31 | import org.junit.jupiter.api.Test; 32 | 33 | import java.util.function.BinaryOperator; 34 | 35 | class BinaryOperatorTest { 36 | @Test 37 | public void withoutException() { 38 | assertEquals(5, execute(sneaked((Integer a, Integer b) -> a / b), 25, 5)); 39 | } 40 | 41 | @Test 42 | public void withException() { 43 | assertThrows( 44 | ArithmeticException.class, () -> execute(sneaked((Integer a, Integer b) -> a / b), 25, 0)); 45 | } 46 | 47 | private int execute(BinaryOperator binaryOperator, Integer a, Integer b) { 48 | return binaryOperator.apply(a, b); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/ConstructorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static org.junit.jupiter.api.Assertions.assertTrue; 28 | 29 | import org.junit.jupiter.api.Test; 30 | 31 | public class ConstructorTest { 32 | @Test 33 | public void newInstance() { 34 | assertTrue(new Sneaky() instanceof Sneaky); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/ConsumerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 28 | import static org.junit.jupiter.api.Assertions.assertEquals; 29 | import static org.junit.jupiter.api.Assertions.assertThrows; 30 | 31 | import java.util.ArrayList; 32 | import java.util.Collections; 33 | import java.util.List; 34 | import java.util.function.Consumer; 35 | 36 | import org.junit.jupiter.api.Test; 37 | 38 | public class ConsumerTest { 39 | @Test 40 | public void withoutException() { 41 | Consumer> consumer = 42 | sneaked( 43 | (List list) -> { 44 | list.add(5); 45 | }); 46 | 47 | List list = new ArrayList<>(); 48 | consumer.accept(list); 49 | 50 | assertEquals(1, list.size()); 51 | } 52 | 53 | @Test 54 | public void withException() { 55 | Consumer> consumer = 56 | sneaked( 57 | (List list) -> { 58 | list.add(5); 59 | }); 60 | 61 | assertThrows( 62 | UnsupportedOperationException.class, () -> consumer.accept(Collections.emptyList())); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/FunctionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 28 | import static java.lang.Integer.parseInt; 29 | import static org.junit.jupiter.api.Assertions.assertEquals; 30 | import static org.junit.jupiter.api.Assertions.assertThrows; 31 | 32 | import org.junit.jupiter.api.Test; 33 | 34 | import java.util.function.Function; 35 | 36 | class FunctionTest { 37 | @Test 38 | void withoutException() { 39 | Function function = sneaked((String b) -> parseInt(b) * 2); 40 | assertEquals(10, (int) function.apply("5")); 41 | } 42 | 43 | @Test 44 | void withRuntimeException() { 45 | Function function = sneaked((String b) -> parseInt(b) * 2); 46 | 47 | assertThrows(NumberFormatException.class, () -> function.apply("foo")); 48 | } 49 | 50 | @Test 51 | void withException() { 52 | Function function = sneaked(this::exceptionOn1); 53 | assertThrows(Exception.class, () -> function.apply(1)); 54 | } 55 | 56 | private int exceptionOn1(int a) throws Exception { 57 | if (a == 1) { 58 | throw new Exception("not working"); 59 | } else { 60 | return a; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/PredicateTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 28 | import static java.lang.Integer.parseInt; 29 | import static org.junit.jupiter.api.Assertions.assertThrows; 30 | import static org.junit.jupiter.api.Assertions.assertTrue; 31 | 32 | import java.util.function.Predicate; 33 | 34 | import org.junit.jupiter.api.Test; 35 | 36 | public class PredicateTest { 37 | @Test 38 | public void withoutException() { 39 | Predicate predicate = sneaked((String a) -> 2 == parseInt(a)); 40 | 41 | assertTrue(predicate.test("2")); 42 | } 43 | 44 | @Test 45 | public void withException() { 46 | Predicate predicate = sneaked((String a) -> 2 == parseInt(a)); 47 | 48 | assertThrows(NumberFormatException.class, () -> predicate.test("foo")); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/RunnableTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 28 | import static org.junit.jupiter.api.Assertions.assertEquals; 29 | import static org.junit.jupiter.api.Assertions.assertThrows; 30 | 31 | import java.util.ArrayList; 32 | import java.util.Collections; 33 | import java.util.List; 34 | 35 | import org.junit.jupiter.api.Test; 36 | 37 | public class RunnableTest { 38 | @Test 39 | public void withoutException() { 40 | List list = new ArrayList(); 41 | Runnable runnable = 42 | sneaked( 43 | () -> { 44 | list.add(5); 45 | }); 46 | runnable.run(); 47 | 48 | assertEquals(1, list.size()); 49 | } 50 | 51 | @Test 52 | public void withException() { 53 | List list = Collections.emptyList(); 54 | Runnable runnable = 55 | sneaked( 56 | () -> { 57 | list.add(5); 58 | }); 59 | 60 | assertThrows(UnsupportedOperationException.class, () -> runnable.run()); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/SneakyBiConsumerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 28 | import static org.junit.jupiter.api.Assertions.assertEquals; 29 | import static org.junit.jupiter.api.Assertions.assertThrows; 30 | 31 | import com.rainerhahnekamp.sneakythrow.functional.SneakyBiConsumer; 32 | import org.junit.jupiter.api.Test; 33 | 34 | import java.util.ArrayList; 35 | import java.util.Collections; 36 | import java.util.List; 37 | import java.util.function.BiConsumer; 38 | 39 | public class SneakyBiConsumerTest { 40 | @Test 41 | public void withoutException() { 42 | executeAndAssert( 43 | sneaked((SneakyBiConsumer, Integer, RuntimeException>) List::add)); 44 | } 45 | 46 | @Test 47 | public void withException() { 48 | List list = Collections.emptyList(); 49 | assertThrows( 50 | ArithmeticException.class, 51 | () -> 52 | executeAndAssert( 53 | sneaked( 54 | (List l, Integer e) -> { 55 | list.add(e / 0); 56 | }))); 57 | } 58 | 59 | private void executeAndAssert(BiConsumer, Integer> consumer) { 60 | List list = new ArrayList<>(); 61 | consumer.accept(list, 5); 62 | assertEquals(1, list.size()); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/SneakyBiFunctionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 28 | import static java.lang.Integer.parseInt; 29 | import static org.junit.jupiter.api.Assertions.assertEquals; 30 | import static org.junit.jupiter.api.Assertions.assertThrows; 31 | 32 | import java.util.function.BiFunction; 33 | 34 | import org.junit.jupiter.api.Test; 35 | 36 | public class SneakyBiFunctionTest { 37 | @Test 38 | public void withoutException() { 39 | assertEquals(500, execute(sneaked((Integer a, String b) -> parseInt(b) * a), 5, "100")); 40 | } 41 | 42 | @Test 43 | public void withException() { 44 | assertThrows( 45 | NumberFormatException.class, 46 | () -> execute(sneaked((Integer a, String b) -> parseInt(b) * a), 5, "foo")); 47 | } 48 | 49 | private int execute(BiFunction biFunction, Integer a, String b) { 50 | return biFunction.apply(a, b); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/SneakyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneak; 28 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 29 | import static org.junit.jupiter.api.Assertions.assertEquals; 30 | import static org.junit.jupiter.api.Assertions.assertThrows; 31 | 32 | import java.net.MalformedURLException; 33 | import java.net.URL; 34 | import java.util.List; 35 | import java.util.stream.Collectors; 36 | import java.util.stream.Stream; 37 | 38 | import org.junit.jupiter.api.Test; 39 | 40 | class SneakyTest { 41 | @Test 42 | public void withoutException() { 43 | assertEquals(String.class, sneak(() -> Class.forName("java.lang.String"))); 44 | 45 | List urls = 46 | Stream.of("www.hahnekamp.com", "www.orf.at") 47 | .map(sneaked(this::createUrl)) 48 | .collect(Collectors.toList()); 49 | } 50 | 51 | private URL createUrl(String url) throws MalformedURLException { 52 | return new URL("https://www.hahnekamp.com"); 53 | } 54 | 55 | @Test 56 | public void withException() { 57 | assertThrows(ClassNotFoundException.class, () -> sneak(() -> Class.forName("java.string"))); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/SupplierTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 28 | import static org.junit.jupiter.api.Assertions.assertEquals; 29 | import static org.junit.jupiter.api.Assertions.assertThrows; 30 | 31 | import org.junit.jupiter.api.Test; 32 | 33 | import java.util.function.Supplier; 34 | 35 | public class SupplierTest { 36 | @Test 37 | public void withoutException() { 38 | Supplier supplier = sneaked(() -> 5); 39 | assertEquals(5, (int) supplier.get()); 40 | } 41 | 42 | @Test 43 | public void withException() { 44 | Supplier supplier = sneaked(() -> 5 / 0); 45 | assertThrows(ArithmeticException.class, supplier::get); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/TutorialTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneak; 28 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 29 | import static org.junit.jupiter.api.Assertions.assertEquals; 30 | 31 | import java.net.MalformedURLException; 32 | import java.net.URL; 33 | import java.util.List; 34 | import java.util.stream.Collectors; 35 | import java.util.stream.Stream; 36 | 37 | import org.junit.jupiter.api.Test; 38 | 39 | public class TutorialTest { 40 | @Test 41 | public void defaultUsage() { 42 | URL url = sneak(() -> new URL("https://www.hahnekamp.com")); 43 | assertEquals("https", url.getProtocol()); 44 | } 45 | 46 | @Test 47 | public void streamUsage() { 48 | List urls = 49 | Stream.of("https://www.hahnekamp.com", "https://www.austria.info") 50 | .map(sneaked(this::createUrl)) 51 | .collect(Collectors.toList()); 52 | 53 | assertEquals("www.hahnekamp.com", urls.get(0).getHost()); 54 | assertEquals("www.austria.info", urls.get(1).getHost()); 55 | } 56 | 57 | private URL createUrl(String url) throws MalformedURLException { 58 | return new URL(url); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/com/rainerhahnekamp/sneakythrow/UnaryOperatorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018-present, Rainer Hahnekamp 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.rainerhahnekamp.sneakythrow; 26 | 27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked; 28 | import static org.junit.jupiter.api.Assertions.assertEquals; 29 | import static org.junit.jupiter.api.Assertions.assertThrows; 30 | 31 | import java.util.function.UnaryOperator; 32 | 33 | import org.junit.jupiter.api.Test; 34 | 35 | public class UnaryOperatorTest { 36 | @Test 37 | public void withoutException() { 38 | UnaryOperator unaryOperator = sneaked((String name) -> "Hello " + name); 39 | assertEquals("Hello Sneaky", unaryOperator.apply("Sneaky")); 40 | } 41 | 42 | @Test 43 | public void withException() { 44 | UnaryOperator unaryOperator = 45 | sneaked( 46 | (String name) -> { 47 | if (name == null) { 48 | throw new NullPointerException(); 49 | } 50 | 51 | return "Hello " + name; 52 | }); 53 | 54 | assertThrows(NullPointerException.class, () -> unaryOperator.apply(null)); 55 | } 56 | } 57 | --------------------------------------------------------------------------------