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


Java CodeAttribute类代码示例

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


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

示例1: isEmpty

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
/**
 * Returns true if the constructor (or static initializer)
 * is the default one.  This method returns true if the constructor
 * takes some arguments but it does not perform anything except
 * calling <code>super()</code> (the no-argument constructor of
 * the super class).
 */
public boolean isEmpty() {
    CodeAttribute ca = getMethodInfo2().getCodeAttribute();
    if (ca == null)
        return false;       // native or abstract??
                            // they are not allowed, though.

    ConstPool cp = ca.getConstPool();
    CodeIterator it = ca.iterator();
    try {
        int pos, desc;
        int op0 = it.byteAt(it.next());
        return op0 == Opcode.RETURN     // empty static initializer
            || (op0 == Opcode.ALOAD_0
                && it.byteAt(pos = it.next()) == Opcode.INVOKESPECIAL
                && (desc = cp.isConstructor(getSuperclassName(),
                                            it.u16bitAt(pos + 1))) != 0
                && "()V".equals(cp.getUtf8Info(desc))
                && it.byteAt(it.next()) == Opcode.RETURN
                && !it.hasNext());
    }
    catch (BadBytecode e) {}
    return false;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:31,代码来源:CtConstructor.java

示例2: callsSuper

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
/**
 * Returns true if this constructor calls a constructor
 * of the super class.  This method returns false if it
 * calls another constructor of this class by <code>this()</code>. 
 */
public boolean callsSuper() throws CannotCompileException {
    CodeAttribute codeAttr = methodInfo.getCodeAttribute();
    if (codeAttr != null) {
        CodeIterator it = codeAttr.iterator();
        try {
            int index = it.skipSuperConstructor();
            return index >= 0;
        }
        catch (BadBytecode e) {
            throw new CannotCompileException(e);
        }
    }

    return false;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:21,代码来源:CtConstructor.java

示例3: scan

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
public Subroutine[] scan(MethodInfo method) throws BadBytecode {
    CodeAttribute code = method.getCodeAttribute();
    CodeIterator iter = code.iterator();

    subroutines = new Subroutine[code.getCodeLength()];
    subTable.clear();
    done.clear();

    scan(0, iter, null);

    ExceptionTable exceptions = code.getExceptionTable();
    for (int i = 0; i < exceptions.size(); i++) {
        int handler = exceptions.handlerPc(i);
        // If an exception is thrown in subroutine, the handler
        // is part of the same subroutine.
        scan(handler, iter, subroutines[exceptions.startPc(i)]);
    }

    return subroutines;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:21,代码来源:SubroutineScanner.java

示例4: makeBlocks

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
/**
 * Divides the method body into basic blocks.
 * The type information of the first block is initialized.
 *
 * @param optimize       if it is true and the method does not include
 *                      branches, this method returns null.
 */
public static TypedBlock[] makeBlocks(MethodInfo minfo, CodeAttribute ca,
                                      boolean optimize)
    throws BadBytecode
{
    TypedBlock[] blocks = (TypedBlock[])new Maker().make(minfo);
    if (optimize && blocks.length < 2)
        if (blocks.length == 0 || blocks[0].incoming == 0)
            return null;

    ConstPool pool = minfo.getConstPool();
    boolean isStatic = (minfo.getAccessFlags() & AccessFlag.STATIC) != 0;
    blocks[0].initFirstBlock(ca.getMaxStack(), ca.getMaxLocals(),
                             pool.getClassName(), minfo.getDescriptor(),
                             isStatic, minfo.isConstructor());
    return blocks;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:24,代码来源:TypedBlock.java

示例5: addLocalVariable

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
/**
 * Declares a new local variable.  The scope of this variable is the
 * whole method body.  The initial value of that variable is not set.
 * The declared variable can be accessed in the code snippet inserted
 * by <code>insertBefore()</code>, <code>insertAfter()</code>, etc.
 *
 * <p>If the second parameter <code>asFinally</code> to
 * <code>insertAfter()</code> is true, the declared local variable
 * is not visible from the code inserted by <code>insertAfter()</code>.
 *
 * @param name      the name of the variable
 * @param type      the type of the variable
 * @see #insertBefore(String)
 * @see #insertAfter(String)
 */
public void addLocalVariable(String name, CtClass type)
    throws CannotCompileException
{
    declaringClass.checkModify();
    ConstPool cp = methodInfo.getConstPool();
    CodeAttribute ca = methodInfo.getCodeAttribute();
    if (ca == null)
        throw new CannotCompileException("no method body");

    LocalVariableAttribute va = (LocalVariableAttribute)ca.getAttribute(
                                            LocalVariableAttribute.tag);
    if (va == null) {
        va = new LocalVariableAttribute(cp);
        ca.getAttributes().add(va);
    }

    int maxLocals = ca.getMaxLocals();
    String desc = Descriptor.of(type);
    va.addEntry(0, ca.getCodeLength(),
                cp.addUtf8Info(name), cp.addUtf8Info(desc), maxLocals);
    ca.setMaxLocals(maxLocals + Descriptor.dataSize(desc));
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:38,代码来源:CtBehavior.java

示例6: insertAuxInitializer

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
private static void insertAuxInitializer(CodeAttribute codeAttr,
                                         Bytecode initializer,
                                         int stacksize)
    throws BadBytecode
{
    CodeIterator it = codeAttr.iterator();
    int index = it.skipSuperConstructor();
    if (index < 0) {
        index = it.skipThisConstructor();
        if (index >= 0)
            return;         // this() is called.

        // Neither this() or super() is called.
    }

    int pos = it.insertEx(initializer.get());
    it.insert(initializer.getExceptionTable(), pos);
    int maxstack = codeAttr.getMaxStack();
    if (maxstack < stacksize)
        codeAttr.setMaxStack(stacksize);
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:22,代码来源:CtClassType.java

示例7: runEditor

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
protected void runEditor(ExprEditor ed, CodeIterator oldIterator)
    throws CannotCompileException
{
    CodeAttribute codeAttr = oldIterator.get();
    int orgLocals = codeAttr.getMaxLocals();
    int orgStack = codeAttr.getMaxStack();
    int newLocals = locals();
    codeAttr.setMaxStack(stack());
    codeAttr.setMaxLocals(newLocals);
    ExprEditor.LoopContext context
        = new ExprEditor.LoopContext(newLocals);
    int size = oldIterator.getCodeLength();
    int endPos = oldIterator.lookAhead();
    oldIterator.move(currentPos);
    if (ed.doit(thisClass, thisMethod, context, oldIterator, endPos))
        edited = true;

    oldIterator.move(endPos + oldIterator.getCodeLength() - size);
    codeAttr.setMaxLocals(orgLocals);
    codeAttr.setMaxStack(orgStack);
    maxLocals = context.maxLocals;
    maxStack += context.maxStack;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:24,代码来源:Expr.java

示例8: recordLocalVariables

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
/**
 * Records local variables available at the specified program counter.
 * If the LocalVariableAttribute is not available, this method does not
 * record any local variable.  It only returns false.
 *
 * @param pc    program counter (&gt;= 0)
 * @return false if the CodeAttribute does not include a
 *              LocalVariableAttribute.
 */
public boolean recordLocalVariables(CodeAttribute ca, int pc)
    throws CompileError
{
    LocalVariableAttribute va
        = (LocalVariableAttribute)
          ca.getAttribute(LocalVariableAttribute.tag);
    if (va == null)
        return false;

    int n = va.tableLength();
    for (int i = 0; i < n; ++i) {
        int start = va.startPc(i);
        int len = va.codeLength(i);
        if (start <= pc && pc < start + len)
            gen.recordVariable(va.descriptor(i), va.variableName(i),
                               va.index(i), stable);
    }

    return true;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:30,代码来源:Javac.java

示例9: recordParamNames

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
/**
 * Records parameter names if the LocalVariableAttribute is available.
 * It returns false unless the LocalVariableAttribute is available.
 *
 * @param numOfLocalVars    the number of local variables used
 *                          for storing the parameters.
 * @return false if the CodeAttribute does not include a
 *              LocalVariableAttribute.
 */
public boolean recordParamNames(CodeAttribute ca, int numOfLocalVars)
    throws CompileError
{
    LocalVariableAttribute va
        = (LocalVariableAttribute)
          ca.getAttribute(LocalVariableAttribute.tag);
    if (va == null)
        return false;

    int n = va.tableLength();
    for (int i = 0; i < n; ++i) {
        int index = va.index(i);
        if (index < numOfLocalVars)
            gen.recordVariable(va.descriptor(i), va.variableName(i),
                               index, stable);
    }

    return true;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:29,代码来源:Javac.java

示例10: isEmpty

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
/**
 * Returns true if the method body is empty, that is, <code>{}</code>.
 * It also returns true if the method is an abstract method.
 */
public boolean isEmpty() {
    CodeAttribute ca = getMethodInfo2().getCodeAttribute();
    if (ca == null)         // abstract or native
        return (getModifiers() & Modifier.ABSTRACT) != 0;

    CodeIterator it = ca.iterator();
    try {
        return it.hasNext() && it.byteAt(it.next()) == Opcode.RETURN
            && !it.hasNext();
    }
    catch (BadBytecode e) {}
    return false;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:18,代码来源:CtMethod.java

示例11: setWrappedBody

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的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

示例12: getStartPosOfBody

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
int getStartPosOfBody(CodeAttribute ca) throws CannotCompileException {
    CodeIterator ci = ca.iterator();
    try {
        ci.skipConstructor();
        return ci.next();
    }
    catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:11,代码来源:CtConstructor.java

示例13: removeConsCall

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
private static void removeConsCall(CodeAttribute ca)
    throws CannotCompileException
{
    CodeIterator iterator = ca.iterator();
    try {
        int pos = iterator.skipConstructor();
        if (pos >= 0) {
            int mref = iterator.u16bitAt(pos + 1);
            String desc = ca.getConstPool().getMethodrefType(mref);
            int num = Descriptor.numOfParameters(desc) + 1;
            if (num > 3)
                pos = iterator.insertGapAt(pos, num - 3, false).position;

            iterator.writeByte(Opcode.POP, pos++);  // this
            iterator.writeByte(Opcode.NOP, pos);
            iterator.writeByte(Opcode.NOP, pos + 1);
            Descriptor.Iterator it = new Descriptor.Iterator(desc);
            while (true) {
                it.next();
                if (it.isParameter())
                    iterator.writeByte(it.is2byte() ? Opcode.POP2 : Opcode.POP,
                                       pos++);
                else
                    break;
            }
        }
    }
    catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:32,代码来源:CtConstructor.java

示例14: analyze

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
/**
 * Performs data-flow analysis on a method and returns an array, indexed by
 * instruction position, containing the starting frame state of all reachable
 * instructions. Non-reachable code, and illegal code offsets are represented
 * as a null in the frame state array. This can be used to detect dead code.
 *
 * If the method does not contain code (it is either native or abstract), null
 * is returned.
 *
 * @param clazz the declaring class of the method
 * @param method the method to analyze
 * @return an array, indexed by instruction position, of the starting frame state,
 *         or null if this method doesn't have code
 * @throws BadBytecode if the bytecode does not comply with the JVM specification
 */
public Frame[] analyze(CtClass clazz, MethodInfo method) throws BadBytecode {
    this.clazz = clazz;
    CodeAttribute codeAttribute = method.getCodeAttribute();
    // Native or Abstract
    if (codeAttribute == null)
        return null;

    int maxLocals = codeAttribute.getMaxLocals();
    int maxStack = codeAttribute.getMaxStack();
    int codeLength = codeAttribute.getCodeLength();

    CodeIterator iter = codeAttribute.iterator();
    IntQueue queue = new IntQueue();

    exceptions = buildExceptionInfo(method);
    subroutines = scanner.scan(method);

    Executor executor = new Executor(clazz.getClassPool(), method.getConstPool());
    frames = new Frame[codeLength];
    frames[iter.lookAhead()] = firstFrame(method, maxLocals, maxStack);
    queue.add(iter.next());
    while (!queue.isEmpty()) {
        analyzeNextEntry(method, iter, queue, executor);
    }

    return frames;
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:43,代码来源:Analyzer.java

示例15: make

import scouter.javassist.bytecode.CodeAttribute; //导入依赖的package包/类
/**
 * Computes the stack map table of the given method and returns it.
 * It returns null if the given method does not have to have a
 * stack map table or it includes JSR.
 */
public static StackMapTable make(ClassPool classes, MethodInfo minfo)
    throws BadBytecode
{
    CodeAttribute ca = minfo.getCodeAttribute();
    if (ca == null)
        return null;

    TypedBlock[] blocks;
    try {
        blocks = TypedBlock.makeBlocks(minfo, ca, true);
    }
    catch (BasicBlock.JsrBytecode e) {
        return null;
    }

    if (blocks == null)
        return null;

    MapMaker mm = new MapMaker(classes, minfo, ca);
    try {
        mm.make(blocks, ca.getCode());
    }
    catch (BadBytecode bb) {
        throw new BadBytecode(minfo, bb);
    }

    return mm.toStackMap(blocks);
}
 
开发者ID:scouter-project,项目名称:scouter,代码行数:34,代码来源:MapMaker.java


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