Repository: apache/netbeans
Issue: `Objects.requireNonNull` should disable "Null Pointer Dereference" warnings

### Apache NetBeans version

Apache NetBeans 18

### What happened

Using `Objects.requireNonNull` should disable "Null Pointer Dereference" warnings. Instead, it doesn't.

### How to reproduce

```java
public static String myToString(Object object) {
    Objects.requireNonNull(object);
    if (object instanceof Number) {
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(((Number) object).doubleValue());
    } else {
        return object.toString();
    }
}
```

### Did this work correctly in an earlier version?

No / Don't know

### Operating System

Windows 10 version 10.0 running on amd64; Cp1252; it_IT (nb)

### JDK

11.0.14.1; OpenJDK 64-Bit Server VM 11.0.14.1+1

### Apache NetBeans packaging

Apache NetBeans binary zip

### Anything else

![null warning](https://github.com/apache/netbeans/assets/18428155/6470fe7e-e4e9-4306-96de-3b02be74788b)

if `Objects.requireNonNull(object);` is replaced by `if (object == null) throw new NullPointerException();` the warning disappears.

### Are you willing to submit a pull request?

No

Changed files:
java/java.hints/src/org/netbeans/modules/java/hints/bugs/NPECheck.java
java/java.hints.test/src/org/netbeans/modules/java/hints/test/api/HintTest.java
java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/mapreduce/ForLoopToFunctionalHintTest.java
java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/NPECheckTest.java
java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java

diff --git a/java/java.hints.test/src/org/netbeans/modules/java/hints/test/api/HintTest.java b/java/java.hints.test/src/org/netbeans/modules/java/hints/test/api/HintTest.java
index d671eec7e4..4e59f3257c 100644
--- a/java/java.hints.test/src/org/netbeans/modules/java/hints/test/api/HintTest.java
+++ b/java/java.hints.test/src/org/netbeans/modules/java/hints/test/api/HintTest.java
@@ -823,17 +823,23 @@ public class HintTest {
         }
 
         /**Assert that the hint(s) produced warnings do not include the given warnings. The provided strings
-         * should match {@code toString()} results of {@link ErrorDescription}s produced
+         * should match {@code getDescription()} results of {@link ErrorDescription}s produced
          * by the hint(s).
          *
-         * @param warnings expected {@code toString()} results of {@link ErrorDescription}s produced
+         * @param warnings expected {@code getDescription()} results of {@link ErrorDescription}s produced
          *                 by the hint
          * @return itself
-         * @throws AssertionError if the given warnings do not match the actual warnings
+         * @throws AssertionError if the given warnings contain the actual warnings
          */
         public HintOutput assertNotContainsWarnings(String... warnings) {
             Set<String> goldenSet = new HashSet<String>(Arrays.asList(warnings));
             List<String> errorsNames = new LinkedList<String>();
+            
+            for (String warning : goldenSet) {
+                if (warning.split(":").length >= 5) {
+                    assertFalse("this method expects hint descriptions, not toString()! errors found: "+errors, true);
+                }
+            }
 
             boolean fail = false;
             for (ErrorDescription d : errors) {
diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/bugs/NPECheck.java b/java/java.hints/src/org/netbeans/modules/java/hints/bugs/NPECheck.java
index e2e3392d3c..32ef1bd43d 100644
--- a/java/java.hints/src/org/netbeans/modules/java/hints/bugs/NPECheck.java
+++ b/java/java.hints/src/org/netbeans/modules/java/hints/bugs/NPECheck.java
@@ -1068,12 +1068,16 @@ public class NPECheck {
                 State targetState = null;
 
                 switch (e.getSimpleName().toString()) {
-                    case "assertNotNull": targetState = State.NOT_NULL; break;
+                    case "assertNotNull":
+                    case "requireNonNull":
+                    case "requireNonNullElse":
+                    case "requireNonNullElseGet": targetState = State.NOT_NULL; break;
                     case "assertNull": targetState = State.NULL; break;
                 }
 
                 switch (ownerFQN) {
-                    case "org.testng.Assert": argument = node.getArguments().get(0); break;
+                    case "org.testng.Assert":
+                    case "java.util.Objects": argument = node.getArguments().get(0); break;
                     case "junit.framework.Assert":
                     case "org.junit.Assert": 
                     case "org.junit.jupiter.api.Assertions": argument = node.getArguments().get(node.getArguments().size() - 1); break;
diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/NPECheckTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/NPECheckTest.java
index e62a098374..20f6663f79 100644
--- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/NPECheckTest.java
+++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/NPECheckTest.java
@@ -1798,6 +1798,38 @@ public class NPECheckTest extends NbTestCase {
                                 "9:7-9:15:verifier:Possibly Dereferencing null");
     }
 
+    public void testObjectsRequireNonNull() throws Exception {
+        HintTest.create()
+                .input("package test;\\n"
+                     + "import java.util.Objects;\\n"
+                     + "public class Test {\\n"
+                     + "  public String test(Object obj) {\\n"
+                     + "    Objects.requireNonNull(obj);\\n"
+                     + "    if (obj instanceof Number)\\n"
+                     + "        return \\"\\";\\n"
+                     + "    return obj.toString();\\n"
+                     + "  }\\n"
+                     + "}")
+                .run(NPECheck.class)
+                .assertNotContainsWarnings("Possibly Dereferencing null");
+    }
+
+    public void testObjectsRequireNonNullElse() throws Exception {
+        HintTest.create()
+                .input("package test;\\n"
+                     + "import java.util.Objects;\\n"
+                     + "public class Test {\\n"
+                     + "  public String test(Object obj) {\\n"
+                     + "    Objects.requireNonNullElse(obj, \\"\\");\\n"
+                     + "    if (obj instanceof Number)\\n"
+                     + "        return \\"\\";\\n"
+                     + "    return obj.toString();\\n"
+                     + "  }\\n"
+                     + "}")
+                .run(NPECheck.class)
+                .assertNotContainsWarnings("Possibly Dereferencing null");
+    }
+
     private void performAnalysisTest(String fileName, String code, String... golden) throws Exception {
         HintTest.create()
                 .input(fileName, code)
diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java
index e89761e214..094a091672 100644
--- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java
+++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java
@@ -105,7 +105,7 @@ public class ConvertToSwitchPatternInstanceOfTest extends NbTestCase {
                 .sourceLevel("17")
                 .options("--enable-preview")
                 .run(ConvertToSwitchPatternInstanceOf.class)
-                .assertNotContainsWarnings("3:8-3:10:verifier:" + Bundle.ERR_ConvertToSwitchPatternInstanceOf());
+                .assertNotContainsWarnings(Bundle.ERR_ConvertToSwitchPatternInstanceOf());
     }
     
     @Test
@@ -184,7 +184,7 @@ public class ConvertToSwitchPatternInstanceOfTest extends NbTestCase {
                 .sourceLevel(SourceVersion.latest().name())
                 .options("--enable-preview")
                 .run(ConvertToSwitchPatternInstanceOf.class)
-                .assertNotContainsWarnings("4:8-4:10:verifier:" + Bundle.ERR_ConvertToSwitchPatternInstanceOf());
+                .assertNotContainsWarnings(Bundle.ERR_ConvertToSwitchPatternInstanceOf());
     }
 
     @Test
@@ -278,7 +278,7 @@ public class ConvertToSwitchPatternInstanceOfTest extends NbTestCase {
                 .sourceLevel(SourceVersion.latest().name())
                 .options("--enable-preview")
                 .run(ConvertToSwitchPatternInstanceOf.class)
-                .assertNotContainsWarnings("4:8-4:24:verifier:" + Bundle.ERR_ConvertToSwitchPatternInstanceOf());
+                .assertNotContainsWarnings(Bundle.ERR_ConvertToSwitchPatternInstanceOf());
     }
 
     @Test
diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/mapreduce/ForLoopToFunctionalHintTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/mapreduce/ForLoopToFunctionalHintTest.java
index 093c3764ec..0752a57c3a 100644
--- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/mapreduce/ForLoopToFunctionalHintTest.java
+++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/mapreduce/ForLoopToFunctionalHintTest.java
@@ -1406,7 +1406,7 @@ public class ForLoopToFunctionalHintTest extends NbTestCase {
                 + "}")
                 .sourceLevel("1.8")
                 .run(ForLoopToFunctionalHint.class)
-                .assertNotContainsWarnings("23:8-23:11:hint:" + Bundle.ERR_ForLoopToFunctionalHint());
+                .assertNotContainsWarnings(Bundle.ERR_ForLoopToFunctionalHint());
     }
 
     public void testNoHintDueToNEF() throws Exception {
@@ -1789,7 +1789,7 @@ public class ForLoopToFunctionalHintTest extends NbTestCase {
                 + "}")
                 .sourceLevel("1.8")
                 .run(ForLoopToFunctionalHint.class)
-                .assertNotContainsWarnings("23:8-23:11:hint:Can use functional operation");
+                .assertNotContainsWarnings("Can use functional operation");
 
     }
 
@@ -1832,7 +1832,7 @@ public class ForLoopToFunctionalHintTest extends NbTestCase {
                 + "}")
                 .sourceLevel("1.8")
                 .run(ForLoopToFunctionalHint.class)
-                .assertNotContainsWarnings("23:8-23:11:hint:Can use functional operation");
+                .assertNotContainsWarnings("Can use functional operation");
 
     }
 
@@ -1873,7 +1873,7 @@ public class ForLoopToFunctionalHintTest extends NbTestCase {
                 + "}")
                 .sourceLevel("1.8")
                 .run(ForLoopToFunctionalHint.class)
-                .assertNotContainsWarnings("23:8-23:11:hint:Can use functional operation");
+                .assertNotContainsWarnings("Can use functional operation");
 
     }