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


Java MethodInfo.setCodeAttribute方法代碼示例

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


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

示例1: wrapped

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
public static CtMethod wrapped(CtClass returnType, String mname,
                               CtClass[] parameterTypes,
                               CtClass[] exceptionTypes,
                               CtMethod body, ConstParameter constParam,
                               CtClass declaring)
    throws CannotCompileException
{
    CtMethod mt = new CtMethod(returnType, mname, parameterTypes,
                               declaring);
    mt.setModifiers(body.getModifiers());
    try {
        mt.setExceptionTypes(exceptionTypes);
    }
    catch (NotFoundException e) {
        throw new CannotCompileException(e);
    }

    Bytecode code = makeBody(declaring, declaring.getClassFile2(), body,
                             parameterTypes, returnType, constParam);
    MethodInfo minfo = mt.getMethodInfo2();
    minfo.setCodeAttribute(code.toCodeAttribute());
    // a stack map has been already created. 
    return mt;
}
 
開發者ID:scouter-project,項目名稱:scouter,代碼行數:25,代碼來源:CtNewWrappedMethod.java

示例2: makeDelegator

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
private MethodInfo makeDelegator(Method meth, String desc,
            ConstPool cp, Class declClass, String delegatorName) {
    MethodInfo delegator = new MethodInfo(cp, delegatorName, desc);
    delegator.setAccessFlags(Modifier.FINAL | Modifier.PUBLIC
            | (meth.getModifiers() & ~(Modifier.PRIVATE
                                       | Modifier.PROTECTED
                                       | Modifier.ABSTRACT
                                       | Modifier.NATIVE
                                       | Modifier.SYNCHRONIZED)));
    setThrows(delegator, cp, meth);
    Bytecode code = new Bytecode(cp, 0, 0);
    code.addAload(0);
    int s = addLoadParameters(code, meth.getParameterTypes(), 1);
    Class targetClass = invokespecialTarget(declClass);
    code.addInvokespecial(targetClass.isInterface(), cp.addClassInfo(targetClass.getName()),
                          meth.getName(), desc);
    addReturn(code, meth.getReturnType());
    code.setMaxLocals(++s);
    delegator.setCodeAttribute(code.toCodeAttribute());
    return delegator;
}
 
開發者ID:scouter-project,項目名稱:scouter,代碼行數:22,代碼來源:ProxyFactory.java

示例3: makeWriteReplace

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
private static MethodInfo makeWriteReplace(ConstPool cp) {
    MethodInfo minfo = new MethodInfo(cp, "writeReplace", "()Ljava/lang/Object;");
    String[] list = new String[1];
    list[0] = "java.io.ObjectStreamException";
    ExceptionsAttribute ea = new ExceptionsAttribute(cp);
    ea.setExceptions(list);
    minfo.setExceptionsAttribute(ea);
    Bytecode code = new Bytecode(cp, 0, 1);
    code.addAload(0);
    code.addInvokestatic("RuntimeSupport",
                         "makeSerializedProxy",
                         "(Ljava/lang/Object;)Ljavassist/util/proxy/SerializedProxy;");
    code.addOpcode(Opcode.ARETURN);
    minfo.setCodeAttribute(code.toCodeAttribute());
    return minfo;
}
 
開發者ID:scouter-project,項目名稱:scouter,代碼行數:17,代碼來源:ProxyFactory.java

示例4: getter

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
/**
 * Creates a public getter method.  The getter method returns the value
 * of the specified field in the class to which this method is added.
 * The created method is initially not static even if the field is
 * static.  Change the modifiers if the method should be static.
 *
 * @param methodName        the name of the getter
 * @param field             the field accessed.
 */
public static CtMethod getter(String methodName, CtField field)
    throws CannotCompileException
{
    FieldInfo finfo = field.getFieldInfo2();
    String fieldType = finfo.getDescriptor();
    String desc = "()" + fieldType;
    ConstPool cp = finfo.getConstPool();
    MethodInfo minfo = new MethodInfo(cp, methodName, desc);
    minfo.setAccessFlags(AccessFlag.PUBLIC);

    Bytecode code = new Bytecode(cp, 2, 1);
    try {
        String fieldName = finfo.getName();
        if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) {
            code.addAload(0);
            code.addGetfield(Bytecode.THIS, fieldName, fieldType);
        }
        else
            code.addGetstatic(Bytecode.THIS, fieldName, fieldType);

        code.addReturn(field.getType());
    }
    catch (NotFoundException e) {
        throw new CannotCompileException(e);
    }

    minfo.setCodeAttribute(code.toCodeAttribute());
    CtClass cc = field.getDeclaringClass();
    // a stack map is not needed.
    return new CtMethod(minfo, cc);
}
 
