How to use visitMethodInsn method of org.evomaster.client.java.instrumentation.coverage.MethodReplacementMethodVisitor class

Best EvoMaster code snippet using org.evomaster.client.java.instrumentation.coverage.MethodReplacementMethodVisitor.visitMethodInsn

Source:MethodReplacementMethodVisitor.java Github

copy

Full Screen

...43 currentLine = line;44 currentIndex = 0; //reset it for current line45 }46 @Override47 public void visitMethodInsn(int opcode, String owner, String name,48 String desc, boolean itf) {49 //don't instrument static initializers50 if (methodName.equals(Constants.CLASS_INIT_METHOD)) {51 super.visitMethodInsn(opcode, owner, name, desc, itf);52 return;53 }54 /*55 * This is a very special case. This can happen when we replace a method56 * for X.foo() in a class Y, when Y extends X, and then it calls foo in57 * the superclass X with super.foo()58 * As it is now, this would lead to an infinite recursion in the replaced59 * method if we call foo() there (as it would be executed on the instance of Y,60 * not the super method in X).61 * Given an instance of Y, it is not possible to directly call X.foo() from outside Y.62 * TODO: Maybe there can be a general solution for this, but, considering it is likely rare,63 * we can do it for later. Eg, it would mainly affect the use of special containers like64 * in Guava when they have "super.()" calls.65 * For now, we just skip them.66 */67 if (opcode == Opcodes.INVOKESPECIAL) {68 super.visitMethodInsn(opcode, owner, name, desc, itf);69 return;70 }71// if (!owner.startsWith("java/")) {72// super.visitMethodInsn(opcode, owner, name, desc, itf);73// return;74// }75//76// /*77// Loading class here could have side-effects if code is executed in static initializer.78// */79// Class<?> klass = null;80// try {81// klass = this.getClass().getClassLoader().loadClass(ClassName.get(owner).getFullNameWithDots());82// } catch (ClassNotFoundException e) {83// //shouldn't really happen84// SimpleLogger.error(e.toString());85// throw new RuntimeException(e);86// }87 List<MethodReplacementClass> candidateClasses = ReplacementList.getReplacements(owner);88 if (candidateClasses.isEmpty()) {89 super.visitMethodInsn(opcode, owner, name, desc, itf);90 return;91 }92 Optional<Method> r = candidateClasses.stream()93 .flatMap(i -> Stream.of(i.getClass().getDeclaredMethods()))94 .filter(m -> m.getDeclaredAnnotation(Replacement.class) != null)95 .filter(m -> m.getName().equals(name))96 .filter(m -> {97 Replacement br = m.getAnnotation(Replacement.class);98 if(!applyTrackingMethods && br.type() == ReplacementType.TRACKER){99 return false;100 }101 int skipFirst = br.replacingStatic() ? 0 : 1;102 int skipLast = br.type() == ReplacementType.TRACKER ? 0 : 1;103 return desc.equals(getDescriptor(m, skipFirst, skipLast));104 })105 .findAny();106 if (!r.isPresent()) {107 super.visitMethodInsn(opcode, owner, name, desc, itf);108 return;109 }110 Method m = r.get();111 replaceMethod(m);112 Replacement a = m.getAnnotation(Replacement.class);113 if (a.type() == ReplacementType.TRACKER) {114 UnitsInfoRecorder.markNewTrackedMethod();115 } else {116 if (registerNewTargets) {117 UnitsInfoRecorder.markNewReplacedMethodInSut();118 } else {119 UnitsInfoRecorder.markNewReplacedMethodInThirdParty();120 }121 }122 }123 private void replaceMethod(Method m) {124 Replacement br = m.getAnnotation(Replacement.class);125 /*126 In the case of replacing a non-static method a.foo(x,y),127 we will need a replacement bar(a,x,y,id)128 So, the stack129 a130 x131 y132 foo # non-static133 will be replaced by134 a135 x136 y137 id138 bar # static139 This means we do not need to handle "a", but still need to create140 "id" and replace "foo" with "bar".141 */142 if(br.type() != ReplacementType.TRACKER) {143 //tracker methods do not add a template id144 if (registerNewTargets) {145 String idTemplate = ObjectiveNaming.methodReplacementObjectiveNameTemplate(146 className, currentLine, currentIndex147 );148 currentIndex++;149 String idTrue = ObjectiveNaming.methodReplacementObjectiveName(idTemplate, true, br.type());150 String idFalse = ObjectiveNaming.methodReplacementObjectiveName(idTemplate, false, br.type());151 ObjectiveRecorder.registerTarget(idTrue);152 ObjectiveRecorder.registerTarget(idFalse);153 this.visitLdcInsn(idTemplate);154 } else {155 //this.visitLdcInsn(null);156 this.visitInsn(Opcodes.ACONST_NULL);157 }158 }159 mv.visitMethodInsn(160 Opcodes.INVOKESTATIC,161 Type.getInternalName(m.getDeclaringClass()),162 m.getName(),163 Type.getMethodDescriptor(m),164 false);165 }166 private static String getDescriptor(Method m, int skipFirsts, int skipLast) {167 Class<?>[] parameters = m.getParameterTypes();168 StringBuilder buf = new StringBuilder();169 buf.append('(');170 //skipping first parameter(s)171 int start = skipFirsts;172 int end = parameters.length - skipLast;173 /*...

