当前位置: 首页>>代码示例>>Java>>正文


Java Bytecode.toCodeAttribute方法代码示例

本文整理汇总了Java中scouter.javassist.bytecode.Bytecode.toCodeAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java Bytecode.toCodeAttribute方法的具体用法?Java Bytecode.toCodeAttribute怎么用?Java Bytecode.toCodeAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scouter.javassist.bytecode.Bytecode的用法示例。


在下文中一共展示了Bytecode.toCodeAttribute方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setWrappedBody

import scouter.javassist.bytecode.Bytecode; //导入方法依赖的package包/类
/**
 * Replace a method body with a new method body wrapping the
 * given method.
 *
 * @param mbody             the wrapped method
 * @param constParam        the constant parameter given to
 *                          the wrapped method
 *                          (maybe <code>null</code>).
 *
 * @see CtNewMethod#wrapped(CtClass,String,CtClass[],CtClass[],CtMethod,CtMethod.ConstParameter,CtClass)
 */
public void setWrappedBody(CtMethod mbody, ConstParameter constParam)
    throws CannotCompileException
{
    declaringClass.checkModify();

    CtClass clazz = getDeclaringClass();
    CtClass[] params;
    CtClass retType;
    try {
        params = getParameterTypes();
        retType = getReturnType();
    }
    catch (NotFoundException e) {
        throw new CannotCompileException(e);
    }

    Bytecode code = CtNewWrappedMethod.makeBody(clazz,
                                                clazz.getClassFile2(),
                                                mbody,
                                                params, retType,
                                                constParam);
    CodeAttribute cattr = code.toCodeAttribute();
    methodInfo.setCodeAttribute(cattr);
    methodInfo.setAccessFlags(methodInfo.getAccessFlags()
                              & ~AccessFlag.ABSTRACT);
    // rebuilding a stack map table is not needed.
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:39,代码来源:CtMethod.java

示例2: makeConstructor

import scouter.javassist.bytecode.Bytecode; //导入方法依赖的package包/类
private static MethodInfo makeConstructor(String thisClassName, Constructor cons,
                                          ConstPool cp, Class superClass, boolean doHandlerInit) {
    String desc = RuntimeSupport.makeDescriptor(cons.getParameterTypes(),
                                                Void.TYPE);
    MethodInfo minfo = new MethodInfo(cp, "<init>", desc);
    minfo.setAccessFlags(Modifier.PUBLIC);      // cons.getModifiers() & ~Modifier.NATIVE
    setThrows(minfo, cp, cons.getExceptionTypes());
    Bytecode code = new Bytecode(cp, 0, 0);

    // legacy: if we are not using caching then we initialise the instance's handler
    // from the class's static default interceptor and skip the next few instructions if
    // it is non-null
    if (doHandlerInit) {
        code.addAload(0);
        code.addGetstatic(thisClassName, DEFAULT_INTERCEPTOR, HANDLER_TYPE);
        code.addPutfield(thisClassName, HANDLER, HANDLER_TYPE);
        code.addGetstatic(thisClassName, DEFAULT_INTERCEPTOR, HANDLER_TYPE);
        code.addOpcode(Opcode.IFNONNULL);
        code.addIndex(10);
    }
    // if caching is enabled then we don't have a handler to initialise so this else branch will install
    // the handler located in the static field of class RuntimeSupport.
    code.addAload(0);
    code.addGetstatic(NULL_INTERCEPTOR_HOLDER, DEFAULT_INTERCEPTOR, HANDLER_TYPE);
    code.addPutfield(thisClassName, HANDLER, HANDLER_TYPE);
    int pc = code.currentPc();

    code.addAload(0);
    int s = addLoadParameters(code, cons.getParameterTypes(), 1);
    code.addInvokespecial(superClass.getName(), "<init>", desc);
    code.addOpcode(Opcode.RETURN);
    code.setMaxLocals(s + 1);
    CodeAttribute ca = code.toCodeAttribute();
    minfo.setCodeAttribute(ca);

    StackMapTable.Writer writer = new StackMapTable.Writer(32);
    writer.sameFrame(pc);
    ca.setAttribute(writer.toStackMapTable(cp));
    return minfo;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:41,代码来源:ProxyFactory.java

示例3: makeForwarder

import scouter.javassist.bytecode.Bytecode; //导入方法依赖的package包/类
/**
 * @param delegatorName     null if the original method is abstract.
 */
private static MethodInfo makeForwarder(String thisClassName,
                Method meth, String desc, ConstPool cp,
                Class declClass, String delegatorName, int index,
                ArrayList forwarders) {
    MethodInfo forwarder = new MethodInfo(cp, meth.getName(), desc);
    forwarder.setAccessFlags(Modifier.FINAL
                | (meth.getModifiers() & ~(Modifier.ABSTRACT
                                           | Modifier.NATIVE
                                           | Modifier.SYNCHRONIZED)));
    setThrows(forwarder, cp, meth);
    int args = Descriptor.paramSize(desc);
    Bytecode code = new Bytecode(cp, 0, args + 2);
    /*
     * static {
     *   methods[index * 2]
     *     = RuntimeSupport.findSuperMethod(this, <overridden name>, <desc>);
     *   methods[index * 2 + 1]
     *     = RuntimeSupport.findMethod(this, <delegator name>, <desc>);
     *     or = null // the original method is abstract.
     * }
     *     :
     * return ($r)handler.invoke(this, methods[index * 2],
     *                methods[index * 2 + 1], $args);
     */
    int origIndex = index * 2;
    int delIndex = index * 2 + 1;
    int arrayVar = args + 1;
    code.addGetstatic(thisClassName, HOLDER, HOLDER_TYPE);
    code.addAstore(arrayVar);

    forwarders.add(new Find2MethodsArgs(meth.getName(), delegatorName, desc, origIndex));

    code.addAload(0);
    code.addGetfield(thisClassName, HANDLER, HANDLER_TYPE);
    code.addAload(0);

    code.addAload(arrayVar);
    code.addIconst(origIndex);
    code.addOpcode(Opcode.AALOAD);

    code.addAload(arrayVar);
    code.addIconst(delIndex);
    code.addOpcode(Opcode.AALOAD);

    makeParameterList(code, meth.getParameterTypes());
    code.addInvokeinterface(MethodHandler.class.getName(), "invoke",
        "(Ljava/lang/Object;Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;",
        5);
    Class retType = meth.getReturnType();
    addUnwrapper(code, retType);
    addReturn(code, retType);

    CodeAttribute ca = code.toCodeAttribute();
    forwarder.setCodeAttribute(ca);
    return forwarder;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:60,代码来源:ProxyFactory.java


注:本文中的scouter.javassist.bytecode.Bytecode.toCodeAttribute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。