開發者ID:scouter-project,項目名稱:scouter,代碼行數:41,代碼來源:CtNewMethod.java

示例5: setter

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
/**
 * Creates a public setter method.  The setter method assigns the
 * value of the first parameter to the specified field
 * in the class to which this method is added.
 * The created method is not static even if the field is
 * static.  You may not change it to be static
 * by <code>setModifiers()</code> in <code>CtBehavior</code>.
 *
 * @param methodName        the name of the setter
 * @param field             the field accessed.
 */
public static CtMethod setter(String methodName, CtField field)
    throws CannotCompileException
{
    FieldInfo finfo = field.getFieldInfo2();
    String fieldType = finfo.getDescriptor();
    String desc = "(" + fieldType + ")V";
    ConstPool cp = finfo.getConstPool();
    MethodInfo minfo = new MethodInfo(cp, methodName, desc);
    minfo.setAccessFlags(AccessFlag.PUBLIC);

    Bytecode code = new Bytecode(cp, 3, 3);
    try {
        String fieldName = finfo.getName();
        if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) {
            code.addAload(0);
            code.addLoad(1, field.getType());
            code.addPutfield(Bytecode.THIS, fieldName, fieldType);
        }
        else {
            code.addLoad(1, field.getType());
            code.addPutstatic(Bytecode.THIS, fieldName, fieldType);
        }

        code.addReturn(null);
    }
    catch (NotFoundException e) {
        throw new CannotCompileException(e);
    }

    minfo.setCodeAttribute(code.toCodeAttribute());
    CtClass cc = field.getDeclaringClass();
    // a stack map is not needed.
    return new CtMethod(minfo, cc);
}
 
開發者ID:scouter-project,項目名稱:scouter,代碼行數:46,代碼來源:CtNewMethod.java

示例6: delegator0

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
private static CtMethod delegator0(CtMethod delegate, CtClass declaring)
    throws CannotCompileException, NotFoundException
{
    MethodInfo deleInfo = delegate.getMethodInfo2();
    String methodName = deleInfo.getName();
    String desc = deleInfo.getDescriptor();
    ConstPool cp = declaring.getClassFile2().getConstPool();
    MethodInfo minfo = new MethodInfo(cp, methodName, desc);
    minfo.setAccessFlags(deleInfo.getAccessFlags());

    ExceptionsAttribute eattr = deleInfo.getExceptionsAttribute();
    if (eattr != null)
        minfo.setExceptionsAttribute(
                            (ExceptionsAttribute)eattr.copy(cp, null));

    Bytecode code = new Bytecode(cp, 0, 0);
    boolean isStatic = Modifier.isStatic(delegate.getModifiers());
    CtClass deleClass = delegate.getDeclaringClass();
    CtClass[] params = delegate.getParameterTypes();
    int s;
    if (isStatic) {
        s = code.addLoadParameters(params, 0);
        code.addInvokestatic(deleClass, methodName, desc);
    }
    else {
        code.addLoad(0, deleClass);
        s = code.addLoadParameters(params, 1);
        code.addInvokespecial(deleClass, methodName, desc);
    }

    code.addReturn(delegate.getReturnType());
    code.setMaxLocals(++s);
    code.setMaxStack(s < 2 ? 2 : s); // for a 2-word return value
    minfo.setCodeAttribute(code.toCodeAttribute());
    // a stack map is not needed. 
    return new CtMethod(minfo, declaring);
}
 
開發者ID:scouter-project,項目名稱:scouter,代碼行數:38,代碼來源:CtNewMethod.java

