-
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.
- Loading branch information
1 parent
4da9adb
commit 6f87d22
Showing
3 changed files
with
120 additions
and
1 deletion.
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
112 changes: 112 additions & 0 deletions
112
...fix/src/org/sandbox/jdt/internal/corext/fix/helper/ChannelsNewReaderExplicitEncoding.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_READER; | ||
|
||
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.newReader(ch,"UTF-8") | ||
* | ||
* Rewrite: Channels.newReader(ch,StandardCharsets.UTF_8) | ||
* | ||
*/ | ||
public class ChannelsNewReaderExplicitEncoding 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_READER, 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 (!"UTF-8".equals(argstring3.getLiteralValue())) { //$NON-NLS-1$ | ||
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 "Charset s=\"StandardCharsets.UTF_8\";\n"+ //$NON-NLS-1$ | ||
"byte[] bytes= s.getBytes("+computeCharsetforPreview(cb)+");\n"; //$NON-NLS-1$ //$NON-NLS-2$ | ||
} | ||
return "Charset s=\"Charset.forName(\"UTF-8\")\";\n"+ //$NON-NLS-1$ | ||
"byte[] bytes= s.getBytes();\n"; //$NON-NLS-1$ | ||
} | ||
} |