summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/analysis/RelatedMethodChecker.java
blob: 08e2dbf087b4b0f141001b8cd9ad0caa0949af95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************************************
 * Copyright (c) 2015 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public
 * License v3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Contributors:
 * Jeff Martin - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma.analysis;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.Map;
import java.util.Set;

import cuchaz.enigma.mapping.*;

public class RelatedMethodChecker {

    private JarIndex m_jarIndex;
    private Map<Set<MethodEntry>, String> m_deobfNamesByGroup;
    private Map<MethodEntry, String> m_deobfNamesByObfMethod;
    private Map<MethodEntry, Set<MethodEntry>> m_groupsByObfMethod;
    private Set<Set<MethodEntry>> m_inconsistentGroups;

    public RelatedMethodChecker(JarIndex jarIndex) {
        m_jarIndex = jarIndex;
        m_deobfNamesByGroup = Maps.newHashMap();
        m_deobfNamesByObfMethod = Maps.newHashMap();
        m_groupsByObfMethod = Maps.newHashMap();
        m_inconsistentGroups = Sets.newHashSet();
    }

    public void checkMethod(ClassEntry classEntry, MethodMapping methodMapping) {

        // TEMP: disable the expensive check for now, maybe we can optimize it later, or just use it for debugging
        if (true) {
            return;
        }

        BehaviorEntry obfBehaviorEntry = EntryFactory.getObfBehaviorEntry(classEntry, methodMapping);
        if (!(obfBehaviorEntry instanceof MethodEntry)) {
            // only methods have related implementations
            return;
        }
        MethodEntry obfMethodEntry = (MethodEntry) obfBehaviorEntry;
        String deobfName = methodMapping.getDeobfName();
        m_deobfNamesByObfMethod.put(obfMethodEntry, deobfName);

        // have we seen this method's group before?
        Set<MethodEntry> group = m_groupsByObfMethod.get(obfMethodEntry);
        if (group == null) {

            // no, compute the group and save the name
            group = m_jarIndex.getRelatedMethodImplementations(obfMethodEntry);
            m_deobfNamesByGroup.put(group, deobfName);

            assert (group.contains(obfMethodEntry));
            for (MethodEntry relatedMethodEntry : group) {
                m_groupsByObfMethod.put(relatedMethodEntry, group);
            }
        }

        // check the name
        if (!sameName(m_deobfNamesByGroup.get(group), deobfName)) {
            m_inconsistentGroups.add(group);
        }
    }

    private boolean sameName(String a, String b) {
        if (a == null && b == null) {
            return true;
        } else if (a != null && b != null) {
            return a.equals(b);
        }
        return false;
    }

    public boolean hasProblems() {
        return m_inconsistentGroups.size() > 0;
    }

    public String getReport() {
        StringBuilder buf = new StringBuilder();
        buf.append(m_inconsistentGroups.size());
        buf.append(" groups of methods related by inheritance and/or interfaces have different deobf names!\n");
        for (Set<MethodEntry> group : m_inconsistentGroups) {
            buf.append("\tGroup with ");
            buf.append(group.size());
            buf.append(" methods:\n");
            for (MethodEntry methodEntry : group) {
                buf.append("\t\t");
                buf.append(methodEntry.toString());
                buf.append(" => ");
                buf.append(m_deobfNamesByObfMethod.get(methodEntry));
                buf.append("\n");
            }
        }
        return buf.toString();
    }
}