-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WINDUPRULE-1013 OpenJDK21 UTF-8 character set used by default (#1011)
- Loading branch information
1 parent
ffe7460
commit c509d75
Showing
11 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FileReaderExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FileWriterExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/FormatterExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...es-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/InputStreamReaderExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...s-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/OutputStreamWriterExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/PrintStreamExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/ScannerExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/URLDecoderExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
rules/rules-reviewed/openjdk21/openjdk17/tests/data/utf-8-by-default/URLEncoderExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
rules/rules-reviewed/openjdk21/openjdk17/tests/utf-8-by-default.windup.test.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0"?> | ||
<ruletest id="utf-8-by-default-test" | ||
xmlns="http://windup.jboss.org/schema/jboss-ruleset" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd"> | ||
<testDataPath>data/utf-8-by-default</testDataPath> | ||
<rulePath>../utf-8-by-default.windup.xml</rulePath> | ||
<ruleset> | ||
<rules> | ||
<rule id="utf-8-by-default-test-00000"> | ||
<when> | ||
<not> | ||
<iterable-filter size="5"> | ||
<hint-exists message="If not supplied, the `java.io." /> | ||
</iterable-filter> | ||
</not> | ||
</when> | ||
<perform> | ||
<fail message="utf-8-by-default-test-00000 - the hint was not found"/> | ||
</perform> | ||
</rule> | ||
<rule id="utf-8-by-default-test-00010"> | ||
<when> | ||
<not> | ||
<iterable-filter size="2"> | ||
<hint-exists message="If not supplied, the `java.util." /> | ||
</iterable-filter> | ||
</not> | ||
</when> | ||
<perform> | ||
<fail message="utf-8-by-default-test-00010 - the hint was not found"/> | ||
</perform> | ||
</rule> | ||
<rule id="utf-8-by-default-test-00020"> | ||
<when> | ||
<not> | ||
<iterable-filter size="1"> | ||
<hint-exists message="If not supplied, the `java.net.URLEncoder.encode`" /> | ||
</iterable-filter> | ||
</not> | ||
</when> | ||
<perform> | ||
<fail message="utf-8-by-default-test-00020 - the hint was not found"/> | ||
</perform> | ||
</rule> | ||
<rule id="utf-8-by-default-test-00030"> | ||
<when> | ||
<not> | ||
<iterable-filter size="1"> | ||
<hint-exists message="If not supplied, the `java.net.URLDecoder.decode`" /> | ||
</iterable-filter> | ||
</not> | ||
</when> | ||
<perform> | ||
<fail message="utf-8-by-default-test-00020 - the hint was not found"/> | ||
</perform> | ||
</rule> | ||
</rules> | ||
</ruleset> | ||
</ruletest> |
81 changes: 81 additions & 0 deletions
81
rules/rules-reviewed/openjdk21/openjdk17/utf-8-by-default.windup.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?xml version="1.0"?> | ||
<ruleset xmlns="http://windup.jboss.org/schema/jboss-ruleset" id="utf-8-by-default" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd"> | ||
<metadata> | ||
<description> | ||
APIs use the default charset which is changing from being platform dependent to defined as UTF-8 | ||
</description> | ||
<dependencies> | ||
<addon id="org.jboss.windup.rules,windup-rules-java,3.0.0.Final" /> | ||
</dependencies> | ||
<targetTechnology id="openjdk" versionRange="[18,)"/> | ||
</metadata> | ||
<rules> | ||
<rule id="utf-8-by-default-00000"> | ||
<when> | ||
<javaclass references="java.io.{class-name}{*}"> | ||
<location>CONSTRUCTOR_CALL</location> | ||
</javaclass> | ||
</when> | ||
<perform> | ||
<hint title="The java.io.{class-name} constructor uses UTF-8 by default" effort="1" category-id="potential"> | ||
<message>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. | ||
</message> | ||
<link title="JEP 400: UTF-8 by Default" href="https://openjdk.org/jeps/400"/> | ||
</hint> | ||
</perform> | ||
<where param="class-name"> | ||
<matches pattern="(FileReader|FileWriter|InputStreamReader|OutputStreamWriter|PrintStream)"/> | ||
</where> | ||
</rule> | ||
<rule id="utf-8-by-default-00010"> | ||
<when> | ||
<javaclass references="java.util.{class-names}{*}"> | ||
<location>CONSTRUCTOR_CALL</location> | ||
</javaclass> | ||
</when> | ||
<perform> | ||
<hint title="The java.util.{class-names} constructor uses UTF-8 by default" effort="1" category-id="potential"> | ||
<message>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. | ||
</message> | ||
<link title="JEP 400: UTF-8 by Default" href="https://openjdk.org/jeps/400"/> | ||
</hint> | ||
</perform> | ||
<where param="class-names"> | ||
<matches pattern="(Formatter|Scanner)"/> | ||
</where> | ||
</rule> | ||
<rule id="utf-8-by-default-00020"> | ||
<when> | ||
<javaclass references="java.net.URLEncoder.encode{*}"> | ||
<location>METHOD_CALL</location> | ||
</javaclass> | ||
</when> | ||
<perform> | ||
<hint title="The java.net.URLEncoder.encode method uses UTF-8 by default" effort="1" category-id="potential"> | ||
<message>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. | ||
</message> | ||
<link title="JEP 400: UTF-8 by Default" href="https://openjdk.org/jeps/400"/> | ||
</hint> | ||
</perform> | ||
</rule> | ||
<rule id="utf-8-by-default-00030"> | ||
<when> | ||
<javaclass references="java.net.URLDecoder.decode{*}"> | ||
<location>METHOD_CALL</location> | ||
</javaclass> | ||
</when> | ||
<perform> | ||
<hint title="The java.net.URLDecoder.decode method uses UTF-8 by default" effort="1" category-id="potential"> | ||
<message>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. | ||
</message> | ||
<link title="JEP 400: UTF-8 by Default" href="https://openjdk.org/jeps/400"/> | ||
</hint> | ||
</perform> | ||
</rule> | ||
</rules> | ||
</ruleset> |