From c509d758d14d833fcad208f01a9e5094a11349bf Mon Sep 17 00:00:00 2001 From: PhilipCattanach <31246010+PhilipCattanach@users.noreply.github.com> Date: Thu, 27 Jul 2023 16:14:10 +0100 Subject: [PATCH] WINDUPRULE-1013 OpenJDK21 UTF-8 character set used by default (#1011) --- .../utf-8-by-default/FileReaderExample.java | 26 ++++++ .../utf-8-by-default/FileWriterExample.java | 24 ++++++ .../utf-8-by-default/FormatterExample.java | 15 ++++ .../InputStreamReaderExample.java | 24 ++++++ .../OutputStreamWriterExample.java | 28 +++++++ .../utf-8-by-default/PrintStreamExample.java | 21 +++++ .../data/utf-8-by-default/ScannerExample.java | 20 +++++ .../utf-8-by-default/URLDecoderExample.java | 9 +++ .../utf-8-by-default/URLEncoderExample.java | 27 +++++++ .../tests/utf-8-by-default.windup.test.xml | 59 ++++++++++++++ .../openjdk17/utf-8-by-default.windup.xml | 81 +++++++++++++++++++ 11 files changed, 334 insertions(+) create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FileReaderExample.java create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FileWriterExample.java create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FormatterExample.java create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/InputStreamReaderExample.java create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/OutputStreamWriterExample.java create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/PrintStreamExample.java create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/ScannerExample.java create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/URLDecoderExample.java create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/URLEncoderExample.java create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/tests/utf-8-by-default.windup.test.xml create mode 100644 rules/rules-reviewed/openjdk21/openjdk17/utf-8-by-default.windup.xml diff --git a/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FileReaderExample.java b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FileReaderExample.java new file mode 100644 index 000000000..1fa0846e5 --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FileReaderExample.java @@ -0,0 +1,26 @@ +import java.io.FileReader; + +class FileReaderExample { + public static void main(String[] args) { + + // Creates an array of character + char[] array = new char[100]; + + try { + // Creates a reader using the FileReader + FileReader input = new FileReader("input.txt"); + + // Reads characters + input.read(array); + System.out.println("Data in the file: "); + System.out.println(array); + + // Closes the reader + input.close(); + } + + catch(Exception e) { + e.getStackTrace(); + } + } +} \ No newline at end of file diff --git a/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FileWriterExample.java b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FileWriterExample.java new file mode 100644 index 000000000..b0b9cb193 --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FileWriterExample.java @@ -0,0 +1,24 @@ +import java.io.FileWriter; + +public class FileWriterExample { + + public static void main(String args[]) { + + String data = "This is the data in the output file"; + + try { + // Creates a FileWriter + FileWriter output = new FileWriter("output.txt"); + + // Writes the string to the file + output.write(data); + + // Closes the writer + output.close(); + } + + catch (Exception e) { + e.getStackTrace(); + } + } +} \ No newline at end of file diff --git a/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FormatterExample.java b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FormatterExample.java new file mode 100644 index 000000000..6e5fb8717 --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FormatterExample.java @@ -0,0 +1,15 @@ +import java.util.Formatter; +import java.util.Locale; + +class FormatterExample +{ + public static void main(String args[]) + { + Formatter formatter = new Formatter(Locale.US); + String name = "Phil"; + int age = 57; + formatter.format("User name is %s and age is %d", name, age); + System.out.println(formatter); + + } +} \ No newline at end of file diff --git a/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/InputStreamReaderExample.java b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/InputStreamReaderExample.java new file mode 100644 index 000000000..f51eb9225 --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/InputStreamReaderExample.java @@ -0,0 +1,24 @@ +import java.io.FileInputStream; +import java.io.InputStreamReader; + +public class InputStreamReaderExample +{ + public static void main(String[] args) + { + // Creates an array of character + char[] array = new char[50]; + + try (InputStreamReader input + = new InputStreamReader(new FileInputStream("demo.txt"))) { + + // Reads characters from the file + input.read(array); + + System.out.println(array); + } + + catch (Exception e) { + e.getStackTrace(); + } + } +} \ No newline at end of file diff --git a/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/OutputStreamWriterExample.java b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/OutputStreamWriterExample.java new file mode 100644 index 000000000..f818aff43 --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/OutputStreamWriterExample.java @@ -0,0 +1,28 @@ +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; + +public class OutputStreamWriterExample { + + public static void main(String args[]) { + + String data = "This is a line of text inside the file."; + + try { + // Creates a FileOutputStream + FileOutputStream file = new FileOutputStream("output.txt"); + + // Creates an OutputStreamWriter + OutputStreamWriter output = new OutputStreamWriter(file); + + // Writes string to the file + output.write(data); + + // Closes the writer + output.close(); + } + + catch (Exception e) { + e.getStackTrace(); + } + } +} \ No newline at end of file diff --git a/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/PrintStreamExample.java b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/PrintStreamExample.java new file mode 100644 index 000000000..d78373e71 --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/PrintStreamExample.java @@ -0,0 +1,21 @@ +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.PrintStream; +import java.util.Locale; +//Java program to demonstrate PrintStream methods +class PrintStreamExample +{ + public static void main(String args[]) throws FileNotFoundException + { + FileOutputStream fout=new FileOutputStream("file.txt"); + PrintStream out=new PrintStream(fout); + String s="Hello world"; + + out.print (s); + out.println(); + + out.flush(); + + out.close(); + } +} \ No newline at end of file diff --git a/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/ScannerExample.java b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/ScannerExample.java new file mode 100644 index 000000000..2476b7294 --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/ScannerExample.java @@ -0,0 +1,20 @@ +import java.util.Scanner; + +class ScannerExample { + public static void main(String[] args) { + + // creates an object of Scanner + Scanner input = new Scanner(System.in); + + System.out.print("Enter your name: "); + + // takes input from the keyboard + String name = input.nextLine(); + + // prints the name + System.out.println("My name is " + name); + + // closes the scanner + input.close(); + } +} \ No newline at end of file diff --git a/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/URLDecoderExample.java b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/URLDecoderExample.java new file mode 100644 index 000000000..b30faeb35 --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/URLDecoderExample.java @@ -0,0 +1,9 @@ +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; + +public class URLDecoderExample { + public static void main(String args[])throws UnsupportedEncodingException + { + System.out.println(URLDecoder.decode("is%3Apr+is%3Aclosed", "UTF-8")); + } +} \ No newline at end of file diff --git a/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/URLEncoderExample.java b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/URLEncoderExample.java new file mode 100644 index 000000000..300be5ef5 --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/URLEncoderExample.java @@ -0,0 +1,27 @@ +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLEncoder; + +public class UrlEncoderExample +{ + public static void main(String[] args) throws MalformedURLException, + UnsupportedEncodingException + { + // base URL + String baseurl = "https://github.com/windup/windup-rulesets/pulls?q="; + + // String to be encoded + String query = "is:pr is:closed "; + + System.out.println("URL without encoding :"); + URL url = new URL(baseurl + query); + System.out.println(url); + + // encode() method + System.out.println("URL after encoding :"); + url = new URL(baseurl + URLEncoder.encode(query, "UTF-8")); + System.out.println(url); + } + +} \ No newline at end of file diff --git a/rules/rules-reviewed/openjdk21/openjdk17/tests/utf-8-by-default.windup.test.xml b/rules/rules-reviewed/openjdk21/openjdk17/tests/utf-8-by-default.windup.test.xml new file mode 100644 index 000000000..7c22a2e6b --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/tests/utf-8-by-default.windup.test.xml @@ -0,0 +1,59 @@ + + + data/utf-8-by-default + ../utf-8-by-default.windup.xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rules/rules-reviewed/openjdk21/openjdk17/utf-8-by-default.windup.xml b/rules/rules-reviewed/openjdk21/openjdk17/utf-8-by-default.windup.xml new file mode 100644 index 000000000..2f9603d2c --- /dev/null +++ b/rules/rules-reviewed/openjdk21/openjdk17/utf-8-by-default.windup.xml @@ -0,0 +1,81 @@ + + + + + APIs use the default charset which is changing from being platform dependent to defined as UTF-8 + + + + + + + + + + + CONSTRUCTOR_CALL + + + + + If not supplied, the `java.io.{class-name}` constructor uses UTF-8 by default. + If you haven't provided the character set, and UTF-8 is not appropriate for your class, then supply the appropriate character set to the constructor call. + + + + + + + + + + + + CONSTRUCTOR_CALL + + + + + If not supplied, the `java.util.{class-names}` constructor uses UTF-8 by default. + If you haven't provided the character set, and UTF-8 is not appropriate for your class, then supply the appropriate character set to the constructor call. + + + + + + + + + + + + METHOD_CALL + + + + + If not supplied, the `java.net.URLEncoder.encode` method uses UTF-8 by default. + If you haven't provided the character set, and UTF-8 is not appropriate for your class, then then supply the appropriate character set to the method call. + + + + + + + + + METHOD_CALL + + + + + If not supplied, the `java.net.URLDecoder.decode` method uses UTF-8 by default. + If you haven't provided the character set, and UTF-8 is not appropriate for your class, then then supply the appropriate character set to the method call. + + + + + + +