BAEL2489 - Avoid check for null statement in Java
This commit is contained in:
parent
2634bdc250
commit
9ed70233ff
@ -129,6 +129,13 @@
|
||||
<scope>system</scope>
|
||||
<systemPath>${java.home}/../lib/tools.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>${intellij.annotations.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -209,7 +216,8 @@
|
||||
<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>
|
||||
@ -363,7 +371,8 @@
|
||||
<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>
|
||||
@ -397,12 +406,14 @@
|
||||
<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>
|
||||
@ -432,12 +443,14 @@
|
||||
<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>
|
||||
@ -488,5 +501,6 @@
|
||||
<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>
|
||||
</project>
|
||||
|
29
core-java/src/main/java/com/baeldung/nulls/APIContracts.java
Normal file
29
core-java/src/main/java/com/baeldung/nulls/APIContracts.java
Normal file
@ -0,0 +1,29 @@
|
||||
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;
|
||||
}
|
||||
}
|
13
core-java/src/main/java/com/baeldung/nulls/Assertions.java
Normal file
13
core-java/src/main/java/com/baeldung/nulls/Assertions.java
Normal 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) {
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
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,28 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class FindBugsAnnotations {
|
||||
|
||||
public void accept(@Nonnull Object param) {
|
||||
System.out.println(param.toString());
|
||||
}
|
||||
|
||||
public void print(@Nullable Object param) {
|
||||
System.out.println("Printing " + param);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
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,32 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
public class Preconditions {
|
||||
|
||||
public void goodAccept(String one, String two, String three) {
|
||||
if (null == one || null == two || three == null)
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public void badAccept(String one, String two, String three){
|
||||
if (null == one)
|
||||
throw new IllegalArgumentException();
|
||||
else
|
||||
process(one);
|
||||
|
||||
if (null == two)
|
||||
throw new IllegalArgumentException();
|
||||
else
|
||||
process(two);
|
||||
|
||||
if (null == three)
|
||||
throw new IllegalArgumentException();
|
||||
else
|
||||
process(three);
|
||||
|
||||
}
|
||||
|
||||
private void process(String one) {
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
public class PrimitivesAndWrapper {
|
||||
|
||||
public static int sum(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public static Integer sum(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();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
sum(0, 0);
|
||||
sum(null, null);
|
||||
}
|
||||
}
|
10
core-java/src/main/java/com/baeldung/nulls/UsingLombok.java
Normal file
10
core-java/src/main/java/com/baeldung/nulls/UsingLombok.java
Normal 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);
|
||||
}
|
||||
}
|
24
core-java/src/main/java/com/baeldung/nulls/UsingObjects.java
Normal file
24
core-java/src/main/java/com/baeldung/nulls/UsingObjects.java
Normal file
@ -0,0 +1,24 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class UsingObjects {
|
||||
|
||||
private String checked;
|
||||
|
||||
public void accept(Object param) {
|
||||
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();
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class UsingOptional {
|
||||
|
||||
public Optional<Object> process() {
|
||||
if (isProcessed())
|
||||
return Optional.of("dummy");
|
||||
else
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public void caller() {
|
||||
Optional<Object> result = process();
|
||||
result.ifPresent(p -> System.out.println(p));
|
||||
result.orElseThrow(() -> new IllegalArgumentException());
|
||||
}
|
||||
|
||||
private boolean isProcessed() {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
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);
|
||||
}
|
||||
|
||||
public void acceptOnlyNonBlank(String param) {
|
||||
if (StringUtils.isNotBlank(param))
|
||||
System.out.println(param);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user