<commit_before>package fr.inria.diverse.melange.utils

import org.eclipse.jdt.core.dom.ASTVisitor
import java.util.List
import java.util.Map
import org.eclipse.jdt.core.dom.ImportDeclaration
import org.eclipse.jdt.core.dom.SimpleType
import org.eclipse.jdt.core.dom.Name
import org.eclipse.jdt.core.dom.MethodInvocation
import org.eclipse.jdt.core.dom.SimpleName
import org.eclipse.jdt.core.dom.MethodDeclaration
import org.eclipse.jdt.core.dom.QualifiedName
import org.eclipse.jdt.core.dom.CastExpression
import org.eclipse.jdt.core.dom.TypeDeclaration
import org.eclipse.jdt.core.dom.NormalAnnotation
import org.eclipse.jdt.core.dom.MemberValuePair
import org.eclipse.jdt.core.dom.CompilationUnit
import org.eclipse.jdt.core.dom.ASTNode
import org.eclipse.jdt.core.dom.NameQualifiedType
import org.eclipse.jdt.core.dom.QualifiedType
import org.eclipse.jdt.core.dom.EnhancedForStatement
import org.eclipse.jdt.core.dom.VariableDeclarationStatement
import org.eclipse.jdt.core.dom.Block
import org.eclipse.jdt.core.dom.Statement
import org.eclipse.jdt.core.dom.Expression
import org.eclipse.jdt.core.dom.VariableDeclarationFragment
import org.eclipse.jdt.core.dom.SingleVariableDeclaration
import org.eclipse.jdt.core.dom.ForStatement
import org.eclipse.jdt.core.dom.VariableDeclarationExpression
import org.eclipse.jdt.core.dom.AST
import com.google.common.collect.SetMultimap

class RenamerVisitor extends ASTVisitor{
	
	List<Pair<String,String>> classRules
	List<Pair<String,String>> packageRules
	List<Pair<String,String>> propertiesRules
	SetMultimap<String,Pair<String,String>> propertiesAspectRules
	
	Map<ImportDeclaration,Name> newImportsNames
	Map<SimpleType,Name> newSimpleTypesNames
	Map<MethodInvocation,SimpleName> newMethodNames
	Map<MethodDeclaration,SimpleName> newMethodDeclNames
	
	new(List<Pair<String,String>> classRenaming, List<Pair<String,String>> packageRenaming, List<Pair<String,String>> propertiesRenaming, SetMultimap<String,Pair<String,String>> propertiesAspectRenaming) {
		classRules = classRenaming
		packageRules = packageRenaming
		propertiesRules = propertiesRenaming
		propertiesAspectRules = propertiesAspectRenaming
		
		newImportsNames = newHashMap
		newSimpleTypesNames = newHashMap
		newMethodNames = newHashMap
		newMethodDeclNames = newHashMap 
	}
	
	override visit(ImportDeclaration node) {
		
		val rule = packageRules.findFirst[key == node.name]
		if(rule != null){
			val newName = node.AST.newSimpleName(rule.value)
			newImportsNames.put(node,newName)
		}
		else{
			val pack = node.name.toString.qualifierPart
			val rule2 = packageRules.findFirst[key == pack]
			if(rule2 != null){
				val packageName = node.AST.toName(rule2.value)
				
				val clazz = node.name.toString.lastPart
				val classRule = classRules.findFirst[clRule |
						clRule.key.lastPart == clazz &&
						clRule.key.qualifierPart == pack
					]
				val clazzName = node.AST.newSimpleName(classRule.value.lastPart)
				
				val newName = node.AST.newQualifiedName(packageName,clazzName)
				newImportsNames.put(node,newName)
			}
		}
		
		super.visit(node)
	}
	
