本文整理匯總了Java中org.objectweb.asm.commons.GeneratorAdapter.visitMethodInsn方法的典型用法代碼示例。如果您正苦於以下問題:Java GeneratorAdapter.visitMethodInsn方法的具體用法?Java GeneratorAdapter.visitMethodInsn怎麽用?Java GeneratorAdapter.visitMethodInsn使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.objectweb.asm.commons.GeneratorAdapter
的用法示例。
在下文中一共展示了GeneratorAdapter.visitMethodInsn方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: writeMissingMessageWithHash
import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
void writeMissingMessageWithHash(GeneratorAdapter mv, String visitedClassName) {
mv.newInstance(INSTANT_RELOAD_EXCEPTION_TYPE);
mv.dup();
mv.push("int switch could not find %d in %s");
mv.push(3);
mv.newArray(OBJECT_TYPE);
mv.dup();
mv.push(0);
visitInt();
mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
"java/lang/Integer",
"valueOf",
"(I)Ljava/lang/Integer;", false);
mv.arrayStore(OBJECT_TYPE);
mv.dup();
mv.push(2);
mv.push(visitedClassName);
mv.arrayStore(OBJECT_TYPE);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
"java/lang/String",
"format",
"(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false);
mv.invokeConstructor(INSTANT_RELOAD_EXCEPTION_TYPE,
Method.getMethod("void <init> (String)"));
mv.throwException();
}
示例2: writeMissingMessageWithHash
import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
/**
* Generates a standard error exception with message similar to:
*
* String switch could not find 'equals.(Ljava/lang/Object;)Z' with hashcode 0
* in com/example/basic/GrandChild
*
* @param mv The generator adaptor used to emit the lookup switch code.
* @param visitedClassName The abstract string trie structure.
*/
void writeMissingMessageWithHash(GeneratorAdapter mv, String visitedClassName) {
mv.newInstance(INSTANT_RELOAD_EXCEPTION_TYPE);
mv.dup();
mv.push("String switch could not find '%s' with hashcode %s in %s");
mv.push(3);
mv.newArray(OBJECT_TYPE);
mv.dup();
mv.push(0);
visitString();
mv.arrayStore(OBJECT_TYPE);
mv.dup();
mv.push(1);
visitString();
visitHashMethod(mv);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
"java/lang/Integer",
"valueOf",
"(I)Ljava/lang/Integer;", false);
mv.arrayStore(OBJECT_TYPE);
mv.dup();
mv.push(2);
mv.push(visitedClassName);
mv.arrayStore(OBJECT_TYPE);
mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
"java/lang/String",
"format",
"(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false);
mv.invokeConstructor(INSTANT_RELOAD_EXCEPTION_TYPE,
Method.getMethod("void <init> (String)"));
mv.throwException();
}
示例3: createInsertCode
import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
/**
* 插入代碼
*
* @param mv
* @param className
* @param args
* @param returnType
* @param isStatic
*/
public static void createInsertCode(GeneratorAdapter mv, String className, List<Type> args, Type returnType, boolean isStatic, int methodId) {
/**
* 調用isSupport方法
*/
prepareMethodParameters(mv, className, args, returnType, isStatic, methodId);
//開始調用
mv.visitMethodInsn(Opcodes.INVOKESTATIC,
PROXYCLASSNAME,
"isSupport",
"([Ljava/lang/Object;Ljava/lang/Object;" + REDIRECTCLASSNAME + "ZI[Ljava/lang/Class;Ljava/lang/Class;)Z");
Label l1 = new Label();
mv.visitJumpInsn(Opcodes.IFEQ, l1);
prepareMethodParameters(mv, className, args, returnType, isStatic, methodId);
//開始調用
mv.visitMethodInsn(Opcodes.INVOKESTATIC,
PROXYCLASSNAME,
"accessDispatch",
"([Ljava/lang/Object;Ljava/lang/Object;" + REDIRECTCLASSNAME + "ZI[Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;");
//判斷是否有返回值,代碼不同
if ("V".equals(returnType.getDescriptor())) {
mv.visitInsn(Opcodes.POP);
mv.visitInsn(Opcodes.RETURN);
} else {
//強製轉化類型
if (!castPrimateToObj(mv, returnType.getDescriptor())) {
//這裏需要注意,如果是數組類型的直接使用即可,如果非數組類型,就得去除前綴了,還有最終是沒有結束符;
//比如:Ljava/lang/String; ==》 java/lang/String
String newTypeStr = null;
int len = returnType.getDescriptor().length();
if (returnType.getDescriptor().startsWith("[")) {
newTypeStr = returnType.getDescriptor().substring(0, len);
} else {
newTypeStr = returnType.getDescriptor().substring(1, len - 1);
}
mv.visitTypeInsn(Opcodes.CHECKCAST, newTypeStr);
}
//這裏還需要做返回類型不同返回指令也不同
mv.visitInsn(getReturnTypeCode(returnType.getDescriptor()));
}
mv.visitLabel(l1);
}
示例4: generateMethodProxy
import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的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();
}
示例5: generateConstructorProxy
import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
@Contract("null, null, null, null, null, _, _ -> fail")
static void generateConstructorProxy(ClassVisitor cv, Method interfaceMethod,
Type reflectorClass, Type targetClass,
Type[] targetParameters,
int flags, int mhIndex) {
String methodName = interfaceMethod.getName();
String methodDesc = Type.getMethodDescriptor(interfaceMethod);
String targetClassName = targetClass.getInternalName();
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);
if((flags & Magic.REFLECTOR_METHOD_USE_METHODHANDLE) == 0) {
adapter.visitTypeInsn(NEW, targetClassName);
adapter.visitInsn(DUP);
}
/* Load method parameters into stack */
loadArguments(adapter, Type.getArgumentTypes(interfaceMethod), targetParameters, (flags & Magic.RETURN_TYPE_PUBLIC) != 0);
if((flags & Magic.REFLECTOR_METHOD_USE_METHODHANDLE) != 0) {
/* Build MethodHandle descriptor */
String mhDescriptor = convertDesc(targetParameters,
(flags & Magic.RETURN_TYPE_PUBLIC) != 0 ? targetClass : OBJECT, null);
/* Select right MethodHandle invoker */
String mhInvoker = (flags & Magic.RETURN_TYPE_PUBLIC) != 0 ? "invokeExact" : "invoke";
/* Invoke MethodHandle */
mv.visitMethodInsn(INVOKEVIRTUAL, MH.getInternalName(), mhInvoker, mhDescriptor, false);
} else {
/* Build target descriptor */
String targetDesc = convertDesc(targetParameters, Type.VOID_TYPE, null);
/* Invoke constructor */
adapter.visitMethodInsn(INVOKESPECIAL, targetClassName, "<init>", targetDesc, false);
}
/* Return */
handleReturn(adapter, interfaceMethod, (flags & Magic.RETURN_TYPE_PUBLIC) != 0 ? targetClass : OBJECT);
adapter.returnValue();
/* End method */
adapter.endMethod();
}
示例6: generateFieldReadMethod
import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的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();
}
示例7: generateFieldWriteMethod
import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的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();
}
示例8: restore
import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
@Override
protected void restore(GeneratorAdapter mv, List<Type> args) {
// At this point, init$args has been called and the result Object is on the stack.
// The value of that Object is Object[] with exactly n + 1 elements.
// The first element is a string with the qualified name of the constructor to call.
// The remaining elements are the constructtor arguments.
// Create a new local that holds the result of init$args call.
mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
int constructorArgs = mv.newLocal(Type.getType("[Ljava/lang/Object;"));
mv.storeLocal(constructorArgs);
// Reinstate local values
mv.loadLocal(locals);
int stackIndex = 0;
for (int arrayIndex = 0; arrayIndex < args.size(); arrayIndex++) {
Type arg = args.get(arrayIndex);
// Do not restore "this"
if (arrayIndex > 0) {
// duplicates the array
mv.dup();
// index in the array of objects to restore the boxed parameter.
mv.push(arrayIndex);
// get it from the array
mv.arrayLoad(Type.getType(Object.class));
// unbox the argument
ByteCodeUtils.unbox(mv, arg);
// restore the argument
mv.visitVarInsn(arg.getOpcode(Opcodes.ISTORE), stackIndex);
}
// stack index must progress according to the parameter type we just processed.
stackIndex += arg.getSize();
}
// pops the array
mv.pop();
// Push a null for the marker parameter.
mv.loadLocal(constructorArgs);
mv.visitInsn(Opcodes.ACONST_NULL);
// Invoke the constructor
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, thisClassName, "<init>", DISPATCHING_THIS_SIGNATURE, false);
mv.goTo(end.getLabel());
}
示例9: generateMethod
import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
/**
* 生成方法
* @param cw
* @param method
* @param exceptions
* @param interfaceClazz
*/
private static ClassWriter generateMethod(ClassWriter cw,java.lang.reflect.Method method,Class<Prepared> prepared){
org.objectweb.asm.commons.Method m = new org.objectweb.asm.commons.Method(method.getName(), Type.getMethodDescriptor(method));
GeneratorAdapter mv = new GeneratorAdapter(ACC_PUBLIC, m, null, null,cw);
mv.visitLdcInsn(method.getName()); // excute的第1參數
mv.visitLdcInsn(Type.getType(method).getDescriptor()); // excute的第2參數
// excute的第3參數(是可變參數)
Parameter[] parameters = method.getParameters();
mv = setIn(mv, parameters);
// excute的第4個參數
mv.visitVarInsn(ALOAD, 0);
// 調用Prepared中的excute方法
// INVOKESTATIC
mv.visitMethodInsn(INVOKESTATIC, Type.getType(prepared).getInternalName(), "excute", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;Lorg/fastquery/core/Repository;)Ljava/lang/Object;", false);
// 返回值處理
String internalName = Type.getInternalName(method.getReturnType());
int sort = Type.getReturnType(method).getSort();
if( sort == 0 ) { // 如果返回值是 Void
mv.visitInsn(POP);
mv.visitInsn(RETURN);
} else if( (1 <= sort) && (sort <= 8) ) { // 如果是基本類型 [1,8]
Object[] objs = TypeUtil.getTypeInfo(Type.getReturnType(method).getDescriptor());
String type = objs[0].toString();
mv.visitTypeInsn(CHECKCAST, type);
mv.visitMethodInsn(INVOKEVIRTUAL, type, objs[1].toString(), objs[2].toString(), false);
// Integer.parseInt(objs[3].toString()) 比 Integer.valueOf(objs[3].toString()).intValue()更優.
mv.visitInsn(Integer.parseInt(objs[3].toString()));
} else { //sort==9 表述數組類型int[]或Integer[], srot=10表示包裝類型
mv.visitTypeInsn(CHECKCAST, internalName);
mv.visitInsn(ARETURN);
}
// mv.visitEnd()
mv.endMethod();
return cw;
}