示例7: setBody0

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
static void setBody0(CtClass srcClass, MethodInfo srcInfo,
                     CtClass destClass, MethodInfo destInfo,
                     ClassMap map)
    throws CannotCompileException
{
    destClass.checkModify();

    map = new ClassMap(map);
    map.put(srcClass.getName(), destClass.getName());
    try {
        CodeAttribute cattr = srcInfo.getCodeAttribute();
        if (cattr != null) {
            ConstPool cp = destInfo.getConstPool();
            CodeAttribute ca = (CodeAttribute)cattr.copy(cp, map);
            destInfo.setCodeAttribute(ca);
            // a stack map table is copied to destInfo.
        }
    }
    catch (CodeAttribute.RuntimeCopyException e) {
        /* the exception may be thrown by copy() in CodeAttribute.
         */
        throw new CannotCompileException(e);
    }

    destInfo.setAccessFlags(destInfo.getAccessFlags()
                            & ~AccessFlag.ABSTRACT);
    destClass.rebuildClassFile();
}
 
開發者ID:scouter-project,項目名稱:scouter,代碼行數:29,代碼來源:CtBehavior.java

示例8: addSetter

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
private static void addSetter(String classname, ClassFile cf, ConstPool cp)
    throws CannotCompileException
{
    MethodInfo minfo = new MethodInfo(cp, HANDLER_SETTER,
                                      HANDLER_SETTER_TYPE);
    minfo.setAccessFlags(AccessFlag.PUBLIC);
    Bytecode code = new Bytecode(cp, 2, 2);
    code.addAload(0);
    code.addAload(1);
    code.addPutfield(classname, HANDLER, HANDLER_TYPE);
    code.addOpcode(Bytecode.RETURN);
    minfo.setCodeAttribute(code.toCodeAttribute());
    cf.addMethod(minfo);
}
 
開發者ID:scouter-project,項目名稱:scouter,代碼行數:15,代碼來源:ProxyFactory.java

示例9: addGetter

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
private static void addGetter(String classname, ClassFile cf, ConstPool cp)
    throws CannotCompileException
{
    MethodInfo minfo = new MethodInfo(cp, HANDLER_GETTER,
                                      HANDLER_GETTER_TYPE);
    minfo.setAccessFlags(AccessFlag.PUBLIC);
    Bytecode code = new Bytecode(cp, 1, 1);
    code.addAload(0);
    code.addGetfield(classname, HANDLER, HANDLER_TYPE);
    code.addOpcode(Bytecode.ARETURN);
    minfo.setCodeAttribute(code.toCodeAttribute());
    cf.addMethod(minfo);
}
 
開發者ID:scouter-project,項目名稱:scouter,代碼行數:14,代碼來源:ProxyFactory.java

示例10: makeConstructor

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的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

示例11: addClassInitializer

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
private static void addClassInitializer(ClassFile cf, ConstPool cp,
                                String classname, int size, ArrayList forwarders)
    throws CannotCompileException
{
    FieldInfo finfo = new FieldInfo(cp, HOLDER, HOLDER_TYPE);
    finfo.setAccessFlags(AccessFlag.PRIVATE | AccessFlag.STATIC);
    cf.addField(finfo);
    MethodInfo minfo = new MethodInfo(cp, "<clinit>", "()V");
    minfo.setAccessFlags(AccessFlag.STATIC);
    setThrows(minfo, cp, new Class[] { ClassNotFoundException.class });

    Bytecode code = new Bytecode(cp, 0, 2);
    code.addIconst(size * 2);
    code.addAnewarray("java.lang.reflect.Method");
    final int varArray = 0;
    code.addAstore(varArray);

    // forName() must be called here.  Otherwise, the class might be
    // invisible.
    code.addLdc(classname);
    code.addInvokestatic("java.lang.Class",
            "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
    final int varClass = 1;
    code.addAstore(varClass);

    Iterator it = forwarders.iterator();
    while (it.hasNext()) {
        Find2MethodsArgs args = (Find2MethodsArgs)it.next();
        callFind2Methods(code, args.methodName, args.delegatorName,
                         args.origIndex, args.descriptor, varClass, varArray);
    }

    code.addAload(varArray);
    code.addPutstatic(classname, HOLDER, HOLDER_TYPE);

    code.addLconst(SERIAL_VERSION_UID_VALUE);
    code.addPutstatic(classname, SERIAL_VERSION_UID_FIELD, SERIAL_VERSION_UID_TYPE);
    code.addOpcode(Bytecode.RETURN);
    minfo.setCodeAttribute(code.toCodeAttribute());
    cf.addMethod(minfo);
}
 
開發者ID:scouter-project,項目名稱:scouter,代碼行數:42,代碼來源:ProxyFactory.java

示例12: makeForwarder

import scouter.javassist.bytecode.MethodInfo; //導入方法依賴的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.MethodInfo.setCodeAttribute方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。