public void initialize(ConstPool cp, CtClass clazz, MethodInfo minfo) throws CannotCompileException { initialize(cp, minfo.getCodeAttribute()); }
public void initialize(ConstPool cp, CtClass clazz, MethodInfo minfo) throws CannotCompileException { initialize(cp, minfo.getCodeAttribute()); }
/** * Rebuilds a stack map table for J2ME (CLDC). If no stack map table is included, * a new one is created. If this <code>MethodInfo</code> does not * include a code attribute, nothing happens. * * @param pool used for making type hierarchy. * @see StackMap * @since 3.12 */ public void rebuildStackMapForME(ClassPool pool) throws BadBytecode { CodeAttribute ca = getCodeAttribute(); if (ca != null) { StackMap sm = MapMaker.make2(pool, this); ca.setAttribute(sm); } }
/** * Rebuilds a stack map table. If no stack map table is included, * a new one is created. If this <code>MethodInfo</code> does not * include a code attribute, nothing happens. * * @param pool used for making type hierarchy. * @see StackMapTable * @since 3.6 */ public void rebuildStackMap(ClassPool pool) throws BadBytecode { CodeAttribute ca = getCodeAttribute(); if (ca != null) { StackMapTable smt = MapMaker.make(pool, this); ca.setAttribute(smt); } }
/** * Returns the line number of the source line corresponding to the specified * bytecode contained in this method. * * @param pos * the position of the bytecode (>= 0). an index into the code * array. * @return -1 if this information is not available. */ public int getLineNumber(int pos) { CodeAttribute ca = getCodeAttribute(); if (ca == null) return -1; LineNumberAttribute ainfo = (LineNumberAttribute)ca .getAttribute(LineNumberAttribute.tag); if (ainfo == null) return -1; return ainfo.toLineNumber(pos); }
/** * Rebuilds a stack map table. If no stack map table is included, * a new one is created. If this <code>MethodInfo</code> does not * include a code attribute, nothing happens. * * @param pool used for making type hierarchy. * @see StackMapTable * @since 3.6 */ public void rebuildStackMap(ClassPool pool) throws BadBytecode { CodeAttribute ca = getCodeAttribute(); if (ca != null) { StackMapTable smt = MapMaker.make(pool, this); ca.setAttribute(smt); } }
/** * 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; }
public BasicBlock[] make(MethodInfo minfo) throws BadBytecode { CodeAttribute ca = minfo.getCodeAttribute(); if (ca == null) return null; CodeIterator ci = ca.iterator(); return make(ci, 0, ci.getCodeLength(), ca.getExceptionTable()); }
private void read(MethodInfo src, String methodname, Map classnames) throws BadBytecode { ConstPool destCp = constPool; accessFlags = src.accessFlags; name = destCp.addUtf8Info(methodname); cachedName = methodname; ConstPool srcCp = src.constPool; String desc = srcCp.getUtf8Info(src.descriptor); String desc2 = Descriptor.rename(desc, classnames); descriptor = destCp.addUtf8Info(desc2); attribute = new ArrayList(); ExceptionsAttribute eattr = src.getExceptionsAttribute(); if (eattr != null) attribute.add(eattr.copy(destCp, classnames)); CodeAttribute cattr = src.getCodeAttribute(); if (cattr != null) attribute.add(cattr.copy(destCp, classnames)); }
/** * Prints the bytecode instructions of a given method. */ public void print(CtMethod method) { MethodInfo info = method.getMethodInfo2(); ConstPool pool = info.getConstPool(); CodeAttribute code = info.getCodeAttribute(); if (code == null) return; CodeIterator iterator = code.iterator(); while (iterator.hasNext()) { int pos; try { pos = iterator.next(); } catch (BadBytecode e) { throw new RuntimeException(e); } stream.println(pos + ": " + instructionString(iterator, pos, pool)); } }
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; }
public BasicBlock[] make(MethodInfo minfo) throws BadBytecode { CodeAttribute ca = minfo.getCodeAttribute(); if (ca == null) return null; CodeIterator ci = ca.iterator(); return make(ci, 0, ci.getCodeLength(), ca.getExceptionTable()); }
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(); }
/** * 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; }
private ExceptionInfo[] buildExceptionInfo(MethodInfo method) { ConstPool constPool = method.getConstPool(); ClassPool classes = clazz.getClassPool(); ExceptionTable table = method.getCodeAttribute().getExceptionTable(); ExceptionInfo[] exceptions = new ExceptionInfo[table.size()]; for (int i = 0; i < table.size(); i++) { int index = table.catchType(i); Type type; try { type = index == 0 ? Type.THROWABLE : Type.get(classes.get(constPool.getClassInfo(index))); } catch (NotFoundException e) { throw new IllegalStateException(e.getMessage()); } exceptions[i] = new ExceptionInfo(table.startPc(i), table.endPc(i), table.handlerPc(i), type); } return exceptions; }
private void modifyConstructors(ClassFile cf) throws CannotCompileException, NotFoundException { if (fieldInitializers == null) return; ConstPool cp = cf.getConstPool(); List list = cf.getMethods(); int n = list.size(); for (int i = 0; i < n; ++i) { MethodInfo minfo = (MethodInfo)list.get(i); if (minfo.isConstructor()) { CodeAttribute codeAttr = minfo.getCodeAttribute(); if (codeAttr != null) try { Bytecode init = new Bytecode(cp, 0, codeAttr.getMaxLocals()); CtClass[] params = Descriptor.getParameterTypes( minfo.getDescriptor(), classPool); int stacksize = makeFieldInitializer(init, params); insertAuxInitializer(codeAttr, init, stacksize); minfo.rebuildStackMapIf6(classPool, cf); } catch (BadBytecode e) { throw new CannotCompileException(e); } } } }
/** * 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); }
/** * Computes the stack map table for J2ME. * It returns null if the given method does not have to have a * stack map table or it includes JSR. */ public static StackMap make2(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.toStackMap2(minfo.getConstPool(), blocks); }
/** * 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; }
CodeIterator iterator = minfo.getCodeAttribute().iterator(); while (iterator.hasNext()) { try {