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:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import lombok.NonNull;
|
||||
|
||||
public class UsingLombok {
|
||||
|
||||
public void accept(@NonNull Object param){
|
||||
System.out.println(param);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class UsingObjects {
|
||||
|
||||
public void accept(Object param) {
|
||||
Objects.requireNonNull(param);
|
||||
// doSomething()
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user