@Override public void edit(MethodCall m) throws CannotCompileException { try { put(m.getMethod().getDeclaringClass().getName() + "." + m.getMethodName() + "(" + parameterNames(m.getMethod().getMethodInfo()) + ")", m.getLineNumber(), key); } catch (NotFoundException e) { throw new ReflectionsException("Could not find member " + m.getClassName() + " in " + key, e); } }
/** * Returns true if the called method is of a superclass of the current * class. */ public boolean isSuper() { return iterator.byteAt(currentPos) == INVOKESPECIAL && !where().getDeclaringClass().getName().equals(getClassName()); }
/** * Returns the called method. */ public CtMethod getMethod() throws NotFoundException { return getCtClass().getMethod(getMethodName(), getSignature()); }
@Override public void edit(MethodCall m) throws CannotCompileException { String className = m.getClassName(); String method = m.getMethodName(); try { className = m.getMethod().getDeclaringClass().getName(); } catch (NotFoundException e) { throw new RuntimeException("Cannot instrument call to " && method.equals("currentTimeMillis")) { m.replace("{ $_ = " + s + ".currentTimeMillis(); }"); m.replace("{ $_ = " + s + ".nanoTime(); }"); m.replace("{ " + s + ".sleep($$); }"); m.replace("{ " + s + ".start(); }");
@Override public void edit(MethodCall m) throws CannotCompileException { switch (m.getClassName() + "." + m.getMethodName()) { case "com.wurmonline.server.items.Item.willLeaveServer": String code = ""; code += "int targetId = " + targetServerName + ";"; code += "$_ = (targetId == 0 || modPlayerTransfer.willItemLeaveServer(targetId, $0, $1)) && $proceed($$);"; m.replace("{" + code + "}"); break; } } }
@Override public void edit(MethodCall m) throws CannotCompileException { if (m.getMethodName().equals(insertionMethod)) { String origMethodCall = "$_ = $proceed($$);;\n"; if (insertAfter) { origMethodCall = origMethodCall + bodyToInsert; } else { origMethodCall = bodyToInsert + origMethodCall; } log.info("Injected : " + origMethodCall); log.info("Class " + classToTransform.getName() + " has been enhanced."); m.replace(origMethodCall); isSuccessful = true; } } }
public class MethodFinder { public static void main(String[] args) throws Throwable { ClassPool cp = ClassPool.getDefault(); CtClass ctClass = cp.get("MyClass"); CtMethod method = ctClass.getDeclaredMethod("getItem1"); method.instrument( new ExprEditor() { public void edit(MethodCall m) throws CannotCompileException { System.out.println(m.getClassName() + "." + m.getMethodName() + " " + m.getSignature()); } }); } }
/** * Returns the class of the target object, * which the method is called on. */ protected CtClass getCtClass() throws NotFoundException { return thisClass.getClassPool().get(getClassName()); }
|| c == Opcode.INVOKEINTERFACE || c == Opcode.INVOKEVIRTUAL) { expr = new MethodCall(pos, iterator, clazz, minfo); edit((MethodCall)expr); int index = iterator.u16bitAt(pos + 1); context.newList = new NewOp(context.newList, pos, minfo.getConstPool().getClassInfo(index)); && minfo.getConstPool().isConstructor(newList.type, iterator.u16bitAt(pos + 1)) > 0) { expr = new NewExpr(pos, iterator, clazz, minfo, MethodCall mcall = new MethodCall(pos, iterator, clazz, minfo); if (mcall.getMethodName().equals(MethodInfo.nameInit)) { ConstructorCall ccall = new ConstructorCall(pos, iterator, clazz, minfo); expr = ccall;
@Override public void edit(MethodCall m) throws CannotCompileException { if (m.getClassName().equals("com.wurmonline.server.creatures.Traits") && m.getMethodName().equals("calcNewTraits") && m.getSignature().equals("(DZJJ)J")) { m.replace("$_ = org.gotti.wurmunlimited.modsupport.creatures.ModCreatures#calcNewTraits($1, $2, this, father);"); } } });
@Override public void edit(final MethodCall call) throws CannotCompileException { if (call.getMethodName().equals(calledMethodName)) try { final boolean isSuper = call.isSuper(); final CtClass[] parameterTypes = isSuper ? ((ConstructorCall) call).getConstructor() .getParameterTypes() : call.getMethod().getParameterTypes(); if (parameterTypes.length < parameterIndex) { maybeThrow(new IllegalArgumentException("Index " + parameterIndex + " is outside of " + call.getMethod() + "'s parameter list!")); return; parameterIndex + " of " + call.getMethod() + " is not a String!")); return; call.replace((isSuper ? "" : "$0.") + calledMethodName + replace + ";"); markEdited();
@Override public void edit(MethodCall m) throws CannotCompileException { if (m.getMethodName().equals("get") && first) { m.replace("$_ = $proceed($$);" + "if ($_ == " + ModCommConstants.CMD_MODCOMM + ") {" + " org.gotti.wurmunlimited.modcomm.ModCommHandler.handlePacket(player, byteBuffer);" + " return;" + "}"); first = false; } } });
public Object visit(ASTCall node, Object data) { try { if (!(node.getBehavior() instanceof ASTMethod)) return Boolean.FALSE; ASTMethod astMethod = (ASTMethod) node.getBehavior(); // do simple checks to avoid loading CtClasses if (astMethod.getClazz().isSimple()) { if (!astMethod.getClazz().matches(call.getClassName())) return Boolean.FALSE; if (!astMethod.getMethodIdentifier().isAnnotation()) { if (!astMethod.getMethodIdentifier().matches(call.getMethodName())) return Boolean.FALSE; } } CtMethod calledMethod = call.getMethod(); Advisor calledAdvisor = AspectManager.instance().getTempClassAdvisor(calledMethod.getDeclaringClass()); MethodMatcher methodMatcher = new MethodMatcher(calledAdvisor, calledMethod, null); return methodMatcher.matches(astMethod); } catch (Exception e) { throw new RuntimeException(e); //To change body of catch statement use Options | File Templates. } }
public void edit(MethodCall call) throws CannotCompileException String classname = call.getClassName(); String methodName = call.getMethodName(); try || methodName.startsWith("_") || classname.startsWith("org.jboss.aop.") || call.getMethodName().equals("class$") // todo not sure why this is part of the method call || !Instrumentor.isTransformable(callingClass) CtBehavior behavior = call.where();
/// ... your existing code // we create an instance using our base class instead of ExprEditor ExprEditorMethodCallAware exprEditor = new ExprEditorMethodCallAware() { public void edit(MethodCall m) throws CannotCompileException { // notice the set setProcessedMethodCalls(true); System.out.println(m.getClassName() + "." + m.getMethodName() + " " + m.getSignature()); } }; // we now instrument the code as you were already doing it method.instrument(exprEditor); // And now you check if there were or not methodCalls processed if(!exprEditor.isProcessedMethodCalls()) { System.out.println("No methodCalls found in " + method.getMethodName()); }
@Override public void edit(MethodCall m) { try { CtClass enclosing = m.getEnclosingClass(); String className = m.getClassName(); if (isAssignableFrom(className, enclosing)) { findMethodCalls(m.getMethod(), methods); } } catch (NotFoundException e) { logger.warn(e.toString(), e); } }
@Override public void edit(MethodCall m) throws CannotCompileException { try { final CtMethod method = m.getMethod(); final CtClass declaringClass = method.getDeclaringClass(); .append(".methodCall(") .append("classOrInstance,\"") .append(m.getMethodName()) .append("\",$args, $sig,\"") .append(getReturnTypeAsString(method)) m.replace(code.toString());
@Override public void edit(MethodCall called) { try { if (targetNames.contains(called.getClassName())) { Method callingMethod = getMethod(caller.getName(), called.where()); if (callingMethod != null && (getRecursive || !result.containsKey(callingMethod)) && methodsToProcess.contains(callingMethod)) { Method calledMethod = getMethod(called.getClassName(), called.getMethod()); if (!targetName.equals(called.getClassName())) { calledMethod = getMethod(targetName, called.getMethod()); if (calledMethod != null) { break; getCallTree(called.getClassName(), called.getMethod().getLongName(), targetNames, calledMethodSet, ctClasses);
new ExprEditor() { @Override public void edit(MethodCall m) throws CannotCompileException { try { if (m.where().getName().equals(sourceMethod)) { if (m.getMethod().getName().equals(methodcalltoReplace)) { if(lineNumberOfMethodcalltoReplace == m.getLineNumber()){ // The content of the hardcoded string can be replaced with runtime data m.replace("$_ = ($r)"+"new com.nuwaza.aqua.sample.SampleForMethodInvocationFieldAccess().helloworld();"); } } } } catch (NotFoundException e) { e.printStackTrace(); } super.edit(m); }
@Override public void edit(MethodCall called) { try { String calledMethodName = called.getMethod().getLongName(); if (targetNames.contains(called.getClassName())) { allMethods.add(getMethod(called.getClassName(), called.getMethod())); getCallTree(called.getClassName(), calledMethodName, targetNames, allMethods, ctClasses); } } catch (NotFoundException e) { throw new RuntimeException(e); } } });