BAEL2489 Avoid check for null statement in Java
This commit is contained in:
parent
37d4b5d643
commit
b1a4f6bb21
@ -216,8 +216,7 @@
|
||||
<configuration>
|
||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||
<transformers>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
@ -371,8 +370,7 @@
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF
|
||||
</manifestFile>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
@ -406,14 +404,12 @@
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF
|
||||
</manifestFile>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/application/MyAtm.class</include>
|
||||
<include>com/baeldung/instrumentation/application/MyAtmApplication.class
|
||||
</include>
|
||||
<include>com/baeldung/instrumentation/application/MyAtmApplication.class</include>
|
||||
<include>com/baeldung/instrumentation/application/Launcher.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
@ -443,14 +439,12 @@
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF
|
||||
</manifestFile>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/agent/AtmTransformer.class</include>
|
||||
<include>com/baeldung/instrumentation/agent/MyInstrumentationAgent.class
|
||||
</include>
|
||||
<include>com/baeldung/instrumentation/agent/MyInstrumentationAgent.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
@ -462,7 +456,6 @@
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- marshalling -->
|
||||
<gson.version>2.8.2</gson.version>
|
||||
|
||||
@ -483,12 +476,10 @@
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||
|
||||
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<esapi.version>2.1.0.1</esapi.version>
|
||||
<jmh-core.version>1.19</jmh-core.version>
|
||||
|
||||
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
|
||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
|
||||
@ -499,7 +490,6 @@
|
||||
<icu4j.version>61.1</icu4j.version>
|
||||
<!-- instrumentation -->
|
||||
<javaassist.version>3.21.0-GA</javaassist.version>
|
||||
|
||||
<sun.tools.version>1.8.0</sun.tools.version>
|
||||
<intellij.annotations.version>16.0.2</intellij.annotations.version>
|
||||
</properties>
|
||||
|
@ -4,6 +4,7 @@ public class APIContracts {
|
||||
|
||||
/**
|
||||
* Prints the value of {@code param} if not null. Prints {@code null} otherwise.
|
||||
*
|
||||
* @param param
|
||||
*/
|
||||
public void print(Object param) {
|
||||
@ -11,17 +12,17 @@ public class APIContracts {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return non null result
|
||||
* @throws Exception - if result is null
|
||||
*/
|
||||
public Object process() throws Exception {
|
||||
Object result = doSomething();
|
||||
if (result == null)
|
||||
if (result == null) {
|
||||
throw new Exception("Processing fail. Got a null response");
|
||||
else
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private Object doSomething() {
|
||||
return null;
|
||||
|
@ -3,22 +3,26 @@ package com.baeldung.nulls;
|
||||
public class Preconditions {
|
||||
|
||||
public void goodAccept(String one, String two, String three) {
|
||||
if (null == one || null == two || three == null)
|
||||
if (one == null || two == null || three == null)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
process(one);
|
||||
process(two);
|
||||
process(three);
|
||||
}
|
||||
|
||||
public void badAccept(String one, String two, String three) {
|
||||
if (null == one)
|
||||
if (one == null)
|
||||
throw new IllegalArgumentException();
|
||||
else
|
||||
process(one);
|
||||
|
||||
if (null == two)
|
||||
if (two == null)
|
||||
throw new IllegalArgumentException();
|
||||
else
|
||||
process(two);
|
||||
|
||||
if (null == three)
|
||||
if (three == null)
|
||||
throw new IllegalArgumentException();
|
||||
else
|
||||
process(three);
|
||||
@ -28,5 +32,4 @@ public class Preconditions {
|
||||
private void process(String one) {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -2,11 +2,11 @@ package com.baeldung.nulls;
|
||||
|
||||
public class PrimitivesAndWrapper {
|
||||
|
||||
public static int sum(int a, int b) {
|
||||
public static int primitiveSum(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public static Integer sum(Integer a, Integer b) {
|
||||
public static Integer wrapperSum(Integer a, Integer b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
@ -17,8 +17,4 @@ public class PrimitivesAndWrapper {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
sum(0, 0);
|
||||
sum(null, null);
|
||||
}
|
||||
}
|
||||
|
@ -4,21 +4,13 @@ import java.util.Objects;
|
||||
|
||||
public class UsingObjects {
|
||||
|
||||
private String checked;
|
||||
|
||||
public void accept(Object param) {
|
||||
public void accept(Object param) throws Exception {
|
||||
try {
|
||||
Objects.requireNonNull(param);
|
||||
} catch (NullPointerException e) {
|
||||
//doSomethingElseToo
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void caller() throws Exception {
|
||||
if (Objects.nonNull(checked))
|
||||
accept(checked);
|
||||
else
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
//doSomething()
|
||||
}
|
||||
}
|
||||
|
@ -4,20 +4,24 @@ import java.util.Optional;
|
||||
|
||||
public class UsingOptional {
|
||||
|
||||
public Optional<Object> process() {
|
||||
if (isProcessed())
|
||||
return Optional.of("dummy");
|
||||
else
|
||||
public Optional<Object> process(boolean processed) {
|
||||
|
||||
String response = doSomething(processed);
|
||||
|
||||
if (response == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public void caller() {
|
||||
Optional<Object> result = process();
|
||||
result.ifPresent(p -> System.out.println(p));
|
||||
result.orElseThrow(() -> new IllegalArgumentException());
|
||||
return Optional.of(response);
|
||||
}
|
||||
|
||||
private boolean isProcessed() {
|
||||
return false;
|
||||
private String doSomething(boolean processed) {
|
||||
|
||||
if (processed) {
|
||||
return "passed";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,22 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class UsingStringUtils {
|
||||
|
||||
public void accept(String param) {
|
||||
if (StringUtils.isNotEmpty(param))
|
||||
if (StringUtils.isNotEmpty(param)) {
|
||||
System.out.println(param);
|
||||
} else {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
public void acceptOnlyNonBlank(String param) {
|
||||
if (StringUtils.isNotBlank(param))
|
||||
if (StringUtils.isNotBlank(param)) {
|
||||
System.out.println(param);
|
||||
} else {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PrimitivesAndWrapperTest {
|
||||
|
||||
@Test
|
||||
public void givenWrappers_whenBothArgsNonNull_thenReturnResult() {
|
||||
|
||||
Integer sum = PrimitivesAndWrapper.wrapperSum(0, 0);
|
||||
|
||||
assertEquals(0, sum.intValue());
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void givenWrappers_whenOneArgIsNull_thenThrowNullPointerException() {
|
||||
assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, 2));
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void givenWrappers_whenBothArgsAreNull_thenThrowNullPointerException() {
|
||||
assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, null));
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void givenWrappersWithNullCheck_whenAnyArgIsNull_thenThrowIllegalArgumentException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> PrimitivesAndWrapper.goodSum(null, 2));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UsingLombokTest {
|
||||
|
||||
private UsingLombok classUnderTest;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
classUnderTest = new UsingLombok();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullArg_thenThrowNullPointerException() {
|
||||
|
||||
assertThrows(NullPointerException.class, () -> classUnderTest.accept(null));
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class UsingObjectsTest {
|
||||
|
||||
private UsingObjects classUnderTest;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
classUnderTest = new UsingObjects();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArgIsNull_thenThrowException() {
|
||||
|
||||
assertThrows(Exception.class, () -> classUnderTest.accept(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArgIsNonNull_thenDoesNotThrowException() {
|
||||
|
||||
assertDoesNotThrow(() -> classUnderTest.accept("test "));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class UsingOptionalTest {
|
||||
|
||||
private UsingOptional classUnderTest;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
classUnderTest = new UsingOptional();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArgIsFalse_thenReturnEmptyResponse() {
|
||||
Optional<Object> result = classUnderTest.process(false);
|
||||
assertFalse(result.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArgIsTrue_thenReturnValidResponse() {
|
||||
Optional<Object> result = classUnderTest.process(true);
|
||||
assertTrue(result.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArgIsFalse_thenChainResponseAndThrowException() {
|
||||
assertThrows(Exception.class, () -> classUnderTest.process(false).orElseThrow(() -> new Exception()));
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UsingStringUtilsTest {
|
||||
|
||||
private UsingStringUtils classUnderTest;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
classUnderTest = new UsingStringUtils();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccept_whenArgIsNull_throwIllegalArgumentException() {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccept_whenArgIsEmpty_throwIllegalArgumentException() {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAcceptOnlyNonBlank_whenArgIsNull_throwIllegalArgumentException() {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAcceptOnlyNonBlank_whenArgIsEmpty_throwIllegalArgumentException() {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAcceptOnlyNonBlank_whenArgIsBlank_throwIllegalArgumentException() {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(" "));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user