Skip to content

Commit

Permalink
add option to remove unnecessary nls tags (not working)
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Dec 8, 2024
1 parent 52d49e5 commit 3ea68cb
Show file tree
Hide file tree
Showing 13 changed files with 1,801 additions and 1,772 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ public ICleanUpFix createFix(final CleanUpContext context) throws CoreException
if (operations.isEmpty()) {
return null;
}

CompilationUnitRewriteOperation[] array= operations.toArray(new CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation[0]);
return new CompilationUnitRewriteOperationsFixCore(ExplicitEncodingCleanUpFix_refactor,
compilationUnit, operations.toArray(new CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation[0]));
compilationUnit, array);
}

private ChangeBehavior computeRefactorDeepth() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@
import java.util.Map;
import java.util.Set;

import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jface.text.BadLocationException;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CatchClause;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldDeclaration;
Expand All @@ -46,6 +53,9 @@
import org.eclipse.jdt.internal.common.ReferenceHolder;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSElement;
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSLine;
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSScanner;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;

/**
Expand Down Expand Up @@ -74,7 +84,7 @@ static class Nodedata {

public String encoding;

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

protected static final String ENCODING= "encoding"; //$NON-NLS-1$
Expand Down Expand Up @@ -201,4 +211,88 @@ protected void removeUnsupportedEncodingException(final ASTNode visited, TextEdi
}
}
}

protected void removeNLSComment(CompilationUnitRewrite cuRewrite, ASTNode node, TextEditGroup group, ASTRewrite rewrite) {
CompilationUnit unit= cuRewrite.getRoot();
ICompilationUnit icu= (ICompilationUnit) cuRewrite.getRoot().getJavaElement();

if (icu == null) {
System.err.println("ICompilationUnit is null."); //$NON-NLS-1$
return;
}

String source= null;
try {
source= icu.getSource();
} catch (JavaModelException e) {
e.printStackTrace();
}

if (source == null) {
System.err.println("Source code is null."); //$NON-NLS-1$
return;
}

int startLine= unit.getLineNumber(node.getStartPosition());
int endOfLine= unit.getPosition(startLine + 1, 0);
String lineText= source.substring(node.getStartPosition(), endOfLine);

try {
NLSLine[] lines= NLSScanner.scan(lineText);

for (NLSLine nlsLine : lines) {
if (nlsLine != null && isConsistent(nlsLine, true)) {
for (NLSElement element : nlsLine.getElements()) {
if (element.hasTag()) {
Comment comment= findCommentNode(unit, element.getTagText());
if (comment != null) {
// Jetzt entfernen wir den Kommentar als ReplaceEdit.
ReplaceEdit edit= new ReplaceEdit(comment.getStartPosition(), comment.getLength(), ""); //$NON-NLS-1$
group.addTextEdit(edit); // Die Bearbeitung zur TextEditGroup hinzufügen
System.out.println("Removed NLS comment: " + comment.getStartPosition()); //$NON-NLS-1$
}
}
}
}
}
} catch (InvalidInputException | BadLocationException e) {
e.printStackTrace();
}
}

private boolean isConsistent(NLSLine nlsLine, boolean isTagged) {
NLSElement[] elements= nlsLine.getElements();
for (NLSElement element : elements) {
if (element.hasTag() != isTagged) {
return false;
}
}
return true;
}

private Comment findCommentNode(CompilationUnit unit, String commentContent) {
List<Comment> commentList= unit.getCommentList();
for (Comment comment : commentList) {
String content= getCommentContent(comment, unit);
if (content != null && content.equals(commentContent)) {
return comment;
}
}
return null;
}

private String getCommentContent(Comment comment, CompilationUnit unit) {
try {
// Holen des ICompilationUnit-Objekts aus dem CompilationUnit
ICompilationUnit cu= (ICompilationUnit) unit.getJavaElement();
if (cu != null) {
// Abrufen des Quelltextes des gesamten ICompilationUnit
String source= cu.getSource();
return source.substring(comment.getStartPosition(), comment.getStartPosition() + comment.getLength());
}
} catch (JavaModelException e) {
e.printStackTrace();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit
ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY);
if (nodedata.replace) {
listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group);
removeNLSComment(cuRewrite, visited, group, rewrite);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@

import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.LineComment;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.eclipse.jdt.internal.common.HelperVisitor;
Expand Down Expand Up @@ -93,83 +86,23 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
}

@Override
public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite,
public void rewrite(UseExplicitEncodingFixCore upp, ClassInstanceCreation visited, CompilationUnitRewrite cuRewrite,
TextEditGroup group, ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
Nodedata nodedata= (Nodedata) data.get(visited);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
Nodedata nodedata = (Nodedata) data.get(visited);

ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding, Nodedata.charsetConstants);
/**
* Add Charset.defaultCharset() as second (last) parameter
*/
ListRewrite listRewrite= rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY);
ASTNode callToCharsetDefaultCharset = cb.computeCharsetASTNode(cuRewrite, cuRewrite.getRoot().getAST(),
nodedata.encoding, Nodedata.charsetConstants);

