/******************************************************************************* * 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.Map; import java.util.Set; import javassist.ClassMap; import javassist.CtClass; import javassist.bytecode.ConstPool; import javassist.bytecode.Descriptor; import javassist.bytecode.InnerClassesAttribute; import com.beust.jcommander.internal.Sets; import com.google.common.collect.Maps; import cuchaz.enigma.mapping.ClassEntry; public class ClassRenamer { public static void renameClasses( CtClass c, Map map ) { // build the map used by javassist ClassMap nameMap = new ClassMap(); for( Map.Entry entry : map.entrySet() ) { nameMap.put( entry.getKey().getName(), entry.getValue().getName() ); } c.replaceClassName( nameMap ); // translate the names in the InnerClasses attribute ConstPool constants = c.getClassFile().getConstPool(); 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 ) ) ); */ } } } public static Set getAllClassEntries( CtClass c ) { // get the classes that javassist knows about 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 ); // also check InnerClassesAttribute InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute( InnerClassesAttribute.tag ); if( attr != null ) { for( int i=0; i map = Maps.newHashMap(); for( ClassEntry classEntry : ClassRenamer.getAllClassEntries( c ) ) { if( classEntry.isInDefaultPackage() ) { map.put( classEntry, new ClassEntry( newPackageName + "/" + classEntry.getName() ) ); } } ClassRenamer.renameClasses( c, map ); } }