This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
forked from FabricMC/tiny-remapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsmClassRemapper.java
146 lines (118 loc) · 5.25 KB
/
AsmClassRemapper.java
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
/*
* Copyright (C) 2016, 2018 Player, asie
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.fabricmc.tinyremapper;
import java.util.HashMap;
import java.util.Map;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.ClassRemapper;
import org.objectweb.asm.commons.MethodRemapper;
import org.objectweb.asm.commons.Remapper;
class AsmClassRemapper extends ClassRemapper {
private final boolean renameInvalidLocals;
public AsmClassRemapper(ClassVisitor cv, AsmRemapper remapper, boolean renameInvalidLocals) {
super(cv, remapper);
this.renameInvalidLocals = renameInvalidLocals;
}
@Override
protected MethodVisitor createMethodRemapper(MethodVisitor mv) {
return new AsmMethodRemapper(mv, remapper, renameInvalidLocals);
}
private static class AsmMethodRemapper extends MethodRemapper {
private final Map<String, Integer> nameCounts = new HashMap<>();
private final boolean renameInvalidLocals;
public AsmMethodRemapper(MethodVisitor methodVisitor, Remapper remapper, boolean renameInvalidLocals) {
super(methodVisitor, remapper);
this.renameInvalidLocals = renameInvalidLocals;
}
@Override
public void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) {
if (mv == null) return;
descriptor = remapper.mapDesc(descriptor);
if (renameInvalidLocals && !isValidJavaIdentifier(name)) {
Type type = Type.getType(descriptor);
boolean plural = false;
if (type.getSort() == Type.ARRAY) {
plural = true;
type = type.getElementType();
}
String varName = type.getClassName();
int dotIdx = varName.lastIndexOf('.');
if (dotIdx != -1) varName = varName.substring(dotIdx + 1);
varName = Character.toLowerCase(varName.charAt(0)) + varName.substring(1);
if (plural) varName += "s";
name = varName + "_" + nameCounts.compute(varName, (k, v) -> (v == null) ? 1 : v + 1);
}
mv.visitLocalVariable(
name,
descriptor,
remapper.mapSignature(signature, true),
start,
end,
index);
}
private static boolean isValidJavaIdentifier(String s) {
if (s.isEmpty()) return false;
int cp = s.codePointAt(0);
if (!Character.isJavaIdentifierStart(cp)) return false;
for (int i = Character.charCount(cp), max = s.length(); i < max; i += Character.charCount(cp)) {
cp = s.codePointAt(i);
if (!Character.isJavaIdentifierPart(cp)) return false;
}
return true;
}
@Override
public void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) {
if (mv == null) return;
Handle implemented = getLambdaImplementedMethod(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
if (implemented != null) {
name = remapper.mapMethodName(implemented.getOwner(), implemented.getName(), implemented.getDesc());
} else {
name = remapper.mapInvokeDynamicMethodName(name, descriptor);
}
for (int i = 0; i < bootstrapMethodArguments.length; i++) {
bootstrapMethodArguments[i] = remapper.mapValue(bootstrapMethodArguments[i]);
}
mv.visitInvokeDynamicInsn(
name,
remapper.mapMethodDesc(descriptor), (Handle) remapper.mapValue(bootstrapMethodHandle),
bootstrapMethodArguments);
}
private static Handle getLambdaImplementedMethod(String name, String desc, Handle bsm, Object... bsmArgs) {
if (isJavaLambdaMetafactory(bsm)) {
assert desc.endsWith(";");
return new Handle(Opcodes.H_INVOKEINTERFACE, desc.substring(desc.lastIndexOf(')') + 2, desc.length() - 1), name, ((Type) bsmArgs[0]).getDescriptor(), true);
} else {
System.out.printf("unknown invokedynamic bsm: %s/%s%s (tag=%d iif=%b)%n", bsm.getOwner(), bsm.getName(), bsm.getDesc(), bsm.getTag(), bsm.isInterface());
return null;
}
}
private static boolean isJavaLambdaMetafactory(Handle bsm) {
return bsm.getTag() == Opcodes.H_INVOKESTATIC
&& bsm.getOwner().equals("java/lang/invoke/LambdaMetafactory")
&& (bsm.getName().equals("metafactory")
&& bsm.getDesc().equals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;")
|| bsm.getName().equals("altMetafactory")
&& bsm.getDesc().equals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;"))
&& !bsm.isInterface();
}
}
}