BAEL2489 - Avoid check for null statement in Java

This commit is contained in:
Chirag Dewan 2019-02-25 11:42:51 +05:30
parent 2634bdc250
commit 9ed70233ff
11 changed files with 251 additions and 13 deletions

View File

@ -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>
@ -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>
@ -488,5 +501,6 @@
<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>

View 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;
}
}

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,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";
}
}

View File

@ -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;
}
}

View File

@ -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) {
}
}

View File

@ -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);
}
}

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,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();
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}