티스토리 뷰
Remove this "BigDecimal" constructor
수정 전
public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (null == value) {
jgen.writeNull();
} else {
jgen.writeNumber(new BigDecimal(value).setScale(2, BigDecimal.ROUND_DOWN));
}
}
수정 후
public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (null == value) {
jgen.writeNull();
} else {
jgen.writeNumber(BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_DOWN));
}
}
Constructors should not be used to instantiate "String", "BigInteger", "BigDecimal" and primitive-wrapper classes (squid:S2129)
Constructors for Strings
, BigInteger
, BigDecimal
and the objects used to wrap primitives should never be used. Doing so is less clear and uses more memory than simply using the desired value in the case of strings, and using valueOf
for everything else.
Further, these constructors are deprecated in Java 9, which is an indication that they will eventually be removed from the language altogether.
Noncompliant Code Example
String empty = new String(); // Noncompliant; yields essentially "", so just use that. String nonempty = new String("Hello world"); // Noncompliant Double myDouble = new Double(1.1); // Noncompliant; use valueOf Integer integer = new Integer(1); // Noncompliant Boolean bool = new Boolean(true); // Noncompliant BigInteger bigInteger1 = new BigInteger("3"); // Noncompliant BigInteger bigInteger2 = new BigInteger("9223372036854775807"); // Noncompliant BigInteger bigInteger3 = new BigInteger("111222333444555666777888999"); // Compliant, greater than Long.MAX_VALUE BigDecimal bigDecimal = new BigDecimal(1.1); // Noncompliant
Compliant Solution
String empty = ""; String nonempty = "Hello world"; Double myDouble = Double.valueOf(1.1); Integer integer = Integer.valueOf(1); Boolean bool = Boolean.valueOf(true); BigInteger bigInteger1 = BigInteger.valueOf(3); BigInteger bigInteger2 = BigInteger.valueOf(9223372036854775807L); BigInteger bigInteger3 = new BigInteger("111222333444555666777888999"); BigDecimal bigDecimal = BigDecimal.valueOf(1.1);
'리팩토링(스프링기반)' 카테고리의 다른 글
java.nio.Files을 이용한 파일 삭제 (0) | 2019.01.08 |
---|---|
private 생성자 추가 (0) | 2019.01.08 |
동일 처리를 하는 catch문 합치기 (0) | 2019.01.08 |
private method의 @Transactional 제거 (0) | 2019.01.08 |
Map 전체 내용을 읽을 때 Key, Value를 모두 사용한다면 "entrySet"을 사용하라 (0) | 2019.01.08 |
- Total
- Today
- Yesterday
- mybatis
- SHEETJS
- spring
- oracle
- thymeleaf
- AG-GRID
- restful서비스
- 스프링
- lombok
- 엑셀
- 메시지
- Spring Boot
- java
- 타임리프
- ag grid
- UI
- Javascript
- REST
- example
- listToMap
- RESTful
- springboot
- 그리드
- 스프링부트
- 설정
- 샘플
- cache
- 예제
- sample
- mapToList
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |