BAEL2489 - Avoid check for null statement in Java
This commit is contained in:
parent
2634bdc250
commit
9ed70233ff
@ -129,6 +129,13 @@
|
|||||||
<scope>system</scope>
|
<scope>system</scope>
|
||||||
<systemPath>${java.home}/../lib/tools.jar</systemPath>
|
<systemPath>${java.home}/../lib/tools.jar</systemPath>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains</groupId>
|
||||||
|
<artifactId>annotations</artifactId>
|
||||||
|
<version>${intellij.annotations.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -209,7 +216,8 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||||
<transformers>
|
<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>
|
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||||
</transformer>
|
</transformer>
|
||||||
</transformers>
|
</transformers>
|
||||||
@ -264,12 +272,12 @@
|
|||||||
<argument>-Xmx300m</argument>
|
<argument>-Xmx300m</argument>
|
||||||
<argument>-XX:+UseParallelGC</argument>
|
<argument>-XX:+UseParallelGC</argument>
|
||||||
<argument>-classpath</argument>
|
<argument>-classpath</argument>
|
||||||
<classpath />
|
<classpath/>
|
||||||
<argument>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</argument>
|
<argument>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</argument>
|
||||||
</arguments>
|
</arguments>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
@ -330,7 +338,7 @@
|
|||||||
<executable>java</executable>
|
<executable>java</executable>
|
||||||
<arguments>
|
<arguments>
|
||||||
<argument>-classpath</argument>
|
<argument>-classpath</argument>
|
||||||
<classpath />
|
<classpath/>
|
||||||
<argument>org.openjdk.jmh.Main</argument>
|
<argument>org.openjdk.jmh.Main</argument>
|
||||||
<argument>.*</argument>
|
<argument>.*</argument>
|
||||||
</arguments>
|
</arguments>
|
||||||
@ -363,7 +371,8 @@
|
|||||||
<manifest>
|
<manifest>
|
||||||
<addClasspath>true</addClasspath>
|
<addClasspath>true</addClasspath>
|
||||||
</manifest>
|
</manifest>
|
||||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF
|
||||||
|
</manifestFile>
|
||||||
</archive>
|
</archive>
|
||||||
|
|
||||||
<includes>
|
<includes>
|
||||||
@ -397,12 +406,14 @@
|
|||||||
<manifest>
|
<manifest>
|
||||||
<addClasspath>true</addClasspath>
|
<addClasspath>true</addClasspath>
|
||||||
</manifest>
|
</manifest>
|
||||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF
|
||||||
|
</manifestFile>
|
||||||
</archive>
|
</archive>
|
||||||
|
|
||||||
<includes>
|
<includes>
|
||||||
<include>com/baeldung/instrumentation/application/MyAtm.class</include>
|
<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>
|
<include>com/baeldung/instrumentation/application/Launcher.class</include>
|
||||||
</includes>
|
</includes>
|
||||||
</configuration>
|
</configuration>
|
||||||
@ -432,12 +443,14 @@
|
|||||||
<manifest>
|
<manifest>
|
||||||
<addClasspath>true</addClasspath>
|
<addClasspath>true</addClasspath>
|
||||||
</manifest>
|
</manifest>
|
||||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF
|
||||||
|
</manifestFile>
|
||||||
</archive>
|
</archive>
|
||||||
|
|
||||||
<includes>
|
<includes>
|
||||||
<include>com/baeldung/instrumentation/agent/AtmTransformer.class</include>
|
<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>
|
</includes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
@ -449,7 +462,7 @@
|
|||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|
||||||
<!-- marshalling -->
|
<!-- marshalling -->
|
||||||
<gson.version>2.8.2</gson.version>
|
<gson.version>2.8.2</gson.version>
|
||||||
|
|
||||||
@ -470,12 +483,12 @@
|
|||||||
|
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||||
|
|
||||||
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
||||||
<h2database.version>1.4.197</h2database.version>
|
<h2database.version>1.4.197</h2database.version>
|
||||||
<esapi.version>2.1.0.1</esapi.version>
|
<esapi.version>2.1.0.1</esapi.version>
|
||||||
<jmh-core.version>1.19</jmh-core.version>
|
<jmh-core.version>1.19</jmh-core.version>
|
||||||
|
|
||||||
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
|
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
|
||||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||||
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
|
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
|
||||||
@ -486,7 +499,8 @@
|
|||||||
<icu4j.version>61.1</icu4j.version>
|
<icu4j.version>61.1</icu4j.version>
|
||||||
<!-- instrumentation -->
|
<!-- instrumentation -->
|
||||||
<javaassist.version>3.21.0-GA</javaassist.version>
|
<javaassist.version>3.21.0-GA</javaassist.version>
|
||||||
|
|
||||||
<sun.tools.version>1.8.0</sun.tools.version>
|
<sun.tools.version>1.8.0</sun.tools.version>
|
||||||
|
<intellij.annotations.version>16.0.2</intellij.annotations.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</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