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/CleanUpConstants.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/CleanUpConstants.java index 6a354bd2680..2d413344597 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/CleanUpConstants.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/CleanUpConstants.java @@ -2352,7 +2352,7 @@ public class CleanUpConstants { /** * */ - public static final String EXPLICITENCODING_CLEANUP= "org.eclipse.jdt.ui.cleanup.explicit_encoding"; //$NON-NLS-1$ + public static final String EXPLICITENCODING_CLEANUP= "cleanup.explicit_encoding"; //$NON-NLS-1$ /** * Don't change behavior - just replace or insert to make use of platform encoding visible in the code. diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UseExplicitEncodingFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UseExplicitEncodingFixCore.java index d8b83785097..626b6780ca6 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UseExplicitEncodingFixCore.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UseExplicitEncodingFixCore.java @@ -86,7 +86,7 @@ public String getPreview(boolean i, ChangeBehavior cb) { preview.append(System.lineSeparator()); } } - return preview.toString(); + return preview.toString()+System.lineSeparator(); } /** * Compute set of CompilationUnitRewriteOperation to refactor supported situations using default encoding to make use of explicit calls 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 47ddc549e65..fad98176878 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 @@ -13,23 +13,39 @@ *******************************************************************************/ package org.eclipse.jdt.internal.corext.fix.helper; +import java.util.HashMap; import java.util.List; 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; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.Name; +import org.eclipse.jdt.core.dom.QualifiedName; +import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.SingleVariableDeclaration; +import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.TryStatement; import org.eclipse.jdt.core.dom.Type; +import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jdt.core.dom.UnionType; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; import org.eclipse.jdt.core.dom.rewrite.ListRewrite; @@ -37,30 +53,43 @@ 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; /** * @param Type found in Visitor */ public abstract class AbstractExplicitEncoding { + private static final String JAVA_IO_UNSUPPORTED_ENCODING_EXCEPTION= "java.io.UnsupportedEncodingException"; //$NON-NLS-1$ + private static final String UNSUPPORTED_ENCODING_EXCEPTION= "UnsupportedEncodingException"; //$NON-NLS-1$ - static Map encodingmap = Map.of( - "UTF-8", "UTF_8", //$NON-NLS-1$ //$NON-NLS-2$ - "UTF-16", "UTF_16", //$NON-NLS-1$ //$NON-NLS-2$ - "UTF-16BE", "UTF_16BE", //$NON-NLS-1$ //$NON-NLS-2$ - "UTF-16LE", "UTF_16LE", //$NON-NLS-1$ //$NON-NLS-2$ - "ISO-8859-1", "ISO_8859_1", //$NON-NLS-1$ //$NON-NLS-2$ - "US-ASCII", "US_ASCII" //$NON-NLS-1$ //$NON-NLS-2$ - ); - static Set encodings=encodingmap.keySet(); + + static Map encodingmap= Map.of( + "UTF-8", "UTF_8", //$NON-NLS-1$ //$NON-NLS-2$ + "UTF-16", "UTF_16", //$NON-NLS-1$ //$NON-NLS-2$ + "UTF-16BE", "UTF_16BE", //$NON-NLS-1$ //$NON-NLS-2$ + "UTF-16LE", "UTF_16LE", //$NON-NLS-1$ //$NON-NLS-2$ + "ISO-8859-1", "ISO_8859_1", //$NON-NLS-1$ //$NON-NLS-2$ + "US-ASCII", "US_ASCII" //$NON-NLS-1$ //$NON-NLS-2$ + ); + + static Set encodings= encodingmap.keySet(); + static class Nodedata { public boolean replace; + public ASTNode visited; + public String encoding; + + public static Map charsetConstants= new HashMap<>(); } - protected static final String ENCODING = "encoding"; //$NON-NLS-1$ - protected static final String REPLACE = "replace"; //$NON-NLS-1$ + protected static final String ENCODING= "encoding"; //$NON-NLS-1$ + + protected static final String REPLACE= "replace"; //$NON-NLS-1$ public abstract void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb); @@ -82,61 +111,188 @@ protected static Name addImport(String typeName, final CompilationUnitRewrite cu return ast.newName(importedName); } + protected static String findVariableValue(SimpleName variable, ASTNode context) { + ASTNode current= context.getParent(); + while (current != null && !(current instanceof MethodDeclaration) && !(current instanceof TypeDeclaration)) { + current= current.getParent(); + } + + if (current instanceof MethodDeclaration) { + MethodDeclaration method= (MethodDeclaration) current; + List statements= method.getBody().statements(); + + for (Object stmt : statements) { + if (stmt instanceof VariableDeclarationStatement) { + VariableDeclarationStatement varDeclStmt= (VariableDeclarationStatement) stmt; + for (Object frag : varDeclStmt.fragments()) { + VariableDeclarationFragment fragment= (VariableDeclarationFragment) frag; + if (fragment.getName().getIdentifier().equals(variable.getIdentifier())) { + Expression initializer= fragment.getInitializer(); + if (initializer instanceof StringLiteral) { + return ((StringLiteral) initializer).getLiteralValue().toUpperCase(); + } + } + } + } + } + } else if (current instanceof TypeDeclaration) { + TypeDeclaration type= (TypeDeclaration) current; + FieldDeclaration[] fields= type.getFields(); + + for (FieldDeclaration field : fields) { + for (Object frag : field.fragments()) { + VariableDeclarationFragment fragment= (VariableDeclarationFragment) frag; + if (fragment.getName().getIdentifier().equals(variable.getIdentifier())) { + Expression initializer= fragment.getInitializer(); + if (initializer instanceof StringLiteral) { + return ((StringLiteral) initializer).getLiteralValue().toUpperCase(); + } + } + } + } + } + return null; + } + public abstract String getPreview(boolean afterRefactoring, ChangeBehavior cb); protected void removeUnsupportedEncodingException(final ASTNode visited, TextEditGroup group, ASTRewrite rewrite, ImportRewrite importRewriter) { - importRewriter.removeImport("java.io.UnsupportedEncodingException"); //$NON-NLS-1$ - - ASTNode parent = visited.getParent(); - while (parent != null && !(parent instanceof MethodDeclaration) && !(parent instanceof TryStatement)) { - parent = parent.getParent(); - } - - if (parent instanceof MethodDeclaration) { - MethodDeclaration method = (MethodDeclaration) parent; - ListRewrite throwsRewrite = rewrite.getListRewrite(method, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY); - List thrownExceptions = method.thrownExceptionTypes(); - for (Type exceptionType : thrownExceptions) { - if (exceptionType.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION)) { - throwsRewrite.remove(exceptionType, group); - } - } - } else if (parent instanceof TryStatement) { - TryStatement tryStatement = (TryStatement) parent; - - List catchClauses = tryStatement.catchClauses(); - for (CatchClause catchClause : catchClauses) { - SingleVariableDeclaration exception = catchClause.getException(); - Type exceptionType = exception.getType(); - - if (exceptionType instanceof UnionType) { - UnionType unionType = (UnionType) exceptionType; - ListRewrite unionRewrite = rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY); - - List types = unionType.types(); - types.stream() - .filter(type -> type.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION)) - .forEach(type -> unionRewrite.remove(type, group)); - - if (types.size() == 1) { - rewrite.replace(unionType, types.get(0), group); - } else if (types.isEmpty()) { - rewrite.remove(catchClause, group); - } - } else if (exceptionType.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION)) { - rewrite.remove(catchClause, group); - } - } - - if (tryStatement.catchClauses().isEmpty() && tryStatement.getFinally() == null) { - Block tryBlock = tryStatement.getBody(); - - if (tryStatement.resources().isEmpty() && tryBlock.statements().isEmpty()) { - rewrite.remove(tryStatement, group); - } else if (tryStatement.resources().isEmpty()) { - rewrite.replace(tryStatement, tryBlock, group); - } - } - } + ASTNode parent= visited.getParent(); + while (parent != null && !(parent instanceof MethodDeclaration) && !(parent instanceof TryStatement)) { + parent= parent.getParent(); + } + + if (parent instanceof MethodDeclaration) { + MethodDeclaration method= (MethodDeclaration) parent; + ListRewrite throwsRewrite= rewrite.getListRewrite(method, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY); + List thrownExceptions= method.thrownExceptionTypes(); + for (Type exceptionType : thrownExceptions) { + if (exceptionType.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION)) { + throwsRewrite.remove(exceptionType, group); + importRewriter.removeImport(JAVA_IO_UNSUPPORTED_ENCODING_EXCEPTION); + } + } + } else if (parent instanceof TryStatement) { + TryStatement tryStatement= (TryStatement) parent; + + List catchClauses= tryStatement.catchClauses(); + for (CatchClause catchClause : catchClauses) { + SingleVariableDeclaration exception= catchClause.getException(); + Type exceptionType= exception.getType(); + + if (exceptionType instanceof UnionType) { + UnionType unionType= (UnionType) exceptionType; + ListRewrite unionRewrite= rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY); + + List types= unionType.types(); + types.stream() + .filter(type -> type.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION)) + .forEach(type -> unionRewrite.remove(type, group)); + + if (types.size() == 1) { + rewrite.replace(unionType, types.get(0), group); + } else if (types.isEmpty()) { + rewrite.remove(catchClause, group); + } + } else if (exceptionType.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION)) { + rewrite.remove(catchClause, group); + importRewriter.removeImport(JAVA_IO_UNSUPPORTED_ENCODING_EXCEPTION); + } + } + + if (tryStatement.catchClauses().isEmpty() && tryStatement.getFinally() == null) { + Block tryBlock= tryStatement.getBody(); + + if (tryStatement.resources().isEmpty() && tryBlock.statements().isEmpty()) { + rewrite.remove(tryStatement, group); + } else if (tryStatement.resources().isEmpty()) { + rewrite.replace(tryStatement, tryBlock, group); + } + } + } + } + + protected void removeNLSComment(CompilationUnitRewrite cuRewrite, ASTNode node, TextEditGroup group) { + 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/ByteArrayOutputStreamExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ByteArrayOutputStreamExplicitEncoding.java index 16787a70a44..281f5e2df48 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ByteArrayOutputStreamExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ByteArrayOutputStreamExplicitEncoding.java @@ -42,27 +42,34 @@ /** * Change from * - *
ByteArrayOutputStream ba=new ByteArrayOutputStream();
- *      String result=ba.toString();
- *  
+ *
+ * ByteArrayOutputStream ba= new ByteArrayOutputStream();
  *
- * 
ByteArrayOutputStream ba=new ByteArrayOutputStream();
- *      try {
- *         String result=ba.toString(Charset.defaultCharset().displayName());
- *      } catch (UnsupportedEncodingException e1) {
- *         e1.printStackTrace();
- *      }
+ * String result= ba.toString(); + *
* - * since Java 10 - *
ByteArrayOutputStream ba=new ByteArrayOutputStream();
- *         String result=ba.toString(Charset.defaultCharset());
- *      
+ *
+ * ByteArrayOutputStream ba= new ByteArrayOutputStream();
+ * try {
+ * 	String result= ba.toString(Charset.defaultCharset().displayName());
+ * } catch (UnsupportedEncodingException e1) {
+ * 	e1.printStackTrace();
+ * }
+ * 
+ * + * since Java 10 + * + *
+ * ByteArrayOutputStream ba= new ByteArrayOutputStream();
+ *
+ * String result= ba.toString(Charset.defaultCharset());
+ * 
* */ public class ByteArrayOutputStreamExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { /** * For Java 9 and older just do nothing @@ -70,7 +77,8 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation return; } ReferenceHolder holder= new ReferenceHolder<>(); - HelperVisitor.callMethodInvocationVisitor(ByteArrayOutputStream.class, METHOD_TOSTRING, compilationUnit, holder, nodesprocessed, (visited, aholder) -> processFoundNode(fixcore, operations, cb, visited, aholder)); + HelperVisitor.callMethodInvocationVisitor(ByteArrayOutputStream.class, METHOD_TOSTRING, compilationUnit, holder, nodesprocessed, + (visited, aholder) -> processFoundNode(fixcore, operations, cb, visited, aholder)); } private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set operations, @@ -78,27 +86,27 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set< ReferenceHolder holder) { List arguments= visited.arguments(); if (ASTNodes.usesGivenSignature(visited, ByteArrayOutputStream.class.getCanonicalName(), METHOD_TOSTRING, String.class.getCanonicalName())) { - if(!(arguments.get(0) instanceof StringLiteral)) { + if (!(arguments.get(0) instanceof StringLiteral)) { return false; } StringLiteral argstring3= (StringLiteral) arguments.get(0); if (!encodings.contains(argstring3.getLiteralValue())) { return false; } - Nodedata nd=new Nodedata(); - nd.encoding=encodingmap.get(argstring3.getLiteralValue()); - nd.replace=true; - nd.visited=argstring3; - holder.put(visited,nd); + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(argstring3.getLiteralValue()); + nd.replace= true; + nd.visited= argstring3; + holder.put(visited, nd); operations.add(fixcore.rewrite(visited, cb, holder)); return false; } if (ASTNodes.usesGivenSignature(visited, ByteArrayOutputStream.class.getCanonicalName(), METHOD_TOSTRING)) { - Nodedata nd2=new Nodedata(); - nd2.encoding=null; - nd2.replace=false; - nd2.visited=visited; - holder.put(visited,nd2); + Nodedata nd2= new Nodedata(); + nd2.encoding= null; + nd2.replace= false; + nd2.visited= visited; + holder.put(visited, nd2); operations.add(fixcore.rewrite(visited, cb, holder)); return false; } @@ -106,19 +114,19 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set< } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visited, final 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); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding, Nodedata.charsetConstants); /** - * Add Charset.defaultCharset().displayName() as second (last) parameter of "toString()" call - * Add Charset.defaultCharset() as second (last) parameter + * Add Charset.defaultCharset().displayName() as second (last) parameter of "toString()" + * call Add Charset.defaultCharset() as second (last) parameter */ ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); @@ -127,35 +135,35 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - String insert=""; //$NON-NLS-1$ - switch(cb) { - case KEEP_BEHAVIOR: - insert="Charset.defaultCharset().displayName()"; //$NON-NLS-1$ - break; - case ENFORCE_UTF8_AGGREGATE: - // insert="charset_constant"; //$NON-NLS-1$ - //$FALL-THROUGH$ - case ENFORCE_UTF8: - insert="StandardCharsets.UTF_8.displayName()"; //$NON-NLS-1$ - break; + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + String insert= ""; //$NON-NLS-1$ + switch (cb) { + case KEEP_BEHAVIOR: + insert= "Charset.defaultCharset().displayName()"; //$NON-NLS-1$ + break; + case ENFORCE_UTF8_AGGREGATE: + // insert="charset_constant"; //$NON-NLS-1$ + //$FALL-THROUGH$ + case ENFORCE_UTF8: + insert= "StandardCharsets.UTF_8.displayName()"; //$NON-NLS-1$ + break; } - if(afterRefactoring) { + if (afterRefactoring) { return "ByteArrayOutputStream ba=new ByteArrayOutputStream();\n" //$NON-NLS-1$ + "try {\n" //$NON-NLS-1$ - + " String result=ba.toString("+insert+");\n" //$NON-NLS-1$ //$NON-NLS-2$ + + " String result=ba.toString(" + insert + ");\n" //$NON-NLS-1$ //$NON-NLS-2$ + "} catch (UnsupportedEncodingException e1) {\n" //$NON-NLS-1$ + " e1.printStackTrace();\n" //$NON-NLS-1$ + "}\n"; //$NON-NLS-1$ } return """ - ByteArrayOutputStream ba=new ByteArrayOutputStream(); - try { - String result=ba.toString(); - } catch (UnsupportedEncodingException e1) { - e1.printStackTrace(); - } - """; //$NON-NLS-1$ + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + try { + String result=ba.toString(); + } catch (UnsupportedEncodingException e1) { + e1.printStackTrace(); + } + """; //$NON-NLS-1$ } @Override diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChangeBehavior.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChangeBehavior.java index d54b66d314d..5f265e241ec 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChangeBehavior.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChangeBehavior.java @@ -18,11 +18,17 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.Map; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.FieldAccess; +import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.Modifier; +import org.eclipse.jdt.core.dom.QualifiedName; +import org.eclipse.jdt.core.dom.TypeDeclaration; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory; @@ -31,7 +37,7 @@ public enum ChangeBehavior { KEEP_BEHAVIOR() { @Override - protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset) { + protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset, Map charsetConstants) { Expression callToCharsetDefaultCharset= null; if (charset != null) { @@ -46,50 +52,112 @@ protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrit @Override protected String computeCharsetforPreview() { - String insert= ""; //$NON-NLS-1$ - insert= "Charset.defaultCharset()"; //$NON-NLS-1$ + String insert= "Charset.defaultCharset()"; //$NON-NLS-1$ return insert; } }, ENFORCE_UTF8() { @Override - protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset) { - Expression callToCharsetDefaultCharset= null; - // needs Java 1.7 - callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast, charset); + protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset, Map charsetConstants) { + String charset2= charset == null ? "UTF_8" : charset; //$NON-NLS-1$ + Expression callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast, charset2); return callToCharsetDefaultCharset; } @Override protected String computeCharsetforPreview() { - String insert= ""; //$NON-NLS-1$ - insert= "StandardCharsets.UTF_8"; //$NON-NLS-1$ + String insert= "StandardCharsets.UTF_8"; //$NON-NLS-1$ return insert; } }, ENFORCE_UTF8_AGGREGATE() { @Override - protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset) { - Expression callToCharsetDefaultCharset= null; - /** - * @TODO not implemented - */ - return callToCharsetDefaultCharset; + protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset2, Map charsetConstants) { + String charset= charset2 == null ? "UTF_8" : charset2; //$NON-NLS-1$ + // Generate a valid Java identifier for the charset name (e.g., UTF_8) + String fieldName = charset.toUpperCase().replace('-', '_'); + + // Check if this charset constant is already stored in the map + if (charsetConstants.containsKey(fieldName)) { + return charsetConstants.get(fieldName); + } + + // Add import for StandardCharsets + ImportRewrite importRewrite = cuRewrite.getImportRewrite(); + importRewrite.addImport(StandardCharsets.class.getCanonicalName()); + importRewrite.addImport(Charset.class.getCanonicalName()); + + // Check if the static field already exists in the class + TypeDeclaration enclosingType = (TypeDeclaration) cuRewrite.getRoot().types().get(0); + FieldDeclaration existingField = findStaticCharsetField(enclosingType, fieldName); + + QualifiedName fieldReference; + if (existingField == null) { + // Create a new static field if it doesn't exist + VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment(); + fragment.setName(ast.newSimpleName(fieldName)); + fragment.setInitializer(createCharsetAccessExpression(ast, charset)); + + FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(fragment); + fieldDeclaration.setType(ast.newSimpleType(ast.newName("Charset"))); //$NON-NLS-1$ + fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD)); + fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD)); + fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD)); + + // Add the new field to the class + cuRewrite.getASTRewrite().getListRewrite(enclosingType, TypeDeclaration.BODY_DECLARATIONS_PROPERTY) + .insertFirst(fieldDeclaration, null); + + // Create a QualifiedName to refer to this new field + fieldReference = ast.newQualifiedName( + ast.newSimpleName(enclosingType.getName().getIdentifier()), + ast.newSimpleName(fragment.getName().getIdentifier()) + ); + } else { + // If the field already exists, find its reference name + VariableDeclarationFragment fragment = (VariableDeclarationFragment) existingField.fragments().get(0); + fieldReference = ast.newQualifiedName( + ast.newSimpleName(enclosingType.getName().getIdentifier()), + fragment.getName() + ); + } + + // Cache the field reference in the map and return it + charsetConstants.put(fieldName, fieldReference); + return fieldReference; } @Override protected String computeCharsetforPreview() { - String insert= ""; //$NON-NLS-1$ - // insert="charset_constant"; //$NON-NLS-1$ - return insert; + return "CharsetConstant"; //$NON-NLS-1$ } }; - - abstract protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset); + abstract protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset, Map charsetConstants); abstract protected String computeCharsetforPreview(); + protected FieldDeclaration findStaticCharsetField(TypeDeclaration type, String fieldName) { + for (FieldDeclaration field : type.getFields()) { + for (Object fragment : field.fragments()) { + if (fragment instanceof VariableDeclarationFragment) { + VariableDeclarationFragment varFrag = (VariableDeclarationFragment) fragment; + if (varFrag.getName().getIdentifier().equals(fieldName)) { + return field; + } + } + } + } + return null; + } + + protected Expression createCharsetAccessExpression(AST ast, String charset) { + FieldAccess fieldAccess = ast.newFieldAccess(); + fieldAccess.setExpression(ast.newName(StandardCharsets.class.getSimpleName())); + fieldAccess.setName(ast.newSimpleName(charset)); + return fieldAccess; + } + /** * Create access to StandardCharsets.UTF_8, needs Java 1.7 or newer * @@ -145,8 +213,8 @@ protected static MethodInvocation addCharsetComputation(final CompilationUnitRew * @param charset Charset as String * @return MethodInvocation that returns String */ - protected MethodInvocation addCharsetStringComputation(final CompilationUnitRewrite cuRewrite, AST ast, ChangeBehavior cb, String charset) { - Expression callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, charset); + protected MethodInvocation addCharsetStringComputation(final CompilationUnitRewrite cuRewrite, AST ast, ChangeBehavior cb, String charset, Map charsetConstants) { + Expression callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, charset, charsetConstants); /** * Add second call to Charset.defaultCharset().displayName() */ 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 ddc39cd960f..f86c0aadc20 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 @@ -26,6 +26,7 @@ import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; @@ -38,20 +39,27 @@ import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; + /** * Java 10 * * Change * - * Find: Reader r=Channels.newReader(ch,"UTF-8") + * Find: Reader r=Channels.newReader(ch,"UTF-8") + * + * Rewrite: Reader r=Channels.newReader(ch,StandardCharsets.UTF_8) + * + * Find: Reader r5 = Channels.newReader(ch, "ISO-8859-1", 0, 1024) + * + * Rewrite: Reader r5 = Channels.newReader(ch, StandardCharsets.ISO_8859_1, 0, 1024) + * * - * Rewrite: Reader r=Channels.newReader(ch,StandardCharsets.UTF_8) * */ public class ChannelsNewReaderExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { /** * For Java 9 and older just do nothing @@ -59,43 +67,54 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation return; } ReferenceHolder datah= new ReferenceHolder<>(); - HelperVisitor.callMethodInvocationVisitor(Channels.class, METHOD_NEW_READER, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); + HelperVisitor.callMethodInvocationVisitor(Channels.class, METHOD_NEW_READER, compilationUnit, datah, nodesprocessed, + (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set operations, ChangeBehavior cb, MethodInvocation visited, ReferenceHolder holder) { List arguments= visited.arguments(); - if (ASTNodes.usesGivenSignature(visited, Channels.class.getCanonicalName(), METHOD_NEW_READER, ReadableByteChannel.class.getCanonicalName(),String.class.getCanonicalName())) { - StringLiteral argstring3= (StringLiteral) arguments.get(1); - if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + if (ASTNodes.usesGivenSignature(visited, Channels.class.getCanonicalName(), METHOD_NEW_READER, + ReadableByteChannel.class.getCanonicalName(), String.class.getCanonicalName())) { + + ASTNode encodingArg= arguments.get(1); + + String encodingValue= null; + if (encodingArg instanceof StringLiteral) { + encodingValue= ((StringLiteral) encodingArg).getLiteralValue().toUpperCase(); + } else if (encodingArg instanceof SimpleName) { + encodingValue= findVariableValue((SimpleName) encodingArg, visited); + } + + if (encodingValue != null && encodings.contains(encodingValue)) { + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(encodingValue); + nd.replace= true; + nd.visited= encodingArg; + holder.put(visited, nd); + operations.add(fixcore.rewrite(visited, cb, holder)); return false; } - Nodedata nd=new Nodedata(); - nd.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase()); - nd.replace=true; - nd.visited=argstring3; - holder.put(visited,nd); - operations.add(fixcore.rewrite(visited, cb, holder)); - return false; } return false; } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visited, final 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); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding,Nodedata.charsetConstants); /** * Add Charset.defaultCharset() as second (last) parameter */ ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); + removeNLSComment(cuRewrite, visited, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); } @@ -103,8 +122,8 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { return "Reader r=Channels.newReader(ch,StandardCharsets.UTF_8);\n"; //$NON-NLS-1$ } return "Reader r=Channels.newReader(ch,\"UTF-8\");\n"; //$NON-NLS-1$ diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChannelsNewWriterExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChannelsNewWriterExplicitEncoding.java index 975b52943f2..c513a67204e 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChannelsNewWriterExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ChannelsNewWriterExplicitEncoding.java @@ -26,6 +26,7 @@ import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; @@ -38,10 +39,11 @@ import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; + /** * Java 10 * - * Find: Channels.newWriter(ch,"UTF-8") + * Find: Channels.newWriter(ch,"UTF-8") * * Rewrite: Channels.newWriter(ch,StandardCharsets.UTF_8) * @@ -49,7 +51,7 @@ public class ChannelsNewWriterExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { /** * For Java 9 and older just do nothing @@ -57,42 +59,51 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation return; } ReferenceHolder datah= new ReferenceHolder<>(); - HelperVisitor.callMethodInvocationVisitor(Channels.class, METHOD_NEW_WRITER, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); + HelperVisitor.callMethodInvocationVisitor(Channels.class, METHOD_NEW_WRITER, compilationUnit, datah, nodesprocessed, + (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set operations, ChangeBehavior cb, MethodInvocation visited, ReferenceHolder holder) { List arguments= visited.arguments(); - if (ASTNodes.usesGivenSignature(visited, Channels.class.getCanonicalName(), METHOD_NEW_WRITER, WritableByteChannel.class.getCanonicalName(),String.class.getCanonicalName())) { - StringLiteral argstring3= (StringLiteral) arguments.get(1); - if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + if (ASTNodes.usesGivenSignature(visited, Channels.class.getCanonicalName(), METHOD_NEW_WRITER, WritableByteChannel.class.getCanonicalName(), String.class.getCanonicalName())) { + + ASTNode encodingArg= arguments.get(1); + + String encodingValue= null; + if (encodingArg instanceof StringLiteral) { + encodingValue= ((StringLiteral) encodingArg).getLiteralValue(); + } else if (encodingArg instanceof SimpleName) { + encodingValue= findVariableValue((SimpleName) encodingArg, visited); + } + + if (encodingValue != null && encodings.contains(encodingValue.toUpperCase())) { + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(encodingValue.toUpperCase()); + nd.replace= true; + nd.visited= encodingArg; + holder.put(visited, nd); + operations.add(fixcore.rewrite(visited, cb, holder)); return false; } - Nodedata nd=new Nodedata(); - nd.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase()); - nd.replace=true; - nd.visited=argstring3; - holder.put(visited,nd); - operations.add(fixcore.rewrite(visited, cb, holder)); - return false; } return false; } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visited, final 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); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding,Nodedata.charsetConstants); /** * Add Charset.defaultCharset() as second (last) parameter */ ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); @@ -101,8 +112,8 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { return "Writer w=Channels.newWriter(ch, StandardCharsets.UTF_8);\n"; //$NON-NLS-1$ } return "Writer w=Channels.newWriter(ch, \"UTF-8\");\n"; //$NON-NLS-1$ diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/CharsetForNameExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/CharsetForNameExplicitEncoding.java index 8d2d00e97c3..965755310d7 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/CharsetForNameExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/CharsetForNameExplicitEncoding.java @@ -25,6 +25,7 @@ import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; @@ -35,21 +36,22 @@ import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; + /** * Java 18 * - * Find: Charset.forName("UTF-8") + * Find: Charset.forName("UTF-8") * * Rewrite: StandardCharsets.UTF_8 * - * Find: Charset.forName("UTF-16") + * Find: Charset.forName("UTF-16") * * Rewrite: StandardCharsets.UTF_16 */ public class CharsetForNameExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { if (!JavaModelUtil.is18OrHigher(compilationUnit.getJavaElement().getJavaProject())) { /** * For Java 17 and older just do nothing @@ -57,7 +59,8 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation return; } ReferenceHolder datah= new ReferenceHolder<>(); - HelperVisitor.callMethodInvocationVisitor(Charset.class, METHOD_FOR_NAME, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); + HelperVisitor.callMethodInvocationVisitor(Charset.class, METHOD_FOR_NAME, compilationUnit, datah, nodesprocessed, + (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, @@ -67,27 +70,40 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, if (!ASTNodes.usesGivenSignature(visited, Charset.class.getCanonicalName(), METHOD_FOR_NAME, String.class.getCanonicalName())) { return true; } - StringLiteral argstring3= (StringLiteral) arguments.get(0); - if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + ASTNode encodingArg= arguments.get(0); + + String encodingValue= null; + if (encodingArg instanceof StringLiteral) { + encodingValue= ((StringLiteral) encodingArg).getLiteralValue().toUpperCase(); + } else if (encodingArg instanceof SimpleName) { + encodingValue= findVariableValue((SimpleName) encodingArg, visited); + } + + if (encodingValue != null && encodings.contains(encodingValue)) { + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(encodingValue); + nd.replace= true; + nd.visited= encodingArg; + holder.put(visited, nd); + operations.add(fixcore.rewrite(visited, cb, holder)); return false; } - holder.put(visited,encodingmap.get(argstring3.getLiteralValue().toUpperCase())); - operations.add(fixcore.rewrite(visited, cb, holder)); return false; } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, + TextEditGroup group, ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, (String) data.get(visited)); + Nodedata nodedata= (Nodedata) data.get(visited); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding,Nodedata.charsetConstants); ASTNodes.replaceButKeepComment(rewrite, visited, callToCharsetDefaultCharset, group); } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { return "Charset s=StandardCharsets.UTF_8;\n"; //$NON-NLS-1$ } return "Charset s=Charset.forName(\"UTF-8\");\n"; //$NON-NLS-1$ diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FileReaderExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FileReaderExplicitEncoding.java index 716f77f371a..60c4eee720a 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FileReaderExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FileReaderExplicitEncoding.java @@ -34,12 +34,14 @@ import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; + /** * Change * - * Find: Reader is=new FileReader("file.txt") + * Find: Reader is=new FileReader("file.txt") * - * Rewrite: Reader is=new InputStreamReader(new FileInputStream("file.txt"),Charset.defaultCharset()); + * Rewrite: Reader is=new InputStreamReader(new + * FileInputStream("file.txt"),Charset.defaultCharset()); * * Charset.defaultCharset() is available since Java 1.5 * @@ -47,7 +49,7 @@ public class FileReaderExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callClassInstanceCreationVisitor(FileReader.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } @@ -57,31 +59,31 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, ClassInstanceCreation visited, ReferenceHolder holder) { List arguments= visited.arguments(); switch (arguments.size()) { - case 1: - break; - case 2: - if(!(arguments.get(1) instanceof StringLiteral)) { - return false; - } - StringLiteral argstring3= (StringLiteral) arguments.get(1); - if (!encodings.contains(argstring3.getLiteralValue())) { + case 1: + break; + case 2: + if (!(arguments.get(1) instanceof StringLiteral)) { + return false; + } + StringLiteral argstring3= (StringLiteral) arguments.get(1); + if (!encodings.contains(argstring3.getLiteralValue())) { + return false; + } + holder.put(argstring3, encodingmap.get(argstring3.getLiteralValue())); + break; + default: return false; - } - holder.put(argstring3,encodingmap.get(argstring3.getLiteralValue())); - break; - default: - return false; } operations.add(fixcore.rewrite(visited, cb, holder)); return false; } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, + TextEditGroup group, ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, (String) data.get(visited)); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, (String) data.get(visited),Nodedata.charsetConstants); /** * new FileInputStream() */ @@ -100,9 +102,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { - return "Reader r=new InputStreamReader(new FileInputStream(inputfile),"+cb.computeCharsetforPreview()+");\n"; //$NON-NLS-1$ //$NON-NLS-2$ + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { + return "Reader r=new InputStreamReader(new FileInputStream(inputfile)," + cb.computeCharsetforPreview() + ");\n"; //$NON-NLS-1$ //$NON-NLS-2$ } return "Reader r=new FileReader(inputfile);\n"; //$NON-NLS-1$ } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FileWriterExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FileWriterExplicitEncoding.java index d6ea6e87557..a51ee99bf11 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FileWriterExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FileWriterExplicitEncoding.java @@ -32,11 +32,12 @@ import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; + /** * Change * - * Writer fw=new FileWriter("file.txt") - * Writer fw=new OutputStreamWriter(new FileOutputStream("file.txt"),defaultCharset); + * Writer fw=new FileWriter("file.txt") Writer fw=new OutputStreamWriter(new + * FileOutputStream("file.txt"),defaultCharset); * * Charset.defaultCharset() is available since Java 1.5 * @@ -44,7 +45,7 @@ public class FileWriterExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callClassInstanceCreationVisitor(FileWriter.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } @@ -57,11 +58,11 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set< } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, + TextEditGroup group, ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, (String) data.get(visited)); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, (String) data.get(visited),Nodedata.charsetConstants); /** * new FileInputStream() */ @@ -80,9 +81,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { - return "Writer w=new OutputStreamWriter(new FileOutputStream(outputfile),"+cb.computeCharsetforPreview()+");\n"; //$NON-NLS-1$ //$NON-NLS-2$ + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { + return "Writer w=new OutputStreamWriter(new FileOutputStream(outputfile)," + cb.computeCharsetforPreview() + ");\n"; //$NON-NLS-1$ //$NON-NLS-2$ } return "Writer w=new FileWriter(outputfile);\n"; //$NON-NLS-1$ } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FormatterExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FormatterExplicitEncoding.java index 6f7323890dd..bb386ea49f0 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FormatterExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/FormatterExplicitEncoding.java @@ -33,43 +33,43 @@ import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; + /** * - * Find: new java.util.Formatter(new File(), String cs) - * throws UnsupportedEncodingException + * Find: new java.util.Formatter(new File(), String cs) throws UnsupportedEncodingException * - * Rewrite: new java.util.Formatter(new File(), Charset cs) + * Rewrite: new java.util.Formatter(new File(), Charset cs) * - * Find: new java.util.Formatter(new File(), String cs,new java.util.Locale()) + * Find: new java.util.Formatter(new File(), String cs,new java.util.Locale()) * - * Rewrite: new java.util.Formatter(new File(), Charset cs,new java.util.Locale()) + * Rewrite: new java.util.Formatter(new File(), Charset cs,new java.util.Locale()) * - * Find: new java.util.Formatter(new java.io.OutputStream(), String cs) + * Find: new java.util.Formatter(new java.io.OutputStream(), String cs) * - * Rewrite: new java.util.Formatter(new java.io.OutputStream(), Charset cs) + * Rewrite: new java.util.Formatter(new java.io.OutputStream(), Charset cs) * - * Find: new java.util.Formatter(new java.io.OutputStream(), String cs,new java.util.Locale()) + * Find: new java.util.Formatter(new java.io.OutputStream(), String cs,new java.util.Locale()) * - * Rewrite: new java.util.Formatter(new java.io.OutputStream(), Charset cs,new java.util.Locale()) + * Rewrite: new java.util.Formatter(new java.io.OutputStream(), Charset cs,new java.util.Locale()) * - * Find: new java.util.Formatter(new String(), String cs) + * Find: new java.util.Formatter(new String(), String cs) * - * Rewrite: new java.util.Formatter(new String(), Charset cs) + * Rewrite: new java.util.Formatter(new String(), Charset cs) * - * Find: new java.util.Formatter(new String(), String cs,new java.util.Locale()) + * Find: new java.util.Formatter(new String(), String cs,new java.util.Locale()) * - * Rewrite: new java.util.Formatter(new String(), Charset cs,new java.util.Locale()) + * Rewrite: new java.util.Formatter(new String(), Charset cs,new java.util.Locale()) * - * Find: new java.util.Formatter(new File()) + * Find: new java.util.Formatter(new File()) * - * Rewrite: new java.util.Formatter(new File(), Charset.defaultCharset()) - * depends on https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/System.html#file.encoding + * Rewrite: new java.util.Formatter(new File(), Charset.defaultCharset()) depends on + * https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/System.html#file.encoding * */ public class FormatterExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callClassInstanceCreationVisitor(Formatter.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } @@ -79,29 +79,29 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, ChangeBehavior cb, ClassInstanceCreation visited, ReferenceHolder holder) { - List arguments = visited.arguments(); - Nodedata nd = new Nodedata(); + List arguments= visited.arguments(); + Nodedata nd= new Nodedata(); switch (arguments.size()) { case 2: case 3: if (arguments.get(1) instanceof StringLiteral) { - StringLiteral argString = (StringLiteral) arguments.get(1); - String encodingKey = argString.getLiteralValue().toUpperCase(); + StringLiteral argString= (StringLiteral) arguments.get(1); + String encodingKey= argString.getLiteralValue().toUpperCase(); if (encodings.contains(encodingKey)) { - nd.encoding = encodingmap.get(encodingKey); - nd.replace = true; - nd.visited = argString; + nd.encoding= encodingmap.get(encodingKey); + nd.replace= true; + nd.visited= argString; holder.put(visited, nd); operations.add(fixcore.rewrite(visited, cb, holder)); } } break; case 1: - nd.encoding = null; - nd.replace = false; - nd.visited = visited; + nd.encoding= null; + nd.replace= false; + nd.visited= visited; holder.put(visited, nd); operations.add(fixcore.rewrite(visited, cb, holder)); break; @@ -112,18 +112,18 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation visited, final 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); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + 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); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); @@ -132,9 +132,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { - return "Formatter r=new java.util.Formatter(out, "+cb.computeCharsetforPreview()+");\n"; //$NON-NLS-1$ //$NON-NLS-2$ + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { + return "Formatter r=new java.util.Formatter(out, " + cb.computeCharsetforPreview() + ");\n"; //$NON-NLS-1$ //$NON-NLS-2$ } return "Formatter r=new java.util.Formatter(out);\n"; //$NON-NLS-1$ } 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 333b906b671..e409f8612e9 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,7 +19,6 @@ import org.eclipse.text.edits.TextEditGroup; -import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ClassInstanceCreation; import org.eclipse.jdt.core.dom.CompilationUnit; @@ -32,11 +31,12 @@ import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; + /** * - * Find: InputStreamReader(InputStream in, String cs) + * Find: InputStreamReader(InputStream in, String cs) * - * Rewrite: InputStreamReader(InputStream in, Charset cs) is available since Java 1.4 + * Rewrite: InputStreamReader(InputStream in, Charset cs) is available since Java 1.4 * * Charset.defaultCharset() is available since Java 1.5 * @@ -44,9 +44,10 @@ public class InputStreamReaderExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { ReferenceHolder datah= new ReferenceHolder<>(); - HelperVisitor.callClassInstanceCreationVisitor(InputStreamReader.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); + HelperVisitor.callClassInstanceCreationVisitor(InputStreamReader.class, compilationUnit, datah, nodesprocessed, + (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, @@ -54,58 +55,60 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, ClassInstanceCreation visited, ReferenceHolder holder) { List arguments= visited.arguments(); switch (arguments.size()) { - case 2: - if(!(arguments.get(1) instanceof StringLiteral)) { - return false; - } - StringLiteral argstring3= (StringLiteral) arguments.get(1); - if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { - return false; - } - Nodedata nd=new Nodedata(); - nd.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase()); - nd.replace=true; - nd.visited=argstring3; - holder.put(visited,nd); - operations.add(fixcore.rewrite(visited, cb, holder)); - break; - case 1: - Nodedata nd2=new Nodedata(); - nd2.encoding=null; - nd2.replace=false; - nd2.visited=visited; - holder.put(visited,nd2); - operations.add(fixcore.rewrite(visited, cb, holder)); - break; - default: - break; + case 2: + if (!(arguments.get(1) instanceof StringLiteral)) { + return false; + } + StringLiteral argstring3= (StringLiteral) arguments.get(1); + if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + return false; + } + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(argstring3.getLiteralValue().toUpperCase()); + nd.replace= true; + nd.visited= argstring3; + holder.put(visited, nd); + operations.add(fixcore.rewrite(visited, cb, holder)); + break; + case 1: + Nodedata nd2= new Nodedata(); + nd2.encoding= null; + nd2.replace= false; + nd2.visited= visited; + holder.put(visited, nd2); + operations.add(fixcore.rewrite(visited, cb, holder)); + break; + default: + break; } return false; } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { - ASTRewrite rewrite= cuRewrite.getASTRewrite(); - AST ast= cuRewrite.getRoot().getAST(); - Nodedata nodedata= (Nodedata) data.get(visited); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); - /** - * Add Charset.defaultCharset() as second (last) parameter - */ - ListRewrite listRewrite= rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY); - if(nodedata.replace) { + public void rewrite(UseExplicitEncodingFixCore upp, ClassInstanceCreation visited, CompilationUnitRewrite cuRewrite, + TextEditGroup group, ChangeBehavior cb, ReferenceHolder data) { + ASTRewrite rewrite = cuRewrite.getASTRewrite(); + Nodedata nodedata = (Nodedata) data.get(visited); + + 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); + removeNLSComment(cuRewrite, visited, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); } + + removeUnsupportedEncodingException(visited, group, rewrite, cuRewrite.getImportRewrite()); } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { - return "Reader r=new InputStreamReader(in, "+cb.computeCharsetforPreview()+");\nInputStreamReader is=new InputStreamReader(new FileInputStream(\"\"), \"UTF-8\");\n"; //$NON-NLS-1$ //$NON-NLS-2$ + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { + return "Reader r=new InputStreamReader(in, " + cb.computeCharsetforPreview() + ");\nInputStreamReader is=new InputStreamReader(new FileInputStream(\"\"), \"UTF-8\");\n"; //$NON-NLS-1$ //$NON-NLS-2$ } return "Reader r=new InputStreamReader(in);\nInputStreamReader is=new InputStreamReader(new FileInputStream(\"\"), StandardCharsets.UTF_8);\n"; //$NON-NLS-1$ } 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 2b9aea86d2c..125664b7fe9 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 @@ -25,6 +25,7 @@ import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.StringLiteral; 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; @@ -36,19 +37,19 @@ /** * Change * - * Find: OutputStreamWriter os=new OutputStreamWriter(InputStream in, String cs) + * Find: OutputStreamWriter os=new OutputStreamWriter(InputStream in, String cs) * - * Rewrite: OutputStreamWriter os=new OutputStreamWriter(InputStream in, CharSet cs) + * Rewrite: OutputStreamWriter os=new OutputStreamWriter(InputStream in, CharSet cs) * * - * Find: OutputStreamWriter os=new OutputStreamWriter(InputStream in) + * Find: OutputStreamWriter os=new OutputStreamWriter(InputStream in) * - * Rewrite: OutputStreamWriter os=new OutputStreamWriter(InputStream in, Charset.defaultCharset()) + * Rewrite: OutputStreamWriter os=new OutputStreamWriter(InputStream in, Charset.defaultCharset()) * * - * Find: OutputStreamWriter os=new OutputStreamWriter(InputStream in) + * Find: OutputStreamWriter os=new OutputStreamWriter(InputStream in) * - * Rewrite: OutputStreamWriter os=new OutputStreamWriter(InputStream in, StandardCharsets.UTF_8) + * Rewrite: OutputStreamWriter os=new OutputStreamWriter(InputStream in, StandardCharsets.UTF_8) * * Charset.defaultCharset() is available since Java 1.5 * @@ -56,9 +57,10 @@ public class OutputStreamWriterExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { ReferenceHolder datah= new ReferenceHolder<>(); - HelperVisitor.callClassInstanceCreationVisitor(OutputStreamWriter.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); + HelperVisitor.callClassInstanceCreationVisitor(OutputStreamWriter.class, compilationUnit, datah, nodesprocessed, + (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, @@ -66,58 +68,62 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, ClassInstanceCreation visited, ReferenceHolder holder) { List arguments= visited.arguments(); switch (arguments.size()) { - case 2: - if(!(arguments.get(1) instanceof StringLiteral)) { - return false; - } - StringLiteral argstring3= (StringLiteral) arguments.get(1); - if (!encodings.contains(argstring3.getLiteralValue())) { - return false; - } - Nodedata nd=new Nodedata(); - nd.encoding=encodingmap.get(argstring3.getLiteralValue()); - nd.replace=true; - nd.visited=argstring3; - holder.put(visited,nd); - operations.add(fixcore.rewrite(visited, cb, holder)); - break; - case 1: - Nodedata nd2=new Nodedata(); - nd2.encoding=null; - nd2.replace=false; - nd2.visited=visited; - holder.put(visited,nd2); - operations.add(fixcore.rewrite(visited, cb, holder)); - break; - default: - break; + case 2: + if (!(arguments.get(1) instanceof StringLiteral)) { + return false; + } + StringLiteral argstring3= (StringLiteral) arguments.get(1); + if (!encodings.contains(argstring3.getLiteralValue())) { + return false; + } + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(argstring3.getLiteralValue()); + nd.replace= true; + nd.visited= argstring3; + holder.put(visited, nd); + operations.add(fixcore.rewrite(visited, cb, holder)); + break; + case 1: + Nodedata nd2= new Nodedata(); + nd2.encoding= null; + nd2.replace= false; + nd2.visited= visited; + holder.put(visited, nd2); + operations.add(fixcore.rewrite(visited, cb, holder)); + break; + default: + break; } return false; } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation visited, final 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); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + 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); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); + removeNLSComment(cuRewrite, visited, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); } + removeUnsupportedEncodingException(visited, group, rewrite, importRewriter); } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { - return "Writer w = new OutputStreamWriter(out, "+cb.computeCharsetforPreview()+");\nOutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(\"\"), StandardCharsets.UTF_8);\n"; //$NON-NLS-1$ //$NON-NLS-2$ + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { + return "Writer w = new OutputStreamWriter(out, " + cb.computeCharsetforPreview() //$NON-NLS-1$ + + ");\nOutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(\"\"), StandardCharsets.UTF_8);\n"; //$NON-NLS-1$ } return "Writer w = new OutputStreamWriter(out);\nOutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(\"\"), \"UTF-8\");\n"; //$NON-NLS-1$ } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PrintStreamExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PrintStreamExplicitEncoding.java index 55b5f5907b5..57fde8df388 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PrintStreamExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PrintStreamExplicitEncoding.java @@ -35,26 +35,27 @@ import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; + /** * Change * - * Find: Stream fw=new PrintStream("file.txt", "UTF-8") + * Find: Stream fw=new PrintStream("file.txt", "UTF-8") * - * Rewrite: Stream fw=new PrintStream("file.txt", StandardCharsets.UTF_8) + * Rewrite: Stream fw=new PrintStream("file.txt", StandardCharsets.UTF_8) * - * Find: Stream fw=new PrintStream(new File("file.txt"), "UTF-8") + * Find: Stream fw=new PrintStream(new File("file.txt"), "UTF-8") * - * Rewrite: Stream fw=new PrintStream(new File("file.txt"), StandardCharsets.UTF_8) + * Rewrite: Stream fw=new PrintStream(new File("file.txt"), StandardCharsets.UTF_8) * - * Find: Stream fw=new PrintStream(new java.io.OutputStream(), boolean, "UTF-8") + * Find: Stream fw=new PrintStream(new java.io.OutputStream(), boolean, "UTF-8") * - * Rewrite: Stream fw=new PrintStream(new java.io.OutputStream(), boolean, StandardCharsets.UTF_8) + * Rewrite: Stream fw=new PrintStream(new java.io.OutputStream(), boolean, StandardCharsets.UTF_8) * */ public class PrintStreamExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callClassInstanceCreationVisitor(PrintStream.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } @@ -64,41 +65,41 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set< ReferenceHolder holder) { List arguments= visited.arguments(); switch (arguments.size()) { - case 1: - break; - case 2: - if(!(arguments.get(1) instanceof StringLiteral)) { - return false; - } - StringLiteral argstring3= (StringLiteral) arguments.get(1); - if (!encodings.contains(argstring3.getLiteralValue())) { - return false; - } - holder.put(argstring3,encodingmap.get(argstring3.getLiteralValue())); - break; - case 3: - if(!(arguments.get(2) instanceof StringLiteral)) { - return false; - } - StringLiteral argstring4= (StringLiteral) arguments.get(2); - if (!encodings.contains(argstring4.getLiteralValue())) { + case 1: + break; + case 2: + if (!(arguments.get(1) instanceof StringLiteral)) { + return false; + } + StringLiteral argstring3= (StringLiteral) arguments.get(1); + if (!encodings.contains(argstring3.getLiteralValue())) { + return false; + } + holder.put(argstring3, encodingmap.get(argstring3.getLiteralValue())); + break; + case 3: + if (!(arguments.get(2) instanceof StringLiteral)) { + return false; + } + StringLiteral argstring4= (StringLiteral) arguments.get(2); + if (!encodings.contains(argstring4.getLiteralValue())) { + return false; + } + holder.put(argstring4, encodingmap.get(argstring4.getLiteralValue())); + break; + default: return false; - } - holder.put(argstring4,encodingmap.get(argstring4.getLiteralValue())); - break; - default: - return false; } operations.add(fixcore.rewrite(visited, cb, holder)); return false; } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, + TextEditGroup group, ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, (String) data.get(visited)); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, (String) data.get(visited),Nodedata.charsetConstants); /** * new FileOutputStream() */ @@ -123,17 +124,17 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { - return "Stream w=new PrintStream(\"out.txt\","+cb.computeCharsetforPreview()+"));\n"+ //$NON-NLS-1$ //$NON-NLS-2$ - "Stream w=new PrintStream(\"out.txt\",StandardCharsets.UTF_8));\n"+ //$NON-NLS-1$ - "Stream w=new PrintStream(new File(\"out.txt\"),StandardCharsets.UTF_8));\n"; //$NON-NLS-1$ + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { + return "Stream w=new PrintStream(\"out.txt\"," + cb.computeCharsetforPreview() + ");\n" + //$NON-NLS-1$ //$NON-NLS-2$ + "Stream w=new PrintStream(\"out.txt\",StandardCharsets.UTF_8);\n" + //$NON-NLS-1$ + "Stream w=new PrintStream(new File(\"out.txt\"),StandardCharsets.UTF_8);\n"; //$NON-NLS-1$ } return """ - Stream w=new PrintStream("out.txt"); - Stream w=new PrintStream("out.txt","UTF-8"); - Stream w=new PrintStream(new File("out.txt"),"UTF-8"); - """; //$NON-NLS-1$ + Stream w=new PrintStream("out.txt"); + Stream w=new PrintStream("out.txt","UTF-8"); + Stream w=new PrintStream(new File("out.txt"),"UTF-8"); + """; //$NON-NLS-1$ } @Override diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PrintWriterExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PrintWriterExplicitEncoding.java index 4b21f3d58e4..5b0ed886d55 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PrintWriterExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PrintWriterExplicitEncoding.java @@ -34,19 +34,22 @@ import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; + /** * * Java 10 * * Change * - * Find: Writer fw=new PrintWriter("file.txt") + * Find: Writer fw=new PrintWriter("file.txt") * - * Rewrite: Writer fw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("file.txt"),defaultCharset))); + * Rewrite: Writer fw=new BufferedWriter(new OutputStreamWriter(new + * FileOutputStream("file.txt"),defaultCharset))); * - * Find: Writer fw=new PrintWriter(new File("file.txt")) + * Find: Writer fw=new PrintWriter(new File("file.txt")) * - * Rewrite: Writer fw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("file.txt"),defaultCharset))); + * Rewrite: Writer fw=new BufferedWriter(new OutputStreamWriter(new + * FileOutputStream("file.txt"),defaultCharset))); * * Charset.defaultCharset() is available since Java 1.5 * @@ -54,7 +57,7 @@ public class PrintWriterExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { /** * For Java 9 and older just do nothing @@ -73,11 +76,11 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set< } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, + TextEditGroup group, ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, (String) data.get(visited)); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, (String) data.get(visited),Nodedata.charsetConstants); /** * new FileOutputStream() */ @@ -102,9 +105,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { - return "Writer w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputfile),"+cb.computeCharsetforPreview()+"));\n"; //$NON-NLS-1$ //$NON-NLS-2$ + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { + return "Writer w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputfile)," + cb.computeCharsetforPreview() + "));\n"; //$NON-NLS-1$ //$NON-NLS-2$ } return "Writer w=new PrintWriter(outputfile);\n"; //$NON-NLS-1$ } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PropertiesStoreToXMLExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PropertiesStoreToXMLExplicitEncoding.java index 5d6853064b0..008cef31145 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PropertiesStoreToXMLExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/PropertiesStoreToXMLExplicitEncoding.java @@ -36,12 +36,12 @@ import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; + /** - * Find: Properties.storeToXML(java.io.OutputStream,"comment","UTF-8") - * throws UnsupportedEncodingException - * By default the UTF-8 character encoding is used - * so Properties.storeToXML(java.io.OutputStream,"comment") - * is the same as Properties.storeToXML(java.io.OutputStream,"comment", StandardCharsets.UTF_8) + * Find: Properties.storeToXML(java.io.OutputStream,"comment","UTF-8") throws + * UnsupportedEncodingException By default the UTF-8 character encoding is used so + * Properties.storeToXML(java.io.OutputStream,"comment") is the same as + * Properties.storeToXML(java.io.OutputStream,"comment", StandardCharsets.UTF_8) * * Rewrite: Properties.storeToXML(java.io.OutputStream,"comment", StandardCharsets.UTF_8) * @@ -49,7 +49,7 @@ public class PropertiesStoreToXMLExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { /** * For Java 9 and older just do nothing @@ -57,7 +57,8 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation return; } ReferenceHolder datah= new ReferenceHolder<>(); - HelperVisitor.callMethodInvocationVisitor(Properties.class, METHOD_STORE_TO_XML, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); + HelperVisitor.callMethodInvocationVisitor(Properties.class, METHOD_STORE_TO_XML, compilationUnit, datah, nodesprocessed, + (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, @@ -65,49 +66,48 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, MethodInvocation visited, ReferenceHolder holder) { List arguments= visited.arguments(); switch (arguments.size()) { - case 3: - if(!(arguments.get(2) instanceof StringLiteral)) { - return false; - } - StringLiteral argstring3= (StringLiteral) arguments.get(2); - if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + case 3: + if (!(arguments.get(2) instanceof StringLiteral)) { + return false; + } + StringLiteral argstring3= (StringLiteral) arguments.get(2); + if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + return false; + } + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(argstring3.getLiteralValue().toUpperCase()); + nd.replace= true; + nd.visited= argstring3; + holder.put(visited, nd); + operations.add(fixcore.rewrite(visited, cb, holder)); + break; + case 2: + Nodedata nd2= new Nodedata(); + nd2.encoding= "UTF_8"; //$NON-NLS-1$ + nd2.replace= false; + nd2.visited= visited; + holder.put(visited, nd2); + operations.add(fixcore.rewrite(visited, cb, holder)); + break; + default: return false; - } - Nodedata nd=new Nodedata(); - nd.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase()); - nd.replace=true; - nd.visited=argstring3; - holder.put(visited,nd); - operations.add(fixcore.rewrite(visited, cb, holder)); - break; - case 2: - Nodedata nd2=new Nodedata(); - nd2.encoding="UTF_8"; //$NON-NLS-1$ - nd2.replace=false; - nd2.visited=visited; - holder.put(visited,nd2); - operations.add(fixcore.rewrite(visited, cb, holder)); - break; - default: - return false; } - return false; } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visited, final 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); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding,Nodedata.charsetConstants); /** * Add StandardCharsets.UTF_8 as third (last) parameter */ ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); @@ -116,13 +116,13 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { - return "Properties p=new Properties();\n"+ //$NON-NLS-1$ + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { + return "Properties p=new Properties();\n" + //$NON-NLS-1$ "p.storeToXML(java.io.OutputStream,String,StandardCharsets.UTF_8);\n"; //$NON-NLS-1$ } - return "Properties p=new Properties();\n"+ //$NON-NLS-1$ - "p.storeToXML(java.io.OutputStream,String,\"UTF-8\");\n"; //$NON-NLS-1$ + return "Properties p=new Properties();\n" + //$NON-NLS-1$ + "p.storeToXML(java.io.OutputStream,String,\"UTF-8\");\n"; //$NON-NLS-1$ } @Override diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ScannerExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ScannerExplicitEncoding.java index 9c5e94e7d7d..398e7838e6b 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ScannerExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/ScannerExplicitEncoding.java @@ -33,32 +33,33 @@ import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; + /** * * Java 10 * * Change * - * Find: new java.util.Scanner(new File("filename.txt"),"UTF-8") + * Find: new java.util.Scanner(new File("filename.txt"),"UTF-8") * - * Rewrite: new java.util.Scanner(new File("filename.txt"),StandardCharsets.UTF_8); + * Rewrite: new java.util.Scanner(new File("filename.txt"),StandardCharsets.UTF_8); * - * Find: new java.util.Scanner("filename.txt", "UTF-8") + * Find: new java.util.Scanner("filename.txt", "UTF-8") * - * Rewrite: new java.util.Scanner("filename.txt", StandardCharsets.UTF_8) + * Rewrite: new java.util.Scanner("filename.txt", StandardCharsets.UTF_8) * - * Find: new java.util.Scanner(java.io.OutputStream, "UTF-8") + * Find: new java.util.Scanner(java.io.OutputStream, "UTF-8") * - * Rewrite: new java.util.Scanner(java.io.OutputStream, StandardCharsets.UTF_8) + * Rewrite: new java.util.Scanner(java.io.OutputStream, StandardCharsets.UTF_8) * - * Find: new java.util.Scanner(java.io.OutputStream) + * Find: new java.util.Scanner(java.io.OutputStream) * - * Rewrite: new java.util.Scanner(java.io.OutputStream, Charset.defaultCharset()) + * Rewrite: new java.util.Scanner(java.io.OutputStream, Charset.defaultCharset()) */ public class ScannerExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { /** * For Java 9 and older just do nothing @@ -72,23 +73,23 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set operations, ChangeBehavior cb, ClassInstanceCreation visited, ReferenceHolder holder) { - List arguments = visited.arguments(); - Nodedata nd = new Nodedata(); + List arguments= visited.arguments(); + Nodedata nd= new Nodedata(); switch (arguments.size()) { case 4: case 2: - int encodingIndex = (arguments.size() == 4) ? 3 : 1; - ASTNode argumentNode = arguments.get(encodingIndex); + int encodingIndex= (arguments.size() == 4) ? 3 : 1; + ASTNode argumentNode= arguments.get(encodingIndex); if (argumentNode instanceof StringLiteral) { - StringLiteral encodingLiteral = (StringLiteral) argumentNode; - String encodingValue = encodingLiteral.getLiteralValue().toUpperCase(); + StringLiteral encodingLiteral= (StringLiteral) argumentNode; + String encodingValue= encodingLiteral.getLiteralValue().toUpperCase(); if (encodings.contains(encodingValue)) { - nd.encoding = encodingmap.get(encodingValue); - nd.replace = true; - nd.visited = encodingLiteral; + nd.encoding= encodingmap.get(encodingValue); + nd.replace= true; + nd.visited= encodingLiteral; holder.put(visited, nd); operations.add(fixcore.rewrite(visited, cb, holder)); } @@ -96,9 +97,9 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set< break; case 1: - nd.encoding = null; - nd.replace = false; - nd.visited = visited; + nd.encoding= null; + nd.replace= false; + nd.visited= visited; holder.put(visited, nd); operations.add(fixcore.rewrite(visited, cb, holder)); break; @@ -111,17 +112,17 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set< } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, + TextEditGroup group, ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); Nodedata nodedata= (Nodedata) data.get(visited); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + 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); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); @@ -129,8 +130,8 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { return "new java.util.Scanner(\"asdf\",StandardCharsets.UTF_8);\n"; //$NON-NLS-1$ } return "new java.util.Scanner(\"asdf\", \"UTF-8\");\n"; //$NON-NLS-1$ diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/StringExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/StringExplicitEncoding.java index d0582506664..ac43bd8932d 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/StringExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/StringExplicitEncoding.java @@ -33,25 +33,26 @@ import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; + /** * * Java 10 * * Change * - * Find: String s=new String(byte[],"UTF-8") + * Find: String s=new String(byte[],"UTF-8") * - * Rewrite: String s=new String(byte[],StandardCharsets.UTF_8); + * Rewrite: String s=new String(byte[],StandardCharsets.UTF_8); * - * Find: String s=new String(byte[],int, int, "UTF-8") + * Find: String s=new String(byte[],int, int, "UTF-8") * - * Rewrite: String s=new String(byte[],int, int, StandardCharsets.UTF_8) + * Rewrite: String s=new String(byte[],int, int, StandardCharsets.UTF_8) * */ public class StringExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { /** * For Java 9 and older just do nothing @@ -67,57 +68,57 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set< ReferenceHolder holder) { List arguments= visited.arguments(); switch (arguments.size()) { - case 4: - if(!(arguments.get(3) instanceof StringLiteral)) { - return false; - } - StringLiteral argstring4= (StringLiteral) arguments.get(3); - if (!encodings.contains(argstring4.getLiteralValue().toUpperCase())) { - return false; - } - Nodedata nd=new Nodedata(); - nd.encoding=encodingmap.get(argstring4.getLiteralValue().toUpperCase()); - nd.replace=true; - nd.visited=argstring4; - holder.put(visited,nd); - operations.add(fixcore.rewrite(visited, cb, holder)); - break; - case 2: - if(!(arguments.get(1) instanceof StringLiteral)) { - return false; - } - StringLiteral argstring3= (StringLiteral) arguments.get(1); - if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { - return false; - } - Nodedata nd2=new Nodedata(); - nd2.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase()); - nd2.replace=true; - nd2.visited=argstring3; - holder.put(visited,nd2); - operations.add(fixcore.rewrite(visited, cb, holder)); - break; - case 1: - default: - break; + case 4: + if (!(arguments.get(3) instanceof StringLiteral)) { + return false; + } + StringLiteral argstring4= (StringLiteral) arguments.get(3); + if (!encodings.contains(argstring4.getLiteralValue().toUpperCase())) { + return false; + } + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(argstring4.getLiteralValue().toUpperCase()); + nd.replace= true; + nd.visited= argstring4; + holder.put(visited, nd); + operations.add(fixcore.rewrite(visited, cb, holder)); + break; + case 2: + if (!(arguments.get(1) instanceof StringLiteral)) { + return false; + } + StringLiteral argstring3= (StringLiteral) arguments.get(1); + if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + return false; + } + Nodedata nd2= new Nodedata(); + nd2.encoding= encodingmap.get(argstring3.getLiteralValue().toUpperCase()); + nd2.replace= true; + nd2.visited= argstring3; + holder.put(visited, nd2); + operations.add(fixcore.rewrite(visited, cb, holder)); + break; + case 1: + default: + break; } return false; } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation visited, final 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); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + 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); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); @@ -126,9 +127,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { - return "String s=new String(byte[],"+cb.computeCharsetforPreview()+");\n"; //$NON-NLS-1$ //$NON-NLS-2$ + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { + return "String s=new String(byte[]," + cb.computeCharsetforPreview() + ");\n"; //$NON-NLS-1$ //$NON-NLS-2$ } return "String s=new String(byte[],\"UTF-8\");\n"; //$NON-NLS-1$ } 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 7f4db14bb7b..a16d6b47669 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 @@ -34,21 +34,23 @@ import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; + /** - * Find: String.getBytes() + * Find: String.getBytes() * * Rewrite: String.getBytes(Charset.defaultCharset()) * - * Find: String.getBytes("Utf-8") + * Find: String.getBytes("Utf-8") * * Rewrite: String.getBytes(StandardCharsets.UTF_8) */ public class StringGetBytesExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { ReferenceHolder datah= new ReferenceHolder<>(); - HelperVisitor.callMethodInvocationVisitor(String.class, METHOD_GET_BYTES, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); + HelperVisitor.callMethodInvocationVisitor(String.class, METHOD_GET_BYTES, compilationUnit, datah, nodesprocessed, + (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, @@ -56,48 +58,49 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, MethodInvocation visited, ReferenceHolder holder) { List arguments= visited.arguments(); switch (arguments.size()) { - case 1: - if(!(arguments.get(0) instanceof StringLiteral)) { - return false; - } - StringLiteral argstring3= (StringLiteral) arguments.get(0); - if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + case 1: + if (!(arguments.get(0) instanceof StringLiteral)) { + return false; + } + StringLiteral argstring3= (StringLiteral) arguments.get(0); + if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + return false; + } + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(argstring3.getLiteralValue().toUpperCase()); + nd.replace= true; + nd.visited= argstring3; + holder.put(visited, nd); + break; + case 0: + Nodedata nd2= new Nodedata(); + nd2.encoding= null; + nd2.replace= false; + nd2.visited= visited; + holder.put(visited, nd2); + break; + default: return false; - } - Nodedata nd=new Nodedata(); - nd.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase()); - nd.replace=true; - nd.visited=argstring3; - holder.put(visited,nd); - break; - case 0: - Nodedata nd2=new Nodedata(); - nd2.encoding=null; - nd2.replace=false; - nd2.visited=visited; - holder.put(visited,nd2); - break; - default: - return false; } operations.add(fixcore.rewrite(visited, cb, holder)); return false; } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visited, final 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); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding,Nodedata.charsetConstants); /** * Add Charset.defaultCharset() as second (last) parameter */ ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); + removeNLSComment(cuRewrite, visited, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); } @@ -105,13 +108,13 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { - return "String s=\"asdf\";\n"+ //$NON-NLS-1$ - "byte[] bytes= s.getBytes("+cb.computeCharsetforPreview()+");\n"; //$NON-NLS-1$ //$NON-NLS-2$ + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { + return "String s=\"asdf\";\n" + //$NON-NLS-1$ + "byte[] bytes= s.getBytes(" + cb.computeCharsetforPreview() + ");\n"; //$NON-NLS-1$ //$NON-NLS-2$ } - return "String s=\"asdf\";\n"+ //$NON-NLS-1$ - "byte[] bytes= s.getBytes();\n"; //$NON-NLS-1$ + return "String s=\"asdf\";\n" + //$NON-NLS-1$ + "byte[] bytes= s.getBytes();\n"; //$NON-NLS-1$ } @Override diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/URLDecoderDecodeExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/URLDecoderDecodeExplicitEncoding.java index d5fa37a0d8a..017892cba76 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/URLDecoderDecodeExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/URLDecoderDecodeExplicitEncoding.java @@ -25,6 +25,7 @@ import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; @@ -37,18 +38,18 @@ import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; + /** * Java 10 * - * Find: java.net.URLDecoder.decode("asdf","UTF-8") + * Find: java.net.URLDecoder.decode("asdf","UTF-8") * * Rewrite: java.net.URLDecoder.decode("asdf",StandardCharsets.UTF_8) * - * Find: java.net.URLDecoder.decode("asdf") - * Without the parameter the default is the file.encoding system property so - * Charset.defaultCharset() - * URLDecoder.decode("asdf") is (nearly) the same as URLDecoder.decode("asdf",Charset.defaultCharset()) - * But it is not really better (other than that you can see that it is depending on the default charset) + * Find: java.net.URLDecoder.decode("asdf") Without the parameter the default is the file.encoding + * system property so Charset.defaultCharset() URLDecoder.decode("asdf") is (nearly) the same as + * URLDecoder.decode("asdf",Charset.defaultCharset()) But it is not really better (other than that + * you can see that it is depending on the default charset) * * KEEP * @@ -56,14 +57,14 @@ * * USE_UTF8 * - * Rewrite: java.net.URLDecoder.decode("asdf",StandardCharsets.UTF_8) - * This changes how the code works but it might be the better choice if you want to get rid of - * depending on environment settings + * Rewrite: java.net.URLDecoder.decode("asdf",StandardCharsets.UTF_8) This changes how the code + * works but it might be the better choice if you want to get rid of depending on environment + * settings */ public class URLDecoderDecodeExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { /** * For Java 9 and older just do nothing @@ -71,41 +72,49 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation return; } ReferenceHolder datah= new ReferenceHolder<>(); - HelperVisitor.callMethodInvocationVisitor(URLDecoder.class, METHOD_DECODE, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); + HelperVisitor.callMethodInvocationVisitor(URLDecoder.class, METHOD_DECODE, compilationUnit, datah, nodesprocessed, + (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set operations, ChangeBehavior cb, MethodInvocation visited, ReferenceHolder holder) { List arguments= visited.arguments(); - if (ASTNodes.usesGivenSignature(visited, URLDecoder.class.getCanonicalName(), METHOD_DECODE, String.class.getCanonicalName(),String.class.getCanonicalName())) { - StringLiteral argstring3= (StringLiteral) arguments.get(1); - if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + if (ASTNodes.usesGivenSignature(visited, URLDecoder.class.getCanonicalName(), METHOD_DECODE, String.class.getCanonicalName(), String.class.getCanonicalName())) { + ASTNode encodingArg= arguments.get(1); + + String encodingValue= null; + if (encodingArg instanceof StringLiteral) { + encodingValue= ((StringLiteral) encodingArg).getLiteralValue().toUpperCase(); + } else if (encodingArg instanceof SimpleName) { + encodingValue= findVariableValue((SimpleName) encodingArg, visited); + } + + if (encodingValue != null && encodings.contains(encodingValue)) { + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(encodingValue); + nd.replace= true; + nd.visited= encodingArg; + holder.put(visited, nd); + operations.add(fixcore.rewrite(visited, cb, holder)); return false; } - Nodedata nd=new Nodedata(); - nd.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase()); - nd.replace=true; - nd.visited=argstring3; - holder.put(visited,nd); - operations.add(fixcore.rewrite(visited, cb, holder)); - return false; } if (ASTNodes.usesGivenSignature(visited, URLDecoder.class.getCanonicalName(), METHOD_DECODE, String.class.getCanonicalName())) { - Nodedata nd=new Nodedata(); - switch(cb) { + Nodedata nd= new Nodedata(); + switch (cb) { case KEEP_BEHAVIOR: - nd.encoding=null; + nd.encoding= null; break; case ENFORCE_UTF8: - nd.encoding="UTF_8"; //$NON-NLS-1$ + nd.encoding= "UTF_8"; //$NON-NLS-1$ break; case ENFORCE_UTF8_AGGREGATE: break; } - nd.replace=false; - nd.visited=visited; - holder.put(visited,nd); + nd.replace= false; + nd.visited= visited; + holder.put(visited, nd); operations.add(fixcore.rewrite(visited, cb, holder)); return false; } @@ -113,18 +122,18 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visited, final 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); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding,Nodedata.charsetConstants); /** * Add Charset.defaultCharset() or StandardCharsets.UTF_8 as second (last) parameter */ ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); @@ -133,8 +142,8 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { return "java.net.URLDecoder.decode(\"asdf\", StandardCharsets.UTF_8);\n"; //$NON-NLS-1$ } return "java.net.URLDecoder.decode(\"asdf\", \"UTF-8\");\n"; //$NON-NLS-1$ diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/URLEncoderEncodeExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/URLEncoderEncodeExplicitEncoding.java index 75218ba4ceb..ea78b5f2461 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/URLEncoderEncodeExplicitEncoding.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/URLEncoderEncodeExplicitEncoding.java @@ -25,6 +25,7 @@ import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; @@ -37,18 +38,18 @@ import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; + /** * Java 10 * - * Find: java.net.URLEncoder.encode("asdf","UTF-8") + * Find: java.net.URLEncoder.encode("asdf","UTF-8") * * Rewrite: java.net.URLEncoder.encode("asdf",StandardCharsets.UTF_8) * - * Find: java.net.URLEncoder.encode("asdf") - * Without the parameter the default is the file.encoding system property so - * Charset.defaultCharset() - * URLEncoder.encode("asdf") is (nearly) the same as URLEncoder.encode("asdf",Charset.defaultCharset()) - * But it is not really better (other than that you can see that it is depending on the default charset) + * Find: java.net.URLEncoder.encode("asdf") Without the parameter the default is the file.encoding + * system property so Charset.defaultCharset() URLEncoder.encode("asdf") is (nearly) the same as + * URLEncoder.encode("asdf",Charset.defaultCharset()) But it is not really better (other than that + * you can see that it is depending on the default charset) * * KEEP * @@ -56,14 +57,14 @@ * * USE_UTF8 * - * Rewrite: java.net.URLEncoder.encode("asdf",StandardCharsets.UTF_8) - * This changes how the code works but it might be the better choice if you want to get rid of - * depending on environment settings + * Rewrite: java.net.URLEncoder.encode("asdf",StandardCharsets.UTF_8) This changes how the code + * works but it might be the better choice if you want to get rid of depending on environment + * settings */ public class URLEncoderEncodeExplicitEncoding extends AbstractExplicitEncoding { @Override - public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb) { if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { /** * For Java 9 and older just do nothing @@ -71,41 +72,50 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation return; } ReferenceHolder datah= new ReferenceHolder<>(); - HelperVisitor.callMethodInvocationVisitor(URLEncoder.class, METHOD_ENCODE, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); + Nodedata.charsetConstants.clear(); + HelperVisitor.callMethodInvocationVisitor(URLEncoder.class, METHOD_ENCODE, compilationUnit, datah, nodesprocessed, + (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set operations, ChangeBehavior cb, MethodInvocation visited, ReferenceHolder holder) { List arguments= visited.arguments(); - if (ASTNodes.usesGivenSignature(visited, URLEncoder.class.getCanonicalName(), METHOD_ENCODE, String.class.getCanonicalName(),String.class.getCanonicalName())) { - StringLiteral argstring3= (StringLiteral) arguments.get(1); - if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { + if (ASTNodes.usesGivenSignature(visited, URLEncoder.class.getCanonicalName(), METHOD_ENCODE, String.class.getCanonicalName(), String.class.getCanonicalName())) { + ASTNode encodingArg= arguments.get(1); + + String encodingValue= null; + if (encodingArg instanceof StringLiteral) { + encodingValue= ((StringLiteral) encodingArg).getLiteralValue().toUpperCase(); + } else if (encodingArg instanceof SimpleName) { + encodingValue= findVariableValue((SimpleName) encodingArg, visited); + } + + if (encodingValue != null && encodings.contains(encodingValue)) { + Nodedata nd= new Nodedata(); + nd.encoding= encodingmap.get(encodingValue); + nd.replace= true; + nd.visited= encodingArg; + holder.put(visited, nd); + operations.add(fixcore.rewrite(visited, cb, holder)); return false; } - Nodedata nd=new Nodedata(); - nd.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase()); - nd.replace=true; - nd.visited=argstring3; - holder.put(visited,nd); - operations.add(fixcore.rewrite(visited, cb, holder)); - return false; } if (ASTNodes.usesGivenSignature(visited, URLEncoder.class.getCanonicalName(), METHOD_ENCODE, String.class.getCanonicalName())) { - Nodedata nd=new Nodedata(); - switch(cb) { + Nodedata nd= new Nodedata(); + switch (cb) { case KEEP_BEHAVIOR: - nd.encoding=null; + nd.encoding= null; break; case ENFORCE_UTF8: - nd.encoding="UTF_8"; //$NON-NLS-1$ + nd.encoding= "UTF_8"; //$NON-NLS-1$ break; case ENFORCE_UTF8_AGGREGATE: break; } - nd.replace=false; - nd.visited=visited; - holder.put(visited,nd); + nd.replace= false; + nd.visited= visited; + holder.put(visited, nd); operations.add(fixcore.rewrite(visited, cb, holder)); return false; } @@ -113,18 +123,18 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, } @Override - public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { + public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visited, final 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); - ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding); + ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding,Nodedata.charsetConstants); /** * Add Charset.defaultCharset() as second (last) parameter */ ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); - if(nodedata.replace) { + if (nodedata.replace) { listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group); } else { listRewrite.insertLast(callToCharsetDefaultCharset, group); @@ -133,8 +143,8 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite } @Override - public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { - if(afterRefactoring) { + public String getPreview(boolean afterRefactoring, ChangeBehavior cb) { + if (afterRefactoring) { return "java.net.URLEncoder.encode(\"asdf\", StandardCharsets.UTF_8);\n"; //$NON-NLS-1$ } return "java.net.URLEncoder.encode(\"asdf\", \"UTF-8\");\n"; //$NON-NLS-1$ diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/AutomatedSuite.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/AutomatedSuite.java index cb270eb86fe..915eb8bed05 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/AutomatedSuite.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/AutomatedSuite.java @@ -41,6 +41,7 @@ import org.eclipse.jdt.ui.tests.packageview.PackageExplorerTests; import org.eclipse.jdt.ui.tests.quickfix.QuickFixTestSuite; import org.eclipse.jdt.ui.tests.quickfix.Java10.QuickFixJava10TestSuite; +import org.eclipse.jdt.ui.tests.quickfix.Java22.QuickFixJava22TestSuite; import org.eclipse.jdt.ui.tests.quickfix.Java8.QuickFixJava8TestSuite; import org.eclipse.jdt.ui.tests.refactoring.RefactoringTests; import org.eclipse.jdt.ui.tests.search.SearchTest; @@ -63,6 +64,7 @@ QuickFixTestSuite.class, QuickFixJava8TestSuite.class, QuickFixJava10TestSuite.class, + QuickFixJava22TestSuite.class, NewJavaProjectWizardTest.class, NewTypeWizardTest.class, diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest12.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest12.java index e76cd4f719a..53292670a08 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest12.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest12.java @@ -17,7 +17,6 @@ import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; @@ -123,7 +122,6 @@ enum Day { assertEqualStringsIgnoreOrder(new String[] { preview }, new String[] { expected }); } - @Ignore("https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/1792") @Test public void testSplitSwitchCaseLabelRuleStatement() throws Exception { fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); 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 34156326826..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 @@ -51,849 +51,51 @@ protected void setUp() throws Exception,UnsupportedCharsetException { } @RegisterExtension - AbstractEclipseJava context = new EclipseJava10(); + AbstractEclipseJava context= new EclipseJava10(); - 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.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(); - } + @ParameterizedTest + @EnumSource(ExplicitEncodingPatternsKeepBehavior.class) + public void testExplicitEncodingParametrizedKeepBehavior(ExplicitEncodingPatternsKeepBehavior test) throws CoreException { + IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); + 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); } -} -"""), - 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(ExplicitEncodingPatternsPreferUTF8.class) + public void testExplicitEncodingParametrizedPreferUTF8(ExplicitEncodingPatternsPreferUTF8 test) throws CoreException { + IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu= pack.createCompilationUnit("E1.java", test.given, false, null); + context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); + context.disable(CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR); + context.enable(CleanUpConstants.EXPLICITENCODING_INSERT_UTF8); + context.disable(CleanUpConstants.EXPLICITENCODING_AGGREGATE_TO_UTF8); + context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); } +// @Disabled("Not Implemented") @ParameterizedTest - @EnumSource(ExplicitEncodingPatterns.class) - public void testExplicitEncodingParametrized(ExplicitEncodingPatterns test) throws CoreException { - IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); - ICompilationUnit cu = pack.createCompilationUnit("E1.java", test.given, false, null); + @EnumSource(ExplicitEncodingPatternsAggregateUTF8.class) + public void testExplicitEncodingParametrizedAggregateUTF8(ExplicitEncodingPatternsAggregateUTF8 test) throws CoreException { + IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu= pack.createCompilationUnit("E1.java", test.given, false, null); context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); + context.disable(CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR); + context.disable(CleanUpConstants.EXPLICITENCODING_INSERT_UTF8); + context.enable(CleanUpConstants.EXPLICITENCODING_AGGREGATE_TO_UTF8); context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); } @Test public void testExplicitEncodingdonttouch() throws CoreException { - IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); - ICompilationUnit cu = pack.createCompilationUnit("E2.java", + IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu= pack.createCompilationUnit("E2.java", """ package test1; @@ -923,6 +125,7 @@ void method() throws UnsupportedEncodingException, IOException { false, null); context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); + context.enable(CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR); context.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); } diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsAggregateUTF8.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsAggregateUTF8.java new file mode 100644 index 00000000000..ed74fde51e0 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsAggregateUTF8.java @@ -0,0 +1,1939 @@ +/******************************************************************************* + * Copyright (c) 2024 Carsten Hammer and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * + *******************************************************************************/ +package org.eclipse.jdt.ui.tests.quickfix.Java10; + +public enum ExplicitEncodingPatternsAggregateUTF8 { + + 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.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.io.FileNotFoundException; + +public class E1 { + private static final Charset UTF_8 = StandardCharsets.UTF_8; + + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(E1.UTF_8); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString(E1.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.nio.charset.StandardCharsets; +import java.io.FileNotFoundException; + +public class E1 { + private static final Charset UTF_8 = StandardCharsets.UTF_8; + + void method(String filename) { + try { + Reader is=new InputStreamReader(new FileInputStream(filename), E1.UTF_8); + } 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.nio.charset.StandardCharsets; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; + +public class E1 { + private static final Charset UTF_8 = StandardCharsets.UTF_8; + + void method(String filename) { + try { + Writer fw=new OutputStreamWriter(new FileOutputStream(filename), E1.UTF_8); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } +} + """), + INPUTSTREAMREADER( +""" +package test1; + +import java.io.InputStreamReader; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; + +public class E1 { + + void method(String filename) { + try { + // Standardkonstruktor ohne Encoding + InputStreamReader is1 = new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$ + + // String Literal Encodings, die nach StandardCharsets umgeschrieben werden sollten + InputStreamReader is2 = new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is3 = new InputStreamReader(new FileInputStream("file3.txt"), "ISO-8859-1"); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is4 = new InputStreamReader(new FileInputStream("file4.txt"), "US-ASCII"); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basiertes Encoding, das in Charset umgeschrieben werden kann, jedoch ohne vordefinierte Konstante + InputStreamReader is5 = new InputStreamReader(new FileInputStream("file5.txt"), "UTF-16"); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basierte Encodings mit Groß-/Kleinschreibungsvarianten + InputStreamReader is6 = new InputStreamReader(new FileInputStream("file6.txt"), "utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is7 = new InputStreamReader(new FileInputStream("file7.txt"), "Utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); // Sollte nach Cleanup entfernt werden + } + } + + void methodWithTryCatch(String filename) { + try { + // Variante, bei der UnsupportedEncodingException behandelt wird + InputStreamReader is8 = new InputStreamReader(new FileInputStream("file8.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); // Sollte nach Cleanup entfernt werden + } + } + + void methodWithoutException(String filename) throws UnsupportedEncodingException, FileNotFoundException { + // Case ohne Try-Catch-Block, sollte Charset-Konstanten direkt ersetzen + InputStreamReader is9 = new InputStreamReader(new FileInputStream("file9.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + } + + void methodWithVariableEncoding(String filename) throws UnsupportedEncodingException, FileNotFoundException { + // Case, bei dem das Encoding aus einer Variablen kommt, Cleanup sollte hier keine Änderungen machen + String encoding = "UTF-8"; //$NON-NLS-1$ + InputStreamReader is10 = new InputStreamReader(new FileInputStream("file10.txt"), encoding); //$NON-NLS-1$ + } + + void methodWithNonStandardEncoding(String filename) { + try { + // Case mit nicht vordefiniertem Charset, sollte keine Umwandlung in StandardCharsets erfolgen + InputStreamReader is11 = new InputStreamReader(new FileInputStream("file11.txt"), "windows-1252"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + // Methode mit "throws UnsupportedEncodingException" zur Prüfung des Cleanups + void methodWithThrows(String filename) throws FileNotFoundException, UnsupportedEncodingException { + InputStreamReader is3 = new InputStreamReader(new FileInputStream(filename), "UTF-8"); //$NON-NLS-1$ + } +} +""", + +""" +package test1; + +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +public class E1 { + + private static final Charset UTF_16 = StandardCharsets.UTF_16; + private static final Charset US_ASCII = StandardCharsets.US_ASCII; + private static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; + private static final Charset UTF_8 = StandardCharsets.UTF_8; + + void method(String filename) { + try { + // Standardkonstruktor ohne Encoding + InputStreamReader is1 = new InputStreamReader(new FileInputStream("file1.txt"), E1.UTF_8); //$NON-NLS-1$ + + // String Literal Encodings, die nach StandardCharsets umgeschrieben werden sollten + InputStreamReader is2 = new InputStreamReader(new FileInputStream("file2.txt"), E1.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is3 = new InputStreamReader(new FileInputStream("file3.txt"), E1.ISO_8859_1); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is4 = new InputStreamReader(new FileInputStream("file4.txt"), E1.US_ASCII); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basiertes Encoding, das in Charset umgeschrieben werden kann, jedoch ohne vordefinierte Konstante + InputStreamReader is5 = new InputStreamReader(new FileInputStream("file5.txt"), E1.UTF_16); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basierte Encodings mit Groß-/Kleinschreibungsvarianten + InputStreamReader is6 = new InputStreamReader(new FileInputStream("file6.txt"), E1.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is7 = new InputStreamReader(new FileInputStream("file7.txt"), E1.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + void methodWithTryCatch(String filename) { + try { + // Variante, bei der UnsupportedEncodingException behandelt wird + InputStreamReader is8 = new InputStreamReader(new FileInputStream("file8.txt"), E1.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + void methodWithoutException(String filename) throws FileNotFoundException { + // Case ohne Try-Catch-Block, sollte Charset-Konstanten direkt ersetzen + InputStreamReader is9 = new InputStreamReader(new FileInputStream("file9.txt"), E1.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + } + + void methodWithVariableEncoding(String filename) throws UnsupportedEncodingException, FileNotFoundException { + // Case, bei dem das Encoding aus einer Variablen kommt, Cleanup sollte hier keine Änderungen machen + String encoding = "UTF-8"; //$NON-NLS-1$ + InputStreamReader is10 = new InputStreamReader(new FileInputStream("file10.txt"), encoding); //$NON-NLS-1$ + } + + void methodWithNonStandardEncoding(String filename) { + try { + // Case mit nicht vordefiniertem Charset, sollte keine Umwandlung in StandardCharsets erfolgen + InputStreamReader is11 = new InputStreamReader(new FileInputStream("file11.txt"), "windows-1252"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + // Methode mit "throws UnsupportedEncodingException" zur Prüfung des Cleanups + void methodWithThrows(String filename) throws FileNotFoundException { + InputStreamReader is3 = new InputStreamReader(new FileInputStream(filename), E1.UTF_8); //$NON-NLS-1$ + } +} +"""), + OUTPUTSTREAMWRITER( +""" +package test1; + +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(String filename) { + try { + // Standard-Konstruktor ohne Encoding + OutputStreamWriter os1 = new OutputStreamWriter(new FileOutputStream(filename)); + + // Konstruktor mit String-Encoding (UTF-8) -> muss durch StandardCharsets.UTF_8 ersetzt werden + OutputStreamWriter os2 = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"); // "UTF-8" als String-Literal + + // Konstruktor mit String-Encoding (ISO-8859-1) -> muss durch StandardCharsets.ISO_8859_1 ersetzt werden + OutputStreamWriter os3 = new OutputStreamWriter(new FileOutputStream(filename), "ISO-8859-1"); // "ISO-8859-1" als String-Literal + + // Konstruktor mit String-Encoding (US-ASCII) -> muss durch StandardCharsets.US_ASCII ersetzt werden + OutputStreamWriter os4 = new OutputStreamWriter(new FileOutputStream(filename), "US-ASCII"); // "US-ASCII" als String-Literal + + // Konstruktor mit String-Encoding (UTF-16) -> muss durch StandardCharsets.UTF_16 ersetzt werden + OutputStreamWriter os5 = new OutputStreamWriter(new FileOutputStream(filename), "UTF-16"); // "UTF-16" als String-Literal + + // Der Konstruktor mit einer benutzerdefinierten Konstante bleibt unverändert + OutputStreamWriter os6 = new OutputStreamWriter(new FileOutputStream(filename), ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (bleiben unverändert) + OutputStreamWriter os7 = new OutputStreamWriter(new FileOutputStream(filename), "windows-1252"); // bleibt unverändert + OutputStreamWriter os8 = new OutputStreamWriter(new FileOutputStream(filename), "Shift_JIS"); // bleibt unverändert + + // Hier wird `UnsupportedEncodingException` geworfen (vor dem Cleanup) + OutputStreamWriter os9 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + + // Aufruf mit einer ungültigen Zeichenkodierung und catch für UnsupportedEncodingException + try { + OutputStreamWriter os10 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + + // Beispiele mit StandardCharsets-Konstanten, die unverändert bleiben + OutputStreamWriter os11 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // bleibt unverändert + OutputStreamWriter os12 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.ISO_8859_1); // bleibt unverändert + OutputStreamWriter os13 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.US_ASCII); // bleibt unverändert + OutputStreamWriter os14 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); // bleibt unverändert + + // Beispiel mit Charset.forName und einer Konstanten, die als Parameter übergeben wird (bleibt unverändert) + OutputStreamWriter os15 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); + } catch (FileNotFoundException e) { + // Datei nicht gefunden + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + } + + // Methodendeklaration, die `UnsupportedEncodingException` wirft (und durch den Cleanup angepasst wird) + void methodWithThrows(String filename) throws UnsupportedEncodingException { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Neue Methode: methodWithThrowsChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr geworfen + void methodWithThrowsChange(String filename) throws FileNotFoundException { + // Nach dem Cleanup, der String "UTF-8" wird zu einer StandardCharset-Konstanten geändert + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"); // wirft keine UnsupportedEncodingException mehr + } + + // Methode mit einem try-catch, um die UnsupportedEncodingException zu behandeln (und durch den Cleanup angepasst wird) + void methodWithCatch(String filename) { + try { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + } + + // Neue Methode: methodWithCatchChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + void methodWithCatchChange(String filename) { + try { + // Nach dem Cleanup wird "UTF-8" ersetzt + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"); // keine UnsupportedEncodingException + } catch (UnsupportedEncodingException e) { + // Dieser Block wird nicht mehr erreicht, da keine UnsupportedEncodingException mehr geworfen wird + e.printStackTrace(); + } + } +} +""", + +""" +package test1; + +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.FileNotFoundException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final Charset UTF_16 = StandardCharsets.UTF_16; + private static final Charset US_ASCII = StandardCharsets.US_ASCII; + private static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; + private static final Charset UTF_8 = StandardCharsets.UTF_8; + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(String filename) { + try { + // Standard-Konstruktor ohne Encoding + OutputStreamWriter os1 = new OutputStreamWriter(new FileOutputStream(filename), E1.UTF_8); + + // Konstruktor mit String-Encoding (UTF-8) -> muss durch StandardCharsets.UTF_8 ersetzt werden + OutputStreamWriter os2 = new OutputStreamWriter(new FileOutputStream(filename), E1.UTF_8); // "UTF-8" als String-Literal + + // Konstruktor mit String-Encoding (ISO-8859-1) -> muss durch StandardCharsets.ISO_8859_1 ersetzt werden + OutputStreamWriter os3 = new OutputStreamWriter(new FileOutputStream(filename), E1.ISO_8859_1); // "ISO-8859-1" als String-Literal + + // Konstruktor mit String-Encoding (US-ASCII) -> muss durch StandardCharsets.US_ASCII ersetzt werden + OutputStreamWriter os4 = new OutputStreamWriter(new FileOutputStream(filename), E1.US_ASCII); // "US-ASCII" als String-Literal + + // Konstruktor mit String-Encoding (UTF-16) -> muss durch StandardCharsets.UTF_16 ersetzt werden + OutputStreamWriter os5 = new OutputStreamWriter(new FileOutputStream(filename), E1.UTF_16); // "UTF-16" als String-Literal + + // Der Konstruktor mit einer benutzerdefinierten Konstante bleibt unverändert + OutputStreamWriter os6 = new OutputStreamWriter(new FileOutputStream(filename), ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (bleiben unverändert) + OutputStreamWriter os7 = new OutputStreamWriter(new FileOutputStream(filename), "windows-1252"); // bleibt unverändert + OutputStreamWriter os8 = new OutputStreamWriter(new FileOutputStream(filename), "Shift_JIS"); // bleibt unverändert + + // Hier wird `UnsupportedEncodingException` geworfen (vor dem Cleanup) + OutputStreamWriter os9 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + + // Aufruf mit einer ungültigen Zeichenkodierung und catch für UnsupportedEncodingException + try { + OutputStreamWriter os10 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + + // Beispiele mit StandardCharsets-Konstanten, die unverändert bleiben + OutputStreamWriter os11 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // bleibt unverändert + OutputStreamWriter os12 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.ISO_8859_1); // bleibt unverändert + OutputStreamWriter os13 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.US_ASCII); // bleibt unverändert + OutputStreamWriter os14 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); // bleibt unverändert + + // Beispiel mit Charset.forName und einer Konstanten, die als Parameter übergeben wird (bleibt unverändert) + OutputStreamWriter os15 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); + } catch (FileNotFoundException e) { + // Datei nicht gefunden + e.printStackTrace(); + } + } + + // Methodendeklaration, die `UnsupportedEncodingException` wirft (und durch den Cleanup angepasst wird) + void methodWithThrows(String filename) throws UnsupportedEncodingException { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Neue Methode: methodWithThrowsChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr geworfen + void methodWithThrowsChange(String filename) throws FileNotFoundException { + // Nach dem Cleanup, der String "UTF-8" wird zu einer StandardCharset-Konstanten geändert + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), E1.UTF_8); // wirft keine UnsupportedEncodingException mehr + } + + // Methode mit einem try-catch, um die UnsupportedEncodingException zu behandeln (und durch den Cleanup angepasst wird) + void methodWithCatch(String filename) { + try { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + } + + // Neue Methode: methodWithCatchChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + void methodWithCatchChange(String filename) { + try { + // Nach dem Cleanup wird "UTF-8" ersetzt + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), E1.UTF_8); // keine UnsupportedEncodingException + } + } +} +"""), + CHANNELSNEWREADER( +""" +package test1; + +import java.io.Reader; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.CharsetDecoder; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(ReadableByteChannel ch, CharsetDecoder decoder) { + // Fälle für StandardCharsets-Konstanten + Reader r1 = Channels.newReader(ch, "UTF-8"); // soll StandardCharsets.UTF_8 werden + Reader r2 = Channels.newReader(ch, "ISO-8859-1"); // soll StandardCharsets.ISO_8859_1 werden + Reader r3 = Channels.newReader(ch, "US-ASCII"); // soll StandardCharsets.US_ASCII werden + Reader r4 = Channels.newReader(ch, "UTF-16"); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Reader r5 = Channels.newReader(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Reader r6 = Channels.newReader(ch, "windows-1252"); // bleibt unverändert + Reader r7 = Channels.newReader(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Reader r8 = Channels.newReader(ch, StandardCharsets.UTF_8); + Reader r9 = Channels.newReader(ch, decoder, 1024); // mit CharsetDecoder und Buffergröße, bleibt unverändert + } +} +""", + +""" +package test1; + +import java.io.Reader; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; + +public class E1 { + private static final Charset UTF_16 = StandardCharsets.UTF_16; + private static final Charset US_ASCII = StandardCharsets.US_ASCII; + private static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; + private static final Charset UTF_8 = StandardCharsets.UTF_8; + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(ReadableByteChannel ch, CharsetDecoder decoder) { + // Fälle für StandardCharsets-Konstanten + Reader r1 = Channels.newReader(ch, E1.UTF_8); // soll StandardCharsets.UTF_8 werden + Reader r2 = Channels.newReader(ch, E1.ISO_8859_1); // soll StandardCharsets.ISO_8859_1 werden + Reader r3 = Channels.newReader(ch, E1.US_ASCII); // soll StandardCharsets.US_ASCII werden + Reader r4 = Channels.newReader(ch, E1.UTF_16); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Reader r5 = Channels.newReader(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Reader r6 = Channels.newReader(ch, "windows-1252"); // bleibt unverändert + Reader r7 = Channels.newReader(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Reader r8 = Channels.newReader(ch, StandardCharsets.UTF_8); + Reader r9 = Channels.newReader(ch, decoder, 1024); // mit CharsetDecoder und Buffergröße, bleibt unverändert + } +} +"""), + CHANNELSNEWWRITER( +""" +package test1; + +import java.io.Writer; +import java.nio.channels.WritableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(WritableByteChannel ch, Charset charset) { + // Fälle für StandardCharsets-Konstanten + Writer w1 = Channels.newWriter(ch, "UTF-8"); // soll StandardCharsets.UTF_8 werden + Writer w2 = Channels.newWriter(ch, "ISO-8859-1"); // soll StandardCharsets.ISO_8859_1 werden + Writer w3 = Channels.newWriter(ch, "US-ASCII"); // soll StandardCharsets.US_ASCII werden + Writer w4 = Channels.newWriter(ch, "UTF-16"); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Writer w5 = Channels.newWriter(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Writer w6 = Channels.newWriter(ch, "windows-1252"); // bleibt unverändert + Writer w7 = Channels.newWriter(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Writer w8 = Channels.newWriter(ch, StandardCharsets.UTF_8); + Writer w9 = Channels.newWriter(ch, charset); // unverändert, da `Charset` Instanz verwendet + } +} +""", + +""" +package test1; + +import java.io.Writer; +import java.nio.channels.WritableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; + +public class E1 { + private static final Charset UTF_16 = StandardCharsets.UTF_16; + private static final Charset US_ASCII = StandardCharsets.US_ASCII; + private static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; + private static final Charset UTF_8 = StandardCharsets.UTF_8; + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(WritableByteChannel ch, Charset charset) { + // Fälle für StandardCharsets-Konstanten + Writer w1 = Channels.newWriter(ch, E1.UTF_8); // soll StandardCharsets.UTF_8 werden + Writer w2 = Channels.newWriter(ch, E1.ISO_8859_1); // soll StandardCharsets.ISO_8859_1 werden + Writer w3 = Channels.newWriter(ch, E1.US_ASCII); // soll StandardCharsets.US_ASCII werden + Writer w4 = Channels.newWriter(ch, E1.UTF_16); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Writer w5 = Channels.newWriter(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Writer w6 = Channels.newWriter(ch, "windows-1252"); // bleibt unverändert + Writer w7 = Channels.newWriter(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Writer w8 = Channels.newWriter(ch, StandardCharsets.UTF_8); + Writer w9 = Channels.newWriter(ch, charset); // unverändert, da `Charset` Instanz verwendet + } +} +"""), + 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.nio.charset.StandardCharsets; +import java.io.BufferedWriter; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; + +public class E1 { + private static final Charset UTF_8 = StandardCharsets.UTF_8; + + void method(String filename) { + try { + Writer w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), E1.UTF_8)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } +} + """), + STRINGGETBYTES( +""" +package test1; + +import java.nio.charset.StandardCharsets; + +public class E1 { + + // Methode 1: Verwendung von StandardCharsets.UTF_8 statt "UTF-8" als String-Literal + void method(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes ohne Angabe der Kodierung (verwendet die Plattform-spezifische Standard-Kodierung) + byte[] bytes = s.getBytes(); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Methode 2: Behandlung von getBytes mit einer expliziten Kodierung + void method2(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes mit expliziter Kodierung (UTF-8 als String-Literal) + byte[] bytes = s.getBytes("UTF-8"); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Erweiterter Testfall: Verwendung von verschiedenen Kodierungen + void methodWithDifferentEncodings(String filename) { + String s = "asdf"; + + // Testen von gängigen Kodierungen + byte[] bytes1 = s.getBytes("ISO-8859-1"); // ISO-8859-1 + byte[] bytes2 = s.getBytes("US-ASCII"); // US-ASCII + byte[] bytes3 = s.getBytes(StandardCharsets.UTF_8); // UTF-8 mit StandardCharsets + byte[] bytes4 = s.getBytes("UTF-16"); // UTF-16 + + System.out.println(bytes1.length); // Ausgabe der Längen + System.out.println(bytes2.length); + System.out.println(bytes3.length); + System.out.println(bytes4.length); + } + + // Testfall: Verwendung von getBytes mit einer ungültigen Kodierung (sollte im Cleanup behandelt werden) + void methodWithInvalidEncoding(String filename) { + String s = "asdf"; + try { + // Ungültige Kodierung, die zu UnsupportedEncodingException führt + byte[] bytes = s.getBytes("non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + // Diese Ausnahme sollte im Cleanup berücksichtigt werden + e.printStackTrace(); + } + } + + // Testfall: Verwendung von getBytes mit einer durch Variable angegebenen Kodierung + void methodWithVariableEncoding(String filename) { + String s = "asdf"; + String encoding = "UTF-8"; // Kodierung als Variable + try { + byte[] bytes = s.getBytes(encoding); // Kodierung aus der Variablen + System.out.println(bytes.length); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } +} +""", + +""" +package test1; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + + private static final Charset UTF_16 = StandardCharsets.UTF_16; + private static final Charset US_ASCII = StandardCharsets.US_ASCII; + private static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; + private static final Charset UTF_8 = StandardCharsets.UTF_8; + + // Methode 1: Verwendung von StandardCharsets.UTF_8 statt "UTF-8" als String-Literal + void method(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes ohne Angabe der Kodierung (verwendet die Plattform-spezifische Standard-Kodierung) + byte[] bytes = s.getBytes(E1.UTF_8); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Methode 2: Behandlung von getBytes mit einer expliziten Kodierung + void method2(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes mit expliziter Kodierung (UTF-8 als String-Literal) + byte[] bytes = s.getBytes(E1.UTF_8); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Erweiterter Testfall: Verwendung von verschiedenen Kodierungen + void methodWithDifferentEncodings(String filename) { + String s = "asdf"; + + // Testen von gängigen Kodierungen + byte[] bytes1 = s.getBytes(E1.ISO_8859_1); // ISO-8859-1 + byte[] bytes2 = s.getBytes(E1.US_ASCII); // US-ASCII + byte[] bytes3 = s.getBytes(StandardCharsets.UTF_8); // UTF-8 mit StandardCharsets + byte[] bytes4 = s.getBytes(E1.UTF_16); // UTF-16 + + System.out.println(bytes1.length); // Ausgabe der Längen + System.out.println(bytes2.length); + System.out.println(bytes3.length); + System.out.println(bytes4.length); + } + + // Testfall: Verwendung von getBytes mit einer ungültigen Kodierung (sollte im Cleanup behandelt werden) + void methodWithInvalidEncoding(String filename) { + String s = "asdf"; + try { + // Ungültige Kodierung, die zu UnsupportedEncodingException führt + byte[] bytes = s.getBytes("non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + // Diese Ausnahme sollte im Cleanup berücksichtigt werden + e.printStackTrace(); + } + } + + // Testfall: Verwendung von getBytes mit einer durch Variable angegebenen Kodierung + void methodWithVariableEncoding(String filename) { + String s = "asdf"; + String encoding = "UTF-8"; // Kodierung als Variable + try { + byte[] bytes = s.getBytes(encoding); // Kodierung aus der Variablen + System.out.println(bytes.length); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } +} +"""), + STRING( +""" +package test1; + +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + + static void bla(String filename) throws FileNotFoundException, UnsupportedEncodingException { + byte[] b = {(byte) 59}; + + // Fälle mit String Encoding als "UTF-8" (soll durch StandardCharsets.UTF_8 ersetzt werden) + String s1 = new String(b, "UTF-8"); // "UTF-8" als String-Literal + String s2 = new String(b, 0, 1, "UTF-8"); // "UTF-8" als String-Literal + + // Fall mit ISO-8859-1 Encoding (soll durch StandardCharsets.ISO_8859_1 ersetzt werden) + String s3 = new String(b, "ISO-8859-1"); // "ISO-8859-1" als String-Literal + String s4 = new String(b, 0, 1, "ISO-8859-1"); // "ISO-8859-1" als String-Literal + + // Fall mit US-ASCII Encoding (soll durch StandardCharsets.US_ASCII ersetzt werden) + String s5 = new String(b, "US-ASCII"); // "US-ASCII" als String-Literal + String s6 = new String(b, 0, 1, "US-ASCII"); // "US-ASCII" als String-Literal + + // Fall mit UTF-16 Encoding (soll durch StandardCharsets.UTF_16 ersetzt werden) + String s7 = new String(b, "UTF-16"); // "UTF-16" als String-Literal + String s8 = new String(b, 0, 1, "UTF-16"); // "UTF-16" als String-Literal + + // Fall mit einer benutzerdefinierten Konstante für Encoding, bleibt unverändert + String s9 = new String(b, "UTF-8"); // bleibt unverändert + String s10 = new String(b, 0, 1, "UTF-8"); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets, bleiben unverändert + String s11 = new String(b, "windows-1252"); // bleibt unverändert + String s12 = new String(b, 0, 1, "windows-1252"); // bleibt unverändert + String s13 = new String(b, "Shift_JIS"); // bleibt unverändert + String s14 = new String(b, 0, 1, "Shift_JIS"); // bleibt unverändert + + // Fall mit Charset.forName() (wird unverändert bleiben, keine Ersetzung möglich) + Charset charset = Charset.forName("UTF-16"); + String s15 = new String(b, charset); // bleibt unverändert + String s16 = new String(b, 0, 1, charset); // bleibt unverändert + + // Fälle, die eine UnsupportedEncodingException werfen (werden im Cleanup angepasst) + try { + String s17 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + + try { + String s18 = new String(b, 0, 1, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Methodendeklaration mit throws für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithThrows(String filename) throws UnsupportedEncodingException { + byte[] b = {(byte) 59}; + String s1 = new String(b, "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Nach dem Cleanup sollte dies keine UnsupportedEncodingException mehr werfen + static void methodWithThrowsChange(String filename) throws FileNotFoundException { + byte[] b = {(byte) 59}; + String s1 = new String(b, "UTF-8"); // wirft keine UnsupportedEncodingException mehr + } + + // Methodendeklaration mit try-catch für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithCatch(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + static void methodWithCatchChange(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, "UTF-8"); // keine UnsupportedEncodingException + } catch (UnsupportedEncodingException e) { + // Dieser Block wird nicht mehr erreicht, da keine UnsupportedEncodingException mehr geworfen wird + e.printStackTrace(); + } + } +} +""", + +""" +package test1; + +import java.io.FileNotFoundException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + + private static final Charset UTF_16 = StandardCharsets.UTF_16; + private static final Charset US_ASCII = StandardCharsets.US_ASCII; + private static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; + private static final Charset UTF_8 = StandardCharsets.UTF_8; + + static void bla(String filename) throws FileNotFoundException { + byte[] b = {(byte) 59}; + + // Fälle mit String Encoding als "UTF-8" (soll durch StandardCharsets.UTF_8 ersetzt werden) + String s1 = new String(b, E1.UTF_8); // "UTF-8" als String-Literal + String s2 = new String(b, 0, 1, E1.UTF_8); // "UTF-8" als String-Literal + + // Fall mit ISO-8859-1 Encoding (soll durch StandardCharsets.ISO_8859_1 ersetzt werden) + String s3 = new String(b, E1.ISO_8859_1); // "ISO-8859-1" als String-Literal + String s4 = new String(b, 0, 1, E1.ISO_8859_1); // "ISO-8859-1" als String-Literal + + // Fall mit US-ASCII Encoding (soll durch StandardCharsets.US_ASCII ersetzt werden) + String s5 = new String(b, E1.US_ASCII); // "US-ASCII" als String-Literal + String s6 = new String(b, 0, 1, E1.US_ASCII); // "US-ASCII" als String-Literal + + // Fall mit UTF-16 Encoding (soll durch StandardCharsets.UTF_16 ersetzt werden) + String s7 = new String(b, E1.UTF_16); // "UTF-16" als String-Literal + String s8 = new String(b, 0, 1, E1.UTF_16); // "UTF-16" als String-Literal + + // Fall mit einer benutzerdefinierten Konstante für Encoding, bleibt unverändert + String s9 = new String(b, E1.UTF_8); // bleibt unverändert + String s10 = new String(b, 0, 1, E1.UTF_8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets, bleiben unverändert + String s11 = new String(b, "windows-1252"); // bleibt unverändert + String s12 = new String(b, 0, 1, "windows-1252"); // bleibt unverändert + String s13 = new String(b, "Shift_JIS"); // bleibt unverändert + String s14 = new String(b, 0, 1, "Shift_JIS"); // bleibt unverändert + + // Fall mit Charset.forName() (wird unverändert bleiben, keine Ersetzung möglich) + Charset charset = Charset.forName("UTF-16"); + String s15 = new String(b, charset); // bleibt unverändert + String s16 = new String(b, 0, 1, charset); // bleibt unverändert + + // Fälle, die eine UnsupportedEncodingException werfen (werden im Cleanup angepasst) + try { + String s17 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + + try { + String s18 = new String(b, 0, 1, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Methodendeklaration mit throws für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithThrows(String filename) throws UnsupportedEncodingException { + byte[] b = {(byte) 59}; + String s1 = new String(b, "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Nach dem Cleanup sollte dies keine UnsupportedEncodingException mehr werfen + static void methodWithThrowsChange(String filename) throws FileNotFoundException { + byte[] b = {(byte) 59}; + String s1 = new String(b, E1.UTF_8); // wirft keine UnsupportedEncodingException mehr + } + + // Methodendeklaration mit try-catch für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithCatch(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + static void methodWithCatchChange(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, E1.UTF_8); // keine UnsupportedEncodingException + } + } +} +"""), + PROPERTIESSTORETOXML( +""" +package test1; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Encoding-Variable + + // Fall 1: UTF-8 als String; Cleanup soll zu StandardCharsets.UTF_8 ändern + void storeWithTryWithResources() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "UTF-8"); + } + } + + // Fall 2: Benutzerdefiniertes Encoding als Variable; Cleanup soll diesen Fall unverändert lassen + void storeWithTryWithResourcesAndCustomEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", encodingVar); + } + } + + // Fall 3: Ungültiges Encoding als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithTryWithResourcesAndInvalidEncoding() { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } + } + + // Fall 4: FileOutputStream außerhalb des try-Blocks und UTF-8 als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithoutTryWithResources(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", "UTF-8"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unexpected UnsupportedEncodingException"); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 5: Gültiges Encoding ohne Konstante in StandardCharsets (windows-1252); Cleanup soll diesen Fall unverändert lassen + void storeWithWindows1252Encoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "windows-1252"); + } + } + + // Fall 6: Gültiges Encoding ohne Konstante in StandardCharsets (Shift_JIS); Cleanup soll diesen Fall unverändert lassen + void storeWithShiftJISEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "Shift_JIS"); + } + } + + // Fall 7: Ungültiges Encoding außerhalb von try-with-resources; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithInvalidEncodingOutsideTry(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 8: StandardCharsets.UTF_8 ohne try-with-resources; Cleanup soll diesen Fall unverändert lassen + void storeWithoutTryWithResourcesStandardCharsets(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } +} +""", + +""" +package test1; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +public class E1 { + private static final Charset UTF_8 = StandardCharsets.UTF_8; + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Encoding-Variable + + // Fall 1: UTF-8 als String; Cleanup soll zu StandardCharsets.UTF_8 ändern + void storeWithTryWithResources() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", E1.UTF_8); + } + } + + // Fall 2: Benutzerdefiniertes Encoding als Variable; Cleanup soll diesen Fall unverändert lassen + void storeWithTryWithResourcesAndCustomEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", encodingVar); + } + } + + // Fall 3: Ungültiges Encoding als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithTryWithResourcesAndInvalidEncoding() { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } + } + + // Fall 4: FileOutputStream außerhalb des try-Blocks und UTF-8 als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithoutTryWithResources(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", E1.UTF_8); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 5: Gültiges Encoding ohne Konstante in StandardCharsets (windows-1252); Cleanup soll diesen Fall unverändert lassen + void storeWithWindows1252Encoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "windows-1252"); + } + } + + // Fall 6: Gültiges Encoding ohne Konstante in StandardCharsets (Shift_JIS); Cleanup soll diesen Fall unverändert lassen + void storeWithShiftJISEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "Shift_JIS"); + } + } + + // Fall 7: Ungültiges Encoding außerhalb von try-with-resources; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithInvalidEncodingOutsideTry(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 8: StandardCharsets.UTF_8 ohne try-with-resources; Cleanup soll diesen Fall unverändert lassen + void storeWithoutTryWithResourcesStandardCharsets(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } +} +"""), + URLDECODER( +""" +package test1; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; + +public class E2 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Variable für Encoding + + // Methode ohne Encoding-Angabe, bleibt unverändert + static void decodeDefault() { + String url = URLDecoder.decode("example"); + } + + // Methode, die "UTF-8" als String verwendet und UnsupportedEncodingException wirft + static void decodeWithThrows() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", "UTF-8"); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void decodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Konstante für Encoding verwendet, bleibt unverändert + void decodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", ENCODING_UTF8); + } + + // Methode, die Encoding als Variable übergibt, bleibt unverändert + void decodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für ungültiges Encoding + static void decodeWithTryCatch() { + try { + String url = URLDecoder.decode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void decodeWithStandardCharset() { + String url = URLDecoder.decode("example", StandardCharsets.UTF_8); + } +} +""", +""" +package test1; + +import java.net.URLDecoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E2 { + private static final Charset UTF_8 = StandardCharsets.UTF_8; + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Variable für Encoding + + // Methode ohne Encoding-Angabe, bleibt unverändert + static void decodeDefault() { + String url = URLDecoder.decode("example", E2.UTF_8); + } + + // Methode, die "UTF-8" als String verwendet und UnsupportedEncodingException wirft + static void decodeWithThrows() { + String url = URLDecoder.decode("example", E2.UTF_8); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void decodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Konstante für Encoding verwendet, bleibt unverändert + void decodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", ENCODING_UTF8); + } + + // Methode, die Encoding als Variable übergibt, bleibt unverändert + void decodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für ungültiges Encoding + static void decodeWithTryCatch() { + try { + String url = URLDecoder.decode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void decodeWithStandardCharset() { + String url = URLDecoder.decode("example", StandardCharsets.UTF_8); + } +} +"""), + URLENCODER( +""" +package test1; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Kodierungskonstante + private String encodingVar = "ISO-8859-1"; // Variable für eine Kodierung + + // Methode ohne explizite Kodierung, bleibt unverändert + static void encodeDefault() { + String url = URLEncoder.encode("example"); + } + + // Methode, die "UTF-8" als String-Literal verwendet und `throws UnsupportedEncodingException` hat + static void encodeWithThrows() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", "UTF-8"); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void encodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Kodierungskonstante verwendet, bleibt unverändert + void encodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", ENCODING_UTF8); + } + + // Methode, die eine Kodierungsvariable verwendet, bleibt unverändert + void encodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für eine ungültige Kodierung + static void encodeWithTryCatch() { + try { + String url = URLEncoder.encode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void encodeWithStandardCharset() { + String url = URLEncoder.encode("example", StandardCharsets.UTF_8); + } +} +""", +""" +package test1; + +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final Charset UTF_8 = StandardCharsets.UTF_8; + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Kodierungskonstante + private String encodingVar = "ISO-8859-1"; // Variable für eine Kodierung + + // Methode ohne explizite Kodierung, bleibt unverändert + static void encodeDefault() { + String url = URLEncoder.encode("example", E1.UTF_8); + } + + // Methode, die "UTF-8" als String-Literal verwendet und `throws UnsupportedEncodingException` hat + static void encodeWithThrows() { + String url = URLEncoder.encode("example", E1.UTF_8); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void encodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Kodierungskonstante verwendet, bleibt unverändert + void encodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", ENCODING_UTF8); + } + + // Methode, die eine Kodierungsvariable verwendet, bleibt unverändert + void encodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für eine ungültige Kodierung + static void encodeWithTryCatch() { + try { + String url = URLEncoder.encode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void encodeWithStandardCharset() { + String url = URLEncoder.encode("example", StandardCharsets.UTF_8); + } +} +"""), + SCANNER( +""" +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +public class E1 { + + // Methode mit File und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla3(File file) throws FileNotFoundException { + // Konstruktor mit String-Encoding, sollte durch StandardCharsets.UTF_8 ersetzt werden + Scanner s = new Scanner(file, "UTF-8"); + } + + // Methode mit InputStream und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla4(InputStream is) throws FileNotFoundException { + Scanner s2 = new Scanner(is, "UTF-8"); + } + + // Methode mit Scanner, aber ohne explizites Encoding, bleibt unverändert + static void bla5() { + Scanner s3 = new Scanner("asdf"); + } + + // Methode, die eine benutzerdefinierte Konstante für die Kodierung verwendet (bleibt unverändert) + private static final String ENCODING_UTF8 = "UTF-8"; + static void bla6(File file) throws FileNotFoundException { + Scanner s = new Scanner(file, ENCODING_UTF8); + } + + // Methode mit einer ungültigen Kodierung (muss UnsupportedEncodingException werfen) + static void bla7(File file) throws FileNotFoundException { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (Exception e) { + e.printStackTrace(); // Catch block für UnsupportedEncodingException + } + } + + // Methode mit Scanner und ungültiger Kodierung, die `throws UnsupportedEncodingException` wirft + static void bla8(InputStream is) throws FileNotFoundException, UnsupportedEncodingException { + Scanner s = new Scanner(is, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet (für FileNotFoundException) + static void bla9(File file) { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (Exception e) { + // UnsupportedEncodingException wird hier abgefangen + e.printStackTrace(); + } + } + + // Beispiel mit StandardCharsets-Konstanten, die keine Änderung brauchen + static void bla10(File file) { + Scanner s = new Scanner(file, StandardCharsets.UTF_8); + } + + // Beispiel mit Scanner und InputStream, ohne explizite Kodierung (bleibt unverändert) + static void bla11(InputStream is) { + Scanner s = new Scanner(is); + } + + // Methode mit Scanner und einer benutzerdefinierten Kodierung als Variable (bleibt unverändert) + private String encodingVar = "ISO-8859-1"; + static void bla12(InputStream is) throws FileNotFoundException { + Scanner s = new Scanner(is, "ISO-8859-1"); + } +} +""", +""" +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +public class E1 { + + private static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; + private static final Charset UTF_8 = StandardCharsets.UTF_8; + // Methode mit File und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla3(File file) throws FileNotFoundException { + // Konstruktor mit String-Encoding, sollte durch StandardCharsets.UTF_8 ersetzt werden + Scanner s = new Scanner(file, E1.UTF_8); + } + + // Methode mit InputStream und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla4(InputStream is) throws FileNotFoundException { + Scanner s2 = new Scanner(is, E1.UTF_8); + } + + // Methode mit Scanner, aber ohne explizites Encoding, bleibt unverändert + static void bla5() { + Scanner s3 = new Scanner("asdf", E1.UTF_8); + } + + // Methode, die eine benutzerdefinierte Konstante für die Kodierung verwendet (bleibt unverändert) + private static final String ENCODING_UTF8 = "UTF-8"; + static void bla6(File file) throws FileNotFoundException { + Scanner s = new Scanner(file, ENCODING_UTF8); + } + + // Methode mit einer ungültigen Kodierung (muss UnsupportedEncodingException werfen) + static void bla7(File file) throws FileNotFoundException { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (Exception e) { + e.printStackTrace(); // Catch block für UnsupportedEncodingException + } + } + + // Methode mit Scanner und ungültiger Kodierung, die `throws UnsupportedEncodingException` wirft + static void bla8(InputStream is) throws FileNotFoundException, UnsupportedEncodingException { + Scanner s = new Scanner(is, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet (für FileNotFoundException) + static void bla9(File file) { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (Exception e) { + // UnsupportedEncodingException wird hier abgefangen + e.printStackTrace(); + } + } + + // Beispiel mit StandardCharsets-Konstanten, die keine Änderung brauchen + static void bla10(File file) { + Scanner s = new Scanner(file, StandardCharsets.UTF_8); + } + + // Beispiel mit Scanner und InputStream, ohne explizite Kodierung (bleibt unverändert) + static void bla11(InputStream is) { + Scanner s = new Scanner(is, E1.UTF_8); + } + + // Methode mit Scanner und einer benutzerdefinierten Kodierung als Variable (bleibt unverändert) + private String encodingVar = "ISO-8859-1"; + static void bla12(InputStream is) throws FileNotFoundException { + Scanner s = new Scanner(is, E1.ISO_8859_1); + } +} +"""), + FORMATTER( +""" +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.Formatter; + +public class E1 { + + // Methode mit explizitem UTF-8, sollte durch StandardCharsets.UTF_8 ersetzt werden + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s = new Formatter(new File("asdf"), "UTF-8"); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } + + // Methode mit try-catch, die eine Kodierung verwendet und Fehler wirft + static void bli() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "UTF-8"); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } catch (FileNotFoundException | UnsupportedEncodingException e) { + // Der Catch-Block für UnsupportedEncodingException sollte im Cleanup entfernt werden + e.printStackTrace(); + } + } + + // Methode mit benutzerdefinierter Konstante für das Encoding + private static final String ENCODING_UTF8 = "UTF-8"; + + static void blc() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s = new Formatter(new File("asdf"), ENCODING_UTF8); // 'UTF-8' als Konstante + } + + // Methode mit einer ungültigen Kodierung (z.B. 'non-existing-encoding') + static void bld() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); // UnsupportedEncodingException wird hier erwartet + } + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet + static void ble() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird hier behandelt + e.printStackTrace(); + } + } + + // Methode mit StandardCharsets.UTF_8 + static void blf() throws FileNotFoundException { + Formatter s = new Formatter(new File("asdf"), StandardCharsets.UTF_8); // Verwendung von StandardCharsets.UTF_8 + } + + // Beispiel, bei dem das Encoding in einer Variablen gespeichert ist + private String encodingVar = "UTF-8"; + + static void blg() throws FileNotFoundException { + String encoding = "UTF-8"; + Formatter s = new Formatter(new File("asdf"), encoding); // encoding als Variable + } +} +""", """ +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Formatter; + +public class E1 { + + private static final Charset UTF_8 = StandardCharsets.UTF_8; + + // Methode mit explizitem UTF-8, sollte durch StandardCharsets.UTF_8 ersetzt werden + static void bla() throws FileNotFoundException { + Formatter s = new Formatter(new File("asdf"), E1.UTF_8); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } + + // Methode mit try-catch, die eine Kodierung verwendet und Fehler wirft + static void bli() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), E1.UTF_8); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } catch (FileNotFoundException e) { + // Der Catch-Block für UnsupportedEncodingException sollte im Cleanup entfernt werden + e.printStackTrace(); + } + } + + // Methode mit benutzerdefinierter Konstante für das Encoding + private static final String ENCODING_UTF8 = "UTF-8"; + + static void blc() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s = new Formatter(new File("asdf"), ENCODING_UTF8); // 'UTF-8' als Konstante + } + + // Methode mit einer ungültigen Kodierung (z.B. 'non-existing-encoding') + static void bld() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); // UnsupportedEncodingException wird hier erwartet + } + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet + static void ble() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird hier behandelt + e.printStackTrace(); + } + } + + // Methode mit StandardCharsets.UTF_8 + static void blf() throws FileNotFoundException { + Formatter s = new Formatter(new File("asdf"), StandardCharsets.UTF_8); // Verwendung von StandardCharsets.UTF_8 + } + + // Beispiel, bei dem das Encoding in einer Variablen gespeichert ist + private String encodingVar = "UTF-8"; + + static void blg() throws FileNotFoundException { + String encoding = "UTF-8"; + Formatter s = new Formatter(new File("asdf"), encoding); // encoding als Variable + } +} +"""), + 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.nio.charset.StandardCharsets; +import java.io.FileNotFoundException; + +public class E1 { + private static final Charset UTF_8 = StandardCharsets.UTF_8; + + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(E1.UTF_8); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(E1.UTF_8); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), E1.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), E1.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), E1.UTF_8); + } 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 { + private static final Charset UTF_8 = StandardCharsets.UTF_8; + + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes(E1.UTF_8); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(E1.UTF_8); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), E1.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), E1.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), E1.UTF_8); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } +} + """); + + String given; + String expected; + + ExplicitEncodingPatternsAggregateUTF8(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/Java10/ExplicitEncodingPatternsKeepBehavior.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsKeepBehavior.java new file mode 100644 index 00000000000..d37cc2b8cf0 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsKeepBehavior.java @@ -0,0 +1,1874 @@ +/******************************************************************************* + * Copyright (c) 2024 Carsten Hammer and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * + *******************************************************************************/ +package org.eclipse.jdt.ui.tests.quickfix.Java10; + +public enum ExplicitEncodingPatternsKeepBehavior { + + CHARSET(""" +package test1; + +import java.nio.charset.Charset; + +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; + +import java.nio.charset.Charset; + +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; + + 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.FileNotFoundException; +import java.io.UnsupportedEncodingException; + +public class E1 { + + void method(String filename) { + try { + // Standardkonstruktor ohne Encoding + InputStreamReader is1 = new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$ + + // String Literal Encodings, die nach StandardCharsets umgeschrieben werden sollten + InputStreamReader is2 = new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is3 = new InputStreamReader(new FileInputStream("file3.txt"), "ISO-8859-1"); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is4 = new InputStreamReader(new FileInputStream("file4.txt"), "US-ASCII"); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basiertes Encoding, das in Charset umgeschrieben werden kann, jedoch ohne vordefinierte Konstante + InputStreamReader is5 = new InputStreamReader(new FileInputStream("file5.txt"), "UTF-16"); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basierte Encodings mit Groß-/Kleinschreibungsvarianten + InputStreamReader is6 = new InputStreamReader(new FileInputStream("file6.txt"), "utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is7 = new InputStreamReader(new FileInputStream("file7.txt"), "Utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); // Sollte nach Cleanup entfernt werden + } + } + + void methodWithTryCatch(String filename) { + try { + // Variante, bei der UnsupportedEncodingException behandelt wird + InputStreamReader is8 = new InputStreamReader(new FileInputStream("file8.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); // Sollte nach Cleanup entfernt werden + } + } + + void methodWithoutException(String filename) throws UnsupportedEncodingException, FileNotFoundException { + // Case ohne Try-Catch-Block, sollte Charset-Konstanten direkt ersetzen + InputStreamReader is9 = new InputStreamReader(new FileInputStream("file9.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + } + + void methodWithVariableEncoding(String filename) throws UnsupportedEncodingException, FileNotFoundException { + // Case, bei dem das Encoding aus einer Variablen kommt, Cleanup sollte hier keine Änderungen machen + String encoding = "UTF-8"; //$NON-NLS-1$ + InputStreamReader is10 = new InputStreamReader(new FileInputStream("file10.txt"), encoding); //$NON-NLS-1$ + } + + void methodWithNonStandardEncoding(String filename) { + try { + // Case mit nicht vordefiniertem Charset, sollte keine Umwandlung in StandardCharsets erfolgen + InputStreamReader is11 = new InputStreamReader(new FileInputStream("file11.txt"), "windows-1252"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + // Methode mit "throws UnsupportedEncodingException" zur Prüfung des Cleanups + void methodWithThrows(String filename) throws FileNotFoundException, UnsupportedEncodingException { + InputStreamReader is3 = new InputStreamReader(new FileInputStream(filename), "UTF-8"); //$NON-NLS-1$ + } +} +""", + +""" +package test1; + +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +public class E1 { + + void method(String filename) { + try { + // Standardkonstruktor ohne Encoding + InputStreamReader is1 = new InputStreamReader(new FileInputStream("file1.txt"), Charset.defaultCharset()); //$NON-NLS-1$ + + // String Literal Encodings, die nach StandardCharsets umgeschrieben werden sollten + InputStreamReader is2 = new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is3 = new InputStreamReader(new FileInputStream("file3.txt"), StandardCharsets.ISO_8859_1); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is4 = new InputStreamReader(new FileInputStream("file4.txt"), StandardCharsets.US_ASCII); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basiertes Encoding, das in Charset umgeschrieben werden kann, jedoch ohne vordefinierte Konstante + InputStreamReader is5 = new InputStreamReader(new FileInputStream("file5.txt"), StandardCharsets.UTF_16); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basierte Encodings mit Groß-/Kleinschreibungsvarianten + InputStreamReader is6 = new InputStreamReader(new FileInputStream("file6.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is7 = new InputStreamReader(new FileInputStream("file7.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + void methodWithTryCatch(String filename) { + try { + // Variante, bei der UnsupportedEncodingException behandelt wird + InputStreamReader is8 = new InputStreamReader(new FileInputStream("file8.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + void methodWithoutException(String filename) throws FileNotFoundException { + // Case ohne Try-Catch-Block, sollte Charset-Konstanten direkt ersetzen + InputStreamReader is9 = new InputStreamReader(new FileInputStream("file9.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + } + + void methodWithVariableEncoding(String filename) throws UnsupportedEncodingException, FileNotFoundException { + // Case, bei dem das Encoding aus einer Variablen kommt, Cleanup sollte hier keine Änderungen machen + String encoding = "UTF-8"; //$NON-NLS-1$ + InputStreamReader is10 = new InputStreamReader(new FileInputStream("file10.txt"), encoding); //$NON-NLS-1$ + } + + void methodWithNonStandardEncoding(String filename) { + try { + // Case mit nicht vordefiniertem Charset, sollte keine Umwandlung in StandardCharsets erfolgen + InputStreamReader is11 = new InputStreamReader(new FileInputStream("file11.txt"), "windows-1252"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + // Methode mit "throws UnsupportedEncodingException" zur Prüfung des Cleanups + void methodWithThrows(String filename) throws FileNotFoundException { + InputStreamReader is3 = new InputStreamReader(new FileInputStream(filename), StandardCharsets.UTF_8); //$NON-NLS-1$ + } +} +"""), + OUTPUTSTREAMWRITER( +""" +package test1; + +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(String filename) { + try { + // Standard-Konstruktor ohne Encoding + OutputStreamWriter os1 = new OutputStreamWriter(new FileOutputStream(filename)); + + // Konstruktor mit String-Encoding (UTF-8) -> muss durch StandardCharsets.UTF_8 ersetzt werden + OutputStreamWriter os2 = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"); // "UTF-8" als String-Literal + + // Konstruktor mit String-Encoding (ISO-8859-1) -> muss durch StandardCharsets.ISO_8859_1 ersetzt werden + OutputStreamWriter os3 = new OutputStreamWriter(new FileOutputStream(filename), "ISO-8859-1"); // "ISO-8859-1" als String-Literal + + // Konstruktor mit String-Encoding (US-ASCII) -> muss durch StandardCharsets.US_ASCII ersetzt werden + OutputStreamWriter os4 = new OutputStreamWriter(new FileOutputStream(filename), "US-ASCII"); // "US-ASCII" als String-Literal + + // Konstruktor mit String-Encoding (UTF-16) -> muss durch StandardCharsets.UTF_16 ersetzt werden + OutputStreamWriter os5 = new OutputStreamWriter(new FileOutputStream(filename), "UTF-16"); // "UTF-16" als String-Literal + + // Der Konstruktor mit einer benutzerdefinierten Konstante bleibt unverändert + OutputStreamWriter os6 = new OutputStreamWriter(new FileOutputStream(filename), ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (bleiben unverändert) + OutputStreamWriter os7 = new OutputStreamWriter(new FileOutputStream(filename), "windows-1252"); // bleibt unverändert + OutputStreamWriter os8 = new OutputStreamWriter(new FileOutputStream(filename), "Shift_JIS"); // bleibt unverändert + + // Hier wird `UnsupportedEncodingException` geworfen (vor dem Cleanup) + OutputStreamWriter os9 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + + // Aufruf mit einer ungültigen Zeichenkodierung und catch für UnsupportedEncodingException + try { + OutputStreamWriter os10 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + + // Beispiele mit StandardCharsets-Konstanten, die unverändert bleiben + OutputStreamWriter os11 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // bleibt unverändert + OutputStreamWriter os12 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.ISO_8859_1); // bleibt unverändert + OutputStreamWriter os13 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.US_ASCII); // bleibt unverändert + OutputStreamWriter os14 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); // bleibt unverändert + + // Beispiel mit Charset.forName und einer Konstanten, die als Parameter übergeben wird (bleibt unverändert) + OutputStreamWriter os15 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); + } catch (FileNotFoundException e) { + // Datei nicht gefunden + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + } + + // Methodendeklaration, die `UnsupportedEncodingException` wirft (und durch den Cleanup angepasst wird) + void methodWithThrows(String filename) throws UnsupportedEncodingException { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Neue Methode: methodWithThrowsChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr geworfen + void methodWithThrowsChange(String filename) throws FileNotFoundException { + // Nach dem Cleanup, der String "UTF-8" wird zu einer StandardCharset-Konstanten geändert + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"); // wirft keine UnsupportedEncodingException mehr + } + + // Methode mit einem try-catch, um die UnsupportedEncodingException zu behandeln (und durch den Cleanup angepasst wird) + void methodWithCatch(String filename) { + try { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + } + + // Neue Methode: methodWithCatchChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + void methodWithCatchChange(String filename) { + try { + // Nach dem Cleanup wird "UTF-8" ersetzt + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"); // keine UnsupportedEncodingException + } catch (UnsupportedEncodingException e) { + // Dieser Block wird nicht mehr erreicht, da keine UnsupportedEncodingException mehr geworfen wird + e.printStackTrace(); + } + } +} +""", + +""" +package test1; + +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.FileNotFoundException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(String filename) { + try { + // Standard-Konstruktor ohne Encoding + OutputStreamWriter os1 = new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset()); + + // Konstruktor mit String-Encoding (UTF-8) -> muss durch StandardCharsets.UTF_8 ersetzt werden + OutputStreamWriter os2 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // "UTF-8" als String-Literal + + // Konstruktor mit String-Encoding (ISO-8859-1) -> muss durch StandardCharsets.ISO_8859_1 ersetzt werden + OutputStreamWriter os3 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.ISO_8859_1); // "ISO-8859-1" als String-Literal + + // Konstruktor mit String-Encoding (US-ASCII) -> muss durch StandardCharsets.US_ASCII ersetzt werden + OutputStreamWriter os4 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.US_ASCII); // "US-ASCII" als String-Literal + + // Konstruktor mit String-Encoding (UTF-16) -> muss durch StandardCharsets.UTF_16 ersetzt werden + OutputStreamWriter os5 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); // "UTF-16" als String-Literal + + // Der Konstruktor mit einer benutzerdefinierten Konstante bleibt unverändert + OutputStreamWriter os6 = new OutputStreamWriter(new FileOutputStream(filename), ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (bleiben unverändert) + OutputStreamWriter os7 = new OutputStreamWriter(new FileOutputStream(filename), "windows-1252"); // bleibt unverändert + OutputStreamWriter os8 = new OutputStreamWriter(new FileOutputStream(filename), "Shift_JIS"); // bleibt unverändert + + // Hier wird `UnsupportedEncodingException` geworfen (vor dem Cleanup) + OutputStreamWriter os9 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + + // Aufruf mit einer ungültigen Zeichenkodierung und catch für UnsupportedEncodingException + try { + OutputStreamWriter os10 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + + // Beispiele mit StandardCharsets-Konstanten, die unverändert bleiben + OutputStreamWriter os11 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // bleibt unverändert + OutputStreamWriter os12 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.ISO_8859_1); // bleibt unverändert + OutputStreamWriter os13 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.US_ASCII); // bleibt unverändert + OutputStreamWriter os14 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); // bleibt unverändert + + // Beispiel mit Charset.forName und einer Konstanten, die als Parameter übergeben wird (bleibt unverändert) + OutputStreamWriter os15 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); + } catch (FileNotFoundException e) { + // Datei nicht gefunden + e.printStackTrace(); + } + } + + // Methodendeklaration, die `UnsupportedEncodingException` wirft (und durch den Cleanup angepasst wird) + void methodWithThrows(String filename) throws UnsupportedEncodingException { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Neue Methode: methodWithThrowsChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr geworfen + void methodWithThrowsChange(String filename) throws FileNotFoundException { + // Nach dem Cleanup, der String "UTF-8" wird zu einer StandardCharset-Konstanten geändert + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // wirft keine UnsupportedEncodingException mehr + } + + // Methode mit einem try-catch, um die UnsupportedEncodingException zu behandeln (und durch den Cleanup angepasst wird) + void methodWithCatch(String filename) { + try { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + } + + // Neue Methode: methodWithCatchChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + void methodWithCatchChange(String filename) { + try { + // Nach dem Cleanup wird "UTF-8" ersetzt + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // keine UnsupportedEncodingException + } + } +} +"""), + CHANNELSNEWREADER( +""" +package test1; + +import java.io.Reader; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.CharsetDecoder; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(ReadableByteChannel ch, CharsetDecoder decoder) { + // Fälle für StandardCharsets-Konstanten + Reader r1 = Channels.newReader(ch, "UTF-8"); // soll StandardCharsets.UTF_8 werden + Reader r2 = Channels.newReader(ch, "ISO-8859-1"); // soll StandardCharsets.ISO_8859_1 werden + Reader r3 = Channels.newReader(ch, "US-ASCII"); // soll StandardCharsets.US_ASCII werden + Reader r4 = Channels.newReader(ch, "UTF-16"); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Reader r5 = Channels.newReader(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Reader r6 = Channels.newReader(ch, "windows-1252"); // bleibt unverändert + Reader r7 = Channels.newReader(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Reader r8 = Channels.newReader(ch, StandardCharsets.UTF_8); + Reader r9 = Channels.newReader(ch, decoder, 1024); // mit CharsetDecoder und Buffergröße, bleibt unverändert + } +} +""", + +""" +package test1; + +import java.io.Reader; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.CharsetDecoder; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(ReadableByteChannel ch, CharsetDecoder decoder) { + // Fälle für StandardCharsets-Konstanten + Reader r1 = Channels.newReader(ch, StandardCharsets.UTF_8); // soll StandardCharsets.UTF_8 werden + Reader r2 = Channels.newReader(ch, StandardCharsets.ISO_8859_1); // soll StandardCharsets.ISO_8859_1 werden + Reader r3 = Channels.newReader(ch, StandardCharsets.US_ASCII); // soll StandardCharsets.US_ASCII werden + Reader r4 = Channels.newReader(ch, StandardCharsets.UTF_16); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Reader r5 = Channels.newReader(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Reader r6 = Channels.newReader(ch, "windows-1252"); // bleibt unverändert + Reader r7 = Channels.newReader(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Reader r8 = Channels.newReader(ch, StandardCharsets.UTF_8); + Reader r9 = Channels.newReader(ch, decoder, 1024); // mit CharsetDecoder und Buffergröße, bleibt unverändert + } +} +"""), + CHANNELSNEWWRITER( +""" +package test1; + +import java.io.Writer; +import java.nio.channels.WritableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(WritableByteChannel ch, Charset charset) { + // Fälle für StandardCharsets-Konstanten + Writer w1 = Channels.newWriter(ch, "UTF-8"); // soll StandardCharsets.UTF_8 werden + Writer w2 = Channels.newWriter(ch, "ISO-8859-1"); // soll StandardCharsets.ISO_8859_1 werden + Writer w3 = Channels.newWriter(ch, "US-ASCII"); // soll StandardCharsets.US_ASCII werden + Writer w4 = Channels.newWriter(ch, "UTF-16"); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Writer w5 = Channels.newWriter(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Writer w6 = Channels.newWriter(ch, "windows-1252"); // bleibt unverändert + Writer w7 = Channels.newWriter(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Writer w8 = Channels.newWriter(ch, StandardCharsets.UTF_8); + Writer w9 = Channels.newWriter(ch, charset); // unverändert, da `Charset` Instanz verwendet + } +} +""", + +""" +package test1; + +import java.io.Writer; +import java.nio.channels.WritableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(WritableByteChannel ch, Charset charset) { + // Fälle für StandardCharsets-Konstanten + Writer w1 = Channels.newWriter(ch, StandardCharsets.UTF_8); // soll StandardCharsets.UTF_8 werden + Writer w2 = Channels.newWriter(ch, StandardCharsets.ISO_8859_1); // soll StandardCharsets.ISO_8859_1 werden + Writer w3 = Channels.newWriter(ch, StandardCharsets.US_ASCII); // soll StandardCharsets.US_ASCII werden + Writer w4 = Channels.newWriter(ch, StandardCharsets.UTF_16); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Writer w5 = Channels.newWriter(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Writer w6 = Channels.newWriter(ch, "windows-1252"); // bleibt unverändert + Writer w7 = Channels.newWriter(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Writer w8 = Channels.newWriter(ch, StandardCharsets.UTF_8); + Writer w9 = Channels.newWriter(ch, charset); // unverändert, da `Charset` Instanz verwendet + } +} +"""), + 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.nio.charset.StandardCharsets; + +public class E1 { + + // Methode 1: Verwendung von StandardCharsets.UTF_8 statt "UTF-8" als String-Literal + void method(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes ohne Angabe der Kodierung (verwendet die Plattform-spezifische Standard-Kodierung) + byte[] bytes = s.getBytes(); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Methode 2: Behandlung von getBytes mit einer expliziten Kodierung + void method2(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes mit expliziter Kodierung (UTF-8 als String-Literal) + byte[] bytes = s.getBytes("UTF-8"); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Erweiterter Testfall: Verwendung von verschiedenen Kodierungen + void methodWithDifferentEncodings(String filename) { + String s = "asdf"; + + // Testen von gängigen Kodierungen + byte[] bytes1 = s.getBytes("ISO-8859-1"); // ISO-8859-1 + byte[] bytes2 = s.getBytes("US-ASCII"); // US-ASCII + byte[] bytes3 = s.getBytes(StandardCharsets.UTF_8); // UTF-8 mit StandardCharsets + byte[] bytes4 = s.getBytes("UTF-16"); // UTF-16 + + System.out.println(bytes1.length); // Ausgabe der Längen + System.out.println(bytes2.length); + System.out.println(bytes3.length); + System.out.println(bytes4.length); + } + + // Testfall: Verwendung von getBytes mit einer ungültigen Kodierung (sollte im Cleanup behandelt werden) + void methodWithInvalidEncoding(String filename) { + String s = "asdf"; + try { + // Ungültige Kodierung, die zu UnsupportedEncodingException führt + byte[] bytes = s.getBytes("non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + // Diese Ausnahme sollte im Cleanup berücksichtigt werden + e.printStackTrace(); + } + } + + // Testfall: Verwendung von getBytes mit einer durch Variable angegebenen Kodierung + void methodWithVariableEncoding(String filename) { + String s = "asdf"; + String encoding = "UTF-8"; // Kodierung als Variable + try { + byte[] bytes = s.getBytes(encoding); // Kodierung aus der Variablen + System.out.println(bytes.length); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } +} +""", + +""" +package test1; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + + // Methode 1: Verwendung von StandardCharsets.UTF_8 statt "UTF-8" als String-Literal + void method(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes ohne Angabe der Kodierung (verwendet die Plattform-spezifische Standard-Kodierung) + byte[] bytes = s.getBytes(Charset.defaultCharset()); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Methode 2: Behandlung von getBytes mit einer expliziten Kodierung + void method2(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes mit expliziter Kodierung (UTF-8 als String-Literal) + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Erweiterter Testfall: Verwendung von verschiedenen Kodierungen + void methodWithDifferentEncodings(String filename) { + String s = "asdf"; + + // Testen von gängigen Kodierungen + byte[] bytes1 = s.getBytes(StandardCharsets.ISO_8859_1); // ISO-8859-1 + byte[] bytes2 = s.getBytes(StandardCharsets.US_ASCII); // US-ASCII + byte[] bytes3 = s.getBytes(StandardCharsets.UTF_8); // UTF-8 mit StandardCharsets + byte[] bytes4 = s.getBytes(StandardCharsets.UTF_16); // UTF-16 + + System.out.println(bytes1.length); // Ausgabe der Längen + System.out.println(bytes2.length); + System.out.println(bytes3.length); + System.out.println(bytes4.length); + } + + // Testfall: Verwendung von getBytes mit einer ungültigen Kodierung (sollte im Cleanup behandelt werden) + void methodWithInvalidEncoding(String filename) { + String s = "asdf"; + try { + // Ungültige Kodierung, die zu UnsupportedEncodingException führt + byte[] bytes = s.getBytes("non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + // Diese Ausnahme sollte im Cleanup berücksichtigt werden + e.printStackTrace(); + } + } + + // Testfall: Verwendung von getBytes mit einer durch Variable angegebenen Kodierung + void methodWithVariableEncoding(String filename) { + String s = "asdf"; + String encoding = "UTF-8"; // Kodierung als Variable + try { + byte[] bytes = s.getBytes(encoding); // Kodierung aus der Variablen + System.out.println(bytes.length); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } +} +"""), + STRING( +""" +package test1; + +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + + static void bla(String filename) throws FileNotFoundException, UnsupportedEncodingException { + byte[] b = {(byte) 59}; + + // Fälle mit String Encoding als "UTF-8" (soll durch StandardCharsets.UTF_8 ersetzt werden) + String s1 = new String(b, "UTF-8"); // "UTF-8" als String-Literal + String s2 = new String(b, 0, 1, "UTF-8"); // "UTF-8" als String-Literal + + // Fall mit ISO-8859-1 Encoding (soll durch StandardCharsets.ISO_8859_1 ersetzt werden) + String s3 = new String(b, "ISO-8859-1"); // "ISO-8859-1" als String-Literal + String s4 = new String(b, 0, 1, "ISO-8859-1"); // "ISO-8859-1" als String-Literal + + // Fall mit US-ASCII Encoding (soll durch StandardCharsets.US_ASCII ersetzt werden) + String s5 = new String(b, "US-ASCII"); // "US-ASCII" als String-Literal + String s6 = new String(b, 0, 1, "US-ASCII"); // "US-ASCII" als String-Literal + + // Fall mit UTF-16 Encoding (soll durch StandardCharsets.UTF_16 ersetzt werden) + String s7 = new String(b, "UTF-16"); // "UTF-16" als String-Literal + String s8 = new String(b, 0, 1, "UTF-16"); // "UTF-16" als String-Literal + + // Fall mit einer benutzerdefinierten Konstante für Encoding, bleibt unverändert + String s9 = new String(b, "UTF-8"); // bleibt unverändert + String s10 = new String(b, 0, 1, "UTF-8"); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets, bleiben unverändert + String s11 = new String(b, "windows-1252"); // bleibt unverändert + String s12 = new String(b, 0, 1, "windows-1252"); // bleibt unverändert + String s13 = new String(b, "Shift_JIS"); // bleibt unverändert + String s14 = new String(b, 0, 1, "Shift_JIS"); // bleibt unverändert + + // Fall mit Charset.forName() (wird unverändert bleiben, keine Ersetzung möglich) + Charset charset = Charset.forName("UTF-16"); + String s15 = new String(b, charset); // bleibt unverändert + String s16 = new String(b, 0, 1, charset); // bleibt unverändert + + // Fälle, die eine UnsupportedEncodingException werfen (werden im Cleanup angepasst) + try { + String s17 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + + try { + String s18 = new String(b, 0, 1, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Methodendeklaration mit throws für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithThrows(String filename) throws UnsupportedEncodingException { + byte[] b = {(byte) 59}; + String s1 = new String(b, "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Nach dem Cleanup sollte dies keine UnsupportedEncodingException mehr werfen + static void methodWithThrowsChange(String filename) throws FileNotFoundException { + byte[] b = {(byte) 59}; + String s1 = new String(b, "UTF-8"); // wirft keine UnsupportedEncodingException mehr + } + + // Methodendeklaration mit try-catch für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithCatch(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + static void methodWithCatchChange(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, "UTF-8"); // keine UnsupportedEncodingException + } catch (UnsupportedEncodingException e) { + // Dieser Block wird nicht mehr erreicht, da keine UnsupportedEncodingException mehr geworfen wird + e.printStackTrace(); + } + } +} +""", + +""" +package test1; + +import java.io.FileNotFoundException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + + static void bla(String filename) throws FileNotFoundException { + byte[] b = {(byte) 59}; + + // Fälle mit String Encoding als "UTF-8" (soll durch StandardCharsets.UTF_8 ersetzt werden) + String s1 = new String(b, StandardCharsets.UTF_8); // "UTF-8" als String-Literal + String s2 = new String(b, 0, 1, StandardCharsets.UTF_8); // "UTF-8" als String-Literal + + // Fall mit ISO-8859-1 Encoding (soll durch StandardCharsets.ISO_8859_1 ersetzt werden) + String s3 = new String(b, StandardCharsets.ISO_8859_1); // "ISO-8859-1" als String-Literal + String s4 = new String(b, 0, 1, StandardCharsets.ISO_8859_1); // "ISO-8859-1" als String-Literal + + // Fall mit US-ASCII Encoding (soll durch StandardCharsets.US_ASCII ersetzt werden) + String s5 = new String(b, StandardCharsets.US_ASCII); // "US-ASCII" als String-Literal + String s6 = new String(b, 0, 1, StandardCharsets.US_ASCII); // "US-ASCII" als String-Literal + + // Fall mit UTF-16 Encoding (soll durch StandardCharsets.UTF_16 ersetzt werden) + String s7 = new String(b, StandardCharsets.UTF_16); // "UTF-16" als String-Literal + String s8 = new String(b, 0, 1, StandardCharsets.UTF_16); // "UTF-16" als String-Literal + + // Fall mit einer benutzerdefinierten Konstante für Encoding, bleibt unverändert + String s9 = new String(b, StandardCharsets.UTF_8); // bleibt unverändert + String s10 = new String(b, 0, 1, StandardCharsets.UTF_8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets, bleiben unverändert + String s11 = new String(b, "windows-1252"); // bleibt unverändert + String s12 = new String(b, 0, 1, "windows-1252"); // bleibt unverändert + String s13 = new String(b, "Shift_JIS"); // bleibt unverändert + String s14 = new String(b, 0, 1, "Shift_JIS"); // bleibt unverändert + + // Fall mit Charset.forName() (wird unverändert bleiben, keine Ersetzung möglich) + Charset charset = Charset.forName("UTF-16"); + String s15 = new String(b, charset); // bleibt unverändert + String s16 = new String(b, 0, 1, charset); // bleibt unverändert + + // Fälle, die eine UnsupportedEncodingException werfen (werden im Cleanup angepasst) + try { + String s17 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + + try { + String s18 = new String(b, 0, 1, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Methodendeklaration mit throws für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithThrows(String filename) throws UnsupportedEncodingException { + byte[] b = {(byte) 59}; + String s1 = new String(b, "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Nach dem Cleanup sollte dies keine UnsupportedEncodingException mehr werfen + static void methodWithThrowsChange(String filename) throws FileNotFoundException { + byte[] b = {(byte) 59}; + String s1 = new String(b, StandardCharsets.UTF_8); // wirft keine UnsupportedEncodingException mehr + } + + // Methodendeklaration mit try-catch für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithCatch(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + static void methodWithCatchChange(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, StandardCharsets.UTF_8); // keine UnsupportedEncodingException + } + } +} +"""), + PROPERTIESSTORETOXML( +""" +package test1; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Encoding-Variable + + // Fall 1: UTF-8 als String; Cleanup soll zu StandardCharsets.UTF_8 ändern + void storeWithTryWithResources() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "UTF-8"); + } + } + + // Fall 2: Benutzerdefiniertes Encoding als Variable; Cleanup soll diesen Fall unverändert lassen + void storeWithTryWithResourcesAndCustomEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", encodingVar); + } + } + + // Fall 3: Ungültiges Encoding als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithTryWithResourcesAndInvalidEncoding() { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } + } + + // Fall 4: FileOutputStream außerhalb des try-Blocks und UTF-8 als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithoutTryWithResources(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", "UTF-8"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unexpected UnsupportedEncodingException"); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 5: Gültiges Encoding ohne Konstante in StandardCharsets (windows-1252); Cleanup soll diesen Fall unverändert lassen + void storeWithWindows1252Encoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "windows-1252"); + } + } + + // Fall 6: Gültiges Encoding ohne Konstante in StandardCharsets (Shift_JIS); Cleanup soll diesen Fall unverändert lassen + void storeWithShiftJISEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "Shift_JIS"); + } + } + + // Fall 7: Ungültiges Encoding außerhalb von try-with-resources; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithInvalidEncodingOutsideTry(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 8: StandardCharsets.UTF_8 ohne try-with-resources; Cleanup soll diesen Fall unverändert lassen + void storeWithoutTryWithResourcesStandardCharsets(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } +} +""", + +""" +package test1; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Encoding-Variable + + // Fall 1: UTF-8 als String; Cleanup soll zu StandardCharsets.UTF_8 ändern + void storeWithTryWithResources() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + } + } + + // Fall 2: Benutzerdefiniertes Encoding als Variable; Cleanup soll diesen Fall unverändert lassen + void storeWithTryWithResourcesAndCustomEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", encodingVar); + } + } + + // Fall 3: Ungültiges Encoding als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithTryWithResourcesAndInvalidEncoding() { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } + } + + // Fall 4: FileOutputStream außerhalb des try-Blocks und UTF-8 als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithoutTryWithResources(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 5: Gültiges Encoding ohne Konstante in StandardCharsets (windows-1252); Cleanup soll diesen Fall unverändert lassen + void storeWithWindows1252Encoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "windows-1252"); + } + } + + // Fall 6: Gültiges Encoding ohne Konstante in StandardCharsets (Shift_JIS); Cleanup soll diesen Fall unverändert lassen + void storeWithShiftJISEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "Shift_JIS"); + } + } + + // Fall 7: Ungültiges Encoding außerhalb von try-with-resources; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithInvalidEncodingOutsideTry(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 8: StandardCharsets.UTF_8 ohne try-with-resources; Cleanup soll diesen Fall unverändert lassen + void storeWithoutTryWithResourcesStandardCharsets(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } +} +"""), + URLDECODER( +""" +package test1; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; + +public class E2 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Variable für Encoding + + // Methode ohne Encoding-Angabe, bleibt unverändert + static void decodeDefault() { + String url = URLDecoder.decode("example"); + } + + // Methode, die "UTF-8" als String verwendet und UnsupportedEncodingException wirft + static void decodeWithThrows() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", "UTF-8"); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void decodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Konstante für Encoding verwendet, bleibt unverändert + void decodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", ENCODING_UTF8); + } + + // Methode, die Encoding als Variable übergibt, bleibt unverändert + void decodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für ungültiges Encoding + static void decodeWithTryCatch() { + try { + String url = URLDecoder.decode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void decodeWithStandardCharset() { + String url = URLDecoder.decode("example", StandardCharsets.UTF_8); + } +} +""", +""" +package test1; + +import java.net.URLDecoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E2 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Variable für Encoding + + // Methode ohne Encoding-Angabe, bleibt unverändert + static void decodeDefault() { + String url = URLDecoder.decode("example", Charset.defaultCharset()); + } + + // Methode, die "UTF-8" als String verwendet und UnsupportedEncodingException wirft + static void decodeWithThrows() { + String url = URLDecoder.decode("example", StandardCharsets.UTF_8); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void decodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Konstante für Encoding verwendet, bleibt unverändert + void decodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", ENCODING_UTF8); + } + + // Methode, die Encoding als Variable übergibt, bleibt unverändert + void decodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für ungültiges Encoding + static void decodeWithTryCatch() { + try { + String url = URLDecoder.decode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void decodeWithStandardCharset() { + String url = URLDecoder.decode("example", StandardCharsets.UTF_8); + } +} +"""), + URLENCODER( +""" +package test1; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Kodierungskonstante + private String encodingVar = "ISO-8859-1"; // Variable für eine Kodierung + + // Methode ohne explizite Kodierung, bleibt unverändert + static void encodeDefault() { + String url = URLEncoder.encode("example"); + } + + // Methode, die "UTF-8" als String-Literal verwendet und `throws UnsupportedEncodingException` hat + static void encodeWithThrows() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", "UTF-8"); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void encodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Kodierungskonstante verwendet, bleibt unverändert + void encodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", ENCODING_UTF8); + } + + // Methode, die eine Kodierungsvariable verwendet, bleibt unverändert + void encodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für eine ungültige Kodierung + static void encodeWithTryCatch() { + try { + String url = URLEncoder.encode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void encodeWithStandardCharset() { + String url = URLEncoder.encode("example", StandardCharsets.UTF_8); + } +} +""", +""" +package test1; + +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Kodierungskonstante + private String encodingVar = "ISO-8859-1"; // Variable für eine Kodierung + + // Methode ohne explizite Kodierung, bleibt unverändert + static void encodeDefault() { + String url = URLEncoder.encode("example", Charset.defaultCharset()); + } + + // Methode, die "UTF-8" als String-Literal verwendet und `throws UnsupportedEncodingException` hat + static void encodeWithThrows() { + String url = URLEncoder.encode("example", StandardCharsets.UTF_8); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void encodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Kodierungskonstante verwendet, bleibt unverändert + void encodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", ENCODING_UTF8); + } + + // Methode, die eine Kodierungsvariable verwendet, bleibt unverändert + void encodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für eine ungültige Kodierung + static void encodeWithTryCatch() { + try { + String url = URLEncoder.encode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void encodeWithStandardCharset() { + String url = URLEncoder.encode("example", StandardCharsets.UTF_8); + } +} +"""), + SCANNER( +""" +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +public class E1 { + + // Methode mit File und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla3(File file) throws FileNotFoundException { + // Konstruktor mit String-Encoding, sollte durch StandardCharsets.UTF_8 ersetzt werden + Scanner s = new Scanner(file, "UTF-8"); + } + + // Methode mit InputStream und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla4(InputStream is) throws FileNotFoundException { + Scanner s2 = new Scanner(is, "UTF-8"); + } + + // Methode mit Scanner, aber ohne explizites Encoding, bleibt unverändert + static void bla5() { + Scanner s3 = new Scanner("asdf"); + } + + // Methode, die eine benutzerdefinierte Konstante für die Kodierung verwendet (bleibt unverändert) + private static final String ENCODING_UTF8 = "UTF-8"; + static void bla6(File file) throws FileNotFoundException { + Scanner s = new Scanner(file, ENCODING_UTF8); + } + + // Methode mit einer ungültigen Kodierung (muss UnsupportedEncodingException werfen) + static void bla7(File file) throws FileNotFoundException { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (Exception e) { + e.printStackTrace(); // Catch block für UnsupportedEncodingException + } + } + + // Methode mit Scanner und ungültiger Kodierung, die `throws UnsupportedEncodingException` wirft + static void bla8(InputStream is) throws FileNotFoundException, UnsupportedEncodingException { + Scanner s = new Scanner(is, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet (für FileNotFoundException) + static void bla9(File file) { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (Exception e) { + // UnsupportedEncodingException wird hier abgefangen + e.printStackTrace(); + } + } + + // Beispiel mit StandardCharsets-Konstanten, die keine Änderung brauchen + static void bla10(File file) { + Scanner s = new Scanner(file, StandardCharsets.UTF_8); + } + + // Beispiel mit Scanner und InputStream, ohne explizite Kodierung (bleibt unverändert) + static void bla11(InputStream is) { + Scanner s = new Scanner(is); + } + + // Methode mit Scanner und einer benutzerdefinierten Kodierung als Variable (bleibt unverändert) + private String encodingVar = "ISO-8859-1"; + static void bla12(InputStream is) throws FileNotFoundException { + Scanner s = new Scanner(is, "ISO-8859-1"); + } +} +""", +""" +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +public class E1 { + + // Methode mit File und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla3(File file) throws FileNotFoundException { + // Konstruktor mit String-Encoding, sollte durch StandardCharsets.UTF_8 ersetzt werden + Scanner s = new Scanner(file, StandardCharsets.UTF_8); + } + + // Methode mit InputStream und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla4(InputStream is) throws FileNotFoundException { + Scanner s2 = new Scanner(is, StandardCharsets.UTF_8); + } + + // Methode mit Scanner, aber ohne explizites Encoding, bleibt unverändert + static void bla5() { + Scanner s3 = new Scanner("asdf", Charset.defaultCharset()); + } + + // Methode, die eine benutzerdefinierte Konstante für die Kodierung verwendet (bleibt unverändert) + private static final String ENCODING_UTF8 = "UTF-8"; + static void bla6(File file) throws FileNotFoundException { + Scanner s = new Scanner(file, ENCODING_UTF8); + } + + // Methode mit einer ungültigen Kodierung (muss UnsupportedEncodingException werfen) + static void bla7(File file) throws FileNotFoundException { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (Exception e) { + e.printStackTrace(); // Catch block für UnsupportedEncodingException + } + } + + // Methode mit Scanner und ungültiger Kodierung, die `throws UnsupportedEncodingException` wirft + static void bla8(InputStream is) throws FileNotFoundException, UnsupportedEncodingException { + Scanner s = new Scanner(is, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet (für FileNotFoundException) + static void bla9(File file) { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (Exception e) { + // UnsupportedEncodingException wird hier abgefangen + e.printStackTrace(); + } + } + + // Beispiel mit StandardCharsets-Konstanten, die keine Änderung brauchen + static void bla10(File file) { + Scanner s = new Scanner(file, StandardCharsets.UTF_8); + } + + // Beispiel mit Scanner und InputStream, ohne explizite Kodierung (bleibt unverändert) + static void bla11(InputStream is) { + Scanner s = new Scanner(is, Charset.defaultCharset()); + } + + // Methode mit Scanner und einer benutzerdefinierten Kodierung als Variable (bleibt unverändert) + private String encodingVar = "ISO-8859-1"; + static void bla12(InputStream is) throws FileNotFoundException { + Scanner s = new Scanner(is, StandardCharsets.ISO_8859_1); + } +} +"""), + FORMATTER( +""" +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.Formatter; + +public class E1 { + + // Methode mit explizitem UTF-8, sollte durch StandardCharsets.UTF_8 ersetzt werden + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s = new Formatter(new File("asdf"), "UTF-8"); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } + + // Methode mit try-catch, die eine Kodierung verwendet und Fehler wirft + static void bli() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "UTF-8"); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } catch (FileNotFoundException | UnsupportedEncodingException e) { + // Der Catch-Block für UnsupportedEncodingException sollte im Cleanup entfernt werden + e.printStackTrace(); + } + } + + // Methode mit benutzerdefinierter Konstante für das Encoding + private static final String ENCODING_UTF8 = "UTF-8"; + + static void blc() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s = new Formatter(new File("asdf"), ENCODING_UTF8); // 'UTF-8' als Konstante + } + + // Methode mit einer ungültigen Kodierung (z.B. 'non-existing-encoding') + static void bld() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); // UnsupportedEncodingException wird hier erwartet + } + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet + static void ble() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird hier behandelt + e.printStackTrace(); + } + } + + // Methode mit StandardCharsets.UTF_8 + static void blf() throws FileNotFoundException { + Formatter s = new Formatter(new File("asdf"), StandardCharsets.UTF_8); // Verwendung von StandardCharsets.UTF_8 + } + + // Beispiel, bei dem das Encoding in einer Variablen gespeichert ist + private String encodingVar = "UTF-8"; + + static void blg() throws FileNotFoundException { + String encoding = "UTF-8"; + Formatter s = new Formatter(new File("asdf"), encoding); // encoding als Variable + } +} +""", """ +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.charset.StandardCharsets; +import java.util.Formatter; + +public class E1 { + + // Methode mit explizitem UTF-8, sollte durch StandardCharsets.UTF_8 ersetzt werden + static void bla() throws FileNotFoundException { + Formatter s = new Formatter(new File("asdf"), StandardCharsets.UTF_8); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } + + // Methode mit try-catch, die eine Kodierung verwendet und Fehler wirft + static void bli() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), StandardCharsets.UTF_8); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } catch (FileNotFoundException e) { + // Der Catch-Block für UnsupportedEncodingException sollte im Cleanup entfernt werden + e.printStackTrace(); + } + } + + // Methode mit benutzerdefinierter Konstante für das Encoding + private static final String ENCODING_UTF8 = "UTF-8"; + + static void blc() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s = new Formatter(new File("asdf"), ENCODING_UTF8); // 'UTF-8' als Konstante + } + + // Methode mit einer ungültigen Kodierung (z.B. 'non-existing-encoding') + static void bld() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); // UnsupportedEncodingException wird hier erwartet + } + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet + static void ble() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird hier behandelt + e.printStackTrace(); + } + } + + // Methode mit StandardCharsets.UTF_8 + static void blf() throws FileNotFoundException { + Formatter s = new Formatter(new File("asdf"), StandardCharsets.UTF_8); // Verwendung von StandardCharsets.UTF_8 + } + + // Beispiel, bei dem das Encoding in einer Variablen gespeichert ist + private String encodingVar = "UTF-8"; + + static void blg() throws FileNotFoundException { + String encoding = "UTF-8"; + Formatter s = new Formatter(new File("asdf"), encoding); // encoding als Variable + } +} +"""), + 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; + + ExplicitEncodingPatternsKeepBehavior(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/Java10/ExplicitEncodingPatternsPreferUTF8.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsPreferUTF8.java new file mode 100644 index 00000000000..cfec11ef7f4 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java10/ExplicitEncodingPatternsPreferUTF8.java @@ -0,0 +1,1878 @@ +/******************************************************************************* + * Copyright (c) 2024 Carsten Hammer and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * + *******************************************************************************/ +package org.eclipse.jdt.ui.tests.quickfix.Java10; + +public enum ExplicitEncodingPatternsPreferUTF8 { + + 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.nio.charset.StandardCharsets; +import java.io.FileNotFoundException; + +public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(StandardCharsets.UTF_8); + 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.StandardCharsets; +import java.io.FileNotFoundException; + +public class E1 { + void method(String filename) { + try { + Reader is=new InputStreamReader(new FileInputStream(filename), StandardCharsets.UTF_8); + } 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.StandardCharsets; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; + +public class E1 { + void method(String filename) { + try { + Writer fw=new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } +} + """), + INPUTSTREAMREADER( +""" +package test1; + +import java.io.InputStreamReader; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; + +public class E1 { + + void method(String filename) { + try { + // Standardkonstruktor ohne Encoding + InputStreamReader is1 = new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$ + + // String Literal Encodings, die nach StandardCharsets umgeschrieben werden sollten + InputStreamReader is2 = new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is3 = new InputStreamReader(new FileInputStream("file3.txt"), "ISO-8859-1"); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is4 = new InputStreamReader(new FileInputStream("file4.txt"), "US-ASCII"); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basiertes Encoding, das in Charset umgeschrieben werden kann, jedoch ohne vordefinierte Konstante + InputStreamReader is5 = new InputStreamReader(new FileInputStream("file5.txt"), "UTF-16"); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basierte Encodings mit Groß-/Kleinschreibungsvarianten + InputStreamReader is6 = new InputStreamReader(new FileInputStream("file6.txt"), "utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is7 = new InputStreamReader(new FileInputStream("file7.txt"), "Utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); // Sollte nach Cleanup entfernt werden + } + } + + void methodWithTryCatch(String filename) { + try { + // Variante, bei der UnsupportedEncodingException behandelt wird + InputStreamReader is8 = new InputStreamReader(new FileInputStream("file8.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); // Sollte nach Cleanup entfernt werden + } + } + + void methodWithoutException(String filename) throws UnsupportedEncodingException, FileNotFoundException { + // Case ohne Try-Catch-Block, sollte Charset-Konstanten direkt ersetzen + InputStreamReader is9 = new InputStreamReader(new FileInputStream("file9.txt"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + } + + void methodWithVariableEncoding(String filename) throws UnsupportedEncodingException, FileNotFoundException { + // Case, bei dem das Encoding aus einer Variablen kommt, Cleanup sollte hier keine Änderungen machen + String encoding = "UTF-8"; //$NON-NLS-1$ + InputStreamReader is10 = new InputStreamReader(new FileInputStream("file10.txt"), encoding); //$NON-NLS-1$ + } + + void methodWithNonStandardEncoding(String filename) { + try { + // Case mit nicht vordefiniertem Charset, sollte keine Umwandlung in StandardCharsets erfolgen + InputStreamReader is11 = new InputStreamReader(new FileInputStream("file11.txt"), "windows-1252"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + // Methode mit "throws UnsupportedEncodingException" zur Prüfung des Cleanups + void methodWithThrows(String filename) throws FileNotFoundException, UnsupportedEncodingException { + InputStreamReader is3 = new InputStreamReader(new FileInputStream(filename), "UTF-8"); //$NON-NLS-1$ + } +} +""", + +""" +package test1; + +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +public class E1 { + + void method(String filename) { + try { + // Standardkonstruktor ohne Encoding + InputStreamReader is1 = new InputStreamReader(new FileInputStream("file1.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ + + // String Literal Encodings, die nach StandardCharsets umgeschrieben werden sollten + InputStreamReader is2 = new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is3 = new InputStreamReader(new FileInputStream("file3.txt"), StandardCharsets.ISO_8859_1); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is4 = new InputStreamReader(new FileInputStream("file4.txt"), StandardCharsets.US_ASCII); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basiertes Encoding, das in Charset umgeschrieben werden kann, jedoch ohne vordefinierte Konstante + InputStreamReader is5 = new InputStreamReader(new FileInputStream("file5.txt"), StandardCharsets.UTF_16); //$NON-NLS-1$ //$NON-NLS-2$ + + // String-basierte Encodings mit Groß-/Kleinschreibungsvarianten + InputStreamReader is6 = new InputStreamReader(new FileInputStream("file6.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + InputStreamReader is7 = new InputStreamReader(new FileInputStream("file7.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + void methodWithTryCatch(String filename) { + try { + // Variante, bei der UnsupportedEncodingException behandelt wird + InputStreamReader is8 = new InputStreamReader(new FileInputStream("file8.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + void methodWithoutException(String filename) throws FileNotFoundException { + // Case ohne Try-Catch-Block, sollte Charset-Konstanten direkt ersetzen + InputStreamReader is9 = new InputStreamReader(new FileInputStream("file9.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ + } + + void methodWithVariableEncoding(String filename) throws UnsupportedEncodingException, FileNotFoundException { + // Case, bei dem das Encoding aus einer Variablen kommt, Cleanup sollte hier keine Änderungen machen + String encoding = "UTF-8"; //$NON-NLS-1$ + InputStreamReader is10 = new InputStreamReader(new FileInputStream("file10.txt"), encoding); //$NON-NLS-1$ + } + + void methodWithNonStandardEncoding(String filename) { + try { + // Case mit nicht vordefiniertem Charset, sollte keine Umwandlung in StandardCharsets erfolgen + InputStreamReader is11 = new InputStreamReader(new FileInputStream("file11.txt"), "windows-1252"); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + // Methode mit "throws UnsupportedEncodingException" zur Prüfung des Cleanups + void methodWithThrows(String filename) throws FileNotFoundException { + InputStreamReader is3 = new InputStreamReader(new FileInputStream(filename), StandardCharsets.UTF_8); //$NON-NLS-1$ + } +} +"""), + OUTPUTSTREAMWRITER( +""" +package test1; + +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(String filename) { + try { + // Standard-Konstruktor ohne Encoding + OutputStreamWriter os1 = new OutputStreamWriter(new FileOutputStream(filename)); + + // Konstruktor mit String-Encoding (UTF-8) -> muss durch StandardCharsets.UTF_8 ersetzt werden + OutputStreamWriter os2 = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"); // "UTF-8" als String-Literal + + // Konstruktor mit String-Encoding (ISO-8859-1) -> muss durch StandardCharsets.ISO_8859_1 ersetzt werden + OutputStreamWriter os3 = new OutputStreamWriter(new FileOutputStream(filename), "ISO-8859-1"); // "ISO-8859-1" als String-Literal + + // Konstruktor mit String-Encoding (US-ASCII) -> muss durch StandardCharsets.US_ASCII ersetzt werden + OutputStreamWriter os4 = new OutputStreamWriter(new FileOutputStream(filename), "US-ASCII"); // "US-ASCII" als String-Literal + + // Konstruktor mit String-Encoding (UTF-16) -> muss durch StandardCharsets.UTF_16 ersetzt werden + OutputStreamWriter os5 = new OutputStreamWriter(new FileOutputStream(filename), "UTF-16"); // "UTF-16" als String-Literal + + // Der Konstruktor mit einer benutzerdefinierten Konstante bleibt unverändert + OutputStreamWriter os6 = new OutputStreamWriter(new FileOutputStream(filename), ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (bleiben unverändert) + OutputStreamWriter os7 = new OutputStreamWriter(new FileOutputStream(filename), "windows-1252"); // bleibt unverändert + OutputStreamWriter os8 = new OutputStreamWriter(new FileOutputStream(filename), "Shift_JIS"); // bleibt unverändert + + // Hier wird `UnsupportedEncodingException` geworfen (vor dem Cleanup) + OutputStreamWriter os9 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + + // Aufruf mit einer ungültigen Zeichenkodierung und catch für UnsupportedEncodingException + try { + OutputStreamWriter os10 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + + // Beispiele mit StandardCharsets-Konstanten, die unverändert bleiben + OutputStreamWriter os11 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // bleibt unverändert + OutputStreamWriter os12 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.ISO_8859_1); // bleibt unverändert + OutputStreamWriter os13 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.US_ASCII); // bleibt unverändert + OutputStreamWriter os14 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); // bleibt unverändert + + // Beispiel mit Charset.forName und einer Konstanten, die als Parameter übergeben wird (bleibt unverändert) + OutputStreamWriter os15 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); + } catch (FileNotFoundException e) { + // Datei nicht gefunden + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + } + + // Methodendeklaration, die `UnsupportedEncodingException` wirft (und durch den Cleanup angepasst wird) + void methodWithThrows(String filename) throws UnsupportedEncodingException { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Neue Methode: methodWithThrowsChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr geworfen + void methodWithThrowsChange(String filename) throws FileNotFoundException { + // Nach dem Cleanup, der String "UTF-8" wird zu einer StandardCharset-Konstanten geändert + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"); // wirft keine UnsupportedEncodingException mehr + } + + // Methode mit einem try-catch, um die UnsupportedEncodingException zu behandeln (und durch den Cleanup angepasst wird) + void methodWithCatch(String filename) { + try { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + } + + // Neue Methode: methodWithCatchChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + void methodWithCatchChange(String filename) { + try { + // Nach dem Cleanup wird "UTF-8" ersetzt + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"); // keine UnsupportedEncodingException + } catch (UnsupportedEncodingException e) { + // Dieser Block wird nicht mehr erreicht, da keine UnsupportedEncodingException mehr geworfen wird + e.printStackTrace(); + } + } +} +""", + +""" +package test1; + +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.FileNotFoundException; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(String filename) { + try { + // Standard-Konstruktor ohne Encoding + OutputStreamWriter os1 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); + + // Konstruktor mit String-Encoding (UTF-8) -> muss durch StandardCharsets.UTF_8 ersetzt werden + OutputStreamWriter os2 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // "UTF-8" als String-Literal + + // Konstruktor mit String-Encoding (ISO-8859-1) -> muss durch StandardCharsets.ISO_8859_1 ersetzt werden + OutputStreamWriter os3 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.ISO_8859_1); // "ISO-8859-1" als String-Literal + + // Konstruktor mit String-Encoding (US-ASCII) -> muss durch StandardCharsets.US_ASCII ersetzt werden + OutputStreamWriter os4 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.US_ASCII); // "US-ASCII" als String-Literal + + // Konstruktor mit String-Encoding (UTF-16) -> muss durch StandardCharsets.UTF_16 ersetzt werden + OutputStreamWriter os5 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); // "UTF-16" als String-Literal + + // Der Konstruktor mit einer benutzerdefinierten Konstante bleibt unverändert + OutputStreamWriter os6 = new OutputStreamWriter(new FileOutputStream(filename), ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (bleiben unverändert) + OutputStreamWriter os7 = new OutputStreamWriter(new FileOutputStream(filename), "windows-1252"); // bleibt unverändert + OutputStreamWriter os8 = new OutputStreamWriter(new FileOutputStream(filename), "Shift_JIS"); // bleibt unverändert + + // Hier wird `UnsupportedEncodingException` geworfen (vor dem Cleanup) + OutputStreamWriter os9 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + + // Aufruf mit einer ungültigen Zeichenkodierung und catch für UnsupportedEncodingException + try { + OutputStreamWriter os10 = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + + // Beispiele mit StandardCharsets-Konstanten, die unverändert bleiben + OutputStreamWriter os11 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // bleibt unverändert + OutputStreamWriter os12 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.ISO_8859_1); // bleibt unverändert + OutputStreamWriter os13 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.US_ASCII); // bleibt unverändert + OutputStreamWriter os14 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); // bleibt unverändert + + // Beispiel mit Charset.forName und einer Konstanten, die als Parameter übergeben wird (bleibt unverändert) + OutputStreamWriter os15 = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_16); + } catch (FileNotFoundException e) { + // Datei nicht gefunden + e.printStackTrace(); + } + } + + // Methodendeklaration, die `UnsupportedEncodingException` wirft (und durch den Cleanup angepasst wird) + void methodWithThrows(String filename) throws UnsupportedEncodingException { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Neue Methode: methodWithThrowsChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr geworfen + void methodWithThrowsChange(String filename) throws FileNotFoundException { + // Nach dem Cleanup, der String "UTF-8" wird zu einer StandardCharset-Konstanten geändert + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // wirft keine UnsupportedEncodingException mehr + } + + // Methode mit einem try-catch, um die UnsupportedEncodingException zu behandeln (und durch den Cleanup angepasst wird) + void methodWithCatch(String filename) { + try { + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // Hier wird die UnsupportedEncodingException abgefangen + e.printStackTrace(); + } + } + + // Neue Methode: methodWithCatchChange() - nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + void methodWithCatchChange(String filename) { + try { + // Nach dem Cleanup wird "UTF-8" ersetzt + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // keine UnsupportedEncodingException + } + } +} +"""), + CHANNELSNEWREADER( +""" +package test1; + +import java.io.Reader; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.CharsetDecoder; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(ReadableByteChannel ch, CharsetDecoder decoder) { + // Fälle für StandardCharsets-Konstanten + Reader r1 = Channels.newReader(ch, "UTF-8"); // soll StandardCharsets.UTF_8 werden + Reader r2 = Channels.newReader(ch, "ISO-8859-1"); // soll StandardCharsets.ISO_8859_1 werden + Reader r3 = Channels.newReader(ch, "US-ASCII"); // soll StandardCharsets.US_ASCII werden + Reader r4 = Channels.newReader(ch, "UTF-16"); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Reader r5 = Channels.newReader(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Reader r6 = Channels.newReader(ch, "windows-1252"); // bleibt unverändert + Reader r7 = Channels.newReader(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Reader r8 = Channels.newReader(ch, StandardCharsets.UTF_8); + Reader r9 = Channels.newReader(ch, decoder, 1024); // mit CharsetDecoder und Buffergröße, bleibt unverändert + } +} +""", + +""" +package test1; + +import java.io.Reader; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.CharsetDecoder; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(ReadableByteChannel ch, CharsetDecoder decoder) { + // Fälle für StandardCharsets-Konstanten + Reader r1 = Channels.newReader(ch, StandardCharsets.UTF_8); // soll StandardCharsets.UTF_8 werden + Reader r2 = Channels.newReader(ch, StandardCharsets.ISO_8859_1); // soll StandardCharsets.ISO_8859_1 werden + Reader r3 = Channels.newReader(ch, StandardCharsets.US_ASCII); // soll StandardCharsets.US_ASCII werden + Reader r4 = Channels.newReader(ch, StandardCharsets.UTF_16); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Reader r5 = Channels.newReader(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Reader r6 = Channels.newReader(ch, "windows-1252"); // bleibt unverändert + Reader r7 = Channels.newReader(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Reader r8 = Channels.newReader(ch, StandardCharsets.UTF_8); + Reader r9 = Channels.newReader(ch, decoder, 1024); // mit CharsetDecoder und Buffergröße, bleibt unverändert + } +} +"""), + CHANNELSNEWWRITER( +""" +package test1; + +import java.io.Writer; +import java.nio.channels.WritableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(WritableByteChannel ch, Charset charset) { + // Fälle für StandardCharsets-Konstanten + Writer w1 = Channels.newWriter(ch, "UTF-8"); // soll StandardCharsets.UTF_8 werden + Writer w2 = Channels.newWriter(ch, "ISO-8859-1"); // soll StandardCharsets.ISO_8859_1 werden + Writer w3 = Channels.newWriter(ch, "US-ASCII"); // soll StandardCharsets.US_ASCII werden + Writer w4 = Channels.newWriter(ch, "UTF-16"); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Writer w5 = Channels.newWriter(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Writer w6 = Channels.newWriter(ch, "windows-1252"); // bleibt unverändert + Writer w7 = Channels.newWriter(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Writer w8 = Channels.newWriter(ch, StandardCharsets.UTF_8); + Writer w9 = Channels.newWriter(ch, charset); // unverändert, da `Charset` Instanz verwendet + } +} +""", + +""" +package test1; + +import java.io.Writer; +import java.nio.channels.WritableByteChannel; +import java.nio.channels.Channels; +import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + + void method(WritableByteChannel ch, Charset charset) { + // Fälle für StandardCharsets-Konstanten + Writer w1 = Channels.newWriter(ch, StandardCharsets.UTF_8); // soll StandardCharsets.UTF_8 werden + Writer w2 = Channels.newWriter(ch, StandardCharsets.ISO_8859_1); // soll StandardCharsets.ISO_8859_1 werden + Writer w3 = Channels.newWriter(ch, StandardCharsets.US_ASCII); // soll StandardCharsets.US_ASCII werden + Writer w4 = Channels.newWriter(ch, StandardCharsets.UTF_16); // soll StandardCharsets.UTF_16 werden + + // Aufruf mit einer String-Konstanten (soll unverändert bleiben) + Writer w5 = Channels.newWriter(ch, ENCODING_UTF8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets (sollen unverändert bleiben) + Writer w6 = Channels.newWriter(ch, "windows-1252"); // bleibt unverändert + Writer w7 = Channels.newWriter(ch, "Shift_JIS"); // bleibt unverändert + + // Aufrufe, die bereits `StandardCharsets` verwenden (bleiben unverändert) + Writer w8 = Channels.newWriter(ch, StandardCharsets.UTF_8); + Writer w9 = Channels.newWriter(ch, charset); // unverändert, da `Charset` Instanz verwendet + } +} +"""), + 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.StandardCharsets; +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), StandardCharsets.UTF_8)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } +} + """), + STRINGGETBYTES( +""" +package test1; + +import java.nio.charset.StandardCharsets; + +public class E1 { + + // Methode 1: Verwendung von StandardCharsets.UTF_8 statt "UTF-8" als String-Literal + void method(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes ohne Angabe der Kodierung (verwendet die Plattform-spezifische Standard-Kodierung) + byte[] bytes = s.getBytes(); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Methode 2: Behandlung von getBytes mit einer expliziten Kodierung + void method2(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes mit expliziter Kodierung (UTF-8 als String-Literal) + byte[] bytes = s.getBytes("UTF-8"); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Erweiterter Testfall: Verwendung von verschiedenen Kodierungen + void methodWithDifferentEncodings(String filename) { + String s = "asdf"; + + // Testen von gängigen Kodierungen + byte[] bytes1 = s.getBytes("ISO-8859-1"); // ISO-8859-1 + byte[] bytes2 = s.getBytes("US-ASCII"); // US-ASCII + byte[] bytes3 = s.getBytes(StandardCharsets.UTF_8); // UTF-8 mit StandardCharsets + byte[] bytes4 = s.getBytes("UTF-16"); // UTF-16 + + System.out.println(bytes1.length); // Ausgabe der Längen + System.out.println(bytes2.length); + System.out.println(bytes3.length); + System.out.println(bytes4.length); + } + + // Testfall: Verwendung von getBytes mit einer ungültigen Kodierung (sollte im Cleanup behandelt werden) + void methodWithInvalidEncoding(String filename) { + String s = "asdf"; + try { + // Ungültige Kodierung, die zu UnsupportedEncodingException führt + byte[] bytes = s.getBytes("non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + // Diese Ausnahme sollte im Cleanup berücksichtigt werden + e.printStackTrace(); + } + } + + // Testfall: Verwendung von getBytes mit einer durch Variable angegebenen Kodierung + void methodWithVariableEncoding(String filename) { + String s = "asdf"; + String encoding = "UTF-8"; // Kodierung als Variable + try { + byte[] bytes = s.getBytes(encoding); // Kodierung aus der Variablen + System.out.println(bytes.length); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } +} +""", + +""" +package test1; + +import java.nio.charset.StandardCharsets; + +public class E1 { + + // Methode 1: Verwendung von StandardCharsets.UTF_8 statt "UTF-8" als String-Literal + void method(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes ohne Angabe der Kodierung (verwendet die Plattform-spezifische Standard-Kodierung) + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Methode 2: Behandlung von getBytes mit einer expliziten Kodierung + void method2(String filename) { + String s = "asdf"; //$NON-NLS-1$ + + // Vorher: getBytes mit expliziter Kodierung (UTF-8 als String-Literal) + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); + + // Nachher: Umstellung auf StandardCharsets.UTF_8 + byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); + + System.out.println(bytes.length); + System.out.println(bytes2.length); + } + + // Erweiterter Testfall: Verwendung von verschiedenen Kodierungen + void methodWithDifferentEncodings(String filename) { + String s = "asdf"; + + // Testen von gängigen Kodierungen + byte[] bytes1 = s.getBytes(StandardCharsets.ISO_8859_1); // ISO-8859-1 + byte[] bytes2 = s.getBytes(StandardCharsets.US_ASCII); // US-ASCII + byte[] bytes3 = s.getBytes(StandardCharsets.UTF_8); // UTF-8 mit StandardCharsets + byte[] bytes4 = s.getBytes(StandardCharsets.UTF_16); // UTF-16 + + System.out.println(bytes1.length); // Ausgabe der Längen + System.out.println(bytes2.length); + System.out.println(bytes3.length); + System.out.println(bytes4.length); + } + + // Testfall: Verwendung von getBytes mit einer ungültigen Kodierung (sollte im Cleanup behandelt werden) + void methodWithInvalidEncoding(String filename) { + String s = "asdf"; + try { + // Ungültige Kodierung, die zu UnsupportedEncodingException führt + byte[] bytes = s.getBytes("non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + // Diese Ausnahme sollte im Cleanup berücksichtigt werden + e.printStackTrace(); + } + } + + // Testfall: Verwendung von getBytes mit einer durch Variable angegebenen Kodierung + void methodWithVariableEncoding(String filename) { + String s = "asdf"; + String encoding = "UTF-8"; // Kodierung als Variable + try { + byte[] bytes = s.getBytes(encoding); // Kodierung aus der Variablen + System.out.println(bytes.length); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } +} +"""), + STRING( +""" +package test1; + +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + + static void bla(String filename) throws FileNotFoundException, UnsupportedEncodingException { + byte[] b = {(byte) 59}; + + // Fälle mit String Encoding als "UTF-8" (soll durch StandardCharsets.UTF_8 ersetzt werden) + String s1 = new String(b, "UTF-8"); // "UTF-8" als String-Literal + String s2 = new String(b, 0, 1, "UTF-8"); // "UTF-8" als String-Literal + + // Fall mit ISO-8859-1 Encoding (soll durch StandardCharsets.ISO_8859_1 ersetzt werden) + String s3 = new String(b, "ISO-8859-1"); // "ISO-8859-1" als String-Literal + String s4 = new String(b, 0, 1, "ISO-8859-1"); // "ISO-8859-1" als String-Literal + + // Fall mit US-ASCII Encoding (soll durch StandardCharsets.US_ASCII ersetzt werden) + String s5 = new String(b, "US-ASCII"); // "US-ASCII" als String-Literal + String s6 = new String(b, 0, 1, "US-ASCII"); // "US-ASCII" als String-Literal + + // Fall mit UTF-16 Encoding (soll durch StandardCharsets.UTF_16 ersetzt werden) + String s7 = new String(b, "UTF-16"); // "UTF-16" als String-Literal + String s8 = new String(b, 0, 1, "UTF-16"); // "UTF-16" als String-Literal + + // Fall mit einer benutzerdefinierten Konstante für Encoding, bleibt unverändert + String s9 = new String(b, "UTF-8"); // bleibt unverändert + String s10 = new String(b, 0, 1, "UTF-8"); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets, bleiben unverändert + String s11 = new String(b, "windows-1252"); // bleibt unverändert + String s12 = new String(b, 0, 1, "windows-1252"); // bleibt unverändert + String s13 = new String(b, "Shift_JIS"); // bleibt unverändert + String s14 = new String(b, 0, 1, "Shift_JIS"); // bleibt unverändert + + // Fall mit Charset.forName() (wird unverändert bleiben, keine Ersetzung möglich) + Charset charset = Charset.forName("UTF-16"); + String s15 = new String(b, charset); // bleibt unverändert + String s16 = new String(b, 0, 1, charset); // bleibt unverändert + + // Fälle, die eine UnsupportedEncodingException werfen (werden im Cleanup angepasst) + try { + String s17 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + + try { + String s18 = new String(b, 0, 1, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Methodendeklaration mit throws für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithThrows(String filename) throws UnsupportedEncodingException { + byte[] b = {(byte) 59}; + String s1 = new String(b, "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Nach dem Cleanup sollte dies keine UnsupportedEncodingException mehr werfen + static void methodWithThrowsChange(String filename) throws FileNotFoundException { + byte[] b = {(byte) 59}; + String s1 = new String(b, "UTF-8"); // wirft keine UnsupportedEncodingException mehr + } + + // Methodendeklaration mit try-catch für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithCatch(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + static void methodWithCatchChange(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, "UTF-8"); // keine UnsupportedEncodingException + } catch (UnsupportedEncodingException e) { + // Dieser Block wird nicht mehr erreicht, da keine UnsupportedEncodingException mehr geworfen wird + e.printStackTrace(); + } + } +} +""", + +""" +package test1; + +import java.io.FileNotFoundException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +public class E1 { + + static void bla(String filename) throws FileNotFoundException { + byte[] b = {(byte) 59}; + + // Fälle mit String Encoding als "UTF-8" (soll durch StandardCharsets.UTF_8 ersetzt werden) + String s1 = new String(b, StandardCharsets.UTF_8); // "UTF-8" als String-Literal + String s2 = new String(b, 0, 1, StandardCharsets.UTF_8); // "UTF-8" als String-Literal + + // Fall mit ISO-8859-1 Encoding (soll durch StandardCharsets.ISO_8859_1 ersetzt werden) + String s3 = new String(b, StandardCharsets.ISO_8859_1); // "ISO-8859-1" als String-Literal + String s4 = new String(b, 0, 1, StandardCharsets.ISO_8859_1); // "ISO-8859-1" als String-Literal + + // Fall mit US-ASCII Encoding (soll durch StandardCharsets.US_ASCII ersetzt werden) + String s5 = new String(b, StandardCharsets.US_ASCII); // "US-ASCII" als String-Literal + String s6 = new String(b, 0, 1, StandardCharsets.US_ASCII); // "US-ASCII" als String-Literal + + // Fall mit UTF-16 Encoding (soll durch StandardCharsets.UTF_16 ersetzt werden) + String s7 = new String(b, StandardCharsets.UTF_16); // "UTF-16" als String-Literal + String s8 = new String(b, 0, 1, StandardCharsets.UTF_16); // "UTF-16" als String-Literal + + // Fall mit einer benutzerdefinierten Konstante für Encoding, bleibt unverändert + String s9 = new String(b, StandardCharsets.UTF_8); // bleibt unverändert + String s10 = new String(b, 0, 1, StandardCharsets.UTF_8); // bleibt unverändert + + // Fälle ohne Entsprechung in StandardCharsets, bleiben unverändert + String s11 = new String(b, "windows-1252"); // bleibt unverändert + String s12 = new String(b, 0, 1, "windows-1252"); // bleibt unverändert + String s13 = new String(b, "Shift_JIS"); // bleibt unverändert + String s14 = new String(b, 0, 1, "Shift_JIS"); // bleibt unverändert + + // Fall mit Charset.forName() (wird unverändert bleiben, keine Ersetzung möglich) + Charset charset = Charset.forName("UTF-16"); + String s15 = new String(b, charset); // bleibt unverändert + String s16 = new String(b, 0, 1, charset); // bleibt unverändert + + // Fälle, die eine UnsupportedEncodingException werfen (werden im Cleanup angepasst) + try { + String s17 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + + try { + String s18 = new String(b, 0, 1, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Methodendeklaration mit throws für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithThrows(String filename) throws UnsupportedEncodingException { + byte[] b = {(byte) 59}; + String s1 = new String(b, "non-existing-encoding"); // wirft UnsupportedEncodingException + } + + // Nach dem Cleanup sollte dies keine UnsupportedEncodingException mehr werfen + static void methodWithThrowsChange(String filename) throws FileNotFoundException { + byte[] b = {(byte) 59}; + String s1 = new String(b, StandardCharsets.UTF_8); // wirft keine UnsupportedEncodingException mehr + } + + // Methodendeklaration mit try-catch für UnsupportedEncodingException (wird im Cleanup angepasst) + static void methodWithCatch(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird geworfen und abgefangen + e.printStackTrace(); + } + } + + // Nach dem Cleanup wird keine UnsupportedEncodingException mehr abgefangen + static void methodWithCatchChange(String filename) { + byte[] b = {(byte) 59}; + try { + String s1 = new String(b, StandardCharsets.UTF_8); // keine UnsupportedEncodingException + } + } +} +"""), + PROPERTIESSTORETOXML( +""" +package test1; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Encoding-Variable + + // Fall 1: UTF-8 als String; Cleanup soll zu StandardCharsets.UTF_8 ändern + void storeWithTryWithResources() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "UTF-8"); + } + } + + // Fall 2: Benutzerdefiniertes Encoding als Variable; Cleanup soll diesen Fall unverändert lassen + void storeWithTryWithResourcesAndCustomEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", encodingVar); + } + } + + // Fall 3: Ungültiges Encoding als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithTryWithResourcesAndInvalidEncoding() { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } + } + + // Fall 4: FileOutputStream außerhalb des try-Blocks und UTF-8 als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithoutTryWithResources(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", "UTF-8"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unexpected UnsupportedEncodingException"); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 5: Gültiges Encoding ohne Konstante in StandardCharsets (windows-1252); Cleanup soll diesen Fall unverändert lassen + void storeWithWindows1252Encoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "windows-1252"); + } + } + + // Fall 6: Gültiges Encoding ohne Konstante in StandardCharsets (Shift_JIS); Cleanup soll diesen Fall unverändert lassen + void storeWithShiftJISEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "Shift_JIS"); + } + } + + // Fall 7: Ungültiges Encoding außerhalb von try-with-resources; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithInvalidEncodingOutsideTry(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 8: StandardCharsets.UTF_8 ohne try-with-resources; Cleanup soll diesen Fall unverändert lassen + void storeWithoutTryWithResourcesStandardCharsets(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } +} +""", + +""" +package test1; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Encoding-Variable + + // Fall 1: UTF-8 als String; Cleanup soll zu StandardCharsets.UTF_8 ändern + void storeWithTryWithResources() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + } + } + + // Fall 2: Benutzerdefiniertes Encoding als Variable; Cleanup soll diesen Fall unverändert lassen + void storeWithTryWithResourcesAndCustomEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", encodingVar); + } + } + + // Fall 3: Ungültiges Encoding als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithTryWithResourcesAndInvalidEncoding() { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } + } + + // Fall 4: FileOutputStream außerhalb des try-Blocks und UTF-8 als String; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithoutTryWithResources(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 5: Gültiges Encoding ohne Konstante in StandardCharsets (windows-1252); Cleanup soll diesen Fall unverändert lassen + void storeWithWindows1252Encoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "windows-1252"); + } + } + + // Fall 6: Gültiges Encoding ohne Konstante in StandardCharsets (Shift_JIS); Cleanup soll diesen Fall unverändert lassen + void storeWithShiftJISEncoding() throws IOException { + Properties p = new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, "Kommentar", "Shift_JIS"); + } + } + + // Fall 7: Ungültiges Encoding außerhalb von try-with-resources; Cleanup soll auf StandardCharsets.UTF_8 ändern und den catch-Block entfernen + void storeWithInvalidEncodingOutsideTry(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", "non-existing-encoding"); + } catch (UnsupportedEncodingException e) { + System.err.println("Unsupported encoding caught!"); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } + + // Fall 8: StandardCharsets.UTF_8 ohne try-with-resources; Cleanup soll diesen Fall unverändert lassen + void storeWithoutTryWithResourcesStandardCharsets(String filename) throws IOException { + Properties p = new Properties(); + FileOutputStream os = new FileOutputStream(filename); + try { + p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + } finally { + os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen + } + } +} +"""), + URLDECODER( +""" +package test1; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; + +public class E2 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Variable für Encoding + + // Methode ohne Encoding-Angabe, bleibt unverändert + static void decodeDefault() { + String url = URLDecoder.decode("example"); + } + + // Methode, die "UTF-8" als String verwendet und UnsupportedEncodingException wirft + static void decodeWithThrows() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", "UTF-8"); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void decodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Konstante für Encoding verwendet, bleibt unverändert + void decodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", ENCODING_UTF8); + } + + // Methode, die Encoding als Variable übergibt, bleibt unverändert + void decodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für ungültiges Encoding + static void decodeWithTryCatch() { + try { + String url = URLDecoder.decode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void decodeWithStandardCharset() { + String url = URLDecoder.decode("example", StandardCharsets.UTF_8); + } +} +""", +""" +package test1; + +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; + +public class E2 { + private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + private String encodingVar = "ISO-8859-1"; // Variable für Encoding + + // Methode ohne Encoding-Angabe, bleibt unverändert + static void decodeDefault() { + String url = URLDecoder.decode("example", StandardCharsets.UTF_8); + } + + // Methode, die "UTF-8" als String verwendet und UnsupportedEncodingException wirft + static void decodeWithThrows() { + String url = URLDecoder.decode("example", StandardCharsets.UTF_8); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void decodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Konstante für Encoding verwendet, bleibt unverändert + void decodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", ENCODING_UTF8); + } + + // Methode, die Encoding als Variable übergibt, bleibt unverändert + void decodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLDecoder.decode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für ungültiges Encoding + static void decodeWithTryCatch() { + try { + String url = URLDecoder.decode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void decodeWithStandardCharset() { + String url = URLDecoder.decode("example", StandardCharsets.UTF_8); + } +} +"""), + URLENCODER( +""" +package test1; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Kodierungskonstante + private String encodingVar = "ISO-8859-1"; // Variable für eine Kodierung + + // Methode ohne explizite Kodierung, bleibt unverändert + static void encodeDefault() { + String url = URLEncoder.encode("example"); + } + + // Methode, die "UTF-8" als String-Literal verwendet und `throws UnsupportedEncodingException` hat + static void encodeWithThrows() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", "UTF-8"); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void encodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Kodierungskonstante verwendet, bleibt unverändert + void encodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", ENCODING_UTF8); + } + + // Methode, die eine Kodierungsvariable verwendet, bleibt unverändert + void encodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für eine ungültige Kodierung + static void encodeWithTryCatch() { + try { + String url = URLEncoder.encode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void encodeWithStandardCharset() { + String url = URLEncoder.encode("example", StandardCharsets.UTF_8); + } +} +""", +""" +package test1; + +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +public class E1 { + private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Kodierungskonstante + private String encodingVar = "ISO-8859-1"; // Variable für eine Kodierung + + // Methode ohne explizite Kodierung, bleibt unverändert + static void encodeDefault() { + String url = URLEncoder.encode("example", StandardCharsets.UTF_8); + } + + // Methode, die "UTF-8" als String-Literal verwendet und `throws UnsupportedEncodingException` hat + static void encodeWithThrows() { + String url = URLEncoder.encode("example", StandardCharsets.UTF_8); // sollte in StandardCharsets.UTF_8 geändert werden + } + + // Methode, die eine ungültige Kodierung verwendet und `UnsupportedEncodingException` wirft + static void encodeWithInvalidEncodingThrows() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", "non-existing-encoding"); + } + + // Methode, die eine benutzerdefinierte Kodierungskonstante verwendet, bleibt unverändert + void encodeWithCustomConstant() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", ENCODING_UTF8); + } + + // Methode, die eine Kodierungsvariable verwendet, bleibt unverändert + void encodeWithVariableEncoding() throws UnsupportedEncodingException { + String url = URLEncoder.encode("example", encodingVar); + } + + // Methode mit `try-catch`-Block für eine ungültige Kodierung + static void encodeWithTryCatch() { + try { + String url = URLEncoder.encode("example", "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (UnsupportedEncodingException e) { + System.err.println("Caught UnsupportedEncodingException for invalid encoding!"); + } + } + + // Beispiel mit StandardCharsets-Konstanten, bleibt unverändert + static void encodeWithStandardCharset() { + String url = URLEncoder.encode("example", StandardCharsets.UTF_8); + } +} +"""), + SCANNER( +""" +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +public class E1 { + + // Methode mit File und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla3(File file) throws FileNotFoundException { + // Konstruktor mit String-Encoding, sollte durch StandardCharsets.UTF_8 ersetzt werden + Scanner s = new Scanner(file, "UTF-8"); + } + + // Methode mit InputStream und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla4(InputStream is) throws FileNotFoundException { + Scanner s2 = new Scanner(is, "UTF-8"); + } + + // Methode mit Scanner, aber ohne explizites Encoding, bleibt unverändert + static void bla5() { + Scanner s3 = new Scanner("asdf"); + } + + // Methode, die eine benutzerdefinierte Konstante für die Kodierung verwendet (bleibt unverändert) + private static final String ENCODING_UTF8 = "UTF-8"; + static void bla6(File file) throws FileNotFoundException { + Scanner s = new Scanner(file, ENCODING_UTF8); + } + + // Methode mit einer ungültigen Kodierung (muss UnsupportedEncodingException werfen) + static void bla7(File file) throws FileNotFoundException { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (Exception e) { + e.printStackTrace(); // Catch block für UnsupportedEncodingException + } + } + + // Methode mit Scanner und ungültiger Kodierung, die `throws UnsupportedEncodingException` wirft + static void bla8(InputStream is) throws FileNotFoundException, UnsupportedEncodingException { + Scanner s = new Scanner(is, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet (für FileNotFoundException) + static void bla9(File file) { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (Exception e) { + // UnsupportedEncodingException wird hier abgefangen + e.printStackTrace(); + } + } + + // Beispiel mit StandardCharsets-Konstanten, die keine Änderung brauchen + static void bla10(File file) { + Scanner s = new Scanner(file, StandardCharsets.UTF_8); + } + + // Beispiel mit Scanner und InputStream, ohne explizite Kodierung (bleibt unverändert) + static void bla11(InputStream is) { + Scanner s = new Scanner(is); + } + + // Methode mit Scanner und einer benutzerdefinierten Kodierung als Variable (bleibt unverändert) + private String encodingVar = "ISO-8859-1"; + static void bla12(InputStream is) throws FileNotFoundException { + Scanner s = new Scanner(is, "ISO-8859-1"); + } +} +""", +""" +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +public class E1 { + + // Methode mit File und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla3(File file) throws FileNotFoundException { + // Konstruktor mit String-Encoding, sollte durch StandardCharsets.UTF_8 ersetzt werden + Scanner s = new Scanner(file, StandardCharsets.UTF_8); + } + + // Methode mit InputStream und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + static void bla4(InputStream is) throws FileNotFoundException { + Scanner s2 = new Scanner(is, StandardCharsets.UTF_8); + } + + // Methode mit Scanner, aber ohne explizites Encoding, bleibt unverändert + static void bla5() { + Scanner s3 = new Scanner("asdf", StandardCharsets.UTF_8); + } + + // Methode, die eine benutzerdefinierte Konstante für die Kodierung verwendet (bleibt unverändert) + private static final String ENCODING_UTF8 = "UTF-8"; + static void bla6(File file) throws FileNotFoundException { + Scanner s = new Scanner(file, ENCODING_UTF8); + } + + // Methode mit einer ungültigen Kodierung (muss UnsupportedEncodingException werfen) + static void bla7(File file) throws FileNotFoundException { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (Exception e) { + e.printStackTrace(); // Catch block für UnsupportedEncodingException + } + } + + // Methode mit Scanner und ungültiger Kodierung, die `throws UnsupportedEncodingException` wirft + static void bla8(InputStream is) throws FileNotFoundException, UnsupportedEncodingException { + Scanner s = new Scanner(is, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet (für FileNotFoundException) + static void bla9(File file) { + try { + Scanner s = new Scanner(file, "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (Exception e) { + // UnsupportedEncodingException wird hier abgefangen + e.printStackTrace(); + } + } + + // Beispiel mit StandardCharsets-Konstanten, die keine Änderung brauchen + static void bla10(File file) { + Scanner s = new Scanner(file, StandardCharsets.UTF_8); + } + + // Beispiel mit Scanner und InputStream, ohne explizite Kodierung (bleibt unverändert) + static void bla11(InputStream is) { + Scanner s = new Scanner(is, StandardCharsets.UTF_8); + } + + // Methode mit Scanner und einer benutzerdefinierten Kodierung als Variable (bleibt unverändert) + private String encodingVar = "ISO-8859-1"; + static void bla12(InputStream is) throws FileNotFoundException { + Scanner s = new Scanner(is, StandardCharsets.ISO_8859_1); + } +} +"""), + FORMATTER( +""" +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.Formatter; + +public class E1 { + + // Methode mit explizitem UTF-8, sollte durch StandardCharsets.UTF_8 ersetzt werden + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s = new Formatter(new File("asdf"), "UTF-8"); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } + + // Methode mit try-catch, die eine Kodierung verwendet und Fehler wirft + static void bli() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "UTF-8"); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } catch (FileNotFoundException | UnsupportedEncodingException e) { + // Der Catch-Block für UnsupportedEncodingException sollte im Cleanup entfernt werden + e.printStackTrace(); + } + } + + // Methode mit benutzerdefinierter Konstante für das Encoding + private static final String ENCODING_UTF8 = "UTF-8"; + + static void blc() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s = new Formatter(new File("asdf"), ENCODING_UTF8); // 'UTF-8' als Konstante + } + + // Methode mit einer ungültigen Kodierung (z.B. 'non-existing-encoding') + static void bld() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); // UnsupportedEncodingException wird hier erwartet + } + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet + static void ble() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird hier behandelt + e.printStackTrace(); + } + } + + // Methode mit StandardCharsets.UTF_8 + static void blf() throws FileNotFoundException { + Formatter s = new Formatter(new File("asdf"), StandardCharsets.UTF_8); // Verwendung von StandardCharsets.UTF_8 + } + + // Beispiel, bei dem das Encoding in einer Variablen gespeichert ist + private String encodingVar = "UTF-8"; + + static void blg() throws FileNotFoundException { + String encoding = "UTF-8"; + Formatter s = new Formatter(new File("asdf"), encoding); // encoding als Variable + } +} +""", """ +package test1; + +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.charset.StandardCharsets; +import java.util.Formatter; + +public class E1 { + + // Methode mit explizitem UTF-8, sollte durch StandardCharsets.UTF_8 ersetzt werden + static void bla() throws FileNotFoundException { + Formatter s = new Formatter(new File("asdf"), StandardCharsets.UTF_8); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } + + // Methode mit try-catch, die eine Kodierung verwendet und Fehler wirft + static void bli() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), StandardCharsets.UTF_8); // 'UTF-8' wird zu StandardCharsets.UTF_8 + } catch (FileNotFoundException e) { + // Der Catch-Block für UnsupportedEncodingException sollte im Cleanup entfernt werden + e.printStackTrace(); + } + } + + // Methode mit benutzerdefinierter Konstante für das Encoding + private static final String ENCODING_UTF8 = "UTF-8"; + + static void blc() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s = new Formatter(new File("asdf"), ENCODING_UTF8); // 'UTF-8' als Konstante + } + + // Methode mit einer ungültigen Kodierung (z.B. 'non-existing-encoding') + static void bld() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // wirft UnsupportedEncodingException + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); // UnsupportedEncodingException wird hier erwartet + } + } + + // Methode, die eine ungültige Kodierung und ein try-catch verwendet + static void ble() throws FileNotFoundException { + try { + Formatter s = new Formatter(new File("asdf"), "non-existing-encoding"); // könnte UnsupportedEncodingException werfen + } catch (FileNotFoundException e) { + // Datei nicht gefunden, hier wird FileNotFoundException behandelt + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // UnsupportedEncodingException wird hier behandelt + e.printStackTrace(); + } + } + + // Methode mit StandardCharsets.UTF_8 + static void blf() throws FileNotFoundException { + Formatter s = new Formatter(new File("asdf"), StandardCharsets.UTF_8); // Verwendung von StandardCharsets.UTF_8 + } + + // Beispiel, bei dem das Encoding in einer Variablen gespeichert ist + private String encodingVar = "UTF-8"; + + static void blg() throws FileNotFoundException { + String encoding = "UTF-8"; + Formatter s = new Formatter(new File("asdf"), encoding); // encoding als Variable + } +} +"""), + 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.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); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(StandardCharsets.UTF_8); + 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), StandardCharsets.UTF_8); + } 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.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(StandardCharsets.UTF_8); + 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), StandardCharsets.UTF_8); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } +} + """); + + String given; + String expected; + + ExplicitEncodingPatternsPreferUTF8(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/Java22/ExplicitEncodingCleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java22/ExplicitEncodingCleanUpTest.java index f4ebda1654f..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 @@ -51,851 +51,26 @@ protected void setUp() throws Exception,UnsupportedCharsetException { } @RegisterExtension - AbstractEclipseJava context = new EclipseJava22(); - - 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.nio.charset.StandardCharsets; -import java.io.FileNotFoundException; - -public class E1 { - void method(String filename) { - Charset cs1= StandardCharsets.UTF_8; - Charset cs1b= StandardCharsets.UTF_8; - 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; - 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.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; - } - } + AbstractEclipseJava context= new EclipseJava22(); @ParameterizedTest @EnumSource(ExplicitEncodingPatterns.class) public void testExplicitEncodingParametrized(ExplicitEncodingPatterns test) throws CoreException { - IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); - ICompilationUnit cu = pack.createCompilationUnit("E1.java", test.given, false, null); + IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); + 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); } @Test public void testExplicitEncodingdonttouch() throws CoreException { - IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); - ICompilationUnit cu = pack.createCompilationUnit("E2.java", + IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu= pack.createCompilationUnit("E2.java", """ package test1; @@ -925,6 +100,7 @@ void method() throws UnsupportedEncodingException, IOException { false, null); context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); + context.enable(CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR); context.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); } 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 9600a33cdea..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 @@ -50,794 +50,22 @@ 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; - } - } + AbstractEclipseJava context= new EclipseJava8(); @ParameterizedTest @EnumSource(ExplicitEncodingPatterns.class) public void testExplicitEncodingParametrized(ExplicitEncodingPatterns test) throws CoreException { - IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); - ICompilationUnit cu = pack.createCompilationUnit("E1.java", test.given, false, null); + IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu= pack.createCompilationUnit("E1.java", test.given, false, null); context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); + context.enable(CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR); context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); } @Test public void testExplicitEncodingdonttouch() throws CoreException { - IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); - ICompilationUnit cu = pack.createCompilationUnit("E2.java", + IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu= pack.createCompilationUnit("E2.java", """ package test1; @@ -867,6 +95,7 @@ void method() throws UnsupportedEncodingException, IOException { false, null); context.enable(CleanUpConstants.EXPLICITENCODING_CLEANUP); + context.enable(CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR); context.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); } 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.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/QuickFixJava8TestSuite.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/QuickFixJava8TestSuite.java index 80207972438..124fd5dd2db 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/QuickFixJava8TestSuite.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/Java8/QuickFixJava8TestSuite.java @@ -13,31 +13,12 @@ *******************************************************************************/ package org.eclipse.jdt.ui.tests.quickfix.Java8; -import java.util.Hashtable; - -import org.junit.jupiter.api.BeforeEach; import org.junit.platform.suite.api.SelectClasses; import org.junit.platform.suite.api.Suite; -import org.eclipse.jdt.testplugin.TestOptions; - -import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; - -import org.eclipse.jdt.internal.ui.JavaPlugin; - @Suite @SelectClasses({ ExplicitEncodingCleanUpTest.class }) public class QuickFixJava8TestSuite { - @BeforeEach - protected void setUp() throws Exception { - Hashtable defaultOptions= TestOptions.getDefaultOptions(); - defaultOptions.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, Integer.toString(120)); - JavaCore.setOptions(defaultOptions); - TestOptions.initializeCodeGenerationOptions(); - // Use load since restore doesn't really restore the defaults. - JavaPlugin.getDefault().getCodeTemplateStore().load(); - } } diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java index 1eed95d8822..074fd548006 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java @@ -475,6 +475,11 @@ public void enable(String key) throws CoreException { commitProfile(); } + public void disable(String key) throws CoreException { + fProfile.getSettings().put(key, CleanUpOptions.FALSE); + commitProfile(); + } + private void commitProfile() throws CoreException { List profiles= CleanUpPreferenceUtil.getBuiltInProfiles(); profiles.add(fProfile); diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/CleanUpConstantsOptions.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/CleanUpConstantsOptions.java index e17ee3ba3e9..91d26fe898a 100644 --- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/CleanUpConstantsOptions.java +++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/CleanUpConstantsOptions.java @@ -210,6 +210,8 @@ private static void setEclipseDefaultSettings(CleanUpOptions options) { options.setOption(CONTROL_STATEMENTS_USE_ADD_ALL, CleanUpOptions.FALSE); options.setOption(EXPLICITENCODING_CLEANUP, CleanUpOptions.FALSE); options.setOption(EXPLICITENCODING_KEEP_BEHAVIOR, CleanUpOptions.TRUE); + options.setOption(EXPLICITENCODING_INSERT_UTF8, CleanUpOptions.FALSE); + options.setOption(EXPLICITENCODING_AGGREGATE_TO_UTF8, CleanUpOptions.FALSE); } private static void setSaveParticipantSettings(CleanUpOptions options) { @@ -400,6 +402,8 @@ private static void setSaveParticipantSettings(CleanUpOptions options) { options.setOption(CONTROL_STATEMENTS_USE_ADD_ALL, CleanUpOptions.FALSE); options.setOption(EXPLICITENCODING_CLEANUP, CleanUpOptions.FALSE); options.setOption(EXPLICITENCODING_KEEP_BEHAVIOR, CleanUpOptions.TRUE); + options.setOption(EXPLICITENCODING_INSERT_UTF8, CleanUpOptions.FALSE); + options.setOption(EXPLICITENCODING_AGGREGATE_TO_UTF8, CleanUpOptions.FALSE); } public static void initDefaults(IPreferenceStore store) { diff --git a/org.eclipse.jdt.ui/plugin.xml b/org.eclipse.jdt.ui/plugin.xml index cc47144dd90..de5e64a3a75 100644 --- a/org.eclipse.jdt.ui/plugin.xml +++ b/org.eclipse.jdt.ui/plugin.xml @@ -7115,10 +7115,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">