summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/mapping/MethodMapping.java
blob: 99b9c88787393ae44fcad7a3155ca68242cb9292 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*******************************************************************************
 * 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.mapping;

import com.google.common.collect.Maps;

import java.util.Map;

import cuchaz.enigma.throwables.MappingConflict;

public class MethodMapping implements Comparable<MethodMapping>, MemberMapping<BehaviorEntry> {

    private String obfName;
    private String deobfName;
    private Signature obfSignature;
    private Map<Integer, ArgumentMapping> arguments;

    public MethodMapping(String obfName, Signature obfSignature) {
        this(obfName, obfSignature, null);
    }

    public MethodMapping(String obfName, Signature obfSignature, String deobfName) {
        if (obfName == null) {
            throw new IllegalArgumentException("obf name cannot be null!");
        }
        if (obfSignature == null) {
            throw new IllegalArgumentException("obf signature cannot be null!");
        }
        this.obfName = obfName;
        this.deobfName = NameValidator.validateMethodName(deobfName);
        this.obfSignature = obfSignature;
        this.arguments = Maps.newTreeMap();
    }
    
    public MethodMapping(MethodMapping other, ClassNameReplacer obfClassNameReplacer) {
        this.obfName = other.obfName;
        this.deobfName = other.deobfName;
        this.obfSignature = new Signature(other.obfSignature, obfClassNameReplacer);
        this.arguments = Maps.newTreeMap();
        for (Map.Entry<Integer,ArgumentMapping> entry : other.arguments.entrySet()) {
            this.arguments.put(entry.getKey(), new ArgumentMapping(entry.getValue()));
        }
    }

    @Override
    public String getObfName() {
        return this.obfName;
    }

    public String getDeobfName() {
        return this.deobfName;
    }

    public void setDeobfName(String val) {
        this.deobfName = NameValidator.validateMethodName(val);
    }

    public Signature getObfSignature() {
        return this.obfSignature;
    }

    public void setObfName(String name) {
        this.obfName = NameValidator.validateMethodName(name);
    }

    public void setObfSignature(Signature val) {
        this.obfSignature = val;
    }

    public Iterable<ArgumentMapping> arguments() {
        return this.arguments.values();
    }

    public void addArgumentMapping(ArgumentMapping argumentMapping) throws MappingConflict {
        if (this.arguments.containsKey(argumentMapping.getIndex())) {
            throw new MappingConflict("argument", argumentMapping.getName(), this.arguments.get(argumentMapping.getIndex()).getName());
        }
        this.arguments.put(argumentMapping.getIndex(), argumentMapping);
    }

    public String getObfArgumentName(int index) {
        ArgumentMapping argumentMapping = this.arguments.get(index);
        if (argumentMapping != null) {
            return argumentMapping.getName();
        }

        return null;
    }

    public String getDeobfArgumentName(int index) {
        ArgumentMapping argumentMapping = this.arguments.get(index);
        if (argumentMapping != null) {
            return argumentMapping.getName();
        }

        return null;
    }

    public void setArgumentName(int index, String name) {
        ArgumentMapping argumentMapping = this.arguments.get(index);
        if (argumentMapping == null) {
            argumentMapping = new ArgumentMapping(index, name);
            boolean wasAdded = this.arguments.put(index, argumentMapping) == null;
            assert (wasAdded);
        } else {
            argumentMapping.setName(name);
        }
    }

    public void removeArgumentName(int index) {
        boolean wasRemoved = this.arguments.remove(index) != null;
        assert (wasRemoved);
    }

    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append("\t");
        buf.append(this.obfName);
        buf.append(" <-> ");
        buf.append(this.deobfName);
        buf.append("\n");
        buf.append("\t");
        buf.append(this.obfSignature);
        buf.append("\n");
        buf.append("\tArguments:\n");
        for (ArgumentMapping argumentMapping : this.arguments.values()) {
            buf.append("\t\t");
            buf.append(argumentMapping.getIndex());
            buf.append(" -> ");
            buf.append(argumentMapping.getName());
            buf.append("\n");
        }
        return buf.toString();
    }

    @Override
    public int compareTo(MethodMapping other) {
        return (this.obfName + this.obfSignature).compareTo(other.obfName + other.obfSignature);
    }

    public boolean containsArgument(String name) {
        for (ArgumentMapping argumentMapping : this.arguments.values()) {
            if (argumentMapping.getName().equals(name)) {
                return true;
            }
        }
        return false;
    }

    public boolean renameObfClass(final String oldObfClassName, final String newObfClassName) {
        // rename obf classes in the signature
        Signature newSignature = new Signature(this.obfSignature, new ClassNameReplacer() {
            @Override
            public String replace(String className) {
                if (className.equals(oldObfClassName)) {
                    return newObfClassName;
                }
                return null;
            }
        });

        if (!newSignature.equals(this.obfSignature)) {
            this.obfSignature = newSignature;
            return true;
        }
        return false;
    }

    public boolean isConstructor() {
        return this.obfName.startsWith("<");
    }

    @Override
    public BehaviorEntry getObfEntry(ClassEntry classEntry) {
        if (isConstructor()) {
            return new ConstructorEntry(classEntry, this.obfSignature);
        } else {
            return new MethodEntry(classEntry, this.obfName, this.obfSignature);
        }
    }
}