summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/mapping/SignatureUpdater.java
blob: dac692e4a39b46c7fb0a858d5e79f8b3e5db528b (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
/*******************************************************************************
 * 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.Lists;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

public class SignatureUpdater {

    public interface ClassNameUpdater {
        String update(String className);
    }

    public static String update(String signature, ClassNameUpdater updater) {
        try {
            StringBuilder buf = new StringBuilder();

            // read the signature character-by-character
            StringReader reader = new StringReader(signature);
            int i = -1;
            while ((i = reader.read()) != -1) {
                char c = (char) i;

                // does this character start a class name?
                if (c == 'L') {
                    // update the class name and add it to the buffer
                    buf.append('L');
                    String className = readClass(reader);
                    if (className == null) {
                        throw new IllegalArgumentException("Malformed signature: " + signature);
                    }
                    buf.append(updater.update(className));
                    buf.append(';');
                } else {
                    // copy the character into the buffer
                    buf.append(c);
                }
            }

            return buf.toString();
        } catch (IOException ex) {
            // I'm pretty sure a StringReader will never throw one of these
            throw new Error(ex);
        }
    }

    private static String readClass(StringReader reader) throws IOException {
        // read all the characters in the buffer until we hit a ';'
        // remember to treat generics correctly
        StringBuilder buf = new StringBuilder();
        int depth = 0;
        int i = -1;
        while ((i = reader.read()) != -1) {
            char c = (char) i;

            if (c == '<') {
                depth++;
            } else if (c == '>') {
                depth--;
            } else if (depth == 0) {
                if (c == ';') {
                    return buf.toString();
                } else {
                    buf.append(c);
                }
            }
        }

        return null;
    }

    public static List<String> getClasses(String signature) {
        final List<String> classNames = Lists.newArrayList();
        update(signature, new ClassNameUpdater() {
            @Override
            public String update(String className) {
                classNames.add(className);
                return className;
            }
        });
        return classNames;
    }
}