Skip to content

Commit

Permalink
support aggregatedutf8 option
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Dec 14, 2024
1 parent 626170c commit 8535ad1
Show file tree
Hide file tree
Showing 21 changed files with 423 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -50,6 +52,8 @@
* @param <T> Type found in Visitor
*/
public abstract class AbstractExplicitEncoding<T extends ASTNode> {
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<String, String> encodingmap= Map.of(
Expand All @@ -69,6 +73,8 @@ static class Nodedata {
public ASTNode visited;

public String encoding;

public static Map<String, QualifiedName> charsetConstants=new HashMap<>();
}

protected static final String ENCODING= "encoding"; //$NON-NLS-1$
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, QualifiedName> charsetConstants) {
Expression callToCharsetDefaultCharset= null;

if (charset != null) {
Expand All @@ -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<String, QualifiedName> charsetConstants) {
String charset2= charset == null ? "UTF_8" : charset; //$NON-NLS-1$
Expression callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast, charset2);
return callToCharsetDefaultCharset;
}
Expand All @@ -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<String, QualifiedName> 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<String, QualifiedName> 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
*
Expand Down Expand Up @@ -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<String, QualifiedName> charsetConstants) {
Expression callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, charset, charsetConstants);
/**
* Add second call to Charset.defaultCharset().displayName()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation
TextEditGroup group, ChangeBehavior cb, ReferenceHolder<ASTNode, Object> 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(<filename>)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation
TextEditGroup group, ChangeBehavior cb, ReferenceHolder<ASTNode, Object> 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(<filename>)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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<Comment> 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<Comment> 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
Expand Down
Loading

0 comments on commit 8535ad1

Please sign in to comment.