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..4ea0a6ec5a7 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; @@ -67,12 +68,43 @@ 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())) { - return false; - } - holder.put(visited,encodingmap.get(argstring3.getLiteralValue().toUpperCase())); - operations.add(fixcore.rewrite(visited, cb, holder)); + + + + + + 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; + } + + + + + + + + +// StringLiteral argstring3= (StringLiteral) arguments.get(0); +// if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) { +// return false; +// } +// holder.put(visited,encodingmap.get(argstring3.getLiteralValue().toUpperCase())); +// operations.add(fixcore.rewrite(visited, cb, holder)); return false; } @@ -81,7 +113,8 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite 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); ASTNodes.replaceButKeepComment(rewrite, visited, callToCharsetDefaultCharset, group); } 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 25df5a0a08b..62d2032f47c 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 @@ -55,55 +55,97 @@ protected void setUp() throws Exception,UnsupportedCharsetException { enum ExplicitEncodingPatterns { - CHARSET(""" - package test1; + 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; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; - public class E1 { - void method(String filename) { - Charset cs1= Charset.forName("UTF-8"); - Charset cs1b= Charset.forName("Utf-8"); - Charset cs2= Charset.forName("UTF-16"); - Charset cs3= Charset.forName("UTF-16BE"); - Charset cs4= Charset.forName("UTF-16LE"); - Charset cs5= Charset.forName("ISO-8859-1"); - Charset cs6= Charset.forName("US-ASCII"); - String result= cs1.toString(); - } - } - } - """, +public class E1 { + + void method(String filename) { + // Ursprüngliche Verwendung von Charset.forName() mit verschiedenen Charsets + Charset cs1 = Charset.forName("UTF-8"); + Charset cs1b = Charset.forName("Utf-8"); // Unterschiedliche Schreibweise (diese sollten gleich behandelt werden) + Charset cs2 = Charset.forName("UTF-16"); + Charset cs3 = Charset.forName("UTF-16BE"); + Charset cs4 = Charset.forName("UTF-16LE"); + Charset cs5 = Charset.forName("ISO-8859-1"); + Charset cs6 = Charset.forName("US-ASCII"); + + // Ausgabe, die durch den Cleanup angepasst wird + System.out.println(cs1.toString()); + System.out.println(cs2.toString()); + + // Beispiel mit einer Variablen + String charsetName = "UTF-8"; // Wird durch eine Variable ersetzt + Charset cs7 = Charset.forName(charsetName); // Umstellung erforderlich + System.out.println(cs7); + + // Testen eines ungültigen Charsets + try { + Charset cs8 = Charset.forName("non-existing-charset"); // Ungültiger Charset + System.out.println(cs8); + } catch (IllegalArgumentException e) { + System.out.println("Fehler: " + e.getMessage()); + } + + // Ein benutzerdefinierter Charset-Test + Charset cs9 = Charset.forName("windows-1252"); + System.out.println(cs9.toString()); + } + + void methodWithVariableCharset(String charsetName) { + Charset cs = Charset.forName(charsetName); // Charset über eine Variable + System.out.println(cs.toString()); + } +} +""", """ package test1; -import java.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(); - } + // Ursprüngliche Verwendung von Charset.forName() mit verschiedenen Charsets + Charset cs1 = StandardCharsets.UTF_8; + Charset cs1b = StandardCharsets.UTF_8; // Unterschiedliche Schreibweise (diese sollten gleich behandelt werden) + Charset cs2 = StandardCharsets.UTF_16; + Charset cs3 = StandardCharsets.UTF_16BE; + Charset cs4 = StandardCharsets.UTF_16LE; + Charset cs5 = StandardCharsets.ISO_8859_1; + Charset cs6 = StandardCharsets.US_ASCII; + + // Ausgabe, die durch den Cleanup angepasst wird + System.out.println(cs1.toString()); + System.out.println(cs2.toString()); + + // Beispiel mit einer Variablen + String charsetName = "UTF-8"; // Wird durch eine Variable ersetzt + Charset cs7 = StandardCharsets.UTF_8; // Umstellung erforderlich + System.out.println(cs7); + + // Testen eines ungültigen Charsets + try { + Charset cs8 = Charset.forName("non-existing-charset"); // Ungültiger Charset + System.out.println(cs8); + } catch (IllegalArgumentException e) { + System.out.println("Fehler: " + e.getMessage()); + } + + // Ein benutzerdefinierter Charset-Test + Charset cs9 = Charset.forName("windows-1252"); + System.out.println(cs9.toString()); + } + + void methodWithVariableCharset(String charsetName) { + Charset cs = Charset.forName(charsetName); // Charset über eine Variable + System.out.println(cs.toString()); } } """),