Skip to content

Commit ea4c6f1

Browse files
committed
Merge branch 'new-map-invokedynamicmethodname-method' into 'master'
Add a new mapInvokeDynamicMethodName method. See merge request asm/asm!431
2 parents 3f12198 + ad4dc0f commit ea4c6f1

File tree

5 files changed

+269
-21
lines changed

5 files changed

+269
-21
lines changed

asm-commons/src/main/java/org/objectweb/asm/commons/MethodRemapper.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,26 @@ public void visitMethodInsn(
170170
}
171171

172172
@Override
173+
@SuppressWarnings("deprecation")
173174
public void visitInvokeDynamicInsn(
174175
final String name,
175176
final String descriptor,
176177
final Handle bootstrapMethodHandle,
177178
final Object... bootstrapMethodArguments) {
179+
String remappedName;
180+
if (remapper.api == 0) {
181+
remappedName = remapper.mapInvokeDynamicMethodName(name, descriptor);
182+
} else {
183+
remappedName =
184+
remapper.mapInvokeDynamicMethodName(
185+
name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
186+
}
178187
Object[] remappedBootstrapMethodArguments = new Object[bootstrapMethodArguments.length];
179188
for (int i = 0; i < bootstrapMethodArguments.length; ++i) {
180189
remappedBootstrapMethodArguments[i] = remapper.mapValue(bootstrapMethodArguments[i]);
181190
}
182191
super.visitInvokeDynamicInsn(
183-
remapper.mapInvokeDynamicMethodName(name, descriptor),
192+
remappedName,
184193
remapper.mapMethodDesc(descriptor),
185194
(Handle) remapper.mapValue(bootstrapMethodHandle),
186195
remappedBootstrapMethodArguments);

asm-commons/src/main/java/org/objectweb/asm/commons/Remapper.java

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@
4343
*/
4444
public abstract class Remapper {
4545

46+
/**
47+
* The ASM API version supported by this remapper, or 0 for instances created with the deprecated
48+
* constructor.
49+
*/
50+
final int api;
51+
52+
/**
53+
* Creates a new {@link Remapper}.
54+
*
55+
* @deprecated use {@link #Remapper(int)} instead.
56+
*/
57+
@Deprecated
58+
public Remapper() {
59+
this.api = 0;
60+
}
61+
62+
/**
63+
* Creates a new {@link Remapper}.
64+
*
65+
* @param api the ASM API version supported by this remapper. Must be one of the {@code
66+
* ASM}<i>x</i> values in {@link Opcodes}.
67+
*/
68+
public Remapper(final int api) {
69+
if (api != Opcodes.ASM9
70+
&& api != Opcodes.ASM8
71+
&& api != Opcodes.ASM7
72+
&& api != Opcodes.ASM6
73+
&& api != Opcodes.ASM5
74+
&& api != Opcodes.ASM4
75+
&& api != Opcodes.ASM10_EXPERIMENTAL) {
76+
throw new IllegalArgumentException("Unsupported api " + api);
77+
}
78+
this.api = api;
79+
}
80+
4681
/**
4782
* Returns the given descriptor, remapped with {@link #map(String)}.
4883
*
@@ -177,17 +212,26 @@ public Object mapValue(final Object value) {
177212
}
178213
if (value instanceof ConstantDynamic) {
179214
ConstantDynamic constantDynamic = (ConstantDynamic) value;
215+
String name = constantDynamic.getName();
216+
String descriptor = constantDynamic.getDescriptor();
217+
Handle bootstrapMethod = constantDynamic.getBootstrapMethod();
180218
int bootstrapMethodArgumentCount = constantDynamic.getBootstrapMethodArgumentCount();
219+
Object[] bootstrapMethodArguments = new Object[bootstrapMethodArgumentCount];
181220
Object[] remappedBootstrapMethodArguments = new Object[bootstrapMethodArgumentCount];
182221
for (int i = 0; i < bootstrapMethodArgumentCount; ++i) {
183-
remappedBootstrapMethodArguments[i] =
184-
mapValue(constantDynamic.getBootstrapMethodArgument(i));
222+
bootstrapMethodArguments[i] = constantDynamic.getBootstrapMethodArgument(i);
223+
remappedBootstrapMethodArguments[i] = mapValue(bootstrapMethodArguments[i]);
224+
}
225+
if (api == 0) {
226+
name = mapInvokeDynamicMethodName(name, descriptor);
227+
} else {
228+
name =
229+
mapInvokeDynamicMethodName(name, descriptor, bootstrapMethod, bootstrapMethodArguments);
185230
}
186-
String descriptor = constantDynamic.getDescriptor();
187231
return new ConstantDynamic(
188-
mapInvokeDynamicMethodName(constantDynamic.getName(), descriptor),
232+
name,
189233
mapDesc(descriptor),
190-
(Handle) mapValue(constantDynamic.getBootstrapMethod()),
234+
(Handle) mapValue(bootstrapMethod),
191235
remappedBootstrapMethodArguments);
192236
}
193237
return value;
@@ -316,11 +360,34 @@ public String mapMethodName(final String owner, final String name, final String
316360
* @param name the name of the method.
317361
* @param descriptor the descriptor of the method.
318362
* @return the new name of the method.
363+
* @deprecated use {@link #mapInvokeDynamicMethodName(String, String, Handle, Object...)} instead.
319364
*/
365+
@Deprecated
320366
public String mapInvokeDynamicMethodName(final String name, final String descriptor) {
321367
return name;
322368
}
323369

370+
/**
371+
* Maps an invokedynamic or a constant dynamic method name to its new name. The default
372+
* implementation of this method returns the given name, unchanged. Subclasses can override.
373+
*
374+
* @param name the name of the method.
375+
* @param descriptor the descriptor of the method.
376+
* @param bootstrapMethodHandle the bootstrap method.
377+
* @param bootstrapMethodArguments the bootstrap method constant arguments. Each argument must be
378+
* an {@link Integer}, {@link Float}, {@link Long}, {@link Double}, {@link String}, {@link
379+
* Type}, {@link Handle} or {@link ConstantDynamic} value. This method is allowed to modify
380+
* the content of the array so a caller should expect that this array may change.
381+
* @return the new name of the method.
382+
*/
383+
public String mapInvokeDynamicMethodName(
384+
final String name,
385+
final String descriptor,
386+
final Handle bootstrapMethodHandle,
387+
final Object... bootstrapMethodArguments) {
388+
return name;
389+
}
390+
324391
/**
325392
* Maps a record component name to its new name. The default implementation of this method returns
326393
* the given name, unchanged. Subclasses can override.

asm-commons/src/main/java/org/objectweb/asm/commons/SimpleRemapper.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
import java.util.Collections;
3232
import java.util.Map;
33+
import org.objectweb.asm.Handle;
34+
import org.objectweb.asm.Opcodes;
3335

3436
/**
3537
* A {@link Remapper} using a {@link Map} to define its mapping.
@@ -58,23 +60,69 @@ public class SimpleRemapper extends Remapper {
5860
* <li>for internal names, the key is the old internal name, and the value is the new
5961
* internal name (see {@link org.objectweb.asm.Type#getInternalName()}).
6062
* </ul>
63+
*
64+
* @deprecated use {@link #SimpleRemapper(int, Map)} instead.
6165
*/
66+
@Deprecated
6267
public SimpleRemapper(final Map<String, String> mapping) {
6368
this.mapping = mapping;
6469
}
6570

71+
/**
72+
* Constructs a new {@link SimpleRemapper} with the given mapping.
73+
*
74+
* @param api the ASM API version supported by this remapper. Must be one of the {@code
75+
* ASM}<i>x</i> values in {@link Opcodes}.
76+
* @param mapping a map specifying a remapping as follows:
77+
* <ul>
78+
* <li>for method names, the key is the owner, name and descriptor of the method (in the
79+
* form &lt;owner&gt;.&lt;name&gt;&lt;descriptor&gt;), and the value is the new method
80+
* name.
81+
* <li>for invokedynamic method names, the key is the name and descriptor of the method (in
82+
* the form .&lt;name&gt;&lt;descriptor&gt;), and the value is the new method name.
83+
* <li>for field names, the key is the owner and name of the field or attribute (in the form
84+
* &lt;owner&gt;.&lt;name&gt;), and the value is the new field name.
85+
* <li>for attribute names, the key is the annotation descriptor and the name of the
86+
* attribute (in the form &lt;descriptor&gt;.&lt;name&gt;), and the value is the new
87+
* attribute name.
88+
* <li>for internal names, the key is the old internal name, and the value is the new
89+
* internal name (see {@link org.objectweb.asm.Type#getInternalName()}).
90+
* </ul>
91+
*/
92+
public SimpleRemapper(final int api, final Map<String, String> mapping) {
93+
super(api);
94+
this.mapping = mapping;
95+
}
96+
6697
/**
6798
* Constructs a new {@link SimpleRemapper} with the given mapping.
6899
*
69100
* @param oldName the key corresponding to a method, field or internal name (see {@link
70101
* #SimpleRemapper(Map)} for the format of these keys).
71102
* @param newName the new method, field or internal name (see {@link
72103
* org.objectweb.asm.Type#getInternalName()}).
104+
* @deprecated use {@link #SimpleRemapper(int, String, String)} instead.
73105
*/
106+
@Deprecated
74107
public SimpleRemapper(final String oldName, final String newName) {
75108
this.mapping = Collections.singletonMap(oldName, newName);
76109
}
77110

111+
/**
112+
* Constructs a new {@link SimpleRemapper} with the given mapping.
113+
*
114+
* @param api the ASM API version supported by this remapper. Must be one of the {@code
115+
* ASM}<i>x</i> values in {@link Opcodes}.
116+
* @param oldName the key corresponding to a method, field or internal name (see {@link
117+
* #SimpleRemapper(Map)} for the format of these keys).
118+
* @param newName the new method, field or internal name (see {@link
119+
* org.objectweb.asm.Type#getInternalName()}).
120+
*/
121+
public SimpleRemapper(final int api, final String oldName, final String newName) {
122+
super(api);
123+
this.mapping = Collections.singletonMap(oldName, newName);
124+
}
125+
78126
@Override
79127
public String mapMethodName(final String owner, final String name, final String descriptor) {
80128
String remappedName = map(owner + '.' + name + descriptor);
@@ -87,6 +135,16 @@ public String mapInvokeDynamicMethodName(final String name, final String descrip
87135
return remappedName == null ? name : remappedName;
88136
}
89137

138+
@Override
139+
public String mapInvokeDynamicMethodName(
140+
final String name,
141+
final String descriptor,
142+
final Handle bootstrapMethodHandle,
143+
final Object... bootstrapMethodArguments) {
144+
String remappedName = map('.' + name + descriptor);
145+
return remappedName == null ? name : remappedName;
146+
}
147+
90148
@Override
91149
public String mapAnnotationAttributeName(final String descriptor, final String name) {
92150
String remappedName = map(descriptor + '.' + name);

0 commit comments

Comments
 (0)