Skip to content

Commit

Permalink
Merge pull request #465 from rockjoon/rockjoon
Browse files Browse the repository at this point in the history
[#463]Object.requireNonNull
  • Loading branch information
rockjoon authored Jan 26, 2021
2 parents 9c7e835 + b05e4d2 commit b2da4b9
Showing 1 changed file with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Objects.requireNonNull()๋ฅผ ์™œ ์“ธ๊นŒ?

`Objects.requireNonNull()` ๋ฉ”์„œ๋“œ๋Š” ์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”๋ฅผ ์ฝ๋‹ค ๋ณด๋ฉด ์ž์ฃผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
์ด๋ฆ„์„ ๋ณด๋ฉด ๋ฌด์—‡์„ ํ•˜๋Š”์ง€ ์–ผ์ถ” ์œ ์ถ”๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•ด๋ณธ ์ ์ด ์—†๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
๋•Œ๋ฌธ์— ์™ ์ง€ ๋ชจ๋ฅผ ํ˜ธ๊ธฐ์‹ฌ์ด ์ƒ๊ฒจ ์•Œ์•„ ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค.

## Objects.requireNonNull()์ด๋ž€?

์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ ์ด ๋ฉ”์„œ๋“œ๋Š” non-null์„ ํ‘œ์‹œํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

```java
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
```
requireNonNull๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€๋Š” ๋‹ค์†Œ ์ถฉ๊ฒฉ์ ์ด๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๋„ˆ๋ฌด๋‚˜๋„ **๋‹น์—ฐํ•œ** ์ฝ”๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

---

## ์™œ ์“ธ๊นŒ?
์ฐธ์กฐ ๊ฐ์ฒด๊ฐ€ ๋„์ผ ๋•Œ ์–ด์จ‹๋“  NPE๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์€ ๋‹น์—ฐํ•œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž˜ ์™€๋‹ฟ์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ํฌ๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

* explicity (๋ช…์‹œ์„ฑ)
* fail_fast

---

## explicit

๋‹ค์Œ๊ณผ ๊ฐ™์ด A๋ฅผ ์ฐธ์กฐํ•˜๋Š” Bํด๋ž˜์Šค๊ฐ€ ์žˆ์„ ๋•Œ
```java
public class A {

}
```

```java
public class B {
A a;

public B(A a) {
this.a = Objects.requireNonNull(a);
}
}
```
์ฝ”๋“œ ์ƒ์—์„œ A๊ฐ€ null์ด ์•„๋‹ˆ์–ด์•ผ ํ•จ์„ ๋ช…์‹œ์ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋”ฐ๋ผ์„œ ๊ณผ๊ฑฐ์— ์ง  ์ฝ”๋“œ๊ฐ€ ๋ฏธ๋ž˜์— ์‚ฌ์šฉ๋  ๋•Œ ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ null์ด๋ฉด ์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๊ฐœ๋ฐœ์ž๊ฐ€ **๋ช…์‹œ์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค.**

---

## fail-fast
`fail-fast`๋ž€ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•œ ์‹œ์ ์—์„œ ์ฆ‰์‹œ (ํ˜น์€ ์ตœ๋Œ€ํ•œ ๋น ๋ฅด๊ฒŒ) ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.
์œ„์˜ Bํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์ ์— ๋ฐ”๋กœ ์ต์…‰์…˜์ด ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค.
```java
A a = null;
B b = new B(a); // ์ƒ์„ฑ ์‹œ์ ์— ๋ฐ”๋กœ NPE ๋ฐœ์ƒ
```
---
๋ฐ˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด requireNonNull์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” ์–ด๋–จ๊นŒ?
```java
public class BfailSlow {

A a;

public BfailSlow(A a) {
this.a = a; //Objects.requireNonNull ์‚ฌ์šฉx
}

//...getter
}
```
๋ฐ”๋กœ ์ต์…‰์…˜์„ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  ์ดํ›„์— ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉ๋  ๋•Œ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
```java
A a = null;
BfailSlow b_slow = new BfailSlow(a);
b_slow.getA(); // ๊ฐ์ฒด ์ƒ์„ฑ ์ดํ›„์— ๋Šฆ๊ฒŒ NPE ๋ฐœ์ƒ
```
์ด๋Š” ์‹œ์Šคํ…œ์ด ๋ณต์žกํ•ด ์งˆ ์ˆ˜๋ก ์žฅ์• ๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ธฐ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

---
## ๊ธฐํƒ€ ์žฅ์ 
* **๋””๋ฒ„๊น…์ด ์šฉ์ด**ํ•ด์ง€๊ณ  ์•ˆ์ •์„ฑ์ด ๋†’์•„ ์ง„๋‹ค.

* ํ•ญ์ƒ ๊ฐ™์€ ์‹œ์ ์— ์ต์…‰์…˜์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์€ ์‹œ์Šคํ…œ์˜ **์ผ๊ด€์„ฑ**์„ ๋†’์ด๊ณ . ๊ฐœ๋ฐœ์ž๊ฐ€ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์— ๋” ์‹ ๊ฒฝ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

* NPE๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋˜์ง€๋Š” ๊ฒƒ์ด JVM์ด ๋ฐœ๊ฒฌํ•ด์„œ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ ๋ณด๋‹ค **์„ฑ๋Šฅ์ƒ์˜ ์ด์ **์ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

---

## vs Optional

### ๋ชฉ์ 

- Optional์€ **null์ผ์ง€๋„ ๋ชจ๋ฅด๋Š” ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ** ์ดˆ์ ์ด ๋งž์ถฐ์ ธ ์žˆ์ง€๋งŒ
- requireNonNull์€ ํ•ด๋‹น ์ฐธ์กฐ๊ฐ€ **null์ผ ๊ฒฝ์šฐ ์ฆ‰์‹œ ๊ฐœ๋ฐœ์ž์—๊ฒŒ ์•Œ๋ฆฌ๋Š” ๊ฒƒ**์ด ๋ชฉ์ ์ด๋‹ค.

### ์‚ฌ์šฉ ํ˜•ํƒœ

- Optional์€ ๋ฆฌํ„ดํƒ€์ž…์— ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋œ๋‹ค.
- requireNonNull์€ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ํฐ ์ œ์•ฝ ์—†์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

### requireNonNullElseGet
์ž๋ฐ”9๋ฒ„์ „ ๋ถ€ํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด์„œ Optional๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
```java
requireNonNullElseGet(T obj, Supplier<? extends T> supplier)
```

---

์ฐธ๊ณ 
* https://stackoverflow.com/questions/45632920/why-should-one-use-objects-requirenonnull
* https://stackoverflow.com/questions/43928556/why-explicitly-throw-a-nullpointerexception-rather-than-letting-it-happen-natura

0 comments on commit b2da4b9

Please sign in to comment.