This product is not supported for your selected Datadog site. ().
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다. 현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.
Metadata
ID:java-best-practices/useless-null
Language: Java
Severity: Notice
Category: Best Practices
Description
This rule flags null checks (== null or != null) performed on expressions that are guaranteed by the Java language specification to never be null.
Certain expressions, such as the this keyword, object creation with new, literals (e.g., "hello", 42), and class literals (.class), can never evaluate to null. A check against null on any of these expressions is redundant and results in dead code that will never execute. This adds unnecessary clutter and can mislead developers into thinking the variable could be null under some circumstances.
Non-Compliant Code Examples
/**
* This class serves as a comprehensive non-compliant test case.
* Each method demonstrates a null check against an expression that is
* guaranteed by the language to be non-null. The static analysis rule
* should flag every "if" statement within this class.
*/publicclassUselessNullChecks{privateStringname;publicUselessNullChecks(Stringname){this.name=name;}// 1. Checking 'this'publicvoidcheckThisKeyword(){if(this==null){// Non-compliant: 'this' is never null.System.out.println("Error: 'this' is null.");}}// 2. Checking a 'new' object creation expressionpublicvoidcheckNewObject(){if(newUselessNullChecks("test")==null){// Non-compliant: 'new' returns a non-null instance or throws.System.out.println("Error: Object creation resulted in null.");}}// 3. Checking various literals that are never nullpublicvoidcheckLiterals(){if("some string"==null){// Non-compliant: String literalSystem.out.println("Error: String literal is null.");}if(42==null){// Non-compliant: Integer literalSystem.out.println("Error: Integer literal is null.");}if(true!=null){// Non-compliant: Boolean literalSystem.out.println("Boolean literal is not null.");}}}
Compliant Code Examples
/**
* This class serves as a comprehensive compliant test case.
* It showcases legitimate scenarios where a null check is necessary to
* prevent NullPointerExceptions. The static analysis rule should not
* flag any of the "if" statements within this class.
*/publicclassNecessaryNullChecks{privateStringvalue;publicNecessaryNullChecks(Stringvalue){this.value=value;}// A method that can return nullprivateStringfindProperty(Stringkey){if("name".equals(key)){returnthis.value;}returnnull;// A method can legitimately return null.}// 1. Checking a method parameter which could be nullpublicvoidprocessInput(Objectinput){if(input==null){// Compliant: Parameters can be null.System.out.println("Input was null, cannot proceed.");return;}System.out.println("Processing: "+input.toString());}// 2. Checking the result of a method call that may return nullpublicvoidcheckMethodResult(){Stringproperty=findProperty("address");if(property==null){// Compliant: The method is designed to return null.System.out.println("Property not found.");}else{System.out.println("Property length: "+property.length());}}// 3. Checking a field that can be nullpublicvoidcheckField(){// Create an instance where the field is nullNecessaryNullChecksinstance=newNecessaryNullChecks(null);if(instance.value==null){// Compliant: A class field can be null.System.out.println("The instance's value field is null.");}}}
원활한 통합. Datadog Code Security를 경험해 보세요
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드를 분석하세요
규칙 사용 방법
1
2
rulesets:- java-best-practices # Rules to enforce Java best practices.
리포지토리 루트에 위의 내용을 포함하는 static-analysis.datadog.yml을 만듭니다
무료 IDE 플러그인을 사용하거나 CI 파이프라인에 Code Security 검사를 추가합니다