From 8fa1741b621644ef84a9395a4c395d4ff3a89207 Mon Sep 17 00:00:00 2001 From: jeff Date: Sat, 23 Aug 2014 23:43:31 -0400 Subject: moved all classes from the default package into a package called "default" so they can be properly imported by other classes --- src/cuchaz/enigma/bytecode/ClassRenamer.java | 115 +++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/cuchaz/enigma/bytecode/ClassRenamer.java (limited to 'src/cuchaz/enigma/bytecode/ClassRenamer.java') diff --git a/src/cuchaz/enigma/bytecode/ClassRenamer.java b/src/cuchaz/enigma/bytecode/ClassRenamer.java new file mode 100644 index 0000000..cba5861 --- /dev/null +++ b/src/cuchaz/enigma/bytecode/ClassRenamer.java @@ -0,0 +1,115 @@ +/******************************************************************************* + * 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 ); + } +} -- cgit v1.2.3