summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/analysis/BridgeFixer.java
diff options
context:
space:
mode:
authorGravatar hg2014-08-17 10:56:17 -0400
committerGravatar hg2014-08-17 10:56:17 -0400
commit6c4440ac1133bfaa7871d1049d174528a289ef30 (patch)
treefe1142b285c5e43dbd3afe8dd3eb0189f027c6a6 /src/cuchaz/enigma/analysis/BridgeFixer.java
parenttrying to get inner/anonymous classes working... I have a working heuristic i... (diff)
downloadenigma-fork-6c4440ac1133bfaa7871d1049d174528a289ef30.tar.gz
enigma-fork-6c4440ac1133bfaa7871d1049d174528a289ef30.tar.xz
enigma-fork-6c4440ac1133bfaa7871d1049d174528a289ef30.zip
added support for automatic reconstruction of inner and anonymous classes
also added class to restore bridge method flags taken out by the obfuscator
Diffstat (limited to 'src/cuchaz/enigma/analysis/BridgeFixer.java')
-rw-r--r--src/cuchaz/enigma/analysis/BridgeFixer.java91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/analysis/BridgeFixer.java b/src/cuchaz/enigma/analysis/BridgeFixer.java
new file mode 100644
index 0000000..db441d2
--- /dev/null
+++ b/src/cuchaz/enigma/analysis/BridgeFixer.java
@@ -0,0 +1,91 @@
1/*******************************************************************************
2 * Copyright (c) 2014 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Public License v3.0
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.analysis;
12
13import java.util.List;
14
15import javassist.CannotCompileException;
16import javassist.CtClass;
17import javassist.CtMethod;
18import javassist.NotFoundException;
19import javassist.bytecode.AccessFlag;
20import javassist.expr.ExprEditor;
21import javassist.expr.MethodCall;
22
23import com.beust.jcommander.internal.Lists;
24
25public class BridgeFixer
26{
27 public void fixBridges( CtClass c )
28 {
29 // bridge methods are scrubbed and marked as synthetic methods by the obfuscator
30 // need to figure out which synthetics are bridge methods and restore them
31 for( CtMethod method : c.getDeclaredMethods() )
32 {
33 // skip non-synthetic methods
34 if( ( method.getModifiers() & AccessFlag.SYNTHETIC ) == 0 )
35 {
36 continue;
37 }
38
39 CtMethod bridgedMethod = getBridgedMethod( method );
40 if( bridgedMethod != null )
41 {
42 bridgedMethod.setName( method.getName() );
43 method.setModifiers( method.getModifiers() | AccessFlag.BRIDGE );
44 }
45 }
46 }
47
48 private CtMethod getBridgedMethod( CtMethod method )
49 {
50 // bridge methods just call another method, cast it to the return type, and return the result
51 // let's see if we can detect this scenario
52
53 // get all the called methods
54 final List<MethodCall> methodCalls = Lists.newArrayList();
55 try
56 {
57 method.instrument( new ExprEditor( )
58 {
59 @Override
60 public void edit( MethodCall call )
61 {
62 methodCalls.add( call );
63 }
64 } );
65 }
66 catch( CannotCompileException ex )
67 {
68 // this is stupid... we're not even compiling anything
69 throw new Error( ex );
70 }
71
72 // is there just one?
73 if( methodCalls.size() != 1 )
74 {
75 return null;
76 }
77 MethodCall call = methodCalls.get( 0 );
78
79 try
80 {
81 // we have a bridge method!
82 return call.getMethod();
83 }
84 catch( NotFoundException ex )
85 {
86 // can't find the type? not a bridge method
87 ex.printStackTrace( System.err );
88 return null;
89 }
90 }
91}