<commit_before>package quickfix;

import static edu.umd.cs.findbugs.plugin.eclipse.quickfix.util.ASTUtil.getASTNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Nonnull;

import org.apache.bcel.generic.Type;
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.Expression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;

import com.mebigfatguy.fbcontrib.detect.CharsetIssues;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.plugin.eclipse.quickfix.BugResolution;
import edu.umd.cs.findbugs.plugin.eclipse.quickfix.exception.BugResolutionException;


public class CharsetIssuesResolution extends BugResolution {

	private boolean isName;

	@Override
	protected boolean resolveBindings() {
		return true;
	}
	
	@Override
	public void setOptions(@Nonnull Map<String, String> options){
		isName = Boolean.parseBoolean(options.get("isName"));
	}

	@Override
	protected void repairBug(ASTRewrite rewrite, CompilationUnit workingUnit, BugInstance bug)
			throws BugResolutionException {
		// TODO Auto-generated method stub
		ASTNode node = getASTNode(workingUnit, bug.getPrimarySourceLineAnnotation());
		CSIVisitor csiFinder = new CSIVisitor(isName, rewrite);
        node.accept(csiFinder);

        ASTNode badMethodInvocation = csiFinder.getBadInvocation();

        ASTNode fixedMethodInvocation = csiFinder.createFixedInvocation(rewrite);

        rewrite.replace(badMethodInvocation, fixedMethodInvocation, null);
	}

	private static class CSIVisitor extends ASTVisitor {
	
		private Map<QTypeAndArgs, Object> csiConstructors;
		private Map<QTypeAndArgs, Object> csiMethods;
		
	    private ASTNode fixedAstNode = null;
	    
		private boolean needsToInvokeName;  //should use StandardCharsets.UTF_8.name() instead of StandardCharsets
		private AST rootAstNode;
		private ASTRewrite rewrite;
		private ASTNode csiConstructorInvocation;
		private ASTNode csiMethodInvocation;
		
	
	    public CSIVisitor(boolean needsToInvokeName, ASTRewrite rewrite) {
			if (needsToInvokeName) {
				parseToTypeArgs(CharsetIssues.UNREPLACEABLE_ENCODING_METHODS);
			} else {
				parseToTypeArgs(CharsetIssues.REPLACEABLE_ENCODING_METHODS);
			}
			this.needsToInvokeName = needsToInvokeName;
			this.rootAstNode = rewrite.getAST();
			this.rewrite = rewrite;
		}
	    
		public ASTNode createFixedInvocation(ASTRewrite rewrite) {
			return fixedAstNode;
		}

		public ASTNode getBadInvocation() {
			if (csiConstructorInvocation != null) {
				return csiConstructorInvocation;
			}
			return csiMethodInvocation;
		}

		private void parseToTypeArgs(Map<String, ? extends Object> map) {
			this.csiConstructors = new HashMap<>();
			this.csiMethods = new HashMap<>();
			
			for(Entry<String, ? extends Object> entry:map.entrySet()) {
				QTypeAndArgs struct = new QTypeAndArgs(entry.getKey());
				if (struct.wasConstructor) {
					csiConstructors.put(struct, entry.getValue());
				}
				else {
					csiMethods.put(struct, entry.getValue());
				}
			}
			
		}
		
