當前位置: 首頁>>代碼示例>>Java>>正文


Java GeneratorAdapter.visitInsn方法代碼示例

本文整理匯總了Java中org.objectweb.asm.commons.GeneratorAdapter.visitInsn方法的典型用法代碼示例。如果您正苦於以下問題:Java GeneratorAdapter.visitInsn方法的具體用法?Java GeneratorAdapter.visitInsn怎麽用?Java GeneratorAdapter.visitInsn使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.objectweb.asm.commons.GeneratorAdapter的用法示例。


在下文中一共展示了GeneratorAdapter.visitInsn方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: loadMH

import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
@Contract("null, null, _, _ -> fail")
private static void loadMH(GeneratorAdapter adapter, Type reflectorClass, int flags, int mhIndex) {
    if((flags & Magic.REFLECTOR_METHOD_USE_METHODHANDLE) == 0) return;

    /* Load MethodHandle field */
    adapter.loadThis();
    adapter.getField(notNull(reflectorClass, "Reflector class shouldn't be null!"), MHF, MH_ARRAY);

    /* Load index */
    if(mhIndex >= 0 && mhIndex <= 5)
        /* ICONST_x offset is 3, iow ICONST_0 = 3, ICONST_1 = 4 */
        adapter.visitInsn(ICONST_0 + mhIndex);
    else
        adapter.visitIntInsn(BIPUSH, mhIndex);

    /* Load MethodHandle from array */
    adapter.visitInsn(AALOAD);
}
 
開發者ID:mikroskeem,項目名稱:Shuriken,代碼行數:19,代碼來源:MethodGenerator.java

示例2: prepareMethodParameters

import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
private static void prepareMethodParameters(GeneratorAdapter mv, String className, List<Type> args, Type returnType, boolean isStatic, int methodId) {
    //第一個參數:new Object[]{...};,如果方法沒有參數直接傳入new Object[0]
    if (args.size() == 0) {
        mv.visitInsn(Opcodes.ICONST_0);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    } else {
        createObjectArray(mv, args, isStatic);
    }

    //第二個參數:this,如果方法是static的話就直接傳入null
    if (isStatic) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
    }

    //第三個參數:changeQuickRedirect
    mv.visitFieldInsn(Opcodes.GETSTATIC,
            className,
            REDIRECTFIELD_NAME,
            REDIRECTCLASSNAME);

    //第四個參數:false,標誌是否為static
    mv.visitInsn(isStatic ? Opcodes.ICONST_1 : Opcodes.ICONST_0);
    //第五個參數:
    mv.push(methodId);
    //第六個參數:參數class數組
    createClassArray(mv, args);
    //第七個參數:返回值類型class
    createReturnClass(mv, returnType);
}
 
開發者ID:Meituan-Dianping,項目名稱:Robust,代碼行數:32,代碼來源:RobustAsmUtils.java

示例3: emit

import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
public void emit(C context, ObjExpr objx, GeneratorAdapter gen){
		//emitting a Fn means constructing an instance, feeding closed-overs from enclosing scope, if any
		//objx arg is enclosing objx, not this
//		getCompiledClass();
		if(isDeftype())
			{
			gen.visitInsn(Opcodes.ACONST_NULL);
			}
		else
			{
			gen.newInstance(objtype);
			gen.dup();
			if(supportsMeta())
				gen.visitInsn(Opcodes.ACONST_NULL);
			for(ISeq s = RT.seq(closesExprs); s != null; s = s.next())
				{
                LocalBindingExpr lbe = (LocalBindingExpr) s.first();
				LocalBinding lb = lbe.b;
				if(lb.getPrimitiveType() != null)
					objx.emitUnboxedLocal(gen, lb);
				else
					objx.emitLocal(gen, lb, lbe.shouldClear);
				}
			gen.invokeConstructor(objtype, new Method("<init>", Type.VOID_TYPE, ctorTypes()));
			}
		if(context == C.STATEMENT)
			gen.pop();
	}
 
開發者ID:mrange,項目名稱:fsharpadvent2016,代碼行數:29,代碼來源:Compiler.java

