/** * Records the currently compiled method. */ public void setThisMethod(CtMethod m) { thisMethod = m.getMethodInfo2(); if (typeChecker != null) typeChecker.setThisMethod(thisMethod); }
/** * Constructs a control-flow analyzer for the given method. */ public ControlFlow(CtMethod method) throws BadBytecode { this(method.getDeclaringClass(), method.getMethodInfo2()); }
/** * Records the currently compiled method. */ public void setThisMethod(CtMethod m) { thisMethod = m.getMethodInfo2(); if (typeChecker != null) typeChecker.setThisMethod(thisMethod); }
/** * Constructs a control-flow analyzer for the given method. */ public ControlFlow(CtMethod method) throws BadBytecode { this(method.getDeclaringClass(), method.getMethodInfo2()); }
public int compare(Object o1, Object o2) { CtMethod m1 = (CtMethod)o1; CtMethod m2 = (CtMethod)o2; int value = m1.getName().compareTo(m2.getName()); if (value == 0) value = m1.getMethodInfo2().getDescriptor() .compareTo(m2.getMethodInfo2().getDescriptor()); return value; } });
public TransformBefore(Transformer next, CtMethod origMethod, CtMethod beforeMethod) throws NotFoundException { super(next, origMethod, beforeMethod); // override methodDescriptor = origMethod.getMethodInfo2().getDescriptor(); parameterTypes = origMethod.getParameterTypes(); locals = 0; maxLocals = 0; saveCode = loadCode = null; }
private static void checkSignature(CtMethod wrappedBody, String descriptor) throws CannotCompileException { if (!descriptor.equals(wrappedBody.getMethodInfo2().getDescriptor())) throw new CannotCompileException( "wrapped method with a bad signature: " + wrappedBody.getDeclaringClass().getName() + '.' + wrappedBody.getName()); }
/** * 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 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(CtMethod method) throws BadBytecode { return analyze(method.getDeclaringClass(), method.getMethodInfo2()); }
@Override public int compare(CtMethod m1, CtMethod m2) { int value = m1.getName().compareTo(m2.getName()); if (value == 0) value = m1.getMethodInfo2().getDescriptor() .compareTo(m2.getMethodInfo2().getDescriptor()); return value; } });
public TransformBefore(Transformer next, CtMethod origMethod, CtMethod beforeMethod) throws NotFoundException { super(next, origMethod, beforeMethod); // override methodDescriptor = origMethod.getMethodInfo2().getDescriptor(); parameterTypes = origMethod.getParameterTypes(); locals = 0; maxLocals = 0; saveCode = loadCode = null; }
private static void checkSignature(CtMethod wrappedBody, String descriptor) throws CannotCompileException { if (!descriptor.equals(wrappedBody.getMethodInfo2().getDescriptor())) throw new CannotCompileException( "wrapped method with a bad signature: " + wrappedBody.getDeclaringClass().getName() + '.' + wrappedBody.getName()); }
public TransformCall(Transformer next, String oldMethodName, CtMethod substMethod) { super(next); methodname = oldMethodName; methodDescriptor = substMethod.getMethodInfo2().getDescriptor(); classname = newClassname = substMethod.getDeclaringClass().getName(); newMethodname = substMethod.getName(); constPool = null; newMethodIsPrivate = Modifier.isPrivate(substMethod.getModifiers()); }
/** * 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 void removeMethod(CtMethod m) throws NotFoundException { checkModify(); MethodInfo mi = m.getMethodInfo2(); ClassFile cf = getClassFile2(); if (cf.getMethods().remove(mi)) { getMembers().remove(m); gcConstPool = true; } else throw new NotFoundException(m.toString()); }
public TransformCall(Transformer next, String oldMethodName, CtMethod substMethod) { super(next); methodname = oldMethodName; methodDescriptor = substMethod.getMethodInfo2().getDescriptor(); classname = newClassname = substMethod.getDeclaringClass().getName(); newMethodname = substMethod.getName(); constPool = null; newMethodIsPrivate = Modifier.isPrivate(substMethod.getModifiers()); }
public CtMethod getDeclaredMethod(String name, CtClass[] params) throws NotFoundException { String desc = Descriptor.ofParameters(params); CtMember.Cache memCache = getMembers(); CtMember mth = memCache.methodHead(); CtMember mthTail = memCache.lastMethod(); while (mth != mthTail) { mth = mth.next(); if (mth.getName().equals(name) && ((CtMethod)mth).getMethodInfo2().getDescriptor().startsWith(desc)) return (CtMethod)mth; } throw new NotFoundException(name + "(..) is not found in " + getName()); }
private static String inferMethodTypeName(CtMethod method) { try { if ( method.getMethodInfo2().getAttribute( SignatureAttribute.tag ) == null ) { return method.getReturnType().getName(); } return inferGenericTypeName( method.getReturnType(), SignatureAttribute.toMethodSignature( method.getGenericSignature() ).getReturnType() ); } catch (BadBytecode ignore) { return null; } catch (NotFoundException e) { return null; } }
/** * 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; }
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; }
public void addMethod(CtMethod m) throws CannotCompileException { checkModify(); if (m.getDeclaringClass() != this) throw new CannotCompileException("bad declaring class"); int mod = m.getModifiers(); if ((getModifiers() & Modifier.INTERFACE) != 0) { if (Modifier.isProtected(mod) || Modifier.isPrivate(mod)) throw new CannotCompileException( "an interface method must be public: " + m.toString()); m.setModifiers(mod | Modifier.PUBLIC); } getMembers().addMethod(m); getClassFile2().addMethod(m.getMethodInfo2()); if ((mod & Modifier.ABSTRACT) != 0) setModifiers(getModifiers() | Modifier.ABSTRACT); }