Skip to content

Commit

Permalink
WINDUPRULE-1013 OpenJDK21 UTF-8 character set used by default (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipCattanach authored Jul 27, 2023
1 parent ffe7460 commit c509d75
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 0 deletions.
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();
}
}
}
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();
}
}
}
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);

}
}
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();
}
}
}
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();
}
}
}
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();
}
}
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();
}
}
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"));
}
}
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);
}

}
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>
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>

0 comments on commit c509d75

Please sign in to comment.