/******************************************************************************* * Copyright (c) 2014 Jeff Martin. * 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: * Jeff Martin - initial API and implementation ******************************************************************************/ package cuchaz.enigma.bytecode; import java.util.HashSet; import java.util.Set; import javassist.ClassMap; import javassist.CtBehavior; import javassist.CtClass; import javassist.CtField; import javassist.CtMethod; import javassist.bytecode.ConstPool; import javassist.bytecode.Descriptor; 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 classNames = getAllClassNames( c ); ClassMap map = new ClassMap(); for( String className : classNames ) { String translatedName = m_translator.translateClass( className ); if( translatedName != null ) { map.put( className, translatedName ); } } if( !map.isEmpty() ) { c.replaceClassName( map ); } } private Set getAllClassNames( CtClass c ) { final Set names = new HashSet(); ClassMap map = new ClassMap( ) { @Override public Object get( Object obj ) { if( obj instanceof String ) { names.add( (String)obj ); } return null; } private static final long serialVersionUID = -202160293602070641L; }; c.replaceClassName( map ); return names; } }