From 8535ad136bfc836a6047f67ce560ec8fdcc908a0 Mon Sep 17 00:00:00 2001 From: Carsten Hammer Date: Tue, 12 Nov 2024 20:52:14 +0100 Subject: [PATCH] support aggregatedutf8 option --- .../fix/helper/AbstractExplicitEncoding.java | 10 +- ...ByteArrayOutputStreamExplicitEncoding.java | 2 +- .../corext/fix/helper/ChangeBehavior.java | 105 +++- .../ChannelsNewReaderExplicitEncoding.java | 2 +- .../ChannelsNewWriterExplicitEncoding.java | 2 +- .../CharsetForNameExplicitEncoding.java | 2 +- .../helper/FileReaderExplicitEncoding.java | 2 +- .../helper/FileWriterExplicitEncoding.java | 2 +- .../fix/helper/FormatterExplicitEncoding.java | 2 +- .../InputStreamReaderExplicitEncoding.java | 108 ++-- .../OutputStreamWriterExplicitEncoding.java | 2 +- .../helper/PrintStreamExplicitEncoding.java | 2 +- .../helper/PrintWriterExplicitEncoding.java | 2 +- .../PropertiesStoreToXMLExplicitEncoding.java | 2 +- .../fix/helper/ScannerExplicitEncoding.java | 2 +- .../fix/helper/StringExplicitEncoding.java | 2 +- .../StringGetBytesExplicitEncoding.java | 2 +- .../URLDecoderDecodeExplicitEncoding.java | 2 +- .../URLEncoderEncodeExplicitEncoding.java | 3 +- .../Java10/ExplicitEncodingCleanUpTest.java | 3 +- ...ExplicitEncodingPatternsAggregateUTF8.java | 463 ++++++++++-------- 21 files changed, 423 insertions(+), 299 deletions(-) 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 c47918c7b4e..3349cdbae23 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,6 +13,7 @@ *******************************************************************************/ package org.eclipse.jdt.internal.corext.fix.helper; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -28,6 +29,7 @@ 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; @@ -50,6 +52,8 @@ * @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( @@ -69,6 +73,8 @@ static class Nodedata { public ASTNode visited; public String encoding; + + public static Map charsetConstants=new HashMap<>(); } protected static final String ENCODING= "encoding"; //$NON-NLS-1$ @@ -153,7 +159,7 @@ protected void removeUnsupportedEncodingException(final ASTNode visited, TextEdi for (Type exceptionType : thrownExceptions) { if (exceptionType.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION)) { throwsRewrite.remove(exceptionType, group); - importRewriter.removeImport("java.io.UnsupportedEncodingException"); //$NON-NLS-1$ + importRewriter.removeImport(JAVA_IO_UNSUPPORTED_ENCODING_EXCEPTION); } } } else if (parent instanceof TryStatement) { @@ -180,7 +186,7 @@ protected void removeUnsupportedEncodingException(final ASTNode visited, TextEdi } } else if (exceptionType.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION)) { rewrite.remove(catchClause, group); - importRewriter.removeImport("java.io.UnsupportedEncodingException"); //$NON-NLS-1$ + importRewriter.removeImport(JAVA_IO_UNSUPPORTED_ENCODING_EXCEPTION); } } 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 280d7688543..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 @@ -120,7 +120,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit 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 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 76c4987f9cb..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) { @@ -52,8 +58,8 @@ protected String computeCharsetforPreview() { }, ENFORCE_UTF8() { @Override - protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset) { - String charset2= charset==null?"UTF_8":charset; //$NON-NLS-1$ + 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; } @@ -66,29 +72,92 @@ protected String computeCharsetforPreview() { }, ENFORCE_UTF8_AGGREGATE() { @Override - protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset) { - /** - * @TODO not implemented - */ - FieldAccess callToCharsetUTF8 = ast.newFieldAccess(); - callToCharsetUTF8.setName(ast.newSimpleName("ENCODING_UTF_8")); // Verwendet die statische Konstante //$NON-NLS-1$ - callToCharsetUTF8.setExpression(ast.newSimpleName("StandardCharsets")); //$NON-NLS-1$ - return callToCharsetUTF8; + 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 * @@ -144,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 8c3adf4e535..aadeb3c84db 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 @@ -107,7 +107,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit 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 */ 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 ef24252aebf..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 @@ -98,7 +98,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit 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 */ 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 367a794cdb8..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 @@ -97,7 +97,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit 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); ASTNodes.replaceButKeepComment(rewrite, visited, callToCharsetDefaultCharset, group); } 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 28b9e975c83..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 @@ -83,7 +83,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation 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() */ 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 1ebe1c40086..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 @@ -62,7 +62,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation 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() */ 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 7545d6510e3..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 @@ -118,7 +118,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation 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 */ 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 37b22dd5ac7..11425274d55 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 @@ -99,7 +99,8 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation 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 */ @@ -116,64 +117,59 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation @SuppressWarnings("unused") private void removeNLSComment(ASTNode node, CompilationUnitRewrite cuRewrite, TextEditGroup group) { - CompilationUnit unit = cuRewrite.getRoot(); - ASTRewrite rewrite = cuRewrite.getASTRewrite(); - - // Liste aller Kommentare in der CompilationUnit - List comments = unit.getCommentList(); - boolean removed = false; - - for (Comment comment : comments) { - if (comment instanceof LineComment) { - // Hole den Text des Kommentars - String commentContent = getCommentContent(comment, cuRewrite); - System.out.println("Checking comment: " + commentContent); //$NON-NLS-1$ - - if (commentContent != null && commentContent.contains("$NON-NLS-")) { //$NON-NLS-1$ - // Stelle sicher, dass der Kommentar nach dem gegebenen node kommt - if (comment.getStartPosition() > node.getStartPosition()) { - // Versuche, den Kommentar zu entfernen - ASTNode parent = comment.getParent(); - if (parent != null) { - StructuralPropertyDescriptor property = comment.getLocationInParent(); - if (property != null) { - // Kommentar in seiner Elternstruktur entfernen - if (property.isChildListProperty()) { - ListRewrite listRewrite = rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) property); - System.out.println("Removing comment at position: " + comment.getStartPosition()); //$NON-NLS-1$ - listRewrite.remove(comment, group); - removed = true; - } else { - rewrite.remove(comment, group); - removed = true; - } - } else { - System.err.println("No valid location found for comment."); //$NON-NLS-1$ - } - } - } - } - } - } - - if (!removed) { - System.out.println("No NLS comment found to remove."); //$NON-NLS-1$ - } + CompilationUnit unit= cuRewrite.getRoot(); + ASTRewrite rewrite= cuRewrite.getASTRewrite(); + + List comments= unit.getCommentList(); + boolean removed= false; + + for (Comment comment : comments) { + if (comment instanceof LineComment) { + String commentContent= getCommentContent(comment, cuRewrite); + System.out.println("Checking comment: " + commentContent); //$NON-NLS-1$ + + if (commentContent != null && commentContent.contains("$NON-NLS-")) { //$NON-NLS-1$ + if (comment.getStartPosition() > node.getStartPosition()) { + ASTNode parent= comment.getParent(); + if (parent != null) { + StructuralPropertyDescriptor property= comment.getLocationInParent(); + if (property != null) { + if (property.isChildListProperty()) { + ListRewrite listRewrite= rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) property); + System.out.println("Removing comment at position: " + comment.getStartPosition()); //$NON-NLS-1$ + listRewrite.remove(comment, group); + removed= true; + } else { + rewrite.remove(comment, group); + removed= true; + } + } else { + System.err.println("No valid location found for comment."); //$NON-NLS-1$ + } + } + } + } + } + } + + if (!removed) { + System.out.println("No NLS comment found to remove."); //$NON-NLS-1$ + } } private String getCommentContent(Comment comment, CompilationUnitRewrite cuRewrite) { - try { - int startPosition = comment.getStartPosition(); - int length = comment.getLength(); - - ICompilationUnit icu = (ICompilationUnit) cuRewrite.getRoot().getJavaElement(); - if (icu != null) { - return icu.getSource().substring(startPosition, startPosition + length); - } - } catch (Exception e) { - e.printStackTrace(); - } - return null; + try { + int startPosition= comment.getStartPosition(); + int length= comment.getLength(); + + ICompilationUnit icu= (ICompilationUnit) cuRewrite.getRoot().getJavaElement(); + if (icu != null) { + return icu.getSource().substring(startPosition, startPosition + length); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; } @Override diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/OutputStreamWriterExplicitEncoding.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/OutputStreamWriterExplicitEncoding.java index d01e99c4760..36490c527cf 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 @@ -105,7 +105,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation 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 */ 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 2a7da89767c..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 @@ -99,7 +99,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation 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() */ 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 cc2cd86abc0..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 @@ -80,7 +80,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation 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() */ 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 6dfeea2ea6c..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 @@ -102,7 +102,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit 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 */ 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 2da43246472..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 @@ -117,7 +117,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation 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 */ 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 241733e5be1..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 @@ -113,7 +113,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation 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 */ 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 c965f12462e..a72851131c4 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 @@ -93,7 +93,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit 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 */ 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 4c6659a52fd..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 @@ -128,7 +128,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit 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 */ 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 e68f99ff8fc..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 @@ -72,6 +72,7 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation return; } ReferenceHolder datah= new ReferenceHolder<>(); + Nodedata.charsetConstants.clear(); HelperVisitor.callMethodInvocationVisitor(URLEncoder.class, METHOD_ENCODE, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, cb, visited, holder)); } @@ -128,7 +129,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit 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 */ 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 a9925cbdc1d..713e85ebe63 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 @@ -17,7 +17,6 @@ import java.util.Hashtable; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.params.ParameterizedTest; @@ -78,7 +77,7 @@ public void testExplicitEncodingParametrizedPreferUTF8(ExplicitEncodingPatternsP context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); } - @Disabled("Not Implemented") +// @Disabled("Not Implemented") @ParameterizedTest @EnumSource(ExplicitEncodingPatternsAggregateUTF8.class) public void testExplicitEncodingParametrizedAggregateUTF8(ExplicitEncodingPatternsAggregateUTF8 test) throws CoreException { 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 index cafdadc2267..ed74fde51e0 100644 --- 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 @@ -87,26 +87,28 @@ void method(String filename) { """, """ - package test1; +package test1; - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - import java.io.FileNotFoundException; +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); - } - } - } +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; @@ -130,26 +132,29 @@ void method(String filename) { """, """ - package test1; +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; +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 { - Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - """), +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; @@ -170,25 +175,28 @@ void method(String filename) { """, """ - package test1; +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; +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 { - void method(String filename) { - try { - Writer fw=new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } +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( """ @@ -274,22 +282,27 @@ void methodWithThrows(String filename) throws FileNotFoundException, Unsupported public class E1 { - void method(String filename) { + 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"), Charset.defaultCharset()); //$NON-NLS-1$ + 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"), 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$ + 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"), StandardCharsets.UTF_16); //$NON-NLS-1$ //$NON-NLS-2$ + 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"), 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$ + 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(); @@ -299,7 +312,7 @@ void method(String filename) { 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$ + InputStreamReader is8 = new InputStreamReader(new FileInputStream("file8.txt"), E1.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ } catch (FileNotFoundException e) { e.printStackTrace(); } @@ -307,7 +320,7 @@ void methodWithTryCatch(String filename) { 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$ + InputStreamReader is9 = new InputStreamReader(new FileInputStream("file9.txt"), E1.UTF_8); //$NON-NLS-1$ //$NON-NLS-2$ } void methodWithVariableEncoding(String filename) throws UnsupportedEncodingException, FileNotFoundException { @@ -327,7 +340,7 @@ void methodWithNonStandardEncoding(String filename) { // 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$ + InputStreamReader is3 = new InputStreamReader(new FileInputStream(filename), E1.UTF_8); //$NON-NLS-1$ } } """), @@ -440,24 +453,28 @@ void methodWithCatchChange(String filename) { import java.nio.charset.StandardCharsets; public class E1 { - private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + 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), Charset.defaultCharset()); + 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), StandardCharsets.UTF_8); // "UTF-8" als String-Literal + 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), StandardCharsets.ISO_8859_1); // "ISO-8859-1" als String-Literal + 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), StandardCharsets.US_ASCII); // "US-ASCII" als String-Literal + 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), StandardCharsets.UTF_16); // "UTF-16" als String-Literal + 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 @@ -499,7 +516,7 @@ void methodWithThrows(String filename) throws 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 + 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) @@ -516,7 +533,7 @@ void methodWithCatch(String filename) { void methodWithCatchChange(String filename) { try { // Nach dem Cleanup wird "UTF-8" ersetzt - OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8); // keine UnsupportedEncodingException + OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(filename), E1.UTF_8); // keine UnsupportedEncodingException } } } @@ -562,17 +579,22 @@ void method(ReadableByteChannel ch, CharsetDecoder decoder) { 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 String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + 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, 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 + 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 @@ -631,14 +653,18 @@ void method(WritableByteChannel ch, Charset charset) { import java.nio.charset.Charset; public class E1 { - private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + 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, 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 + 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 @@ -673,26 +699,29 @@ void method(String filename) { """, """ - package test1; +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; +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 { - void method(String filename) { - try { - Writer w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset())); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } +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( """ @@ -780,12 +809,17 @@ void methodWithVariableEncoding(String filename) { public class E1 { - // Methode 1: Verwendung von StandardCharsets.UTF_8 statt "UTF-8" als String-Literal + 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(Charset.defaultCharset()); + byte[] bytes = s.getBytes(E1.UTF_8); // Nachher: Umstellung auf StandardCharsets.UTF_8 byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); @@ -799,7 +833,7 @@ 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); + byte[] bytes = s.getBytes(E1.UTF_8); // Nachher: Umstellung auf StandardCharsets.UTF_8 byte[] bytes2 = s.getBytes(StandardCharsets.UTF_8); @@ -813,10 +847,10 @@ 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[] 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(StandardCharsets.UTF_16); // UTF-16 + byte[] bytes4 = s.getBytes(E1.UTF_16); // UTF-16 System.out.println(bytes1.length); // Ausgabe der Längen System.out.println(bytes2.length); @@ -955,28 +989,33 @@ static void methodWithCatchChange(String filename) { public class E1 { - static void bla(String filename) throws FileNotFoundException { + 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, StandardCharsets.UTF_8); // "UTF-8" als String-Literal - String s2 = new String(b, 0, 1, StandardCharsets.UTF_8); // "UTF-8" als String-Literal + 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, 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 + 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, StandardCharsets.US_ASCII); // "US-ASCII" als String-Literal - String s6 = new String(b, 0, 1, StandardCharsets.US_ASCII); // "US-ASCII" als String-Literal + 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, StandardCharsets.UTF_16); // "UTF-16" als String-Literal - String s8 = new String(b, 0, 1, StandardCharsets.UTF_16); // "UTF-16" als String-Literal + 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, StandardCharsets.UTF_8); // bleibt unverändert - String s10 = new String(b, 0, 1, StandardCharsets.UTF_8); // 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 @@ -1014,7 +1053,7 @@ static void methodWithThrows(String filename) throws UnsupportedEncodingExceptio // 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 + String s1 = new String(b, E1.UTF_8); // wirft keine UnsupportedEncodingException mehr } // Methodendeklaration mit try-catch für UnsupportedEncodingException (wird im Cleanup angepasst) @@ -1032,7 +1071,7 @@ static void methodWithCatch(String filename) { static void methodWithCatchChange(String filename) { byte[] b = {(byte) 59}; try { - String s1 = new String(b, StandardCharsets.UTF_8); // keine UnsupportedEncodingException + String s1 = new String(b, E1.UTF_8); // keine UnsupportedEncodingException } } } @@ -1139,18 +1178,20 @@ void storeWithoutTryWithResourcesStandardCharsets(String filename) throws IOExce 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 String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Konstante für Encoding + 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", StandardCharsets.UTF_8); + p.storeToXML(os, "Kommentar", E1.UTF_8); } } @@ -1177,7 +1218,7 @@ void storeWithoutTryWithResources(String filename) throws IOException { Properties p = new Properties(); FileOutputStream os = new FileOutputStream(filename); try { - p.storeToXML(os, "Kommentar", StandardCharsets.UTF_8); + p.storeToXML(os, "Kommentar", E1.UTF_8); } finally { os.close(); // Bleibt erhalten, um die Ressource korrekt zu schließen } @@ -1284,17 +1325,18 @@ static void decodeWithStandardCharset() { import java.nio.charset.StandardCharsets; public class E2 { - private static final String ENCODING_UTF8 = "UTF-8"; // Konstante für Encoding + 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", Charset.defaultCharset()); + 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", StandardCharsets.UTF_8); // sollte in StandardCharsets.UTF_8 geändert werden + 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 @@ -1387,17 +1429,18 @@ static void encodeWithStandardCharset() { import java.nio.charset.StandardCharsets; public class E1 { - private static final String ENCODING_UTF8 = "UTF-8"; // Benutzerdefinierte Kodierungskonstante + 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", Charset.defaultCharset()); + 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", StandardCharsets.UTF_8); // sollte in StandardCharsets.UTF_8 geändert werden + 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 @@ -1520,20 +1563,22 @@ static void bla12(InputStream is) throws FileNotFoundException { public class E1 { - // Methode mit File und explizitem "UTF-8" (wird durch StandardCharsets.UTF_8 ersetzt) + 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, StandardCharsets.UTF_8); + 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, StandardCharsets.UTF_8); + 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", Charset.defaultCharset()); + Scanner s3 = new Scanner("asdf", E1.UTF_8); } // Methode, die eine benutzerdefinierte Konstante für die Kodierung verwendet (bleibt unverändert) @@ -1576,13 +1621,13 @@ static void bla10(File file) { // Beispiel mit Scanner und InputStream, ohne explizite Kodierung (bleibt unverändert) static void bla11(InputStream is) { - Scanner s = new Scanner(is, Charset.defaultCharset()); + 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, StandardCharsets.ISO_8859_1); + Scanner s = new Scanner(is, E1.ISO_8859_1); } } """), @@ -1660,20 +1705,23 @@ static void blg() throws FileNotFoundException { 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 { - // Methode mit explizitem UTF-8, sollte durch StandardCharsets.UTF_8 ersetzt werden + 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"), StandardCharsets.UTF_8); // 'UTF-8' wird zu StandardCharsets.UTF_8 + 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"), StandardCharsets.UTF_8); // 'UTF-8' wird zu StandardCharsets.UTF_8 + 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(); @@ -1761,41 +1809,44 @@ void method(String filename) { """, """ - package test1; +package test1; - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.io.FileNotFoundException; +import java.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()); - 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(); - } - } - } - } +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( """ @@ -1837,43 +1888,45 @@ void method(String filename) { """, """ - package test1; +package test1; - import java.io.ByteArrayOutputStream; - import java.io.InputStreamReader; - import java.io.FileInputStream; - import java.io.FileReader; - import java.io.Reader; - import java.nio.charset.Charset; - import java.nio.charset.StandardCharsets; - import java.io.FileNotFoundException; +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(); - } - } - } - } +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;