示例4: emitClearLocalsOld

import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
void emitClearLocalsOld(GeneratorAdapter gen){
		for(int i=0;i<argLocals.count();i++)
			{
			LocalBinding lb = (LocalBinding) argLocals.nth(i);
			if(!localsUsedInCatchFinally.contains(lb.idx) && lb.getPrimitiveType() == null)
				{
				gen.visitInsn(Opcodes.ACONST_NULL);
				gen.storeArg(lb.idx - 1);
				}

			}
//		for(int i = 1; i < numParams() + 1; i++)
//			{
//			if(!localsUsedInCatchFinally.contains(i))
//				{
//				gen.visitInsn(Opcodes.ACONST_NULL);
//				gen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ISTORE), i);
//				}
//			}
		for(int i = numParams() + 1; i < maxLocal + 1; i++)
			{
			if(!localsUsedInCatchFinally.contains(i))
				{
				LocalBinding b = (LocalBinding) RT.get(indexlocals, i);
				if(b == null || maybePrimitiveType(b.init) == null)
					{
					gen.visitInsn(Opcodes.ACONST_NULL);
					gen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ISTORE), i);
					}
				}
			}
	}
 
開發者ID:mrange,項目名稱:fsharpadvent2016,代碼行數:33,代碼來源:Compiler.java

示例5: emitShiftMask

import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
private void emitShiftMask(GeneratorAdapter gen){
    if (isShiftMasked())
        {
           gen.push(shift);
           gen.visitInsn(ISHR);
           gen.push(mask);
           gen.visitInsn(IAND);
        }
}
 
開發者ID:mrange,項目名稱:fsharpadvent2016,代碼行數:10,代碼來源:Compiler.java

示例6: 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);
}
 
開發者ID:Meituan-Dianping,項目名稱:Robust,代碼行數:55,代碼來源:RobustAsmUtils.java

示例7: 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();
}
 
開發者ID:mikroskeem,項目名稱:Shuriken,代碼行數:49,代碼來源:MethodGenerator.java

示例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());
}
 
開發者ID:dodola,項目名稱:AnoleFix,代碼行數:46,代碼來源:ConstructorArgsRedirection.java

示例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;
}
 
開發者ID:xixifeng,項目名稱:fastquery,代碼行數:50,代碼來源:AsmRepository.java

示例10: emitBody

import org.objectweb.asm.commons.GeneratorAdapter; //導入方法依賴的package包/類
static void emitBody(ObjExpr objx, GeneratorAdapter gen, Class retClass, Expr body) {
		MaybePrimitiveExpr be = (MaybePrimitiveExpr) body;
		if(Util.isPrimitive(retClass) && be.canEmitPrimitive())
			{
			Class bc = maybePrimitiveType(be);
			if(bc == retClass)
				be.emitUnboxed(C.RETURN, objx, gen);
			else if(retClass == long.class && bc == int.class)
				{
				be.emitUnboxed(C.RETURN, objx, gen);
				gen.visitInsn(I2L);
				}
			else if(retClass == double.class && bc == float.class)
				{
				be.emitUnboxed(C.RETURN, objx, gen);
				gen.visitInsn(F2D);
				}
			else if(retClass == int.class && bc == long.class)
				{
				be.emitUnboxed(C.RETURN, objx, gen);
				gen.invokeStatic(RT_TYPE, Method.getMethod("int intCast(long)"));
				}
			else if(retClass == float.class && bc == double.class)
				{
				be.emitUnboxed(C.RETURN, objx, gen);
				gen.visitInsn(D2F);
				}
			else
				throw new IllegalArgumentException("Mismatched primitive return, expected: "
				                                   + retClass + ", had: " + be.getJavaClass());
			}
		else
			{
			body.emit(C.RETURN, objx, gen);
			if(retClass == void.class)
				{
				gen.pop();
				}
			else
				gen.unbox(Type.getType(retClass));
			}
}
 
開發者ID:mrange,項目名稱:fsharpadvent2016,代碼行數:43,代碼來源:Compiler.java


注:本文中的org.objectweb.asm.commons.GeneratorAdapter.visitInsn方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。