我正在创建一个 java 框架来在 Invoke dynamic 的帮助下转换 bean。我用 ASM 创建了转换类。为了生成如下所示的转换:
target.setter( convert(source.getter()) );
我用 ASM 编写了以下字节码:
mv.visitVarInsn(ALOAD, ARGUMENT_2);
mv.visitVarInsn(ALOAD, ARGUMENT_1);
mv.visitMethodInsn(INVOKEVIRTUAL, sourceClass, sourceGetter.getName(), Type.getMethodDescriptor(sourceGetter), false);
mv.visitInvokeDynamicInsn("convert", Type.getMethodDescriptor(Type.getType(targetSetter.getParameterTypes()[0]), Type.getType(sourceGetter.getReturnType())), converterBootstrapMethod);
mv.visitMethodInsn(INVOKEVIRTUAL, targetClass, targetSetter.getName(), Type.getMethodDescriptor(targetSetter), false);
然后 convert 方法搜索可以处理给定类型的转换器。这看起来像:
public static CallSite bootstrap(final MethodHandles.Lookup caller, final String name, final MethodType type) throws Exception {
final Class<?> sourceType = type.parameterType(0);
final Class<?> targetType = type.returnType();
MethodHandle converter = findConverter(sourceType, targetType);
return new ConstantCallSite( converter.asType(type) );
}
这适用于说字符串到整数的转换。但不适用于泛型。sourceType 只是Ljava/util/List;
而不是完整的Ljava/util/List<Ljava/lang/String;>;
如何在此引导方法中获得完整类型?