Added printing arrays with java.lang.IO warning. - #9632
Conversation
0a9fcda to
bcfeca7
Compare
|
Tests for java.hints passed |
|
The change looks sane to me. Could you please see if you can expand the existing testcase for the hint to cover this case or add a new testcase for that? This is the relevant file: The tests can be run directly from the IDE. Open the file and in the context menu you can find "Test File" to run all tests of that file. A single test can be run from the context menu using "Run Focused Test Method" when the cursor is placed inside the method or by invoking the action from the menu generated when clicking on the green play button in front of the test method. |
bcfeca7 to
bc4c36e
Compare
|
@matthiasblaesing I updated the method and it passed |
eb54764 to
4317a6e
Compare
|
right. so we have two options: split Assume.assumeTrue(Runtime.version().feature() >= 25);or I suppose we don't test it. Sorry that I haven't noticed earlier. |
|
@gem-bricks sorry for the wrong information/request. I think this change: # This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/ArrayStringOperationsTest.java
+++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/ArrayStringOperationsTest.java
@@ -37,38 +37,36 @@
*/
public void testDetectArrayStrings() throws Exception {
HintTest.create()
- .input(
- "package test;\n" +
- "\n" +
- "import java.io.PrintStream;\n" +
- "import java.text.MessageFormat;\n" +
- "import java.util.Locale;\n" +
- "\n" +
- "public class Test {\n" +
- " private int[] intArr;\n" +
- " private Object[] objArr;\n" +
- " private PrintStream stream;\n" +
- " \n" +
- " public void test() {\n" +
- " String si = intArr.toString();\n" +
- " String so = objArr.toString();\n" +
- " // this is OK\n" +
- " String s2 = String.format(\"ee\", objArr);\n" +
- " String s1 = String.format(\"ee\", intArr, 2);\n" +
- " String s3 = MessageFormat.format(\"eee\", intArr);\n" +
- " stream.format(\"ee\", intArr);\n" +
- " // not ok, not a last parameter\n" +
- " stream.format(\"ee\", objArr, 2);\n" +
- " System.err.format(Locale.getDefault(), \"ee\", objArr, 1);\n" +
- " stream.print(intArr);\n" +
- " stream.println(intArr);\n" +
- " IO.print(intArr);\n" +
- " IO.println(intArr);\n" +
- " s1 = s2 + intArr;\n" +
- " s1 = objArr + s2;\n" +
- " }\n" +
- "}"
- )
+ .input("""
+ package test;
+
+ import java.io.PrintStream;
+ import java.text.MessageFormat;
+ import java.util.Locale;
+
+ public class Test {
+ private int[] intArr;
+ private Object[] objArr;
+ private PrintStream stream;
+
+ public void test() {
+ String si = intArr.toString();
+ String so = objArr.toString();
+ // this is OK
+ String s2 = String.format(\"ee\", objArr);
+ String s1 = String.format(\"ee\", intArr, 2);
+ String s3 = MessageFormat.format(\"eee\", intArr);
+ stream.format(\"ee\", intArr);
+ // not ok, not a last parameter
+ stream.format(\"ee\", objArr, 2);
+ System.err.format(Locale.getDefault(), \"ee\", objArr, 1);
+ stream.print(intArr);
+ stream.println(intArr);
+ s1 = s2 + intArr;
+ s1 = objArr + s2;
+ }
+ }
+ """)
.run(ArrayStringConversions.class).
assertWarnings(
"12:20-12:37:verifier:toString() called on array instance",
@@ -80,13 +78,31 @@
"21:53-21:59:verifier:Array instance passed as parameter to a formatter function",
"22:21-22:27:verifier:Array instance printed on PrintStream",
"23:23-23:29:verifier:Array instance printed on PrintStream",
- "24:17-24:23:verifier:Array instance printed with java.lang.IO",
- "25:19-25:25:verifier:Array instance printed with java.lang.IO",
- "26:18-26:24:verifier:Array concatenated with String",
- "27:13-27:19:verifier:Array concatenated with String"
+ "24:18-24:24:verifier:Array concatenated with String",
+ "25:13-25:19:verifier:Array concatenated with String"
);
+ if (Runtime.version().feature() >= 25) {
+ HintTest.create()
+ .input("""
+ package test;
+
+ public class Test {
+ private int[] intArr;
+
+ public void test() {
+ IO.print(intArr);
+ IO.println(intArr);
+ }
+ }
+ """)
+ .run(ArrayStringConversions.class).
+ assertWarnings(
+ "6:17-6:23:verifier:Array instance printed with java.lang.IO",
+ "7:19-7:25:verifier:Array instance printed with java.lang.IO"
+ );
+ }
}
-
+
/**
* .toString() is converted in a different way than Array passing, check the fix is OK
* @throws Exception
would make sense. It moves the whole test to multiline strings and separates the JDK21+ and JDK25+ tests while keeping them runnable on the "right" JDK. |
4317a6e to
a99a21f
Compare
|
@matthiasblaesing Done |
java.lang.IO was added with JRE 25, which provides the alternative methods
printandprintln. These delegate to thejava.lang.System.outPrintStream, and currently, while array printing withoutshows a warning, the IO methods do not.My pull request adds warnings for these methods. Using the hint has the same behavior as the
System.outhints.