diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/UseExplicitEncodingCleanUpCore.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/UseExplicitEncodingCleanUpCore.java index 810b05245fd..9a6d42bdba5 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/UseExplicitEncodingCleanUpCore.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/UseExplicitEncodingCleanUpCore.java @@ -77,8 +77,10 @@ public ICleanUpFix createFix(final CleanUpContext context) throws CoreException if (operations.isEmpty()) { return null; } + + CompilationUnitRewriteOperation[] array= operations.toArray(new CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation[0]); return new CompilationUnitRewriteOperationsFixCore(ExplicitEncodingCleanUpFix_refactor, - compilationUnit, operations.toArray(new CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation[0])); + compilationUnit, array); } private ChangeBehavior computeRefactorDeepth() { diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/AbstractExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/AbstractExplicitEncoding.java index 3349cdbae23..8983a06004b 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/AbstractExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/AbstractExplicitEncoding.java @@ -18,12 +18,19 @@ import java.util.Map; import java.util.Set; +import org.eclipse.text.edits.ReplaceEdit; import org.eclipse.text.edits.TextEditGroup; +import org.eclipse.jface.text.BadLocationException; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.compiler.InvalidInputException; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.Block; import org.eclipse.jdt.core.dom.CatchClause; +import org.eclipse.jdt.core.dom.Comment; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.FieldDeclaration; @@ -46,6 +53,9 @@ import org.eclipse.jdt.internal.common.ReferenceHolder; import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; +import org.eclipse.jdt.internal.corext.refactoring.nls.NLSElement; +import org.eclipse.jdt.internal.corext.refactoring.nls.NLSLine; +import org.eclipse.jdt.internal.corext.refactoring.nls.NLSScanner; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; /** @@ -74,7 +84,7 @@ static class Nodedata { public String encoding; - public static Map charsetConstants=new HashMap<>(); + public static Map charsetConstants= new HashMap<>(); } protected static final String ENCODING= "encoding"; //$NON-NLS-1$ @@ -201,4 +211,88 @@ protected void removeUnsupportedEncodingException(final ASTNode visited, TextEdi } } } + + protected void removeNLSComment(CompilationUnitRewrite cuRewrite, ASTNode node, TextEditGroup group, ASTRewrite rewrite) { + CompilationUnit unit= cuRewrite.getRoot(); + ICompilationUnit icu= (ICompilationUnit) cuRewrite.getRoot().getJavaElement(); + + if (icu == null) { + System.err.println("ICompilationUnit is null."); //$NON-NLS-1$ + return; + } + + String source= null; + try { + source= icu.getSource(); + } catch (JavaModelException e) { + e.printStackTrace(); + } + + if (source == null) { + System.err.println("Source code is null."); //$NON-NLS-1$ + return; + } + + int startLine= unit.getLineNumber(node.getStartPosition()); + int endOfLine= unit.getPosition(startLine + 1, 0); + String lineText= source.substring(node.getStartPosition(), endOfLine); + + try { + NLSLine[] lines= NLSScanner.scan(lineText); + + for (NLSLine nlsLine : lines) { + if (nlsLine != null && isConsistent(nlsLine, true)) { + for (NLSElement element : nlsLine.getElements()) { + if (element.hasTag()) { + Comment comment= findCommentNode(unit, element.getTagText()); + if (comment != null) { + // Jetzt entfernen wir den Kommentar als ReplaceEdit. + ReplaceEdit edit= new ReplaceEdit(comment.getStartPosition(), comment.getLength(), ""); //$NON-NLS-1$ + group.addTextEdit(edit); // Die Bearbeitung zur TextEditGroup hinzufügen + System.out.println("Removed NLS comment: " + comment.getStartPosition()); //$NON-NLS-1$ + } + } + } + } + } + } catch (InvalidInputException | BadLocationException e) { + e.printStackTrace(); + } + } + + private boolean isConsistent(NLSLine nlsLine, boolean isTagged) { + NLSElement[] elements= nlsLine.getElements(); + for (NLSElement element : elements) { + if (element.hasTag() != isTagged) { + return false; + } + } + return true; + } + + private Comment findCommentNode(CompilationUnit unit, String commentContent) { + List commentList= unit.getCommentList(); + for (Comment comment : commentList) { + String content= getCommentContent(comment, unit); + if (content != null && content.equals(commentContent)) { + return comment; + } + } + return null; + } + + private String getCommentContent(Comment comment, CompilationUnit unit) { + try { + // Holen des ICompilationUnit-Objekts aus dem CompilationUnit + ICompilationUnit cu= (ICompilationUnit) unit.getJavaElement(); + if (cu != null) { + // Abrufen des Quelltextes des gesamten ICompilationUnit + String source= cu.getSource(); + return source.substring(comment.getStartPosition(), comment.getStartPosition() + comment.getLength()); + } + } catch (JavaModelException e) { + e.printStackTrace(); + } + return null; + } } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChannelsNewReaderExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChannelsNewReaderExplicitEncoding.java index aadeb3c84db..e399ca8607f 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChannelsNewReaderExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChannelsNewReaderExplicitEncoding.java @@ -114,6 +114,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); + removeNLSComment(cuRewrite, visited, group, rewrite); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/InputStreamReaderExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/InputStreamReaderExplicitEncoding.java index 11425274d55..f1ea47a622b 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/InputStreamReaderExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/InputStreamReaderExplicitEncoding.java @@ -19,18 +19,11 @@ import org.eclipse.text.edits.TextEditGroup; -import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; -import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; import org.eclipse.jdt.core.dom.ClassInstanceCreation; -import org.eclipse.jdt.core.dom.Comment; import org.eclipse.jdt.core.dom.CompilationUnit; -import org.eclipse.jdt.core.dom.LineComment; import org.eclipse.jdt.core.dom.StringLiteral; -import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; -import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; import org.eclipse.jdt.core.dom.rewrite.ListRewrite; import org.eclipse.jdt.internal.common.HelperVisitor; @@ -93,83 +86,23 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, } @Override - public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, + public void rewrite(UseExplicitEncodingFixCore upp, ClassInstanceCreation visited, CompilationUnitRewrite cuRewrite, TextEditGroup group, ChangeBehavior cb, ReferenceHolder data) { - ASTRewrite rewrite= cuRewrite.getASTRewrite(); - AST ast= cuRewrite.getRoot().getAST(); - ImportRewrite importRewriter= cuRewrite.getImportRewrite(); - Nodedata nodedata= (Nodedata) data.get(visited); + ASTRewrite rewrite = cuRewrite.getASTRewrite(); + Nodedata nodedata = (Nodedata) data.get(visited); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding, Nodedata.charsetConstants); - /** - * Add Charset.defaultCharset() as second (last) parameter - */ - ListRewrite listRewrite= rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY); + ASTNode callToCharsetDefaultCharset = cb.computeCharsetASTNode(cuRewrite, cuRewrite.getRoot().getAST(), + nodedata.encoding, Nodedata.charsetConstants); + + ListRewrite listRewrite = rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY); if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); - // Remove NLS comment -// removeNLSComment(visited, cuRewrite, group); + removeNLSComment(cuRewrite, visited, group, rewrite); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); } - removeUnsupportedEncodingException(visited, group, rewrite, importRewriter); - } - - @SuppressWarnings("unused") - private void removeNLSComment(ASTNode node, CompilationUnitRewrite cuRewrite, TextEditGroup group) { - CompilationUnit unit= cuRewrite.getRoot(); - ASTRewrite rewrite= cuRewrite.getASTRewrite(); - - List comments= unit.getCommentList(); - boolean removed= false; - for (Comment comment : comments) { - if (comment instanceof LineComment) { - String commentContent= getCommentContent(comment, cuRewrite); - System.out.println("Checking comment: " + commentContent); //$NON-NLS-1$ - - if (commentContent != null && commentContent.contains("$NON-NLS-")) { //$NON-NLS-1$ - if (comment.getStartPosition() > node.getStartPosition()) { - ASTNode parent= comment.getParent(); - if (parent != null) { - StructuralPropertyDescriptor property= comment.getLocationInParent(); - if (property != null) { - if (property.isChildListProperty()) { - ListRewrite listRewrite= rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) property); - System.out.println("Removing comment at position: " + comment.getStartPosition()); //$NON-NLS-1$ - listRewrite.remove(comment, group); - removed= true; - } else { - rewrite.remove(comment, group); - removed= true; - } - } else { - System.err.println("No valid location found for comment."); //$NON-NLS-1$ - } - } - } - } - } - } - - if (!removed) { - System.out.println("No NLS comment found to remove."); //$NON-NLS-1$ - } - } - - private String getCommentContent(Comment comment, CompilationUnitRewrite cuRewrite) { - try { - int startPosition= comment.getStartPosition(); - int length= comment.getLength(); - - ICompilationUnit icu= (ICompilationUnit) cuRewrite.getRoot().getJavaElement(); - if (icu != null) { - return icu.getSource().substring(startPosition, startPosition + length); - } - } catch (Exception e) { - e.printStackTrace(); - } - return null; + removeUnsupportedEncodingException(visited, group, rewrite, cuRewrite.getImportRewrite()); } @Override diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/OutputStreamWriterExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/OutputStreamWriterExplicitEncoding.java index 36490c527cf..749d5edb1e4 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/OutputStreamWriterExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/OutputStreamWriterExplicitEncoding.java @@ -112,6 +112,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation ListRewrite listRewrite= rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY); if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); + removeNLSComment(cuRewrite, visited, group, rewrite); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/StringGetBytesExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/StringGetBytesExplicitEncoding.java index a72851131c4..b8f773efbe0 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/StringGetBytesExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/StringGetBytesExplicitEncoding.java @@ -100,6 +100,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); + removeNLSComment(cuRewrite, visited, group, rewrite); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); } diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingCleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingCleanUpTest.java index 713e85ebe63..c1f05c344ca 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingCleanUpTest.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingCleanUpTest.java @@ -62,6 +62,8 @@ public void testExplicitEncodingParametrizedKeepBehavior(ExplicitEncodingPattern context.enable(CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR); context.disable(CleanUpConstants.EXPLICITENCODING_INSERT_UTF8); context.disable(CleanUpConstants.EXPLICITENCODING_AGGREGATE_TO_UTF8); +// context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); + context.enable(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS); context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); } diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsKeepBehavior.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsKeepBehavior.java index 1937a6ca6d8..d37cc2b8cf0 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsKeepBehavior.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsKeepBehavior.java @@ -16,54 +16,42 @@ public enum ExplicitEncodingPatternsKeepBehavior { CHARSET(""" - package test1; +package test1; - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; +import java.nio.charset.Charset; - public class E1 { - void method(String filename) { - Charset cs1= Charset.forName("UTF-8"); - Charset cs1b= Charset.forName("Utf-8"); - Charset cs2= Charset.forName("UTF-16"); - Charset cs3= Charset.forName("UTF-16BE"); - Charset cs4= Charset.forName("UTF-16LE"); - Charset cs5= Charset.forName("ISO-8859-1"); - Charset cs6= Charset.forName("US-ASCII"); - String result= cs1.toString(); - } - } - } +public class E1 { + @SuppressWarnings("unused") + void method(String filename) { + Charset cs1= Charset.forName("UTF-8"); //$NON-NLS-1$ + Charset cs1b= Charset.forName("Utf-8"); //$NON-NLS-1$ + Charset cs2= Charset.forName("UTF-16"); //$NON-NLS-1$ + Charset cs3= Charset.forName("UTF-16BE"); //$NON-NLS-1$ + Charset cs4= Charset.forName("UTF-16LE"); //$NON-NLS-1$ + Charset cs5= Charset.forName("ISO-8859-1"); //$NON-NLS-1$ + Charset cs6= Charset.forName("US-ASCII"); //$NON-NLS-1$ + String result= cs1.toString(); + } +} """, """ - package test1; +package test1; - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; +import java.nio.charset.Charset; - public class E1 { - void method(String filename) { - Charset cs1= Charset.forName("UTF-8"); - Charset cs1b= Charset.forName("Utf-8"); - Charset cs2= Charset.forName("UTF-16"); - Charset cs3= Charset.forName("UTF-16BE"); - Charset cs4= Charset.forName("UTF-16LE"); - Charset cs5= Charset.forName("ISO-8859-1"); - Charset cs6= Charset.forName("US-ASCII"); - String result= cs1.toString(); - } - } - } +public class E1 { + @SuppressWarnings("unused") + void method(String filename) { + Charset cs1= Charset.forName("UTF-8"); //$NON-NLS-1$ + Charset cs1b= Charset.forName("Utf-8"); //$NON-NLS-1$ + Charset cs2= Charset.forName("UTF-16"); //$NON-NLS-1$ + Charset cs3= Charset.forName("UTF-16BE"); //$NON-NLS-1$ + Charset cs4= Charset.forName("UTF-16LE"); //$NON-NLS-1$ + Charset cs5= Charset.forName("ISO-8859-1"); //$NON-NLS-1$ + Charset cs6= Charset.forName("US-ASCII"); //$NON-NLS-1$ + String result= cs1.toString(); + } +} """), BYTEARRAYOUTSTREAM(""" package test1; diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java22/ExplicitEncodingCleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java22/ExplicitEncodingCleanUpTest.java index cc2d02a9056..7be88953b25 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java22/ExplicitEncodingCleanUpTest.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java22/ExplicitEncodingCleanUpTest.java @@ -53,878 +53,6 @@ protected void setUp() throws Exception,UnsupportedCharsetException { @RegisterExtension AbstractEclipseJava context= new EclipseJava22(); - enum ExplicitEncodingPatterns { - - CHARSET( -""" -package test1; - -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; - -public class E1 { - - void method(String filename) { - // Ursprüngliche Verwendung von Charset.forName() mit verschiedenen Charsets - Charset cs1 = Charset.forName("UTF-8"); - Charset cs1b = Charset.forName("Utf-8"); // Unterschiedliche Schreibweise (diese sollten gleich behandelt werden) - Charset cs2 = Charset.forName("UTF-16"); - Charset cs3 = Charset.forName("UTF-16BE"); - Charset cs4 = Charset.forName("UTF-16LE"); - Charset cs5 = Charset.forName("ISO-8859-1"); - Charset cs6 = Charset.forName("US-ASCII"); - - // Ausgabe, die durch den Cleanup angepasst wird - System.out.println(cs1.toString()); - System.out.println(cs2.toString()); - - // Beispiel mit einer Variablen - String charsetName = "UTF-8"; // Wird durch eine Variable ersetzt - Charset cs7 = Charset.forName(charsetName); // Umstellung erforderlich - System.out.println(cs7); - - // Testen eines ungültigen Charsets - try { - Charset cs8 = Charset.forName("non-existing-charset"); // Ungültiger Charset - System.out.println(cs8); - } catch (IllegalArgumentException e) { - System.out.println("Fehler: " + e.getMessage()); - } - - // Ein benutzerdefinierter Charset-Test - Charset cs9 = Charset.forName("windows-1252"); - System.out.println(cs9.toString()); - } - - void methodWithVariableCharset(String charsetName) { - Charset cs = Charset.forName(charsetName); // Charset über eine Variable - System.out.println(cs.toString()); - } -} -""", - -""" -package test1; - -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; - -public class E1 { - - void method(String filename) { - // Ursprüngliche Verwendung von Charset.forName() mit verschiedenen Charsets - Charset cs1 = StandardCharsets.UTF_8; - Charset cs1b = StandardCharsets.UTF_8; // Unterschiedliche Schreibweise (diese sollten gleich behandelt werden) - Charset cs2 = StandardCharsets.UTF_16; - Charset cs3 = StandardCharsets.UTF_16BE; - Charset cs4 = StandardCharsets.UTF_16LE; - Charset cs5 = StandardCharsets.ISO_8859_1; - Charset cs6 = StandardCharsets.US_ASCII; - - // Ausgabe, die durch den Cleanup angepasst wird - System.out.println(cs1.toString()); - System.out.println(cs2.toString()); - - // Beispiel mit einer Variablen - String charsetName = "UTF-8"; // Wird durch eine Variable ersetzt - Charset cs7 = StandardCharsets.UTF_8; // Umstellung erforderlich - System.out.println(cs7); - - // Testen eines ungültigen Charsets - try { - Charset cs8 = Charset.forName("non-existing-charset"); // Ungültiger Charset - System.out.println(cs8); - } catch (IllegalArgumentException e) { - System.out.println("Fehler: " + e.getMessage()); - } - - // Ein benutzerdefinierter Charset-Test - Charset cs9 = Charset.forName("windows-1252"); - System.out.println(cs9.toString()); - } - - void methodWithVariableCharset(String charsetName) { - Charset cs = Charset.forName(charsetName); // Charset über eine Variable - System.out.println(cs.toString()); - } -} -"""), - BYTEARRAYOUTSTREAM(""" - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(); - ByteArrayOutputStream ba2=new ByteArrayOutputStream(); - String result2=ba2.toString("UTF-8"); - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(Charset.defaultCharset()); - ByteArrayOutputStream ba2=new ByteArrayOutputStream(); - String result2=ba2.toString(StandardCharsets.UTF_8); - } - } - } - """), - FILEREADER(""" - package test1; - - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - Reader is=new FileReader(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - FILEWRITER(""" - package test1; - - import java.io.FileWriter; - import java.io.Writer; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - Writer fw=new FileWriter(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.FileWriter; - import java.io.OutputStreamWriter; - import java.io.Writer; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; - import java.io.FileOutputStream; - - public class E1 { - void method(String filename) { - try { - Writer fw=new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - INPUTSTREAMREADER( - """ - package test1; - - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$ - InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt"), Charset.defaultCharset()); //$NON-NLS-1$ - InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - OUTPUTSTREAMWRITER( - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - CHANNELSNEWREADER(""" - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.channels.ReadableByteChannel; - import java.nio.charset.StandardCharsets; - import java.nio.channels.Channels; - import java.io.FileNotFoundException; - import java.io.UnsupportedEncodingException; - - public class E1 { - void method(String filename) throws UnsupportedEncodingException { - ReadableByteChannel ch; - Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.channels.ReadableByteChannel; - import java.nio.charset.StandardCharsets; - import java.nio.channels.Channels; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - ReadableByteChannel ch; - Reader r=Channels.newReader(ch,StandardCharsets.UTF_8); //$NON-NLS-1$ - } - } - } - """), - CHANNELSNEWWRITER(""" - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Writer; - import java.nio.channels.WritableByteChannel; - import java.nio.charset.StandardCharsets; - import java.nio.channels.Channels; - import java.io.FileNotFoundException; - import java.io.UnsupportedEncodingException; - - public class E1 { - void method(String filename) throws UnsupportedEncodingException { - WritableByteChannel ch; - Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Writer; - import java.nio.channels.WritableByteChannel; - import java.nio.charset.StandardCharsets; - import java.nio.channels.Channels; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - WritableByteChannel ch; - Writer w=Channels.newWriter(ch,StandardCharsets.UTF_8); //$NON-NLS-1$ - } - } - } - """), - PRINTWRITER(""" - package test1; - - import java.io.PrintWriter; - import java.io.Writer; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - Writer w=new PrintWriter(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.PrintWriter; - import java.io.Writer; - import java.nio.charset.Charset; - import java.io.BufferedWriter; - import java.io.FileNotFoundException; - import java.io.FileOutputStream; - import java.io.OutputStreamWriter; - - public class E1 { - void method(String filename) { - try { - Writer w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset())); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - STRINGGETBYTES( -""" -package test1; - -import java.io.ByteArrayOutputStream; -import java.io.InputStreamReader; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.Reader; -import java.io.FileNotFoundException; - -public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - byte[] bytes= s.getBytes(); - byte[] bytes2= s.getBytes("UTF-8"); - System.out.println(bytes.length); - } - } - - void method2(String filename) { - String s="asdf"; //$NON-NLS-1$ - byte[] bytes= s.getBytes(); - try { - byte[] bytes2= s.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - System.out.println(bytes.length); - } -} -""", - -""" -package test1; - -import java.io.ByteArrayOutputStream; -import java.io.InputStreamReader; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.Reader; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.io.FileNotFoundException; - -public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - byte[] bytes= s.getBytes(Charset.defaultCharset()); - byte[] bytes2= s.getBytes(StandardCharsets.UTF_8); - System.out.println(bytes.length); - } - } - - void method2(String filename) { - String s="asdf"; //$NON-NLS-1$ - byte[] bytes= s.getBytes(Charset.defaultCharset()); - try { - byte[] bytes2= s.getBytes(StandardCharsets.UTF_8); - } - System.out.println(bytes.length); - } -} -"""), - STRING(""" - package test1; - - import java.io.FileNotFoundException; - import java.io.UnsupportedEncodingException; - - public class E5 { - - static void bla() throws FileNotFoundException, UnsupportedEncodingException { - byte[] b= {(byte)59}; - String s1=new String(b,"UTF-8"); - String s2=new String(b,0,1,"UTF-8"); - } - } - """, - - """ - package test1; - - import java.io.FileNotFoundException; - import java.nio.charset.StandardCharsets; - - public class E5 { - - static void bla() throws FileNotFoundException { - byte[] b= {(byte)59}; - String s1=new String(b,StandardCharsets.UTF_8); - String s2=new String(b,0,1,StandardCharsets.UTF_8); - } - } - """), - PROPERTIESSTORETOXML(""" - package test1; - - import java.io.FileOutputStream; - import java.io.IOException; - import java.util.Properties; - - public class E1 { - static void blu() throws IOException { - Properties p=new Properties(); - try (FileOutputStream os = new FileOutputStream("out.xml")) { - p.storeToXML(os, null, "UTF-8"); - } - try (FileOutputStream os = new FileOutputStream("out.xml")) { - p.storeToXML(os, null); - } - } - } - """, - - """ - package test1; - - import java.io.FileOutputStream; - import java.io.IOException; - import java.nio.charset.StandardCharsets; - import java.util.Properties; - - public class E1 { - static void blu() throws IOException { - Properties p=new Properties(); - try (FileOutputStream os = new FileOutputStream("out.xml")) { - p.storeToXML(os, null, StandardCharsets.UTF_8); - } - try (FileOutputStream os = new FileOutputStream("out.xml")) { - p.storeToXML(os, null, StandardCharsets.UTF_8); - } - } - } - """), - URLDECODER(""" - package test1; - import java.io.UnsupportedEncodingException; - import java.net.URLDecoder; - - public class E2 { - - static void bla() throws UnsupportedEncodingException { - String url=URLDecoder.decode("asdf","UTF-8"); - String url2=URLDecoder.decode("asdf"); - } - } - """, """ - package test1; - import java.net.URLDecoder; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - - public class E2 { - - static void bla() { - String url=URLDecoder.decode("asdf",StandardCharsets.UTF_8); - String url2=URLDecoder.decode("asdf", Charset.defaultCharset()); - } - } - """), - URLENCODER(""" - package test1; - import java.io.UnsupportedEncodingException; - import java.net.URLEncoder; - - public class E2 { - - static void bla() throws UnsupportedEncodingException { - String url=URLEncoder.encode("asdf","UTF-8"); - String url4=URLEncoder.encode("asdf"); - } - } - """, """ - package test1; - import java.net.URLEncoder; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - - public class E2 { - - static void bla() { - String url=URLEncoder.encode("asdf",StandardCharsets.UTF_8); - String url4=URLEncoder.encode("asdf", Charset.defaultCharset()); - } - } - """), - SCANNER(""" - package test1; - import java.io.File; - import java.io.FileNotFoundException; - import java.util.Scanner; - - public class E3 { - - static void bla3(InputStream is) throws FileNotFoundException { - Scanner s=new Scanner(new File("asdf"),"UTF-8"); - Scanner s2=new Scanner(is,"UTF-8"); - Scanner s3=new Scanner("asdf"); - } - } - """, -""" -package test1; -import java.io.File; -import java.io.FileNotFoundException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.util.Scanner; - -public class E3 { - - static void bla3(InputStream is) throws FileNotFoundException { - Scanner s=new Scanner(new File("asdf"),StandardCharsets.UTF_8); - Scanner s2=new Scanner(is,StandardCharsets.UTF_8); - Scanner s3=new Scanner("asdf", Charset.defaultCharset()); - } -} -"""), - FORMATTER( -""" -package test1; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.UnsupportedEncodingException; -import java.util.Formatter; - -public class E4 { - - static void bla() throws FileNotFoundException, UnsupportedEncodingException { - Formatter s=new Formatter(new File("asdf"),"UTF-8"); - } - - static void bli() throws FileNotFoundException { - try { - Formatter s=new Formatter(new File("asdf"),"UTF-8"); - } catch (FileNotFoundException | UnsupportedEncodingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } -} -""", """ -package test1; -import java.io.File; -import java.io.FileNotFoundException; -import java.nio.charset.StandardCharsets; -import java.util.Formatter; - -public class E4 { - - static void bla() throws FileNotFoundException { - Formatter s=new Formatter(new File("asdf"),StandardCharsets.UTF_8); - } - - static void bli() throws FileNotFoundException { - try { - Formatter s=new Formatter(new File("asdf"),StandardCharsets.UTF_8); - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } -} -"""), - THREE(""" - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - byte[] bytes= s.getBytes(); - System.out.println(bytes.length); - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(); - try { - InputStreamReader is=new InputStreamReader(new FileInputStream("")); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - Reader is=new FileReader(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - byte[] bytes= s.getBytes(Charset.defaultCharset()); - System.out.println(bytes.length); - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(Charset.defaultCharset()); - try { - InputStreamReader is=new InputStreamReader(new FileInputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - ENCODINGASSTRINGPARAMETER( - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); - byte[] bytes= s.getBytes("Utf-8"); - System.out.println(bytes.length); - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(); - try { - InputStreamReader is=new InputStreamReader(new FileInputStream(""), "UTF-8"); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - Reader is=new FileReader(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); - byte[] bytes= s.getBytes(StandardCharsets.UTF_8); - System.out.println(bytes.length); - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(Charset.defaultCharset()); - try { - InputStreamReader is=new InputStreamReader(new FileInputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """); - - String given; - String expected; - - ExplicitEncodingPatterns(String given, String expected) { - this.given= given; - this.expected= expected; - } - } - @ParameterizedTest @EnumSource(ExplicitEncodingPatterns.class) public void testExplicitEncodingParametrized(ExplicitEncodingPatterns test) throws CoreException { @@ -932,6 +60,10 @@ public void testExplicitEncodingParametrized(ExplicitEncodingPatterns test) thro ICompilationUnit cu= pack.createCompilationUnit("E1.java", test.given, false, null); context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); context.enable(CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR); + context.disable(CleanUpConstants.EXPLICITENCODING_INSERT_UTF8); + context.disable(CleanUpConstants.EXPLICITENCODING_AGGREGATE_TO_UTF8); +// context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); + context.enable(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS); context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); } diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java22/ExplicitEncodingPatterns.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java22/ExplicitEncodingPatterns.java new file mode 100644 index 00000000000..322c6c35565 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java22/ExplicitEncodingPatterns.java @@ -0,0 +1,874 @@ +package org.eclipse.jdt.ui.tests.quickfix.Java22; + +enum ExplicitEncodingPatterns { + + CHARSET( +""" +package test1; + +import java.nio.charset.Charset; + +public class E1 { + + @SuppressWarnings("unused") + void method(String filename) { + // Ursprüngliche Verwendung von Charset.forName() mit verschiedenen Charsets + Charset cs1 = Charset.forName("UTF-8"); //$NON-NLS-1$ + Charset cs1b = Charset.forName("Utf-8"); // Unterschiedliche Schreibweise (diese sollten gleich behandelt werden) //$NON-NLS-1$ + Charset cs2 = Charset.forName("UTF-16"); //$NON-NLS-1$ + Charset cs3 = Charset.forName("UTF-16BE"); //$NON-NLS-1$ + Charset cs4 = Charset.forName("UTF-16LE"); //$NON-NLS-1$ + Charset cs5 = Charset.forName("ISO-8859-1"); //$NON-NLS-1$ + Charset cs6 = Charset.forName("US-ASCII"); //$NON-NLS-1$ + + // Ausgabe, die durch den Cleanup angepasst wird + System.out.println(cs1.toString()); + System.out.println(cs2.toString()); + + // Beispiel mit einer Variablen + String charsetName = "UTF-8"; // Wird durch eine Variable ersetzt //$NON-NLS-1$ + Charset cs7 = Charset.forName(charsetName); // Umstellung erforderlich + System.out.println(cs7); + + // Testen eines ungültigen Charsets + try { + Charset cs8 = Charset.forName("non-existing-charset"); // Ungültiger Charset //$NON-NLS-1$ + System.out.println(cs8); + } catch (IllegalArgumentException e) { + System.out.println("Fehler: " + e.getMessage()); //$NON-NLS-1$ + } + + // Ein benutzerdefinierter Charset-Test + Charset cs9 = Charset.forName("windows-1252"); //$NON-NLS-1$ + System.out.println(cs9.toString()); + } + + void methodWithVariableCharset(String charsetName) { + Charset cs = Charset.forName(charsetName); // Charset über eine Variable + System.out.println(cs.toString()); + } +} +""", + +""" +package test1; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + + @SuppressWarnings("unused") + void method(String filename) { + // Ursprüngliche Verwendung von Charset.forName() mit verschiedenen Charsets + Charset cs1 = StandardCharsets.UTF_8; //$NON-NLS-1$ + Charset cs1b = StandardCharsets.UTF_8; // Unterschiedliche Schreibweise (diese sollten gleich behandelt werden) //$NON-NLS-1$ + Charset cs2 = StandardCharsets.UTF_16; //$NON-NLS-1$ + Charset cs3 = StandardCharsets.UTF_16BE; //$NON-NLS-1$ + Charset cs4 = StandardCharsets.UTF_16LE; //$NON-NLS-1$ + Charset cs5 = StandardCharsets.ISO_8859_1; //$NON-NLS-1$ + Charset cs6 = StandardCharsets.US_ASCII; //$NON-NLS-1$ + + // Ausgabe, die durch den Cleanup angepasst wird + System.out.println(cs1.toString()); + System.out.println(cs2.toString()); + + // Beispiel mit einer Variablen + String charsetName = "UTF-8"; // Wird durch eine Variable ersetzt //$NON-NLS-1$ + Charset cs7 = StandardCharsets.UTF_8; // Umstellung erforderlich + System.out.println(cs7); + + // Testen eines ungültigen Charsets + try { + Charset cs8 = Charset.forName("non-existing-charset"); // Ungültiger Charset //$NON-NLS-1$ + System.out.println(cs8); + } catch (IllegalArgumentException e) { + System.out.println("Fehler: " + e.getMessage()); //$NON-NLS-1$ + } + + // Ein benutzerdefinierter Charset-Test + Charset cs9 = Charset.forName("windows-1252"); //$NON-NLS-1$ + System.out.println(cs9.toString()); + } + + void methodWithVariableCharset(String charsetName) { + Charset cs = Charset.forName(charsetName); // Charset über eine Variable + System.out.println(cs.toString()); + } +} +"""), + BYTEARRAYOUTSTREAM(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString("UTF-8"); + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(Charset.defaultCharset()); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString(StandardCharsets.UTF_8); + } + } + } + """), + FILEREADER(""" + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + FILEWRITER(""" + package test1; + + import java.io.FileWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer fw=new FileWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.FileWriter; + import java.io.OutputStreamWriter; + import java.io.Writer; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + import java.io.FileOutputStream; + + public class E1 { + void method(String filename) { + try { + Writer fw=new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + INPUTSTREAMREADER( + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$ + InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt"), Charset.defaultCharset()); //$NON-NLS-1$ + InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + OUTPUTSTREAMWRITER( + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + CHANNELSNEWREADER(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.channels.ReadableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E1 { + void method(String filename) throws UnsupportedEncodingException { + ReadableByteChannel ch; + Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.channels.ReadableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ReadableByteChannel ch; + Reader r=Channels.newReader(ch,StandardCharsets.UTF_8); //$NON-NLS-1$ + } + } + } + """), + CHANNELSNEWWRITER(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Writer; + import java.nio.channels.WritableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E1 { + void method(String filename) throws UnsupportedEncodingException { + WritableByteChannel ch; + Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Writer; + import java.nio.channels.WritableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + WritableByteChannel ch; + Writer w=Channels.newWriter(ch,StandardCharsets.UTF_8); //$NON-NLS-1$ + } + } + } + """), + PRINTWRITER(""" + package test1; + + import java.io.PrintWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer w=new PrintWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.PrintWriter; + import java.io.Writer; + import java.nio.charset.Charset; + import java.io.BufferedWriter; + import java.io.FileNotFoundException; + import java.io.FileOutputStream; + import java.io.OutputStreamWriter; + + public class E1 { + void method(String filename) { + try { + Writer w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset())); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + STRINGGETBYTES( +""" +package test1; + +import java.io.ByteArrayOutputStream; +import java.io.InputStreamReader; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.Reader; +import java.io.FileNotFoundException; + +public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + byte[] bytes2= s.getBytes("UTF-8"); + System.out.println(bytes.length); + } + } + + void method2(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + try { + byte[] bytes2= s.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(bytes.length); + } +} +""", + +""" +package test1; + +import java.io.ByteArrayOutputStream; +import java.io.InputStreamReader; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.Reader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.io.FileNotFoundException; + +public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + byte[] bytes2= s.getBytes(StandardCharsets.UTF_8); + System.out.println(bytes.length); + } + } + + void method2(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + try { + byte[] bytes2= s.getBytes(StandardCharsets.UTF_8); + } + System.out.println(bytes.length); + } +} +"""), + STRING(""" + package test1; + + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E5 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + byte[] b= {(byte)59}; + String s1=new String(b,"UTF-8"); + String s2=new String(b,0,1,"UTF-8"); + } + } + """, + + """ + package test1; + + import java.io.FileNotFoundException; + import java.nio.charset.StandardCharsets; + + public class E5 { + + static void bla() throws FileNotFoundException { + byte[] b= {(byte)59}; + String s1=new String(b,StandardCharsets.UTF_8); + String s2=new String(b,0,1,StandardCharsets.UTF_8); + } + } + """), + PROPERTIESSTORETOXML(""" + package test1; + + import java.io.FileOutputStream; + import java.io.IOException; + import java.util.Properties; + + public class E1 { + static void blu() throws IOException { + Properties p=new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, "UTF-8"); + } + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null); + } + } + } + """, + + """ + package test1; + + import java.io.FileOutputStream; + import java.io.IOException; + import java.nio.charset.StandardCharsets; + import java.util.Properties; + + public class E1 { + static void blu() throws IOException { + Properties p=new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, StandardCharsets.UTF_8); + } + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, StandardCharsets.UTF_8); + } + } + } + """), + URLDECODER(""" + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLDecoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLDecoder.decode("asdf","UTF-8"); + String url2=URLDecoder.decode("asdf"); + } + } + """, """ + package test1; + import java.net.URLDecoder; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + + public class E2 { + + static void bla() { + String url=URLDecoder.decode("asdf",StandardCharsets.UTF_8); + String url2=URLDecoder.decode("asdf", Charset.defaultCharset()); + } + } + """), + URLENCODER(""" + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLEncoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLEncoder.encode("asdf","UTF-8"); + String url4=URLEncoder.encode("asdf"); + } + } + """, """ + package test1; + import java.net.URLEncoder; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + + public class E2 { + + static void bla() { + String url=URLEncoder.encode("asdf",StandardCharsets.UTF_8); + String url4=URLEncoder.encode("asdf", Charset.defaultCharset()); + } + } + """), + SCANNER(""" + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.util.Scanner; + + public class E3 { + + static void bla3(InputStream is) throws FileNotFoundException { + Scanner s=new Scanner(new File("asdf"),"UTF-8"); + Scanner s2=new Scanner(is,"UTF-8"); + Scanner s3=new Scanner("asdf"); + } + } + """, +""" +package test1; +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +public class E3 { + + static void bla3(InputStream is) throws FileNotFoundException { + Scanner s=new Scanner(new File("asdf"),StandardCharsets.UTF_8); + Scanner s2=new Scanner(is,StandardCharsets.UTF_8); + Scanner s3=new Scanner("asdf", Charset.defaultCharset()); + } +} +"""), + FORMATTER( +""" +package test1; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.util.Formatter; + +public class E4 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s=new Formatter(new File("asdf"),"UTF-8"); + } + + static void bli() throws FileNotFoundException { + try { + Formatter s=new Formatter(new File("asdf"),"UTF-8"); + } catch (FileNotFoundException | UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} +""", """ +package test1; +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.charset.StandardCharsets; +import java.util.Formatter; + +public class E4 { + + static void bla() throws FileNotFoundException { + Formatter s=new Formatter(new File("asdf"),StandardCharsets.UTF_8); + } + + static void bli() throws FileNotFoundException { + try { + Formatter s=new Formatter(new File("asdf"),StandardCharsets.UTF_8); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} +"""), + THREE(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream("")); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(Charset.defaultCharset()); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + ENCODINGASSTRINGPARAMETER( + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes("Utf-8"); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(Charset.defaultCharset()); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """); + + String given; + String expected; + + ExplicitEncodingPatterns(String given, String expected) { + this.given= given; + this.expected= expected; + } + } \ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java index 18efd338714..582ccfc87ab 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java @@ -52,779 +52,6 @@ protected void setUp() throws Exception { @RegisterExtension AbstractEclipseJava context= new EclipseJava8(); - enum ExplicitEncodingPatterns { - - CHARSET(""" - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - Charset cs1= Charset.forName("UTF-8"); - Charset cs1b= Charset.forName("Utf-8"); - Charset cs2= Charset.forName("UTF-16"); - Charset cs3= Charset.forName("UTF-16BE"); - Charset cs4= Charset.forName("UTF-16LE"); - Charset cs5= Charset.forName("ISO-8859-1"); - Charset cs6= Charset.forName("US-ASCII"); - String result= cs1.toString(); - } - } - } - """, - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - Charset cs1= Charset.forName("UTF-8"); - Charset cs1b= Charset.forName("Utf-8"); - Charset cs2= Charset.forName("UTF-16"); - Charset cs3= Charset.forName("UTF-16BE"); - Charset cs4= Charset.forName("UTF-16LE"); - Charset cs5= Charset.forName("ISO-8859-1"); - Charset cs6= Charset.forName("US-ASCII"); - String result= cs1.toString(); - } - } - } - """), - BYTEARRAYOUTSTREAM(""" - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(); - ByteArrayOutputStream ba2=new ByteArrayOutputStream(); - String result2=ba2.toString("UTF-8"); - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(); - ByteArrayOutputStream ba2=new ByteArrayOutputStream(); - String result2=ba2.toString("UTF-8"); - } - } - } - """), - FILEREADER(""" - package test1; - - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - Reader is=new FileReader(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - FILEWRITER(""" - package test1; - - import java.io.FileWriter; - import java.io.Writer; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - Writer fw=new FileWriter(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.FileWriter; - import java.io.OutputStreamWriter; - import java.io.Writer; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; - import java.io.FileOutputStream; - - public class E1 { - void method(String filename) { - try { - Writer fw=new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - INPUTSTREAMREADER( - """ - package test1; - - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$ - InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt"), Charset.defaultCharset()); //$NON-NLS-1$ - InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - OUTPUTSTREAMWRITER( - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - CHANNELSNEWREADER(""" - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.channels.ReadableByteChannel; - import java.nio.charset.StandardCharsets; - import java.nio.channels.Channels; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - ReadableByteChannel ch; - Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.channels.ReadableByteChannel; - import java.nio.charset.StandardCharsets; - import java.nio.channels.Channels; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - ReadableByteChannel ch; - Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ - } - } - } - """), - CHANNELSNEWWRITER(""" - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Writer; - import java.nio.channels.WritableByteChannel; - import java.nio.charset.StandardCharsets; - import java.nio.channels.Channels; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - WritableByteChannel ch; - Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Writer; - import java.nio.channels.WritableByteChannel; - import java.nio.charset.StandardCharsets; - import java.nio.channels.Channels; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - WritableByteChannel ch; - Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ - } - } - } - """), - PRINTWRITER(""" - package test1; - - import java.io.PrintWriter; - import java.io.Writer; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - Writer w=new PrintWriter(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.PrintWriter; - import java.io.Writer; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - try { - Writer w=new PrintWriter(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - STRINGGETBYTES(""" - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - byte[] bytes= s.getBytes(); - byte[] bytes2= s.getBytes("UTF-8"); - System.out.println(bytes.length); - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - byte[] bytes= s.getBytes(Charset.defaultCharset()); - byte[] bytes2= s.getBytes(StandardCharsets.UTF_8); - System.out.println(bytes.length); - } - } - } - """), - STRING(""" - package test1; - - import java.io.FileNotFoundException; - import java.io.UnsupportedEncodingException; - - public class E5 { - - static void bla() throws FileNotFoundException, UnsupportedEncodingException { - byte[] b= {(byte)59}; - String s1=new String(b,"UTF-8"); - String s2=new String(b,0,1,"UTF-8"); - } - } - """, - - """ - package test1; - - import java.io.FileNotFoundException; - import java.io.UnsupportedEncodingException; - - public class E5 { - - static void bla() throws FileNotFoundException, UnsupportedEncodingException { - byte[] b= {(byte)59}; - String s1=new String(b,"UTF-8"); - String s2=new String(b,0,1,"UTF-8"); - } - } - """), - PROPERTIESSTORETOXML(""" - package test1; - - import java.io.FileOutputStream; - import java.io.IOException; - import java.util.Properties; - - public class E1 { - static void blu() throws IOException { - Properties p=new Properties(); - try (FileOutputStream os = new FileOutputStream("out.xml")) { - p.storeToXML(os, null, "UTF-8"); - } - try (FileOutputStream os = new FileOutputStream("out.xml")) { - p.storeToXML(os, null); - } - } - } - """, -""" -package test1; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.Properties; - -public class E1 { - static void blu() throws IOException { - Properties p=new Properties(); - try (FileOutputStream os = new FileOutputStream("out.xml")) { - p.storeToXML(os, null, "UTF-8"); - } - try (FileOutputStream os = new FileOutputStream("out.xml")) { - p.storeToXML(os, null); - } - } -} -"""), - URLDECODER(""" - package test1; - import java.io.UnsupportedEncodingException; - import java.net.URLDecoder; - - public class E2 { - - static void bla() throws UnsupportedEncodingException { - String url=URLDecoder.decode("asdf","UTF-8"); - String url2=URLDecoder.decode("asdf"); - } - } - """, """ - package test1; - import java.io.UnsupportedEncodingException; - import java.net.URLDecoder; - - public class E2 { - - static void bla() throws UnsupportedEncodingException { - String url=URLDecoder.decode("asdf","UTF-8"); - String url2=URLDecoder.decode("asdf"); - } - } - """), - URLENCODER(""" - package test1; - import java.io.UnsupportedEncodingException; - import java.net.URLEncoder; - - public class E2 { - - static void bla() throws UnsupportedEncodingException { - String url=URLEncoder.encode("asdf","UTF-8"); - String url4=URLEncoder.encode("asdf"); - } - } - """, """ - package test1; - import java.io.UnsupportedEncodingException; - import java.net.URLEncoder; - - public class E2 { - - static void bla() throws UnsupportedEncodingException { - String url=URLEncoder.encode("asdf","UTF-8"); - String url4=URLEncoder.encode("asdf"); - } - } - """), - SCANNER(""" - package test1; - import java.io.File; - import java.io.FileNotFoundException; - import java.util.Scanner; - - public class E3 { - - static void bla() throws FileNotFoundException { - Scanner s=new Scanner(new File("asdf"),"UTF-8"); - Scanner s2=new Scanner("asdf","UTF-8"); - Scanner s3=new Scanner("asdf"); - } - } - """, """ - package test1; - import java.io.File; - import java.io.FileNotFoundException; - import java.util.Scanner; - - public class E3 { - - static void bla() throws FileNotFoundException { - Scanner s=new Scanner(new File("asdf"),"UTF-8"); - Scanner s2=new Scanner("asdf","UTF-8"); - Scanner s3=new Scanner("asdf"); - } - } - """), - FORMATTER(""" - package test1; - import java.io.File; - import java.io.FileNotFoundException; - import java.io.UnsupportedEncodingException; - import java.util.Formatter; - - public class E4 { - - static void bla() throws FileNotFoundException, UnsupportedEncodingException { - Formatter s=new Formatter(new File("asdf"),"UTF-8"); - } - } - """, -""" -package test1; -import java.io.File; -import java.io.FileNotFoundException; -import java.nio.charset.StandardCharsets; -import java.util.Formatter; - -public class E4 { - - static void bla() throws FileNotFoundException { - Formatter s=new Formatter(new File("asdf"),StandardCharsets.UTF_8); - } -} -"""), - THREE(""" - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - byte[] bytes= s.getBytes(); - System.out.println(bytes.length); - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(); - try { - InputStreamReader is=new InputStreamReader(new FileInputStream("")); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - Reader is=new FileReader(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - byte[] bytes= s.getBytes(Charset.defaultCharset()); - System.out.println(bytes.length); - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(); - try { - InputStreamReader is=new InputStreamReader(new FileInputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), - ENCODINGASSTRINGPARAMETER( - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); - byte[] bytes= s.getBytes("Utf-8"); - System.out.println(bytes.length); - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(); - try { - InputStreamReader is=new InputStreamReader(new FileInputStream(""), "UTF-8"); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - Reader is=new FileReader(filename); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """, - - """ - package test1; - - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - import java.io.FileNotFoundException; - - public class E1 { - void method(String filename) { - String s="asdf"; //$NON-NLS-1$ - //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); - byte[] bytes= s.getBytes(StandardCharsets.UTF_8); - System.out.println(bytes.length); - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - String result=ba.toString(); - try { - InputStreamReader is=new InputStreamReader(new FileInputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - try { - Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """); - - String given; - String expected; - - ExplicitEncodingPatterns(String given, String expected) { - this.given= given; - this.expected= expected; - } - } - @ParameterizedTest @EnumSource(ExplicitEncodingPatterns.class) public void testExplicitEncodingParametrized(ExplicitEncodingPatterns test) throws CoreException { diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingPatterns.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingPatterns.java new file mode 100644 index 00000000000..aa5933f75fa --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingPatterns.java @@ -0,0 +1,774 @@ +package org.eclipse.jdt.ui.tests.quickfix.Java8; + +enum ExplicitEncodingPatterns { + + CHARSET(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + Charset cs1= Charset.forName("UTF-8"); + Charset cs1b= Charset.forName("Utf-8"); + Charset cs2= Charset.forName("UTF-16"); + Charset cs3= Charset.forName("UTF-16BE"); + Charset cs4= Charset.forName("UTF-16LE"); + Charset cs5= Charset.forName("ISO-8859-1"); + Charset cs6= Charset.forName("US-ASCII"); + String result= cs1.toString(); + } + } + } + """, + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + Charset cs1= Charset.forName("UTF-8"); + Charset cs1b= Charset.forName("Utf-8"); + Charset cs2= Charset.forName("UTF-16"); + Charset cs3= Charset.forName("UTF-16BE"); + Charset cs4= Charset.forName("UTF-16LE"); + Charset cs5= Charset.forName("ISO-8859-1"); + Charset cs6= Charset.forName("US-ASCII"); + String result= cs1.toString(); + } + } + } + """), + BYTEARRAYOUTSTREAM(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString("UTF-8"); + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString("UTF-8"); + } + } + } + """), + FILEREADER(""" + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + FILEWRITER(""" + package test1; + + import java.io.FileWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer fw=new FileWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.FileWriter; + import java.io.OutputStreamWriter; + import java.io.Writer; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + import java.io.FileOutputStream; + + public class E1 { + void method(String filename) { + try { + Writer fw=new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + INPUTSTREAMREADER( + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$ + InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt"), Charset.defaultCharset()); //$NON-NLS-1$ + InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + OUTPUTSTREAMWRITER( + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + CHANNELSNEWREADER(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.channels.ReadableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ReadableByteChannel ch; + Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.channels.ReadableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ReadableByteChannel ch; + Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """), + CHANNELSNEWWRITER(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Writer; + import java.nio.channels.WritableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + WritableByteChannel ch; + Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Writer; + import java.nio.channels.WritableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + WritableByteChannel ch; + Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """), + PRINTWRITER(""" + package test1; + + import java.io.PrintWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer w=new PrintWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.PrintWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer w=new PrintWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + STRINGGETBYTES(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + byte[] bytes2= s.getBytes("UTF-8"); + System.out.println(bytes.length); + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + byte[] bytes2= s.getBytes(StandardCharsets.UTF_8); + System.out.println(bytes.length); + } + } + } + """), + STRING(""" + package test1; + + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E5 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + byte[] b= {(byte)59}; + String s1=new String(b,"UTF-8"); + String s2=new String(b,0,1,"UTF-8"); + } + } + """, + + """ + package test1; + + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E5 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + byte[] b= {(byte)59}; + String s1=new String(b,"UTF-8"); + String s2=new String(b,0,1,"UTF-8"); + } + } + """), + PROPERTIESSTORETOXML(""" + package test1; + + import java.io.FileOutputStream; + import java.io.IOException; + import java.util.Properties; + + public class E1 { + static void blu() throws IOException { + Properties p=new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, "UTF-8"); + } + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null); + } + } + } + """, +""" +package test1; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Properties; + +public class E1 { + static void blu() throws IOException { + Properties p=new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, "UTF-8"); + } + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null); + } + } +} +"""), + URLDECODER(""" + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLDecoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLDecoder.decode("asdf","UTF-8"); + String url2=URLDecoder.decode("asdf"); + } + } + """, """ + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLDecoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLDecoder.decode("asdf","UTF-8"); + String url2=URLDecoder.decode("asdf"); + } + } + """), + URLENCODER(""" + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLEncoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLEncoder.encode("asdf","UTF-8"); + String url4=URLEncoder.encode("asdf"); + } + } + """, """ + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLEncoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLEncoder.encode("asdf","UTF-8"); + String url4=URLEncoder.encode("asdf"); + } + } + """), + SCANNER(""" + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.util.Scanner; + + public class E3 { + + static void bla() throws FileNotFoundException { + Scanner s=new Scanner(new File("asdf"),"UTF-8"); + Scanner s2=new Scanner("asdf","UTF-8"); + Scanner s3=new Scanner("asdf"); + } + } + """, """ + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.util.Scanner; + + public class E3 { + + static void bla() throws FileNotFoundException { + Scanner s=new Scanner(new File("asdf"),"UTF-8"); + Scanner s2=new Scanner("asdf","UTF-8"); + Scanner s3=new Scanner("asdf"); + } + } + """), + FORMATTER(""" + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + import java.util.Formatter; + + public class E4 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s=new Formatter(new File("asdf"),"UTF-8"); + } + } + """, +""" +package test1; +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.charset.StandardCharsets; +import java.util.Formatter; + +public class E4 { + + static void bla() throws FileNotFoundException { + Formatter s=new Formatter(new File("asdf"),StandardCharsets.UTF_8); + } +} +"""), + THREE(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream("")); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + ENCODINGASSTRINGPARAMETER( + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes("Utf-8"); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """); + + String given; + String expected; + + ExplicitEncodingPatterns(String given, String expected) { + this.given= given; + this.expected= expected; + } + } \ No newline at end of file diff --git a/org.eclipse.jdt.ui/plugin.xml b/org.eclipse.jdt.ui/plugin.xml index e1c2d6e4b5a..3a695b9ff91 100644 --- a/org.eclipse.jdt.ui/plugin.xml +++ b/org.eclipse.jdt.ui/plugin.xml @@ -7116,10 +7116,15 @@ id="org.eclipse.jdt.ui.cleanup.switch" runAfter="org.eclipse.jdt.ui.cleanup.toolscleanup"> + + + runAfter="org.eclipse.jdt.ui.cleanup.explicit_encoding"> - - + runAfter="org.eclipse.jdt.ui.cleanup.ternary_operator">