Skip to content

Instantly share code, notes, and snippets.

@mbjelac
Created November 17, 2017 09:22
Show Gist options
  • Save mbjelac/faee5b754558f2243bf550989b85c75c to your computer and use it in GitHub Desktop.
Save mbjelac/faee5b754558f2243bf550989b85c75c to your computer and use it in GitHub Desktop.
Lombok @nonnull doesn't work for local variables (by design)
import lombok.NonNull;
import lombok.Value;
import lombok.val;
import org.junit.Test;
import static org.junit.Assert.fail;
public class LombokNonNullTest {
@Value
class Bean {
String message;
}
class SomeCode {
void process(Bean bean) {
@NonNull
val message = bean.getMessage();
// ... go on happily because message cannot be null
}
}
@Test
public void nonNull_works_on_local_variable() {
try {
new SomeCode().process(new Bean(null));
fail();
} catch (IllegalArgumentException expected) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment