/******************************************************************************* * 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.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 javassist.bytecode.InnerClassesAttribute; import com.beust.jcommander.internal.Sets; 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 classEntries = getAllClassEntries( c ); ClassMap map = new ClassMap(); for( ClassEntry obfClassEntry : classEntries ) { map.put( obfClassEntry.getName(), m_translator.translateEntry( obfClassEntry ).getName() ); } c.replaceClassName( map ); // translate the names in the InnerClasses attribute InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute( InnerClassesAttribute.tag ); if( attr != null ) { for( int i=0; i ATTR: %s,%s,%s", obfClassEntry, deobfClassEntry, attr.outerClass( i ), attr.innerClass( i ), attr.innerName( i ) ) ); */ } } } private Set getAllClassEntries( CtClass c ) { final Set entries = Sets.newHashSet(); ClassMap map = new ClassMap( ) { @Override public Object get( Object obj ) { if( obj instanceof String ) { entries.add( new ClassEntry( (String)obj ) ); } return null; } private static final long serialVersionUID = -202160293602070641L; }; c.replaceClassName( map ); return entries; } }