BAEL2489 - Avoid check for null statement in Java (#6411)

* BAEL2489 - Avoid check for null statement in Java

* BAEL2489 Avoid check for null statement in Java

* BAEL2489 Avoid check for null statement in Java

* BAEL2489 Avoid check for null statement in Java

* BAEL2489 Avoid check for null statement in Java

* BAEL2489 Avoid check for null statement in Java - Removing unused pom changes

* BAEL2489 Avoid check for null statement in Java - Removing unused pom changes

* Delete pom.xml

Removing unused changes in core-java

* BAEL2489 Avoid check for null statement in Java - Removing unused pom changes
This commit is contained in:
dev-chirag 2019-03-29 06:26:15 +05:30 committed by KevinGilmore
parent 580ed9f626
commit 9687c88abf
16 changed files with 419 additions and 0 deletions

View File

@ -15,11 +15,29 @@
</parent>
<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>${intellij.annotations.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<intellij.annotations.version>16.0.2</intellij.annotations.version>
<commons-lang3.version>3.5</commons-lang3.version>
</properties>
</project>

View File

@ -0,0 +1,30 @@
package com.baeldung.nulls;
public class APIContracts {
/**
* Prints the value of {@code param} if not null. Prints {@code null} otherwise.
*
* @param param
*/
public void print(Object param) {
System.out.println("Printing " + param);
}
/**
* @return non null result
* @throws Exception - if result is null
*/
public Object process() throws Exception {
Object result = doSomething();
if (result == null) {
throw new Exception("Processing fail. Got a null response");
} else {
return result;
}
}
private Object doSomething() {
return null;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.nulls;
public class Assertions {
public void accept(Object param){
assert param != null;
doSomething(param);
}
private void doSomething(Object param) {
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.nulls;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class EmptyCollections {
public List<String> names() {
if (userExist()) {
return Stream.of(readName()).collect(Collectors.toList());
} else {
return Collections.emptyList();
}
}
private boolean userExist() {
return false;
}
private String readName() {
return "test";
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.nulls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class FindBugsAnnotations {
public void accept(@NotNull Object param) {
System.out.println(param.toString());
}
public void print(@Nullable Object param) {
System.out.println("Printing " + param);
}
@NotNull
public Object process() throws Exception {
Object result = doSomething();
if (result == null) {
throw new Exception("Processing fail. Got a null response");
} else {
return result;
}
}
private Object doSomething() {
return null;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.nulls;
public class Preconditions {
public void goodAccept(String one, String two, String three) {
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 (one == null) {
throw new IllegalArgumentException();
} else {
process(one);
}
if (two == null) {
throw new IllegalArgumentException();
} else {
process(two);
}
if (three == null) {
throw new IllegalArgumentException();
} else {
process(three);
}
}
private void process(String one) {
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.nulls;
public class PrimitivesAndWrapper {
public static int primitiveSum(int a, int b) {
return a + b;
}
public static Integer wrapperSum(Integer a, Integer b) {
return a + b;
}
public static Integer goodSum(Integer a, Integer b) {
if (a != null && b != null) {
return a + b;
} else {
throw new IllegalArgumentException();
}
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.nulls;
import lombok.NonNull;
public class UsingLombok {
public void accept(@NonNull Object param){
System.out.println(param);
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.nulls;
import java.util.Objects;
public class UsingObjects {
public void accept(Object param) {
Objects.requireNonNull(param);
// doSomething()
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.nulls;
import java.util.Optional;
public class UsingOptional {
public Optional<Object> process(boolean processed) {
String response = doSomething(processed);
if (response == null) {
return Optional.empty();
}
return Optional.of(response);
}
private String doSomething(boolean processed) {
if (processed) {
return "passed";
} else {
return null;
}
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.nulls;
import org.apache.commons.lang3.StringUtils;
public class UsingStringUtils {
public void accept(String param) {
if (StringUtils.isNotEmpty(param)) {
System.out.println(param);
} else {
throw new IllegalArgumentException();
}
}
public void acceptOnlyNonBlank(String param) {
if (StringUtils.isNotBlank(param)) {
System.out.println(param);
} else {
throw new IllegalArgumentException();
}
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.nulls;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class PrimitivesAndWrapperUnitTest {
@Test
public void givenBothArgsNonNull_whenCallingWrapperSum_thenReturnSum() {
Integer sum = PrimitivesAndWrapper.wrapperSum(0, 0);
assertEquals(0, sum.intValue());
}
@Test()
public void givenOneArgIsNull_whenCallingWrapperSum_thenThrowNullPointerException() {
assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, 2));
}
@Test()
public void givenBothArgsNull_whenCallingWrapperSum_thenThrowNullPointerException() {
assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, null));
}
@Test()
public void givenOneArgNull_whenCallingGoodSum_thenThrowIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> PrimitivesAndWrapper.goodSum(null, 2));
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.nulls;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
class UsingLombokUnitTest {
private UsingLombok classUnderTest;
@BeforeEach
public void setup() {
classUnderTest = new UsingLombok();
}
@Test
public void whenNullArg_thenThrowNullPointerException() {
assertThrows(NullPointerException.class, () -> classUnderTest.accept(null));
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.nulls;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
class UsingObjectsUnitTest {
private UsingObjects classUnderTest;
@BeforeEach
public void setup() {
classUnderTest = new UsingObjects();
}
@Test
public void whenArgIsNull_thenThrowException() {
assertThrows(NullPointerException.class, () -> classUnderTest.accept(null));
}
@Test
public void whenArgIsNonNull_thenDoesNotThrowException() {
assertDoesNotThrow(() -> classUnderTest.accept("test "));
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.nulls;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class UsingOptionalUnitTest {
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()));
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.nulls;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
class UsingStringUtilsUnitTest {
private UsingStringUtils classUnderTest;
@BeforeEach
public void setup() {
classUnderTest = new UsingStringUtils();
}
@Test
public void givenArgIsNull_whenCallingAccept_throwIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(null));
}
@Test
public void givenArgIsEmpty_whenCallingAccept_throwIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(""));
}
@Test
public void givenArgIsNull_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(null));
}
@Test
public void givenArgIsEmpty_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(""));
}
@Test
public void givenArgIsBlank_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(" "));
}
}