	override visit(SimpleType node) {
		
		val typeName = node.name
		
		//TODO: typeName can be a QualifiedName
		val rule = typeName.getClassRule()
		if(rule != null){
			<commit_msg>Fix renaming operator: when importing a not-renamed class in an Aspect, just try to rename the package<commit_after>package fr.inria.diverse.melange.utils

import org.eclipse.jdt.core.dom.ASTVisitor
import java.util.List
import java.util.Map
import org.eclipse.jdt.core.dom.ImportDeclaration
import org.eclipse.jdt.core.dom.SimpleType
import org.eclipse.jdt.core.dom.Name
import org.eclipse.jdt.core.dom.MethodInvocation
import org.eclipse.jdt.core.dom.SimpleName
import org.eclipse.jdt.core.dom.MethodDeclaration
import org.eclipse.jdt.core.dom.QualifiedName
import org.eclipse.jdt.core.dom.CastExpression
import org.eclipse.jdt.core.dom.TypeDeclaration
import org.eclipse.jdt.core.dom.NormalAnnotation
import org.eclipse.jdt.core.dom.MemberValuePair
import org.eclipse.jdt.core.dom.CompilationUnit
import org.eclipse.jdt.core.dom.ASTNode
import org.eclipse.jdt.core.dom.NameQualifiedType
import org.eclipse.jdt.core.dom.QualifiedType
import org.eclipse.jdt.core.dom.EnhancedForStatement
import org.eclipse.jdt.core.dom.VariableDeclarationStatement
import org.eclipse.jdt.core.dom.Block
import org.eclipse.jdt.core.dom.Statement
import org.eclipse.jdt.core.dom.Expression
import org.eclipse.jdt.core.dom.VariableDeclarationFragment
import org.eclipse.jdt.core.dom.SingleVariableDeclaration
import org.eclipse.jdt.core.dom.ForStatement
import org.eclipse.jdt.core.dom.VariableDeclarationExpression
import org.eclipse.jdt.core.dom.AST
import com.google.common.collect.SetMultimap

class RenamerVisitor extends ASTVisitor{
	
	List<Pair<String,String>> classRules
	List<Pair<String,String>> packageRules
	List<Pair<String,String>> propertiesRules
	SetMultimap<String,Pair<String,String>> propertiesAspectRules
	
	Map<ImportDeclaration,Name> newImportsNames
	Map<SimpleType,Name> newSimpleTypesNames
	Map<MethodInvocation,SimpleName> newMethodNames
	Map<MethodDeclaration,SimpleName> newMethodDeclNames
	
	new(List<Pair<String,String>> classRenaming, List<Pair<String,String>> packageRenaming, List<Pair<String,String>> propertiesRenaming, SetMultimap<String,Pair<String,String>> propertiesAspectRenaming) {
		classRules = classRenaming
		packageRules = packageRenaming
		propertiesRules = propertiesRenaming
		propertiesAspectRules = propertiesAspectRenaming
		
		newImportsNames = newHashMap
		newSimpleTypesNames = newHashMap
		newMethodNames = newHashMap
		newMethodDeclNames = newHashMap 
	}
	
	override visit(ImportDeclaration node) {
		
		val rule = packageRules.findFirst[key == node.name]
		if(rule != null){
			val newName = node.AST.newSimpleName(rule.value)
			newImportsNames.put(node,newName)
		}
		else{
			val pack = node.name.toString.qualifierPart
			val rule2 = packageRules.findFirst[key == pack]
			if(rule2 != null){
				val packageName = node.AST.toName(rule2.value)
				
				val clazz = node.name.toString.lastPart
				val classRule = classRules.findFirst[clRule |
						clRule.key.lastPart == clazz &&
						clRule.key.qualifierPart == pack
					]
				val clazzName = 
					if(classRule != null){
						node.AST.newSimpleName(classRule.value.lastPart)
					}
					else{
						node.AST.newSimpleName(clazz)
					}
				
				val newName = node.AST.newQualifiedName(packageName,clazzName)
				newImportsNames.put(node,newName)
			}
		}
		
		super.visit(node)
	}
	
	override visit(SimpleType node) {
		
		val typeName = node.name
		
		//TODO: typeName can be a QualifiedName
		val rule = typeName.getClassRule()
		if(rule != null){
			
{'fraction_non_alphanumeric': 0.0981092749739467, 'fraction_numerical': 0.0008932559178204555, 'mean_word_length': 5.0413669064748206, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '1094923', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2111, 'n_words': 350}