Skip to content

Commit

Permalink
backport changes from jdt.ui
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Sep 15, 2024
1 parent df6ef5c commit c8f90f3
Show file tree
Hide file tree
Showing 22 changed files with 1,145 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
import java.util.Set;

import org.eclipse.core.runtime.CoreException;

import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer;

import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Map;
import java.util.Set;

import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
Expand All @@ -29,10 +31,10 @@
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;

import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;

Expand All @@ -50,13 +52,13 @@ public abstract class AbstractExplicitEncoding<T extends ASTNode> {
);
static Set<String> encodings=encodingmap.keySet();
public enum ChangeBehavior {KEEP, USE_UTF8, USE_UTF8_AGGREGATE}

static class Nodedata {
public boolean replace;
public ASTNode visited;
public String encoding;
}


protected static final String ENCODING = "encoding"; //$NON-NLS-1$
protected static final String REPLACE = "replace"; //$NON-NLS-1$
Expand Down Expand Up @@ -111,7 +113,7 @@ protected String computeCharsetforPreview(ChangeBehavior cb) {
*
* @param cuRewrite CompilationUnitRewrite
* @param ast AST
* @param charset
* @param charset
* @return FieldAccess that returns Charset for UTF_8
*/
protected FieldAccess addCharsetUTF8(CompilationUnitRewrite cuRewrite, AST ast, String charset) {
Expand All @@ -125,7 +127,7 @@ protected FieldAccess addCharsetUTF8(CompilationUnitRewrite cuRewrite, AST ast,
*/
FieldAccess fieldaccess= ast.newFieldAccess();
fieldaccess.setExpression(ASTNodeFactory.newName(ast, StandardCharsets.class.getSimpleName()));

fieldaccess.setName(ast.newSimpleName(charset));
return fieldaccess;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@
import java.util.List;
import java.util.Set;

import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
import org.eclipse.jdt.core.dom.AST;
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.StringLiteral;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;


/**
Expand All @@ -49,7 +50,7 @@
* } catch (UnsupportedEncodingException e1) {
* e1.printStackTrace();
* }</pre>
*
*
* since Java 10
* <pre>ByteArrayOutputStream ba=new ByteArrayOutputStream();
* String result=ba.toString(Charset.defaultCharset());
Expand Down Expand Up @@ -82,7 +83,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<
nd.visited=argstring3;
holder.put(visited,nd);
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}
if (ASTNodes.usesGivenSignature(visited, ByteArrayOutputStream.class.getCanonicalName(), METHOD_TOSTRING)) {
Expand All @@ -92,7 +92,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<
nd2.visited=visited;
holder.put(visited,nd2);
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@
import java.util.List;
import java.util.Set;

import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
* Java 10
*
*
* Change
*
* Find: Reader r=Channels.newReader(ch,"UTF-8")
*
*
* Rewrite: Reader r=Channels.newReader(ch,StandardCharsets.UTF_8)
*
*/
Expand Down Expand Up @@ -98,10 +100,10 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Reader r=\"Channels.newReader(ch,StandardCharsets.UTF_8)\";\n"+ //$NON-NLS-1$
return "Reader r=Channels.newReader(ch,StandardCharsets.UTF_8);\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}
return "Reader r=\"Channels.newReader(ch,\"UTF-8\")\";\n"+ //$NON-NLS-1$
return "Reader r=Channels.newReader(ch,\"UTF-8\");\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@
import java.util.List;
import java.util.Set;

import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
* Java 10
*
*
* Find: Channels.newWriter(ch,"UTF-8")
*
* Rewrite: Channels.newWriter(ch,StandardCharsets.UTF_8)
Expand Down Expand Up @@ -97,10 +99,10 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Writer w=\"Channels.newWriter(ch, StandardCharsets.UTF_8)\";\n"+ //$NON-NLS-1$
return "Writer w=Channels.newWriter(ch, StandardCharsets.UTF_8);\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}
return "Writer w=\"Channels.newWriter(ch, \"UTF-8\")\";\n"+ //$NON-NLS-1$
return "Writer w=Channels.newWriter(ch, \"UTF-8\");\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@
import java.util.List;
import java.util.Set;

import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;


import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
* Java 18
*
*
* Find: Charset.forName("UTF-8")
*
* Rewrite: StandardCharsets.UTF_8
Expand Down Expand Up @@ -92,10 +95,10 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Charset s=\"StandardCharsets.UTF_8\";\n"+ //$NON-NLS-1$
return "Charset s=StandardCharsets.UTF_8;\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}
return "Charset s=\"Charset.forName(\"UTF-8\")\";\n"+ //$NON-NLS-1$
return "Charset s=Charset.forName(\"UTF-8\");\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,28 @@
import java.util.List;
import java.util.Set;

import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;


import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
* Change
*
* Find: Reader is=new FileReader("file.txt")
*
*
* Rewrite: Reader is=new InputStreamReader(new FileInputStream("file.txt"),Charset.defaultCharset());
*
* Charset.defaultCharset() is available since Java 1.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
import java.io.OutputStreamWriter;
import java.util.Set;

import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;

import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
Expand Down
Loading

0 comments on commit c8f90f3

Please sign in to comment.