https://www.ossez.com/t/java-tostring/14000 Java 对象如何安全的 toString
This commit is contained in:
parent
4f1cdf5208
commit
aade7b9c56
|
@ -0,0 +1,55 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-strings</artifactId>
|
||||
<name>core-java-strings</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ossez</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ibm.icu</groupId>
|
||||
<artifactId>icu4j</artifactId>
|
||||
<version>${icu4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-strings</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<icu4j.version>61.1</icu4j.version>
|
||||
<maven.compiler.release>15</maven.compiler.release>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java9.compactstring;
|
||||
package com.ossez.java9.compactstring;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.localization;
|
||||
package com.ossez.localization;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Arrays;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.localization;
|
||||
package com.ossez.localization;
|
||||
|
||||
import com.ibm.icu.text.MessageFormat;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.localization;
|
||||
package com.ossez.localization;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Date;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.localization;
|
||||
package com.ossez.localization;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
|
@ -1,7 +1,8 @@
|
|||
package com.baeldung.multiline;
|
||||
package com.ossez.multiline;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
@ -65,10 +66,11 @@ public class MultiLineString {
|
|||
}
|
||||
|
||||
public String textBlocks() {
|
||||
return """
|
||||
Get busy living
|
||||
or
|
||||
get busy dying.
|
||||
--Stephen King""";
|
||||
// return """
|
||||
// Get busy living
|
||||
// or
|
||||
// get busy dying.
|
||||
// --Stephen King""";
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.stringperformance;
|
||||
package com.ossez.stringperformance;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.chararraypassword;
|
||||
package com.ossez.chararraypassword;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package com.ossez.interview;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
|
||||
|
||||
public class StringNullCheckUnitTest {
|
||||
@Test
|
||||
public void toString_null_safe() {
|
||||
Object obj = null;
|
||||
|
||||
ObjectUtils.toString(obj, StringUtils.EMPTY);
|
||||
ObjectUtils.toString(obj, ()->StringUtils.EMPTY);
|
||||
assertThat("null").isEqualTo(Objects.toString(obj));
|
||||
assertThat("").isEqualTo(Objects.toString(obj,StringUtils.EMPTY));
|
||||
|
||||
assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->{
|
||||
obj.toString();
|
||||
});
|
||||
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.interview;
|
||||
package com.ossez.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.localization;
|
||||
package com.ossez.localization;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.multiline;
|
||||
package com.ossez.multiline;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.randomstrings;
|
||||
package com.ossez.randomstrings;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.Test;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.stringnotempty;
|
||||
package com.ossez.stringnotempty;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.apache.commons.lang3.StringUtils;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.stringpool;
|
||||
package com.ossez.stringpool;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.stringtoolong;
|
||||
package com.ossez.stringtoolong;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
Loading…
Reference in New Issue