55import com .intellij .openapi .components .ServiceManager ;
66import com .intellij .psi .*;
77import cuchaz .enigma .mapping .*;
8+ import cuchaz .enigma .throwables .MappingConflict ;
89
910public class RenameHandler {
11+ public static final String NO_PACKAGE_PREFIX = "nopackage/" ;
12+ public static final int MAX_OBFUSCATED_NAME_LENGTH = 3 ;
1013 private final MappingsService mappingsService = ServiceManager .getService (MappingsService .class );
1114
1215 public void handleRename (String oldName , PsiElement element ) {
@@ -15,49 +18,61 @@ public void handleRename(String oldName, PsiElement element) {
1518 }
1619
1720 if (element instanceof PsiClass ) {
18- ClassMapping mapping = getClassMapping (oldName );
21+ PsiClass clazz = (PsiClass ) element ;
22+
23+ ClassMapping mapping = getOrCreateClassMapping (oldName );
1924 if (mapping != null ) {
20- mappingsService .getMappings ().setClassDeobfName (mapping , getClassName (( PsiClass ) element ));
25+ mappingsService .getMappings ().setClassDeobfName (mapping , getClassName (clazz ));
2126 }
2227 }
2328
2429 if (element instanceof PsiField ) {
25- ClassMapping classMapping = getClassMapping (getClassName (((PsiField ) element ).getContainingClass ()));
26- FieldMapping fieldMapping = getOrCreateFieldMapping ((PsiField ) element , oldName );
27- classMapping .setFieldName (fieldMapping .getObfName (), fieldMapping .getObfDesc (), ((PsiField ) element ).getName ());
30+ PsiField field = (PsiField ) element ;
31+
32+ ClassMapping classMapping = getOrCreateClassMapping (getClassName (field .getContainingClass ()));
33+ FieldMapping fieldMapping = getOrCreateFieldMapping (field , oldName );
34+ classMapping .setFieldName (fieldMapping .getObfName (), fieldMapping .getObfDesc (), field .getName ());
2835 }
2936
3037 if (element instanceof PsiMethod ) {
31- ClassMapping classMapping = getClassMapping (getClassName (((PsiMethod ) element ).getContainingClass ()));
32- MethodMapping methodMapping = getOrCreateMethodMapping ((PsiMethod ) element , oldName );
33- classMapping .setMethodName (methodMapping .getObfName (), methodMapping .getObfDesc (), ((PsiMethod ) element ).getName ());
38+ PsiMethod method = (PsiMethod ) element ;
39+
40+ if (method .isConstructor ()) {
41+ return ;
42+ }
43+
44+ ClassMapping classMapping = getOrCreateClassMapping (getClassName (method .getContainingClass ()));
45+ MethodMapping methodMapping = getOrCreateMethodMapping (method , oldName );
46+ classMapping .setMethodName (methodMapping .getObfName (), methodMapping .getObfDesc (), method .getName ());
3447 }
3548
3649 if (element instanceof PsiParameter ) {
37- PsiElement declarationScope = ((PsiParameter ) element ).getDeclarationScope ();
50+ PsiParameter parameter = (PsiParameter ) element ;
51+
52+ PsiElement declarationScope = parameter .getDeclarationScope ();
3853 if (declarationScope instanceof PsiMethod ) {
3954 MethodMapping mapping = getOrCreateMethodMapping ((PsiMethod ) declarationScope , ((PsiMethod ) declarationScope ).getName ());
4055
4156 if (mapping != null ) {
4257 int index = ((PsiMethod ) declarationScope ).hasModifier (JvmModifier .STATIC ) ? 0 : 1 ;
4358 boolean found = false ;
44- for (PsiParameter parameter : ((PsiMethod ) declarationScope ).getParameterList ().getParameters ()) {
45- if (parameter .equals (element )) {
59+ for (PsiParameter currentParameter : ((PsiMethod ) declarationScope ).getParameterList ().getParameters ()) {
60+ if (currentParameter .equals (parameter )) {
4661 found = true ;
4762 break ;
4863 }
49- index += parameter .getType ().equals (PsiType .LONG ) || parameter .getType ().equals (PsiType .DOUBLE ) ? 2 : 1 ;
64+ index += currentParameter .getType ().equals (PsiType .LONG ) || currentParameter .getType ().equals (PsiType .DOUBLE ) ? 2 : 1 ;
5065 }
5166
5267 if (found ) {
53- mapping .setLocalVariableName (index , (( PsiParameter ) element ) .getName ());
68+ mapping .setLocalVariableName (index , parameter .getName ());
5469 }
5570 }
5671 }
5772 }
5873 }
5974
60- private ClassMapping getClassMapping (String className ) {
75+ private ClassMapping getOrCreateClassMapping (String className ) {
6176 // Try getting deobfuscated class mapping
6277 ClassMapping mapping = mappingsService .getMappings ().getClassByDeobf (className );
6378
@@ -66,12 +81,27 @@ private ClassMapping getClassMapping(String className) {
6681 mapping = mappingsService .getMappings ().getClassByObf (className );
6782 }
6883
84+ // If that doesn't work either, create a new class mapping
85+ if (mapping == null && className .length () <= MAX_OBFUSCATED_NAME_LENGTH ) {
86+ try {
87+ mapping = new ClassMapping (className );
88+ mappingsService .getMappings ().addClassMapping (mapping );
89+ } catch (MappingConflict e ) {
90+ throw new IllegalStateException ("Both getClassByDeobf and getClassByObf returned null yet addClassMapping " +
91+ "threw a MappingConflict exception" , e );
92+ }
93+ }
94+
6995 return mapping ;
7096 }
7197
7298 private MethodMapping getOrCreateMethodMapping (PsiMethod method , String actualName ) {
99+ if (method .isConstructor ()) {
100+ actualName = "<init>" ;
101+ }
102+
73103 // Get mapping for containing class
74- ClassMapping classMapping = getClassMapping (getClassName (method .getContainingClass ()));
104+ ClassMapping classMapping = getOrCreateClassMapping (getClassName (method .getContainingClass ()));
75105 if (classMapping == null ) {
76106 return null ;
77107 }
@@ -86,7 +116,7 @@ private MethodMapping getOrCreateMethodMapping(PsiMethod method, String actualNa
86116 }
87117
88118 // If that doesn't work either, create a new method mapping
89- if (mapping == null ) {
119+ if (mapping == null && actualName . length () <= MAX_OBFUSCATED_NAME_LENGTH ) {
90120 mapping = new MethodMapping (actualName , descriptor );
91121 classMapping .addMethodMapping (mapping );
92122 }
@@ -96,7 +126,7 @@ private MethodMapping getOrCreateMethodMapping(PsiMethod method, String actualNa
96126
97127 private FieldMapping getOrCreateFieldMapping (PsiField field , String actualName ) {
98128 // Get mapping for containing class
99- ClassMapping classMapping = getClassMapping (getClassName (field .getContainingClass ()));
129+ ClassMapping classMapping = getOrCreateClassMapping (getClassName (field .getContainingClass ()));
100130 if (classMapping == null ) {
101131 return null ;
102132 }
@@ -111,7 +141,7 @@ private FieldMapping getOrCreateFieldMapping(PsiField field, String actualName)
111141 }
112142
113143 // If that doesn't work either, create a new field mapping
114- if (mapping == null ) {
144+ if (mapping == null && actualName . length () <= MAX_OBFUSCATED_NAME_LENGTH ) {
115145 mapping = new FieldMapping (actualName , descriptor , actualName , Mappings .EntryModifier .UNCHANGED );
116146 classMapping .addFieldMapping (mapping );
117147 }
@@ -134,7 +164,12 @@ public TypeDescriptor obfuscateDescriptor(TypeDescriptor descriptor) {
134164 }
135165
136166 public static String getClassName (PsiClass element ) {
137- return JVMNameUtil .getClassVMName (element ).replace ('.' , '/' );
167+ String className = JVMNameUtil .getClassVMName (element ).replace ('.' , '/' );
168+
169+ if (className .startsWith (NO_PACKAGE_PREFIX )) {
170+ className = className .substring (NO_PACKAGE_PREFIX .length ());
171+ }
172+ return className ;
138173 }
139174
140175 public static String getDescriptor (PsiType type ) {
0 commit comments