Object的equals方法容易抛空指针异常,用java8的Objects.equals替换

This commit is contained in:
liuhuan 2019-12-28 18:58:10 +08:00
parent 3eedc8a761
commit 0a915248fc
11 changed files with 28 additions and 41 deletions

View File

@ -2,6 +2,8 @@ package cn.hutool.core.convert;
import cn.hutool.core.util.StrUtil;
import java.util.Objects;
/**
* 将浮点数类型的number转换成英语的表达方式 <br>
* 参考博客http://blog.csdn.net/eric_sunah/article/details/8713226
@ -68,7 +70,7 @@ public class NumberWordFormater {
StringBuilder lm = new StringBuilder(); // 用来存放转换後的整数部分
for (int i = 0; i < lstrrev.length() / 3; i++) {
a[i] = StrUtil.reverse(lstrrev.substring(3 * i, 3 * i + 3)); // 截取第一个叁位
if (!a[i].equals("000")) { // 用来避免这种情况1000000 = one million
if (!Objects.equals(a[i],"000")) { // 用来避免这种情况1000000 = one million
// thousand only
if (i != 0) {
lm.insert(0, transThree(a[i]) + " " + parseMore(i) + " "); // :
@ -134,7 +136,7 @@ public class NumberWordFormater {
String value;
if (s.startsWith("0")) {// 是否小於100
value = transTwo(s.substring(1));
} else if (s.substring(1).equals("00")) {// 是否被100整除
} else if (Objects.equals(s.substring(1),"00")) {// 是否被100整除
value = parseFirst(s.substring(0, 1)) + " HUNDRED";
} else {
value = parseFirst(s.substring(0, 1)) + " HUNDRED AND " + transTwo(s.substring(1));

View File

@ -5,18 +5,7 @@ import java.io.ObjectInputStream;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeSet;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
@ -688,7 +677,7 @@ class FastDateParser extends AbstractDateBasic implements DateParser {
for (final String[] zoneNames : zones) {
// offset 0 is the time zone ID and is not localized
final String tzId = zoneNames[ID];
if (tzId.equalsIgnoreCase("GMT")) {
if ("GMT".equalsIgnoreCase(tzId)) {
continue;
}
final TimeZone tz = TimeZone.getTimeZone(tzId);
@ -761,7 +750,7 @@ class FastDateParser extends AbstractDateBasic implements DateParser {
*/
@Override
void setCalendar(final FastDateParser parser, final Calendar cal, final String value) {
if (value.equals("Z")) {
if (Objects.equals(value,"Z")) {
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
} else {
cal.setTimeZone(TimeZone.getTimeZone("GMT" + value));

View File

@ -175,7 +175,7 @@ public class IdcardUtil {
return isValidCard15(idCard);
case 10: {// 10位身份证港澳台地区
String[] cardval = isValidCard10(idCard);
return null != cardval && cardval[2].equals("true");
return null != cardval && Objects.equals(cardval[2],"true");
}
default:
return false;
@ -280,9 +280,9 @@ public class IdcardUtil {
if (idCard.matches("^[a-zA-Z][0-9]{9}$")) { // 台湾
info[0] = "台湾";
String char2 = idCard.substring(1, 2);
if (char2.equals("1")) {
if (Objects.equals(char2,"1")) {
info[1] = "M";
} else if (char2.equals("2")) {
} else if (Objects.equals(char2,"2")) {
info[1] = "F";
} else {
info[1] = "N";

View File

@ -12,12 +12,7 @@ import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* 反射工具类
@ -652,7 +647,7 @@ public class ReflectUtil {
* @return 是否为equals方法
*/
public static boolean isEqualsMethod(Method method) {
if (method == null || false == method.getName().equals("equals")) {
if (method == null || false == Objects.equals(method.getName(),"equals")) {
return false;
}
final Class<?>[] paramTypes = method.getParameterTypes();
@ -666,7 +661,7 @@ public class ReflectUtil {
* @return 是否为hashCode方法
*/
public static boolean isHashCodeMethod(Method method) {
return (method != null && method.getName().equals("hashCode") && method.getParameterTypes().length == 0);
return (method != null && Objects.equals(method.getName(),"hashCode") && method.getParameterTypes().length == 0);
}
/**
@ -676,7 +671,7 @@ public class ReflectUtil {
* @return 是否为toString方法
*/
public static boolean isToStringMethod(Method method) {
return (method != null && method.getName().equals("toString") && method.getParameterTypes().length == 0);
return (method != null && Objects.equals(method.getName(),"toString") && method.getParameterTypes().length == 0);
}
// --------------------------------------------------------------------------------------------------------- newInstance

View File

@ -2,6 +2,8 @@ package cn.hutool.cron.pattern.parser;
import cn.hutool.cron.CronException;
import java.util.Objects;
/**
* 每月的几号值处理<br>
* 每月最多31天32和L都表示最后一天
@ -17,7 +19,7 @@ public class DayOfMonthValueParser extends SimpleValueParser {
@Override
public int parse(String value) throws CronException {
if (value.equalsIgnoreCase("L") || value.equals("32")) {// 每月最后一天
if ("L".equalsIgnoreCase(value) || Objects.equals("32",value)) {// 每月最后一天
return 32;
} else {
return super.parse(value);

View File

@ -38,7 +38,7 @@ public class DayOfWeekValueParser extends SimpleValueParser {
* @throws CronException 无效别名抛出此异常
*/
private int parseAlias(String value) throws CronException {
if(value.equalsIgnoreCase("L")){
if("L".equalsIgnoreCase(value)){
//最后一天为星期六
return ALIASES.length - 1;
}

View File

@ -1,9 +1,6 @@
package cn.hutool.db.sql;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.*;
/**
* SQL格式化器 from Hibernate
@ -129,7 +126,7 @@ public class SqlFormatter {
values();
} else if ("on".equals(this.lcToken)) {
on();
} else if ((this.afterBetween) && (this.lcToken.equals("and"))) {
} else if ((this.afterBetween) && (Objects.equals(this.lcToken,"and"))) {
misc();
this.afterBetween = false;
} else if (LOGICAL.contains(this.lcToken)) {

View File

@ -7,6 +7,7 @@ import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
@ -339,7 +340,7 @@ public class Ftp extends AbstractFtp {
childPath = StrUtil.format("{}/{}", dirPath, name);
if (ftpFile.isDirectory()) {
// 上级和本级目录除外
if (false == name.equals(".") && false == name.equals("..")) {
if (false == Objects.equals(name,".") && false == Objects.equals(name,"..")) {
delDir(childPath);
}
} else {

View File

@ -15,6 +15,7 @@ import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Vector;
/**
@ -311,7 +312,7 @@ public class Sftp extends AbstractFtp {
String fileName;
for (LsEntry entry : list) {
fileName = entry.getFilename();
if (false == fileName.equals(".") && false == fileName.equals("..")) {
if (false == Objects.equals(fileName,".") && false == Objects.equals(fileName,"..")) {
if (entry.getAttrs().isDir()) {
delDir(fileName);
} else {

View File

@ -381,7 +381,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
return !httpVersion.equalsIgnoreCase(HTTP_1_0);
}
return !connection.equalsIgnoreCase("close");
return !"close".equalsIgnoreCase(connection);
}
/**

View File

@ -111,7 +111,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
*/
public boolean isGzip() {
final String contentEncoding = contentEncoding();
return contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip");
return contentEncoding != null && "gzip".equalsIgnoreCase(contentEncoding);
}
/**
@ -122,7 +122,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
*/
public boolean isDeflate() {
final String contentEncoding = contentEncoding();
return contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate");
return contentEncoding != null && "deflate".equalsIgnoreCase(contentEncoding);
}
/**
@ -133,7 +133,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
*/
public boolean isChunked() {
final String transferEncoding = header(Header.TRANSFER_ENCODING);
return transferEncoding != null && transferEncoding.equalsIgnoreCase("Chunked");
return transferEncoding != null && "Chunked".equalsIgnoreCase(transferEncoding);
}
/**