ListRewrite listRewrite = rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY);
if (nodedata.replace) {
listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group);
// Remove NLS comment
// removeNLSComment(visited, cuRewrite, group);
removeNLSComment(cuRewrite, visited, group, rewrite);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
removeUnsupportedEncodingException(visited, group, rewrite, importRewriter);
}

@SuppressWarnings("unused")
private void removeNLSComment(ASTNode node, CompilationUnitRewrite cuRewrite, TextEditGroup group) {
CompilationUnit unit= cuRewrite.getRoot();
ASTRewrite rewrite= cuRewrite.getASTRewrite();

List<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;
removeUnsupportedEncodingException(visited, group, rewrite, cuRewrite.getImportRewrite());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final ClassInstanceCreation
ListRewrite listRewrite= rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY);
if (nodedata.replace) {
listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group);
removeNLSComment(cuRewrite, visited, group, rewrite);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visit
ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY);
if (nodedata.replace) {
listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group);
removeNLSComment(cuRewrite, visited, group, rewrite);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public void testExplicitEncodingParametrizedKeepBehavior(ExplicitEncodingPattern
context.enable(CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR);
context.disable(CleanUpConstants.EXPLICITENCODING_INSERT_UTF8);
context.disable(CleanUpConstants.EXPLICITENCODING_AGGREGATE_TO_UTF8);
// context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null);
context.enable(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS);
context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,42 @@
public enum ExplicitEncodingPatternsKeepBehavior {

CHARSET("""
package test1;
package test1;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
public class E1 {
void method(String filename) {
Charset cs1= Charset.forName("UTF-8");
Charset cs1b= Charset.forName("Utf-8");
Charset cs2= Charset.forName("UTF-16");
Charset cs3= Charset.forName("UTF-16BE");
Charset cs4= Charset.forName("UTF-16LE");
Charset cs5= Charset.forName("ISO-8859-1");
Charset cs6= Charset.forName("US-ASCII");
String result= cs1.toString();
}
}
}
public class E1 {
@SuppressWarnings("unused")
void method(String filename) {
Charset cs1= Charset.forName("UTF-8"); //$NON-NLS-1$
Charset cs1b= Charset.forName("Utf-8"); //$NON-NLS-1$
Charset cs2= Charset.forName("UTF-16"); //$NON-NLS-1$
Charset cs3= Charset.forName("UTF-16BE"); //$NON-NLS-1$
Charset cs4= Charset.forName("UTF-16LE"); //$NON-NLS-1$
Charset cs5= Charset.forName("ISO-8859-1"); //$NON-NLS-1$
Charset cs6= Charset.forName("US-ASCII"); //$NON-NLS-1$
String result= cs1.toString();
}
}
""",
"""
package test1;
package test1;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
public class E1 {
void method(String filename) {
Charset cs1= Charset.forName("UTF-8");
Charset cs1b= Charset.forName("Utf-8");
Charset cs2= Charset.forName("UTF-16");
Charset cs3= Charset.forName("UTF-16BE");
Charset cs4= Charset.forName("UTF-16LE");
Charset cs5= Charset.forName("ISO-8859-1");
Charset cs6= Charset.forName("US-ASCII");
String result= cs1.toString();
}
}
}
public class E1 {
@SuppressWarnings("unused")
void method(String filename) {
Charset cs1= Charset.forName("UTF-8"); //$NON-NLS-1$
Charset cs1b= Charset.forName("Utf-8"); //$NON-NLS-1$
Charset cs2= Charset.forName("UTF-16"); //$NON-NLS-1$
Charset cs3= Charset.forName("UTF-16BE"); //$NON-NLS-1$
Charset cs4= Charset.forName("UTF-16LE"); //$NON-NLS-1$
Charset cs5= Charset.forName("ISO-8859-1"); //$NON-NLS-1$
Charset cs6= Charset.forName("US-ASCII"); //$NON-NLS-1$
String result= cs1.toString();
}
}
"""),
BYTEARRAYOUTSTREAM("""
package test1;
Expand Down
Loading

0 comments on commit 3ea68cb

Please sign in to comment.