aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/zwirn/Descriptor.java
blob: a121264c7a9a9824b42e6165cd4c376ebdd962af (plain)
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
package moe.nea.zwirn;

import java.util.ArrayList;
import java.util.List;

public interface Descriptor {
    Descriptor mapClassName(SimpleRemapper remapper);

    String toJvmDescriptor();

    interface Type extends Descriptor {
        Type mapClassName(SimpleRemapper remapper);

        static Type readType(GoodStringReader reader) {
            if (reader.peekChar() == 'V') {
                reader.nextChar();
                return VoidType.VOID;
            }
            return Field.readField(reader);
        }
    }

    interface Field extends Type {
        Field mapClassName(SimpleRemapper remapper);

        static Field readField(GoodStringReader reader) {
            switch (reader.nextChar()) {
                case 'L':
                    return new Class(reader.readUntil(';').replace('/', '.'));
                case 'Z':
                    return DefaultField.BOOLEAN;
                case 'B':
                    return DefaultField.BYTE;
                case 'I':
                    return DefaultField.INT;
                case 'D':
                    return DefaultField.DOUBLE;
                case 'J':
                    return DefaultField.LONG;
                case 'S':
                    return DefaultField.SHORT;
                case 'F':
                    return DefaultField.FLOAT;
                case 'C':
                    return DefaultField.CHAR;
                case '[':
                    return new Array(readField(reader));
            }
            throw new IllegalStateException("Unknown type");
        }
    }

    record Method(List<Field> argumentTypes, Type returnType) implements Descriptor {

        @Override
        public Descriptor mapClassName(SimpleRemapper remapper) {
            List<Field> newArgumentTypes = new ArrayList<>(argumentTypes.size());
            for (Field argumentType : argumentTypes) {
                newArgumentTypes.add(argumentType.mapClassName(remapper));
            }
            return new Method(newArgumentTypes, returnType.mapClassName(remapper));
        }

        @Override
        public String toJvmDescriptor() {
            StringBuilder sb = new StringBuilder();
            sb.append("(");
            for (Field argumentType : argumentTypes) {
                sb.append(argumentType.toJvmDescriptor());
            }
            sb.append(")");
            sb.append(returnType.toJvmDescriptor());
            return sb.toString();
        }

        public static Method readMethod(GoodStringReader reader) {
            if (reader.nextChar() != '(')
                throw new IllegalStateException("Expected (");
            List<Field> argument = new ArrayList<>();
            while (reader.peekChar() != ')') {
                argument.add(Field.readField(reader));
            }
            reader.nextChar(); // Consume )
            return new Method(argument, Type.readType(reader));
        }
    }

    record Array(Field field) implements Field {

        @Override
        public String toJvmDescriptor() {
            return "[" + field.toJvmDescriptor();
        }

        @Override
        public Array mapClassName(SimpleRemapper remapper) {
            return new Array(field.mapClassName(remapper));
        }
    }

    record Class(String dottedName) implements Field {
        @Override
        public String toJvmDescriptor() {
            return "L" + dottedName.replace('.', '/') + ";";
        }

        @Override
        public Field mapClassName(SimpleRemapper remapper) {
            return new Class(remapper.remapClass(dottedName));
        }
    }


    enum VoidType implements Type {
        VOID;

        @Override
        public String toJvmDescriptor() {
            return "V";
        }

        @Override
        public VoidType mapClassName(SimpleRemapper remapper) {
            return this;
        }
    }

    enum DefaultField implements Field {
        BOOLEAN("Z"),
        BYTE("B"),
        INT("I"),
        DOUBLE("D"),
        LONG("J"),
        SHORT("S"),
        FLOAT("F"),
        CHAR("C");
        private final String jvmDescriptor;

        DefaultField(String jvmDescriptor) {
            this.jvmDescriptor = jvmDescriptor;
        }

        @Override
        public String toJvmDescriptor() {
            return jvmDescriptor;
        }

        @Override
        public DefaultField mapClassName(SimpleRemapper remapper) {
            return this;
        }
    }

}