@Test public void test2() throws Exception { DefaultInterceptorClassParser defaultInterceptorClassParser = new DefaultInterceptorClassParser(); List<InterceptorProcessor> interceptorProcessors = defaultInterceptorClassParser .parse(SpyTraceInterceptor2.class); ClassNode classNode = AsmUtils.loadClass(TestAAA.class); List<MethodNode> matchedMethods = new ArrayList<MethodNode>(); for (MethodNode methodNode : classNode.methods) { if (MatchUtils.wildcardMatch(methodNode.name, "*")) { matchedMethods.add(methodNode); } } LocationFilter enterFilter = new InvokeCheckLocationFilter(Type.getInternalName(SpyAPI.class), "atBeforeInvoke", LocationType.INVOKE); LocationFilter existFilter = new InvokeCheckLocationFilter(Type.getInternalName(SpyAPI.class), "atInvokeException", LocationType.INVOKE_COMPLETED); LocationFilter exceptionFilter = new InvokeCheckLocationFilter(Type.getInternalName(SpyAPI.class), "atInvokeException", LocationType.INVOKE_EXCEPTION_EXIT); GroupLocationFilter groupLocationFilter = new GroupLocationFilter(); groupLocationFilter.addFilter(enterFilter); groupLocationFilter.addFilter(existFilter); groupLocationFilter.addFilter(exceptionFilter); for (MethodNode methodNode : matchedMethods) { MethodProcessor methodProcessor = new MethodProcessor(classNode, methodNode, groupLocationFilter); for (InterceptorProcessor interceptor : interceptorProcessors) { interceptor.process(methodProcessor); } } for (MethodNode methodNode : matchedMethods) { MethodProcessor methodProcessor = new MethodProcessor(classNode, methodNode, groupLocationFilter); for (InterceptorProcessor interceptor : interceptorProcessors) { interceptor.process(methodProcessor); } } for (MethodNode methodNode : matchedMethods) { MethodProcessor methodProcessor = new MethodProcessor(classNode, methodNode, groupLocationFilter); for (InterceptorProcessor interceptor : interceptorProcessors) { interceptor.process(methodProcessor); } } byte[] bytes = AsmUtils.toBytes(classNode); VerifyUtils.asmVerify(bytes); VerifyUtils.instanceVerity(bytes); System.out.println(Decompiler.decompile(bytes)); ClassNode classNode2 = AsmUtils.toClassNode(bytes); for (MethodNode methodNode : classNode2.methods) { if (!methodNode.name.equals("xxx")) { System.err.println("method name: " + methodNode.name); Assertions.assertThat( AsmUtils.findMethodInsnNode(methodNode, Type.getInternalName(SpyAPI.class), "atBeforeInvoke")) .size().isEqualTo(1); } } }
private MethodInsnNode findMethodInsnNode(AbstractInsnNode insnNode, boolean complete) { if (complete) { while (insnNode != null) { insnNode = insnNode.getNext(); if (insnNode instanceof MethodInsnNode) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; return methodInsnNode; } } } else { while (insnNode != null) { insnNode = insnNode.getPrevious(); if (insnNode instanceof MethodInsnNode) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; return methodInsnNode; } } } return null; }
InvokeCheckLocationFilter implements LocationFilter { private MethodInsnNode findMethodInsnNode(AbstractInsnNode insnNode, boolean complete) { if (complete) { while (insnNode != null) { insnNode = insnNode.getNext(); if (insnNode instanceof MethodInsnNode) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; return methodInsnNode; } } } else { while (insnNode != null) { insnNode = insnNode.getPrevious(); if (insnNode instanceof MethodInsnNode) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; return methodInsnNode; } } } return null; } }
InvokeCheckLocationFilter implements LocationFilter { private MethodInsnNode findMethodInsnNode(AbstractInsnNode insnNode, boolean complete) { if (complete) { while (insnNode != null) { insnNode = insnNode.getNext(); if (insnNode instanceof MethodInsnNode) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; return methodInsnNode; } } } else { while (insnNode != null) { insnNode = insnNode.getPrevious(); if (insnNode instanceof MethodInsnNode) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; return methodInsnNode; } } } return null; } InvokeCheckLocationFilter(String owner, String methodName, LocationType locationType); }
InvokeCheckLocationFilter implements LocationFilter { private MethodInsnNode findMethodInsnNode(AbstractInsnNode insnNode, boolean complete) { if (complete) { while (insnNode != null) { insnNode = insnNode.getNext(); if (insnNode instanceof MethodInsnNode) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; return methodInsnNode; } } } else { while (insnNode != null) { insnNode = insnNode.getPrevious(); if (insnNode instanceof MethodInsnNode) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; return methodInsnNode; } } } return null; } InvokeCheckLocationFilter(String owner, String methodName, LocationType locationType); @Override boolean allow(AbstractInsnNode insnNode, LocationType locationType, boolean complete); }
InvokeCheckLocationFilter implements LocationFilter { private MethodInsnNode findMethodInsnNode(AbstractInsnNode insnNode, boolean complete) { if (complete) { while (insnNode != null) { insnNode = insnNode.getNext(); if (insnNode instanceof MethodInsnNode) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; return methodInsnNode; } } } else { while (insnNode != null) { insnNode = insnNode.getPrevious(); if (insnNode instanceof MethodInsnNode) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; return methodInsnNode; } } } return null; } InvokeCheckLocationFilter(String owner, String methodName, LocationType locationType); @Override boolean allow(AbstractInsnNode insnNode, LocationType locationType, boolean complete);  }