@Test public void testGetTraceForProxiedMethod() { TestTraceExtractor traceExtractor = new TestTraceExtractor(); TestBase testProxy = (TestBase) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{TestBase.class}, traceExtractor); testProxy.testBaseMethod(); Assert.assertTrue(traceExtractor.traceElements.size() > 0); Assert.assertEquals("Should start with base method", "testBaseMethod", traceExtractor.traceElements.get(0).getMethodName()); Assert.assertEquals("Should be followed by unit test name", "testGetTraceForProxiedMethod", traceExtractor.traceElements.get(1).getMethodName()); }
public static List<StackTraceElement> getTraceForProxiedMethod(Method method) throws ClassNotFoundException { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); Class<?> baseClass = method.getDeclaringClass(); int startIndex = 0; StackTraceElement baseMethodTrace = null; for (int i = 0; i < stackTraceElements.length; i++) { StackTraceElement traceElement = stackTraceElements[i]; String traceElementClassName = traceElement.getClassName(); if (traceElementClassName.contains("$Proxy") && baseClass.isAssignableFrom(Class.forName(traceElementClassName))) { baseMethodTrace = createTraceElement(method, traceElement); startIndex = i + 1; break; } } if (startIndex == 0) { return Arrays.asList(stackTraceElements); } else { return replaceStackTraceElements(stackTraceElements, startIndex, baseMethodTrace); } }
StackTraceExtractor { public static List<StackTraceElement> getTraceForProxiedMethod(Method method) throws ClassNotFoundException { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); Class<?> baseClass = method.getDeclaringClass(); int startIndex = 0; StackTraceElement baseMethodTrace = null; for (int i = 0; i < stackTraceElements.length; i++) { StackTraceElement traceElement = stackTraceElements[i]; String traceElementClassName = traceElement.getClassName(); if (traceElementClassName.contains("$Proxy") && baseClass.isAssignableFrom(Class.forName(traceElementClassName))) { baseMethodTrace = createTraceElement(method, traceElement); startIndex = i + 1; break; } } if (startIndex == 0) { return Arrays.asList(stackTraceElements); } else { return replaceStackTraceElements(stackTraceElements, startIndex, baseMethodTrace); } } }
StackTraceExtractor { public static List<StackTraceElement> getTraceForProxiedMethod(Method method) throws ClassNotFoundException { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); Class<?> baseClass = method.getDeclaringClass(); int startIndex = 0; StackTraceElement baseMethodTrace = null; for (int i = 0; i < stackTraceElements.length; i++) { StackTraceElement traceElement = stackTraceElements[i]; String traceElementClassName = traceElement.getClassName(); if (traceElementClassName.contains("$Proxy") && baseClass.isAssignableFrom(Class.forName(traceElementClassName))) { baseMethodTrace = createTraceElement(method, traceElement); startIndex = i + 1; break; } } if (startIndex == 0) { return Arrays.asList(stackTraceElements); } else { return replaceStackTraceElements(stackTraceElements, startIndex, baseMethodTrace); } } private  StackTraceExtractor(); }
StackTraceExtractor { public static List<StackTraceElement> getTraceForProxiedMethod(Method method) throws ClassNotFoundException { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); Class<?> baseClass = method.getDeclaringClass(); int startIndex = 0; StackTraceElement baseMethodTrace = null; for (int i = 0; i < stackTraceElements.length; i++) { StackTraceElement traceElement = stackTraceElements[i]; String traceElementClassName = traceElement.getClassName(); if (traceElementClassName.contains("$Proxy") && baseClass.isAssignableFrom(Class.forName(traceElementClassName))) { baseMethodTrace = createTraceElement(method, traceElement); startIndex = i + 1; break; } } if (startIndex == 0) { return Arrays.asList(stackTraceElements); } else { return replaceStackTraceElements(stackTraceElements, startIndex, baseMethodTrace); } } private  StackTraceExtractor(); static List<StackTraceElement> getTraceForProxiedMethod(Method method); static List<StackTraceElement> getTraceForImplementingMethod(Method method, Method methodImpl); static List<StackTraceElement> getTraceTillPackage(String packageName); static String printStackTrace(List<StackTraceElement> stackTraceElements); }
StackTraceExtractor { public static List<StackTraceElement> getTraceForProxiedMethod(Method method) throws ClassNotFoundException { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); Class<?> baseClass = method.getDeclaringClass(); int startIndex = 0; StackTraceElement baseMethodTrace = null; for (int i = 0; i < stackTraceElements.length; i++) { StackTraceElement traceElement = stackTraceElements[i]; String traceElementClassName = traceElement.getClassName(); if (traceElementClassName.contains("$Proxy") && baseClass.isAssignableFrom(Class.forName(traceElementClassName))) { baseMethodTrace = createTraceElement(method, traceElement); startIndex = i + 1; break; } } if (startIndex == 0) { return Arrays.asList(stackTraceElements); } else { return replaceStackTraceElements(stackTraceElements, startIndex, baseMethodTrace); } } private  StackTraceExtractor(); static List<StackTraceElement> getTraceForProxiedMethod(Method method); static List<StackTraceElement> getTraceForImplementingMethod(Method method, Method methodImpl); static List<StackTraceElement> getTraceTillPackage(String packageName); static String printStackTrace(List<StackTraceElement> stackTraceElements);  }