-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add first part of channelsNewWriter and PrintStream
- Loading branch information
1 parent
6f87d22
commit 88cfe82
Showing
12 changed files
with
277 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...fix/src/org/sandbox/jdt/internal/corext/fix/helper/ChannelsNewWriterExplicitEncoding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Carsten Hammer. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Carsten Hammer | ||
*******************************************************************************/ | ||
package org.sandbox.jdt.internal.corext.fix.helper; | ||
|
||
import static org.sandbox.jdt.internal.common.LibStandardNames.METHOD_NEW_WRITER; | ||
|
||
import java.nio.channels.Channels; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
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.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; | ||
/** | ||
* Find: Channels.newWriter(ch,"UTF-8") | ||
* | ||
* Rewrite: Channels.newWriter(ch,StandardCharsets.UTF_8) | ||
* | ||
*/ | ||
public class ChannelsNewWriterExplicitEncoding extends AbstractExplicitEncoding<MethodInvocation> { | ||
|
||
@Override | ||
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) { | ||
HelperVisitor.callMethodInvocationVisitor(Channels.class, METHOD_NEW_WRITER, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder)); | ||
} | ||
|
||
private boolean processFoundNode(UseExplicitEncodingFixCore fixcore, | ||
Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed, ChangeBehavior cb, | ||
MethodInvocation visited, ReferenceHolder<String, Object> holder) { | ||
List<ASTNode> arguments= visited.arguments(); | ||
if(nodesprocessed.contains(visited) || (arguments.size()>1)) { | ||
return false; | ||
} | ||
switch (arguments.size()) { | ||
case 1: | ||
if(!(arguments.get(0) instanceof StringLiteral)) { | ||
return false; | ||
} | ||
StringLiteral argstring3= (StringLiteral) arguments.get(0); | ||
if (!encodings.contains(argstring3.getLiteralValue())) { | ||
return false; | ||
} | ||
holder.put(ENCODING,StandardCharsets.UTF_8); | ||
holder.put(REPLACE,argstring3); | ||
break; | ||
case 0: | ||
break; | ||
default: | ||
return false; | ||
} | ||
operations.add(fixcore.rewrite(visited, cb, datah)); | ||
nodesprocessed.add(visited); | ||
return false; | ||
} | ||
|
||
@Override | ||
public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, | ||
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<String, Object> data) { | ||
ASTRewrite rewrite= cuRewrite.getASTRewrite(); | ||
AST ast= cuRewrite.getRoot().getAST(); | ||
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) { | ||
/** | ||
* For Java 1.4 and older just do nothing | ||
*/ | ||
return; | ||
} | ||
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, (Charset) data.get(ENCODING)); | ||
// ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); | ||
// listRewrite.insertLast(callToCharsetDefaultCharset, group); | ||
|
||
ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); | ||
if(data.get(ENCODING)!= null) { | ||
listRewrite.replace((ASTNode) data.get(REPLACE), callToCharsetDefaultCharset, group); | ||
} else { | ||
listRewrite.insertLast(callToCharsetDefaultCharset, group); | ||
} | ||
} | ||
|
||
@Override | ||
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { | ||
if(afterRefactoring) { | ||
return "Writer w=\"Channels.newWriter(StandardCharsets.UTF_8)\";\n"+ //$NON-NLS-1$ | ||
"byte[] bytes= s.getBytes("+computeCharsetforPreview(cb)+");\n"; //$NON-NLS-1$ //$NON-NLS-2$ | ||
} | ||
return "Writer w=\"Channels.newWriter(\"UTF-8\")\";\n"+ //$NON-NLS-1$ | ||
"byte[] bytes= s.getBytes();\n"; //$NON-NLS-1$ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
..._quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/PrintStreamExplicitEncoding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Carsten Hammer. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Carsten Hammer | ||
*******************************************************************************/ | ||
package org.sandbox.jdt.internal.corext.fix.helper; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.FileOutputStream; | ||
import java.io.FileReader; | ||
import java.io.OutputStreamWriter; | ||
import java.io.PrintStream; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.eclipse.jdt.core.dom.AST; | ||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.ASTVisitor; | ||
import org.eclipse.jdt.core.dom.ClassInstanceCreation; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.dom.ITypeBinding; | ||
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: Stream fw=new PrintStream("file.txt", "UTF-8") | ||
* | ||
* Rewrite: Stream fw=new PrintStream("file.txt", StandardCharsets.UTF_8) | ||
* | ||
* Find: Stream fw=new PrintStream(new File("file.txt"), "UTF-8") | ||
* | ||
* Rewrite: Stream fw=new PrintStream(new File("file.txt"), StandardCharsets.UTF_8) | ||
* | ||
*/ | ||
public class PrintStreamExplicitEncoding extends AbstractExplicitEncoding<ClassInstanceCreation> { | ||
|
||
@Override | ||
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) { | ||
HelperVisitor.callClassInstanceCreationVisitor(PrintStream.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder)); | ||
} | ||
|
||
private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<CompilationUnitRewriteOperation> operations, | ||
Set<ASTNode> nodesprocessed, ChangeBehavior cb, ClassInstanceCreation visited, | ||
ReferenceHolder<String, Object> holder) { | ||
List<ASTNode> arguments= visited.arguments(); | ||
if(nodesprocessed.contains(visited) || (arguments.size()>2)) { | ||
return false; | ||
} | ||
switch (arguments.size()) { | ||
case 1: | ||
break; | ||
case 2: | ||
if(!(arguments.get(1) instanceof StringLiteral)) { | ||
return false; | ||
} | ||
StringLiteral argstring3= (StringLiteral) arguments.get(1); | ||
if (!encodings.contains(argstring3.getLiteralValue())) { | ||
return false; | ||
} | ||
holder.put(ENCODING,StandardCharsets.UTF_8); | ||
holder.put(REPLACE,argstring3); | ||
break; | ||
default: | ||
return false; | ||
} | ||
operations.add(fixcore.rewrite(visited, cb, holder)); | ||
nodesprocessed.add(visited); | ||
return false; | ||
} | ||
|
||
@Override | ||
public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, | ||
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<String, Object> data) { | ||
ASTRewrite rewrite= cuRewrite.getASTRewrite(); | ||
AST ast= cuRewrite.getRoot().getAST(); | ||
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) { | ||
/** | ||
* For Java 1.4 and older just do nothing | ||
*/ | ||
return; | ||
} | ||
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, (Charset) data.get(ENCODING)); | ||
/** | ||
* new FileOutputStream(<filename>) | ||
*/ | ||
ClassInstanceCreation fosclassInstance= ast.newClassInstanceCreation(); | ||
fosclassInstance.setType(ast.newSimpleType(addImport(FileOutputStream.class.getCanonicalName(), cuRewrite, ast))); | ||
fosclassInstance.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((ASTNode) visited.arguments().get(0)))); | ||
/** | ||
* new OutputStreamWriter(new FileOutputStream(<filename>)) | ||
*/ | ||
ClassInstanceCreation oswclassInstance= ast.newClassInstanceCreation(); | ||
oswclassInstance.setType(ast.newSimpleType(addImport(OutputStreamWriter.class.getCanonicalName(), cuRewrite, ast))); | ||
oswclassInstance.arguments().add(fosclassInstance); | ||
oswclassInstance.arguments().add(callToCharsetDefaultCharset); | ||
/** | ||
* new BufferedWriter(new OutputStreamWriter(new FileOutputStream(<filename>))) | ||
*/ | ||
ClassInstanceCreation bwclassInstance= ast.newClassInstanceCreation(); | ||
bwclassInstance.setType(ast.newSimpleType(addImport(BufferedWriter.class.getCanonicalName(), cuRewrite, ast))); | ||
bwclassInstance.arguments().add(oswclassInstance); | ||
|
||
ASTNodes.replaceButKeepComment(rewrite, visited, bwclassInstance, group); | ||
} | ||
|
||
@Override | ||
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) { | ||
if(afterRefactoring) { | ||
return "Stream w=new PrintStream(\"out.txt\","+computeCharsetforPreview(cb)+"));\n"+ //$NON-NLS-1$ //$NON-NLS-2$ | ||
"Stream w=new PrintStream(\"out.txt\",StandardCharsets.UTF_8));\n"+ //$NON-NLS-1$ | ||
"Stream w=new PrintStream(new File(\"out.txt\"),StandardCharsets.UTF_8));\n"; //$NON-NLS-1$ | ||
} | ||
return "Stream w=new PrintStream(\"out.txt\");\n"+ //$NON-NLS-1$ | ||
"Stream w=new PrintStream(\"out.txt\",\"UTF-8\");\n"+ //$NON-NLS-1$ | ||
"Stream w=new PrintStream(new File(\"out.txt\"),\"UTF-8\");\n"; //$NON-NLS-1$ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
sandbox_platform_helper_test/src/org/sandbox/jdt/ui/tests/quickfix/E2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.sandbox.jdt.ui.tests.quickfix; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
import org.eclipse.core.runtime.IStatus; | ||
import org.eclipse.core.runtime.Status; | ||
public class E2 { | ||
void bla(Throwable e) throws UnsupportedEncodingException { | ||
IStatus status = new Status(IStatus.WARNING, "plugin id","important message",null); | ||
byte[] bytes = "asdf".getBytes("Utf-8"); | ||
} | ||
} |