本文整理汇总了Java中org.objectweb.asm.ClassVisitor.visitMethod方法的典型用法代码示例。如果您正苦于以下问题:Java ClassVisitor.visitMethod方法的具体用法?Java ClassVisitor.visitMethod怎么用?Java ClassVisitor.visitMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.objectweb.asm.ClassVisitor
的用法示例。
在下文中一共展示了ClassVisitor.visitMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateMethodTest2
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
/**
* Generate test with an invokedynamic, a static bootstrap method without extra args and
* args to the target method.
*/
private void generateMethodTest2(ClassVisitor cv) {
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test2", "()V",
null, null);
MethodType mt = MethodType.methodType(
CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
Handle bootstrap = new Handle( Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
"bsmLookupStatic", mt.toMethodDescriptorString(), false);
mv.visitLdcInsn(new Boolean(true));
mv.visitLdcInsn(new Byte((byte) 127));
mv.visitLdcInsn(new Character('c'));
mv.visitLdcInsn(new Short((short) 1024));
mv.visitLdcInsn(new Integer(123456));
mv.visitLdcInsn(new Float(1.2f));
mv.visitLdcInsn(new Long(123456789));
mv.visitLdcInsn(new Double(3.5123456789));
mv.visitLdcInsn("String");
mv.visitInvokeDynamicInsn("targetMethodTest2", "(ZBCSIFJDLjava/lang/String;)V", bootstrap);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(-1, -1);
}
示例2: generateMethodTest4
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
/**
* Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a
* MethodHandle of kind invokespecial.
*/
private void generateMethodTest4(ClassVisitor cv) {
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test4", "()V",
null, null);
MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
MethodType.class, MethodHandle.class);
Handle bootstrap = new Handle( Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
"bsmCreateCallSite", mt.toMethodDescriptorString(), false);
mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class));
mv.visitInsn(Opcodes.DUP);
mv.visitMethodInsn(
Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false);
mv.visitInvokeDynamicInsn("targetMethodTest5", "(Linvokecustom/InvokeCustom;)V", bootstrap,
new Handle( Opcodes.H_INVOKESPECIAL, Type.getInternalName(Super.class),
"targetMethodTest5", "()V", false));
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(-1, -1);
}
示例3: generateMethodTest5
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
/**
* Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a
* MethodHandle of kind invoke interface. The target method is a default method into an interface
* that shadows another default method from a super interface.
*/
private void generateMethodTest5(ClassVisitor cv) {
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test5", "()V",
null, null);
MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
MethodType.class, MethodHandle.class);
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
"bsmCreateCallCallingtargetMethodTest6", mt.toMethodDescriptorString(), false);
mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class));
mv.visitInsn(Opcodes.DUP);
mv.visitMethodInsn(
Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false);
mv.visitInvokeDynamicInsn("targetMethodTest6", "(Linvokecustom/I;)V", bootstrap,
new Handle(Opcodes.H_INVOKEINTERFACE, Type.getInternalName(I.class),
"targetMethodTest6", "()V", true));
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(-1, -1);
}
示例4: generateMethodTest7
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
/**
* Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a
* MethodHandle of kind invoke interface. The target method is a method into an interface
* that is shadowed by another definition into a sub interfaces.
*/
private void generateMethodTest7(ClassVisitor cv) {
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test7", "()V",
null, null);
MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
MethodType.class, MethodHandle.class);
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
"bsmCreateCallCallingtargetMethodTest8", mt.toMethodDescriptorString(), false);
mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class));
mv.visitInsn(Opcodes.DUP);
mv.visitMethodInsn(
Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false);
mv.visitInvokeDynamicInsn("targetMethodTest8", "(Linvokecustom/J;)V", bootstrap,
new Handle(Opcodes.H_INVOKEINTERFACE, Type.getInternalName(J.class),
"targetMethodTest8", "()V", true));
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(-1, -1);
}
示例5: generateMethodTest8
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
/**
* Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a
* MethodHandle of kind invoke virtual. The target method is a method into an interface that is
* not shadowed by an implementation into a classes implementing the interface.
*/
private void generateMethodTest8(ClassVisitor cv) {
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test8", "()V",
null, null);
MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
MethodType.class, MethodHandle.class);
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
"bsmCreateCallCallingtargetMethodTest9", mt.toMethodDescriptorString(), false);
mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class));
mv.visitInsn(Opcodes.DUP);
mv.visitMethodInsn(
Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false);
mv.visitInvokeDynamicInsn("targetMethodTest9", "(Linvokecustom/InvokeCustom;)V", bootstrap,
new Handle(Opcodes.H_INVOKEVIRTUAL, Type.getInternalName(InvokeCustom.class),
"targetMethodTest9", "()V", false));
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(-1, -1);
}
示例6: writeEqualsMethod
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
private void writeEqualsMethod(ClassVisitor cw, Type generatedType) {
MethodVisitor methodVisitor = cw.visitMethod(Opcodes.ACC_PUBLIC, "equals", EQUALS_METHOD_DESCRIPTOR, null, null);
methodVisitor.visitCode();
// if (arg == this) { return true; }
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitVarInsn(ALOAD, 1);
Label notSameLabel = new Label();
methodVisitor.visitJumpInsn(IF_ACMPNE, notSameLabel);
methodVisitor.visitInsn(ICONST_1);
methodVisitor.visitInsn(IRETURN);
// if (!(age instanceof GeneratedView)) { return false; }
methodVisitor.visitLabel(notSameLabel);
methodVisitor.visitVarInsn(ALOAD, 1);
methodVisitor.visitTypeInsn(INSTANCEOF, GENERATED_VIEW_TYPE.getInternalName());
Label generatedViewLabel = new Label();
methodVisitor.visitJumpInsn(IFNE, generatedViewLabel);
methodVisitor.visitInsn(ICONST_0);
methodVisitor.visitInsn(IRETURN);
// return state.equals(((GeneratedView)arg).__view_state());
methodVisitor.visitLabel(generatedViewLabel);
putStateFieldValueOnStack(methodVisitor, generatedType);
methodVisitor.visitVarInsn(ALOAD, 1);
methodVisitor.visitTypeInsn(CHECKCAST, GENERATED_VIEW_TYPE.getInternalName());
methodVisitor.visitMethodInsn(INVOKEINTERFACE, GENERATED_VIEW_TYPE.getInternalName(), "__view_state__", GET_VIEW_STATE_METHOD_DESCRIPTOR, true);
methodVisitor.visitMethodInsn(INVOKEINTERFACE, GENERATED_VIEW_STATE_TYPE_NAME, "equals", EQUALS_METHOD_DESCRIPTOR, true);
finishVisitingMethod(methodVisitor, Opcodes.IRETURN);
}
示例7: accept
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
/**
* Makes the given class visitor visit this method.
*
* @param cv
* a class visitor.
*/
public void accept(final ClassVisitor cv) {
String[] exceptions = new String[this.exceptions.size()];
this.exceptions.toArray(exceptions);
MethodVisitor mv = cv.visitMethod(access, name, desc, signature,
exceptions);
if (mv != null) {
accept(mv);
}
}
示例8: generateMethodMain
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
private void generateMethodMain(ClassVisitor cv) {
MethodVisitor mv = cv.visitMethod(
Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test1", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test2", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test3", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test4", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test5", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test6", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test7", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test8", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test9", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test10", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test11", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test12", "()V", false);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test13", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(-1, -1);
}
示例9: generateMethodTest1
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
/**
* Generate test with an invokedynamic, a static bootstrap method without extra args and no arg
* to the target method.
*/
private void generateMethodTest1(ClassVisitor cv) {
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test1", "()V",
null, null);
MethodType mt =
MethodType.methodType(
CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
Handle bootstrap = new Handle( Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
"bsmLookupStatic", mt.toMethodDescriptorString(), false);
mv.visitInvokeDynamicInsn("targetMethodTest1", "()V", bootstrap);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(-1, -1);
}
示例10: generateMethodTest3
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
/**
* Generate test with an invokedynamic, a static bootstrap method with extra args and no arg
* to the target method.
*/
private void generateMethodTest3(ClassVisitor cv) {
MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test3", "()V",
null, null);
MethodType mt = MethodType.methodType(
CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class, int.class,
long.class, float.class, double.class);
Handle bootstrap = new Handle( Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
"bsmLookupStaticWithExtraArgs", mt.toMethodDescriptorString(), false);
mv.visitInvokeDynamicInsn("targetMethodTest3", "()V", bootstrap, new Integer(1),
new Long(123456789), new Float(123.456), new Double(123456.789123));
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(-1, -1);
}
示例11: MethodWriter
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
public MethodWriter(int access, Method method, ClassVisitor cw, BitSet statements, CompilerSettings settings) {
super(Opcodes.ASM5, cw.visitMethod(access, method.getName(), method.getDescriptor(), null, null),
access, method.getName(), method.getDescriptor());
this.statements = statements;
this.settings = settings;
}
示例12: declareMethod
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
private MethodVisitor declareMethod(ClassVisitor visitor, String methodName, String methodDescriptor, String methodSignature, int access) {
MethodVisitor methodVisitor = visitor.visitMethod(access, methodName, methodDescriptor, methodSignature, NO_EXCEPTIONS);
methodVisitor.visitCode();
return methodVisitor;
}
示例13: generateMethodProxy
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
@Contract("null, null, null, null, null, null, null, null, _, _ -> fail")
static void generateMethodProxy(ClassVisitor cv, Method interfaceMethod,
Type reflectorClass, Type targetClass, Type interfaceClass,
String targetMethodName, Type[] targetParameters, Type targetReturnType,
int flags, int mhIndex) {
String methodName = interfaceMethod.getName();
String methodDesc = Type.getMethodDescriptor(interfaceMethod);
MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, methodName, methodDesc, null, null);
GeneratorAdapter adapter = new GeneratorAdapter(mv, ACC_PUBLIC, methodName, methodDesc);
adapter.visitCode();
/* Load MethodHandle, if required */
loadMH(adapter, reflectorClass, flags, mhIndex);
/* Load instance, if required */
loadInstance(adapter, reflectorClass, targetClass, flags);
/* Load method parameters into stack */
loadArguments(adapter, Type.getArgumentTypes(interfaceMethod), targetParameters, (flags & Magic.TARGET_CLASS_VISIBILITY_PUBLIC) != 0);
if((flags & Magic.REFLECTOR_METHOD_USE_METHODHANDLE) != 0) {
/* Build MethodHandle descriptor (invokeExact is polymorphic) */
String mhDescriptor = convertDesc(targetParameters,
((flags & Magic.RETURN_TYPE_PUBLIC) != 0 ? targetReturnType : OBJECT),
(flags & Magic.REFLECTOR_METHOD_USE_INSTANCE) != 0 ? ((flags & Magic.TARGET_CLASS_VISIBILITY_PUBLIC) != 0 ? targetClass : OBJECT) : null);
/* Select right MethodHandle invoker */
String mhInvoker = (flags & Magic.TARGET_CLASS_VISIBILITY_PUBLIC) != 0 && (flags & Magic.RETURN_TYPE_PUBLIC) != 0 ? "invokeExact" : "invoke";
/* Invoke MethodHandle */
mv.visitMethodInsn(INVOKEVIRTUAL, MH.getInternalName(), mhInvoker, mhDescriptor, false);
} else {
/* Figure out what opcode to use */
int opCode = (flags & Magic.REFLECTOR_METHOD_USE_INVOKEINTERFACE) != 0 ?
((flags & Magic.REFLECTOR_METHOD_USE_INSTANCE) != 0 ? INVOKEINTERFACE : INVOKESTATIC)
:
((flags & Magic.REFLECTOR_METHOD_USE_INSTANCE) != 0 ? INVOKEVIRTUAL : INVOKESTATIC);
/* Build descriptor & select target class */
String targetDescriptor = convertDesc(targetParameters, targetReturnType, null);
String targetName = (flags & Magic.REFLECTOR_METHOD_USE_INVOKEINTERFACE) != 0 ? interfaceClass.getInternalName() : targetClass.getInternalName();
/* Invoke method */
adapter.visitMethodInsn(opCode, targetName, targetMethodName, targetDescriptor, (flags & Magic.REFLECTOR_METHOD_USE_INVOKEINTERFACE) != 0);
}
/* Return */
handleReturn(adapter, interfaceMethod, targetReturnType);
adapter.returnValue();
/* End method */
adapter.endMethod();
}
示例14: generateFieldReadMethod
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
@Contract("null, null, null, null, null, null, _, _ -> fail")
static void generateFieldReadMethod(ClassVisitor cv, Method interfaceMethod,
Type reflectorClass, Type targetClass, Type fieldType, String fieldName,
int flags, int mhIndex) {
String methodName = interfaceMethod.getName();
String methodDesc = Type.getMethodDescriptor(interfaceMethod);
MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, methodName, methodDesc, null, null);
GeneratorAdapter adapter = new GeneratorAdapter(mv, ACC_PUBLIC, methodName, methodDesc);
adapter.visitCode();
/* Load MethodHandle, if required */
loadMH(adapter, reflectorClass, flags, mhIndex);
/* Load instance, if required */
loadInstance(adapter, reflectorClass, targetClass, flags);
if((flags & Magic.REFLECTOR_METHOD_USE_METHODHANDLE) != 0) {
/* Build MethodHandle descriptor */
String mhDescriptor = newDescriptor()
.accepts((flags & Magic.REFLECTOR_METHOD_USE_INSTANCE) != 0 ? ((flags & Magic.TARGET_CLASS_VISIBILITY_PUBLIC) != 0 ? targetClass : OBJECT).getDescriptor() : "")
.returns(fieldType.getDescriptor())
.toString();
/* Select right MethodHandle invoker */
String mhInvoker = (flags & Magic.TARGET_CLASS_VISIBILITY_PUBLIC) != 0 && (flags & Magic.RETURN_TYPE_PUBLIC) != 0 ? "invokeExact" : "invoke";
/* Invoke MethodHandle */
adapter.visitMethodInsn(INVOKEVIRTUAL, MH.getInternalName(), mhInvoker, mhDescriptor, false);
} else {
if((flags & Magic.REFLECTOR_METHOD_USE_INSTANCE) != 0)
adapter.getField(targetClass, fieldName, fieldType);
else
adapter.getStatic(targetClass, fieldName, fieldType);
}
/* Return */
handleReturn(adapter, interfaceMethod, fieldType);
adapter.returnValue();
/* End method */
adapter.endMethod();
}
示例15: generateFieldWriteMethod
import org.objectweb.asm.ClassVisitor; //导入方法依赖的package包/类
@Contract("null, null, null, null, null, null, _, _ -> fail")
static void generateFieldWriteMethod(ClassVisitor cv, Method interfaceMethod,
Type reflectorClass, Type targetClass, Type fieldType, String fieldName,
int flags, int mhIndex) {
String methodName = interfaceMethod.getName();
String methodDesc = Type.getMethodDescriptor(interfaceMethod);
MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, methodName, methodDesc, null, null);
GeneratorAdapter adapter = new GeneratorAdapter(mv, ACC_PUBLIC, methodName, methodDesc);
adapter.visitCode();
/* Load MethodHandle, if required */
loadMH(adapter, reflectorClass, flags, mhIndex);
/* Load instance, if required */
loadInstance(adapter, reflectorClass, targetClass, flags);
/* Load method parameter into stack */
adapter.loadArg(0);
if((flags & Magic.REFLECTOR_METHOD_USE_METHODHANDLE) != 0) {
/* Build MethodHandle descriptor */
String mhDescriptor = newDescriptor()
.accepts(((flags & Magic.REFLECTOR_METHOD_USE_INSTANCE) != 0 ?
((flags & Magic.TARGET_CLASS_VISIBILITY_PUBLIC) != 0 ? targetClass : OBJECT).getDescriptor(): "")
+ fieldType.getDescriptor())
.toString();
/* Select right MethodHandle invoker */
String mhInvoker = (flags & Magic.TARGET_CLASS_VISIBILITY_PUBLIC) != 0 && (flags & Magic.RETURN_TYPE_PUBLIC) != 0 ? "invokeExact" : "invoke";
/* Invoke MethodHandle */
adapter.visitMethodInsn(INVOKEVIRTUAL, MH.getInternalName(), mhInvoker, mhDescriptor, false);
} else {
if((flags & Magic.REFLECTOR_METHOD_USE_INSTANCE) != 0)
adapter.putField(targetClass, fieldName, fieldType);
else
adapter.putStatic(targetClass, fieldName, fieldType);
}
/* Return */
adapter.returnValue();
/* End method */
adapter.endMethod();
}