Full Screen

Full Screen

visitMethodInsn

Using AI Code Generation

copy

Full Screen

1public class MethodReplacementClassVisitor extends ClassVisitor {2 private String className;3 private String methodName;4 private String methodDesc;5 private String methodReplacementClassName;6 private String methodReplacementMethodName;7 private String methodReplacementMethodDesc;8 public MethodReplacementClassVisitor(int api, ClassVisitor cv, String className, String methodName, String methodDesc, String methodReplacementClassName, String methodReplacementMethodName, String methodReplacementMethodDesc) {9 super(api, cv);10 this.className = className;11 this.methodName = methodName;12 this.methodDesc = methodDesc;13 this.methodReplacementClassName = methodReplacementClassName;14 this.methodReplacementMethodName = methodReplacementMethodName;15 this.methodReplacementMethodDesc = methodReplacementMethodDesc;16 }17 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {18 MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);19 if (name.equals(methodName) && desc.equals(methodDesc)) {20 mv = new MethodReplacementMethodVisitor(api, mv, className, methodName, methodDesc, methodReplacementClassName, methodReplacementMethodName, methodReplacementMethodDesc);21 }22 return mv;23 }24}25public class MethodReplacementMethodVisitor extends MethodVisitor {26 private String className;27 private String methodName;28 private String methodDesc;29 private String methodReplacementClassName;30 private String methodReplacementMethodName;31 private String methodReplacementMethodDesc;32 public MethodReplacementMethodVisitor(int api, MethodVisitor mv, String className, String methodName, String methodDesc, String methodReplacementClassName, String methodReplacementMethodName, String methodReplacementMethodDesc) {33 super(api, mv);34 this.className = className;35 this.methodName = methodName;36 this.methodDesc = methodDesc;37 this.methodReplacementClassName = methodReplacementClassName;38 this.methodReplacementMethodName = methodReplacementMethodName;39 this.methodReplacementMethodDesc = methodReplacementMethodDesc;40 }41 public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {42 if (owner.equals(className) && name.equals(methodName) && desc.equals

Full Screen

Full Screen

visitMethodInsn

Using AI Code Generation

copy

Full Screen

1public class MethodReplacementMethodVisitor extends MethodVisitor {2 private final String methodName;3 private final String methodDesc;4 private final String methodOwner;5 private final List<MethodReplacement> methodReplacements;6 private final boolean isStatic;7 public MethodReplacementMethodVisitor(int api, MethodVisitor mv, String methodName, String methodDesc, String methodOwner, List<MethodReplacement> methodReplacements, boolean isStatic) {8 super(api, mv);9 this.methodName = methodName;10 this.methodDesc = methodDesc;11 this.methodOwner = methodOwner;12 this.methodReplacements = methodReplacements;13 this.isStatic = isStatic;14 }15 public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {16 if (isStatic && (opcode == Opcodes.INVOKEVIRTUAL || opcode == Opcodes.INVOKEINTERFACE)) {17 super.visitMethodInsn(opcode, owner, name, desc, itf);18 return;19 }20 if (isStatic && opcode == Opcodes.INVOKESPECIAL) {21 super.visitMethodInsn(opcode, owner, name, desc, itf);22 return;23 }24 if (!isStatic && opcode == Opcodes.INVOKESTATIC) {25 super.visitMethodInsn(opcode, owner, name, desc, itf);26 return;27 }28 if (!isStatic && opcode == Opcodes.INVOKESPECIAL && !"<init>".equals(name)) {29 super.visitMethodInsn(opcode, owner, name, desc, itf);30 return;31 }32 if (!isStatic && opcode == Opcodes.INVOKESPECIAL && "<init>".equals(name)) {33 super.visitMethodInsn(opcode, owner, name, desc, itf);34 return;35 }36 for (MethodReplacement mr : methodReplacements) {37 if (mr.matches(owner, name, desc)) {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run EvoMaster automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful