本文整理汇总了Java中org.objectweb.asm.MethodVisitor.visitInsn方法的典型用法代码示例。如果您正苦于以下问题:Java MethodVisitor.visitInsn方法的具体用法?Java MethodVisitor.visitInsn怎么用?Java MethodVisitor.visitInsn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.objectweb.asm.MethodVisitor
的用法示例。
在下文中一共展示了MethodVisitor.visitInsn方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateMethodTest2
import org.objectweb.asm.MethodVisitor; //导入方法依赖的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: hookMethod
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
/**
* (none-javadoc)
*
* @see AbstractClassHook#hookMethod(int, String, String, String, String[], MethodVisitor)
*/
@Override
public MethodVisitor hookMethod(int access, String name, String desc, String signature, String[] exceptions, MethodVisitor mv) {
if (name.equals("targetUrl")) {
return new AdviceAdapter(Opcodes.ASM5, mv, access, name, desc) {
@Override
protected void onMethodExit(int opcode) {
if (opcode != Opcodes.ATHROW) {
mv.visitInsn(Opcodes.DUP);
invokeStatic(Type.getType(JstlImportHook.class),
new Method("checkJstlImport", "(Ljava/lang/String;)V"));
}
}
};
}
return mv;
}
示例3: createBooleanObj
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private static void createBooleanObj(MethodVisitor mv, int argsPostion) {
mv.visitTypeInsn(Opcodes.NEW, "java/lang/Byte");
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(Opcodes.ILOAD, argsPostion);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Byte", "<init>", "(B)V");
mv.visitInsn(Opcodes.AASTORE);
}
示例4: addActionMethod
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
public void addActionMethod(Method method) throws Exception {
Type returnType = Type.getType(method.getReturnType());
Type[] originalParameterTypes = CollectionUtils.collectArray(method.getParameterTypes(), Type.class, new Transformer<Type, Class>() {
public Type transform(Class clazz) {
return Type.getType(clazz);
}
});
int numParams = originalParameterTypes.length;
Type[] closurisedParameterTypes = new Type[numParams];
System.arraycopy(originalParameterTypes, 0, closurisedParameterTypes, 0, numParams);
closurisedParameterTypes[numParams - 1] = CLOSURE_TYPE;
String methodDescriptor = Type.getMethodDescriptor(returnType, closurisedParameterTypes);
// GENERATE public <return type> <method>(Closure v) { return <method>(…, ConfigureUtil.configureUsing(v)); }
MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methodDescriptor, null, EMPTY_STRINGS);
methodVisitor.visitCode();
// GENERATE <method>(…, ConfigureUtil.configureUsing(v));
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
for (int stackVar = 1; stackVar < numParams; ++stackVar) {
methodVisitor.visitVarInsn(closurisedParameterTypes[stackVar - 1].getOpcode(Opcodes.ILOAD), stackVar);
}
// GENERATE ConfigureUtil.configureUsing(v);
methodVisitor.visitVarInsn(Opcodes.ALOAD, numParams);
methodDescriptor = Type.getMethodDescriptor(ACTION_TYPE, CLOSURE_TYPE);
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, CONFIGURE_UTIL_TYPE.getInternalName(), "configureUsing", methodDescriptor, false);
methodDescriptor = Type.getMethodDescriptor(Type.getType(method.getReturnType()), originalParameterTypes);
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, generatedType.getInternalName(), method.getName(), methodDescriptor, false);
methodVisitor.visitInsn(returnType.getOpcode(Opcodes.IRETURN));
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
}
示例5: generateToStringMethod
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private static void generateToStringMethod(ClassWriter cw, String selfClassInternalName, String selfClassDescriptor,
Parameter[] parameters) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitLdcInsn(Type.getType(selfClassDescriptor));
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getSimpleName", "()Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKESTATIC, "java/lang/String", "valueOf", "(Ljava/lang/Object;)Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
mv.visitLdcInsn(" [");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
for (int i = 0; i < parameters.length; i++) {
Parameter param = parameters[i];
mv.visitLdcInsn(param.getName() + "=");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;",
false);
mv.visitVarInsn(ALOAD, 0); // this
mv.visitFieldInsn(GETFIELD, selfClassInternalName, param.getName(), Type.getDescriptor(param.getType()));
Class<?> paramType = param.getType();
if (paramType.isPrimitive()) {
// special case with StringBuilder, no specific method we default to append(int)
if (paramType == short.class || paramType == byte.class) {
paramType = int.class;
}
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
Type.getMethodDescriptor(Type.getType(StringBuilder.class), Type.getType(paramType)), false);
} else {
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
"(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false);
}
if (i + 1 < parameters.length) {
mv.visitLdcInsn(", ");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;",
false);
}
}
mv.visitLdcInsn("]");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
mv.visitInsn(ARETURN);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLocalVariable("this", selfClassDescriptor, null, l0, l1, 0);
mv.visitMaxs(-1, -1);
mv.visitEnd();
}
示例6: writeGroovyMethods
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private void writeGroovyMethods(ClassVisitor visitor, Class<?> viewClass) {
// Object propertyMissing(String name)
MethodVisitor methodVisitor = declareMethod(visitor, "propertyMissing", GET_PROPERTY_MISSING_METHOD_DESCRIPTOR, CONCRETE_SIGNATURE);
// throw new MissingPropertyException(name, <view-type>.class)
methodVisitor.visitTypeInsn(NEW, MISSING_PROPERTY_EXCEPTION_TYPE);
methodVisitor.visitInsn(DUP);
putFirstMethodArgumentOnStack(methodVisitor);
putClassOnStack(methodVisitor, viewClass);
methodVisitor.visitMethodInsn(INVOKESPECIAL, MISSING_PROPERTY_EXCEPTION_TYPE, "<init>", MISSING_PROPERTY_CONSTRUCTOR_DESCRIPTOR, false);
finishVisitingMethod(methodVisitor, ATHROW);
// Object propertyMissing(String name, Object value)
methodVisitor = declareMethod(visitor, "propertyMissing", SET_PROPERTY_MISSING_METHOD_DESCRIPTOR, CONCRETE_SIGNATURE);
// throw new MissingPropertyException(name, <view-type>.class)
methodVisitor.visitTypeInsn(NEW, MISSING_PROPERTY_EXCEPTION_TYPE);
methodVisitor.visitInsn(DUP);
putFirstMethodArgumentOnStack(methodVisitor);
putClassOnStack(methodVisitor, viewClass);
methodVisitor.visitMethodInsn(INVOKESPECIAL, MISSING_PROPERTY_EXCEPTION_TYPE, "<init>", MISSING_PROPERTY_CONSTRUCTOR_DESCRIPTOR, false);
finishVisitingMethod(methodVisitor, ATHROW);
// Object methodMissing(String name, Object args)
methodVisitor = declareMethod(visitor, "methodMissing", METHOD_MISSING_METHOD_DESCRIPTOR, CONCRETE_SIGNATURE);
// throw new MissingMethodException(name, <view-type>.class, args)
methodVisitor.visitTypeInsn(NEW, MISSING_METHOD_EXCEPTION_TYPE);
methodVisitor.visitInsn(DUP);
putMethodArgumentOnStack(methodVisitor, 1);
putClassOnStack(methodVisitor, viewClass);
putMethodArgumentOnStack(methodVisitor, 2);
methodVisitor.visitTypeInsn(CHECKCAST, OBJECT_ARRAY_TYPE);
methodVisitor.visitMethodInsn(INVOKESPECIAL, MISSING_METHOD_EXCEPTION_TYPE, "<init>", MISSING_METHOD_EXCEPTION_CONSTRUCTOR_DESCRIPTOR, false);
finishVisitingMethod(methodVisitor, ATHROW);
}
示例7: insertNewInstance
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
static void insertNewInstance(ClassWriter cw, String classNameInternal) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "()Ljava/lang/Object;", null, null);
mv.visitCode();
mv.visitTypeInsn(NEW, classNameInternal);
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, classNameInternal, "<init>", "()V");
mv.visitInsn(ARETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
示例8: insertThrowExceptionForFieldNotFound
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
static private MethodVisitor insertThrowExceptionForFieldNotFound(MethodVisitor mv) {
mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
mv.visitInsn(DUP);
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitLdcInsn("Field not found: ");
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V");
mv.visitVarInsn(ILOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V");
mv.visitInsn(ATHROW);
return mv;
}
示例9: addGetMetaClass
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private void addGetMetaClass() {
Label lookup = new Label();
MethodVisitor methodVisitor = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, "getMetaClass", RETURN_META_CLASS, null, null);
methodVisitor.visitCode();
// if (this.metaClass != null) { return this.metaClass; }
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, className, META_CLASS_FIELD, META_CLASS_TYPE.getDescriptor());
methodVisitor.visitInsn(Opcodes.DUP);
methodVisitor.visitJumpInsn(Opcodes.IFNULL, lookup);
methodVisitor.visitInsn(Opcodes.ARETURN);
methodVisitor.visitLabel(lookup);
methodVisitor.visitFrame(Opcodes.F_NEW, 1, new Object[]{className}, 1, new Object[]{META_CLASS_TYPE.getInternalName()});
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); // for storing to field
// GroovySystem.getMetaClassRegistry()
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, GROOVY_SYSTEM_TYPE.getInternalName(), "getMetaClassRegistry", RETURN_META_CLASS_REGISTRY, false);
// this.getClass()
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, OBJECT_TYPE.getInternalName(), "getClass", RETURN_CLASS, false);
// getMetaClass(..)
methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, META_CLASS_REGISTRY_TYPE.getInternalName(), "getMetaClass", RETURN_META_CLASS_FROM_CLASS, true);
// this.metaClass = <value>
methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, className, META_CLASS_FIELD, META_CLASS_TYPE.getDescriptor());
// return this.metaClass
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, className, META_CLASS_FIELD, META_CLASS_TYPE.getDescriptor());
methodVisitor.visitInsn(Opcodes.ARETURN);
methodVisitor.visitMaxs(4, 1);
methodVisitor.visitEnd();
}
示例10: generateMethodTest3
import org.objectweb.asm.MethodVisitor; //导入方法依赖的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: addConstructor
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
public void addConstructor(Constructor<?> constructor) throws Exception {
List<Type> paramTypes = new ArrayList<Type>();
for (Class<?> paramType : constructor.getParameterTypes()) {
paramTypes.add(Type.getType(paramType));
}
String methodDescriptor = Type.getMethodDescriptor(VOID_TYPE, paramTypes.toArray(EMPTY_TYPES));
MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, signature(constructor), EMPTY_STRINGS);
for (Annotation annotation : constructor.getDeclaredAnnotations()) {
if (annotation.annotationType().getAnnotation(Inherited.class) != null) {
continue;
}
Retention retention = annotation.annotationType().getAnnotation(Retention.class);
AnnotationVisitor annotationVisitor = methodVisitor.visitAnnotation(Type.getType(annotation.annotationType()).getDescriptor(), retention != null && retention.value() == RetentionPolicy.RUNTIME);
annotationVisitor.visitEnd();
}
methodVisitor.visitCode();
// this.super(p0 .. pn)
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
for (int i = 0; i < constructor.getParameterTypes().length; i++) {
methodVisitor.visitVarInsn(Type.getType(constructor.getParameterTypes()[i]).getOpcode(Opcodes.ILOAD), i + 1);
}
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>", methodDescriptor, false);
methodVisitor.visitInsn(Opcodes.RETURN);
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
}
示例12: genExceptionThrowForUnknownParam
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private static void genExceptionThrowForUnknownParam(String paramType, MethodVisitor mv) {
mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
mv.visitInsn(DUP);
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitLdcInsn("No " + paramType + " parameter named ");
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;",
false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V", false);
mv.visitInsn(ATHROW);
}
示例13: insertArrayStart
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private void insertArrayStart(MethodVisitor mv, BytecodeGenerator generator) {
// Store the array in a local variable
mv.visitVarInsn(ASTORE, varArray);
// array.length
mv.visitVarInsn(ALOAD, varArray);
mv.visitInsn(ARRAYLENGTH);
mv.visitVarInsn(ISTORE, varLength);
// i = 0
mv.visitInsn(ICONST_0);
mv.visitVarInsn(ISTORE, varIndex);
mv.visitLabel(loopStart);
// Create a compressed frame
mv.visitFrame(F_FULL, 6, new Object[]{generator.className,
generator.data.getDataType().getInternalName(), BUILDER.getInternalName(),
member.clazzType.getInternalName(), INTEGER, INTEGER}, 0, new Object[0]);
// if(index >= length) break;
mv.visitVarInsn(ILOAD, varIndex);
mv.visitVarInsn(ILOAD, varLength);
mv.visitJumpInsn(IF_ICMPGE, loopEnd);
// array[i]
mv.visitVarInsn(ALOAD, varArray);
mv.visitVarInsn(ILOAD, varIndex);
mv.visitInsn(AALOAD);
mv.visitVarInsn(ASTORE, varObject);
}
示例14: generateHashCodeMethod
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private static void generateHashCodeMethod(ClassWriter cw, String selfClassInternalName, String selfClassDescriptor,
Parameter[] parameters) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null);
mv.visitCode();
// int result = 1;
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitInsn(ICONST_1);
mv.visitVarInsn(ISTORE, 1);
for (Parameter param : parameters) {
Class<?> type = param.getType();
mv.visitIntInsn(BIPUSH, 31);
mv.visitVarInsn(ILOAD, 1);
mv.visitInsn(IMUL);
mv.visitVarInsn(ALOAD, 0);
Type typeType = Type.getType(type);
mv.visitFieldInsn(GETFIELD, selfClassInternalName, param.getName(), typeType.getDescriptor());
if (type.isPrimitive()) {
Class<?> boxed = BytecodeGenUtils.getBoxed(type);
mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(boxed), "hashCode",
Type.getMethodDescriptor(Type.INT_TYPE, typeType), false);
} else {
mv.visitMethodInsn(INVOKESTATIC, "java/util/Objects", "hashCode", "(Ljava/lang/Object;)I", false);
}
mv.visitInsn(IADD);
mv.visitVarInsn(ISTORE, 1);
}
mv.visitVarInsn(ILOAD, 1);
mv.visitInsn(IRETURN);
Label l7 = new Label();
mv.visitLabel(l7);
mv.visitLocalVariable("this", selfClassDescriptor, null, l0, l7, 0);
mv.visitLocalVariable("result", "I", null, l0, l7, 1);
mv.visitMaxs(-1, -1);
mv.visitEnd();
}
示例15: insertSectionEnd
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
@Override
public void insertSectionEnd(MethodVisitor mv, BytecodeGenerator generator) {
mv.visitInsn(POP);
mv.visitLabel(ifEnd);
mv.visitFrame(F_APPEND, 1, new Object[]{BUILDER.getInternalName()}, 0, null);
}