		@SuppressWarnings("unchecked")
		@Override
		public boolean visit(MethodInvocation node) {
	        if (foundThingToReplace()) {
	            return false;
	        }
	        QTypeAndArgs key = new QTypeAndArgs(node);
			
			if (csiMethods.containsKey(key)) {
				List<Expression> arguments = (List<Expression>) node.arguments();
				Integer indexVal = (Integer) csiMethods.get(key);		
				int indexOfArgumentToReplace = (arguments.size() - indexVal) - 1; 
	
				//if this was a constant string, resolveConstantExpressionValue() will be nonnull
				Expression argument = arguments.get(indexOfArgumentToReplace);
				if (null != argument.resolveConstantExpressionValue()) {
					this.csiMethodInvocation = node;
					MethodInvocation newNode = rootAstNode.newMethodInvocation();
					newNode.setName(rootAstNode.newSimpleName(node.getName().getIdentifier()));
					
					List<Expression> newArgs = newNode.arguments();
					
					for(int i = 0;i< arguments.size(); i++) {
						if (i != indexOfArgumentToReplace) {
							newArgs.add((Expression) rewrite.createCopyTarget(arguments.get(i)));
						} else {
							newArgs.add(makeCharsetReplacement(argument));
						}
					}
					fixedAstNode = newNode;
					return false;  //don't keep parsing
				}
	
			}<commit_msg>getBytes works properly, imports and all<commit_after>package quickfix;

import static edu.umd.cs.findbugs.plugin.eclipse.quickfix.util.ASTUtil.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Nonnull;

import org.apache.bcel.generic.Type;
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.Expression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;

import com.mebigfatguy.fbcontrib.detect.CharsetIssues;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.plugin.eclipse.quickfix.BugResolution;
import edu.umd.cs.findbugs.plugin.eclipse.quickfix.exception.BugResolutionException;


public class CharsetIssuesResolution extends BugResolution {

	private boolean isName;

	@Override
	protected boolean resolveBindings() {
		return true;
	}
	
	@Override
	public void setOptions(@Nonnull Map<String, String> options){
		isName = Boolean.parseBoolean(options.get("isName"));
	}

	@Override
	protected void repairBug(ASTRewrite rewrite, CompilationUnit workingUnit, BugInstance bug)
			throws BugResolutionException {
		ASTNode node = getASTNode(workingUnit, bug.getPrimarySourceLineAnnotation());
		CSIVisitor csiFinder = new CSIVisitor(isName, rewrite);
        node.accept(csiFinder);

        ASTNode badMethodInvocation = csiFinder.getBadInvocation();

        ASTNode fixedMethodInvocation = csiFinder.createFixedInvocation(rewrite);

        rewrite.replace(badMethodInvocation, fixedMethodInvocation, null);
        addImports(rewrite, workingUnit, "java.nio.charset.StandardCharsets");
	}

	private static class CSIVisitor extends ASTVisitor {
	
		private Map<QTypeAndArgs, Object> csiConstructors;
		private Map<QTypeAndArgs, Object> csiMethods;
		
	    private ASTNode fixedAstNode = null;
	    
		private boolean needsToInvokeName;  //should use StandardCharsets.UTF_8.name() instead of StandardCharsets
		private AST rootAstNode;
		private ASTRewrite rewrite;
		private ASTNode csiConstructorInvocation;
		private ASTNode csiMethodInvocation;
		
	
	    public CSIVisitor(boolean needsToInvokeName, ASTRewrite rewrite) {
			if (needsToInvokeName) {
				parseToTypeArgs(CharsetIssues.UNREPLACEABLE_ENCODING_METHODS);
			} else {
				parseToTypeArgs(CharsetIssues.REPLACEABLE_ENCODING_METHODS);
			}
			this.needsToInvokeName = needsToInvokeName;
			this.rootAstNode = rewrite.getAST();
			this.rewrite = rewrite;
		}
	    
		public ASTNode createFixedInvocation(ASTRewrite rewrite) {
			return fixedAstNode;
		}

		public ASTNode getBadInvocation() {
			if (csiConstructorInvocation != null) {
				return csiConstructorInvocation;
			}
			return csiMethodInvocation;
		}

		private void parseToTypeArgs(Map<String, ? extends Object> map) {
			this.csiConstructors = new HashMap<>();
			this.csiMethods = new HashMap<>();
			
			for(Entry<String, ? extends Object> entry:map.entrySet()) {
				QTypeAndArgs struct = new QTypeAndArgs(entry.getKey());
				if (struct.wasConstructor) {
					csiConstructors.put(struct, entry.getValue());
				}
				else {
					csiMethods.put(struct, entry.getValue());
				}
			}
			
		}
		
		@SuppressWarnings("unchecked")
		@Override
		public boolean visit(MethodInvocation node) {
	        if (foundThingToReplace()) {
	            return false;
	        }
	        QTypeAndArgs key = new QTypeAndArgs(node);
			
			if (csiMethods.containsKey(key)) {
				List<Expression> arguments = (List<Expression>) node.arguments();
				Integer indexVal = (Integer) csiMethods.get(key);		
				int indexOfArgumentToReplace = (arguments.size() - indexVal) - 1; 
	
				//if this was a constant string, resolveConstantExpressionValue() will be nonnull
				Expression argument = arguments.get(indexOfArgumentToReplace);
				if (null != argument.resolveConstantExpressionValue()) {
					this.csiMethodInvocation = node;
					MethodInvocation newNode = rootAstNode.newMethodInvocation();
					newNode.setExpression((Expression) rewrite.createCopyTarget(node.getExpression()));
					newNode.setName(rootAstNode.newSimpleName(node.getName().getIdentifier()));
					
					List<Expression> newArgs = newNode.arguments();
					
					for(int i = 0;i< arguments.size(); i++) {
						if (i != indexOfArgumentToReplace) {
							newArgs.add((Expression) rewrite.createCopyTarget(arguments.get(i)));
						} else {
							newArgs.add(makeCharsetReplacement(argument));
						}
					}
					fixedAstNode = newNode;
					return false;  //don't keep parsing
				}
	
			}
{'fraction_non_alphanumeric': 0.10126987514672928, 'fraction_numerical': 0.0006402731832248426, 'mean_word_length': 4.206666666666667, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '964828', 'n_tokens_mistral': 3354, 'n_tokens_neox': 2841, 'n_words': 548}