<commit_before>/*******************************************************************************
 * Copyright (c) 2014 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     <NAME> - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma.bytecode;

import java.util.Map;

import javassist.CtBehavior;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
import javassist.bytecode.SourceFileAttribute;

import com.google.common.collect.Maps;

import cuchaz.enigma.mapping.BehaviorEntry;
import cuchaz.enigma.mapping.BehaviorEntryFactory;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.FieldEntry;
import cuchaz.enigma.mapping.MethodEntry;
import cuchaz.enigma.mapping.Translator;

public class ClassTranslator {
	
	private Translator m_translator;
	
	public ClassTranslator(Translator translator) {
		m_translator = translator;
	}
	
	public void translate(CtClass c) {
		
		// NOTE: the order of these translations is very important
		
		// translate all the field and method references in the code by editing the constant pool
		ConstPool constants = c.getClassFile().getConstPool();
		ConstPoolEditor editor = new ConstPoolEditor(constants);
		for (int i = 1; i < constants.getSize(); i++) {
			switch (constants.getTag(i)) {
				
				case ConstPool.CONST_Fieldref: {
					
					// translate the name
					FieldEntry entry = new FieldEntry(
						new ClassEntry(Descriptor.toJvmName(constants.getFieldrefClassName(i))),
						constants.getFieldrefName(i)
					);
					FieldEntry translatedEntry = m_translator.translateEntry(entry);
					
					// TEMP
					if (entry.toString().equals("none/bxq.m")) {
						System.out.println("FIELD: " + entry + " -> " + translatedEntry);
					}
					
					// translate the type
					String type = constants.getFieldrefType(i);
					String translatedType = m_translator.translateSignature(type);
					
					if (!entry.equals(translatedEntry) || !type.equals(translatedType)) {
						editor.changeMemberrefNameAndType(i, translatedEntry.getName(), translatedType);
					}
				}
				break;
				
				case ConstPool.CONST_Methodref:
				case ConstPool.CONST_InterfaceMethodref: {
					
					// translate the name and type
					BehaviorEntry entry = BehaviorEntryFactory.create(
						Descriptor.toJvmName(editor.getMemberrefClassname(i)),
						editor.getMemberrefName(i),
						editor.getMemberrefType(i)
					);
					BehaviorEntry translatedEntry = m_translator.translateEntry(entry);
					
					if (!entry.getName().equals(translatedEntry.getName()) || !entry.getSignature().equals(translatedEntry.getSignature())) {
						editor.changeMemberrefNameAndType(i, translatedEntry.getName(), translatedEntry.getSignature());
					}
				}
				break;
			}
		}
		<commit_msg>remove debug code, silly...<commit_after>/*******************************************************************************
 * Copyright (c) 2014 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     <NAME> - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma.bytecode;

import java.util.Map;

import javassist.CtBehavior;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
import javassist.bytecode.SourceFileAttribute;

import com.google.common.collect.Maps;

import cuchaz.enigma.mapping.BehaviorEntry;
import cuchaz.enigma.mapping.BehaviorEntryFactory;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.FieldEntry;
import cuchaz.enigma.mapping.MethodEntry;
import cuchaz.enigma.mapping.Translator;

public class ClassTranslator {
	
	private Translator m_translator;
	
	public ClassTranslator(Translator translator) {
		m_translator = translator;
	}
	
	public void translate(CtClass c) {
		
		// NOTE: the order of these translations is very important
		
		// translate all the field and method references in the code by editing the constant pool
		ConstPool constants = c.getClassFile().getConstPool();
		ConstPoolEditor editor = new ConstPoolEditor(constants);
		for (int i = 1; i < constants.getSize(); i++) {
			switch (constants.getTag(i)) {
				
				case ConstPool.CONST_Fieldref: {
					
					// translate the name
					FieldEntry entry = new FieldEntry(
						new ClassEntry(Descriptor.toJvmName(constants.getFieldrefClassName(i))),
						constants.getFieldrefName(i)
					);
					FieldEntry translatedEntry = m_translator.translateEntry(entry);
					
					// translate the type
					String type = constants.getFieldrefType(i);
					String translatedType = m_translator.translateSignature(type);
					
					if (!entry.equals(translatedEntry) || !type.equals(translatedType)) {
						editor.changeMemberrefNameAndType(i, translatedEntry.getName(), translatedType);
					}
				}
				break;
				
				case ConstPool.CONST_Methodref:
				case ConstPool.CONST_InterfaceMethodref: {
					
					// translate the name and type
					BehaviorEntry entry = BehaviorEntryFactory.create(
						Descriptor.toJvmName(editor.getMemberrefClassname(i)),
						editor.getMemberrefName(i),
						editor.getMemberrefType(i)
					);
					BehaviorEntry translatedEntry = m_translator.translateEntry(entry);
					
					if (!entry.getName().equals(translatedEntry.getName()) || !entry.getSignature().equals(translatedEntry.getSignature())) {
						editor.changeMemberrefNameAndType(i, translatedEntry.getName(), translatedEntry.getSignature());
					}
				}
				break;
			}
		}
		
{'fraction_non_alphanumeric': 0.14451718494271687, 'fraction_numerical': 0.0022913256955810145, 'mean_word_length': 4.156962025316456, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2643597', 'n_tokens_mistral': 2085, 'n_tokens_neox': 1654, 'n_words': 377}