/** * Find the given method on the stack trace. * * @param clazz * the clazz * @param methodName * the method name * @return the stack trace element */ public static StackTraceElement find(final Class<?> clazz, final String methodName) { return find(clazz.getName(), methodName); }
/** * Gets the caller class by examing the stacktrace. * * @param excluded * a list of filters which should be not considered as caller * @return the caller of */ public static Class<?> getCallerClass(final Pattern... excluded) { return getCallerClass(excluded, new Class<?>[0]); }
/** * Gets the caller of the given method. * * @param clazz * the clazz * @param methodName * the method name * @return the caller of */ public static StackTraceElement getCallerOf(final Class<?> clazz, final String methodName) { return getCallerOf(clazz, methodName, new Pattern[0]); }
/** * Gets the caller of the given method. * * @param classname * the classname * @param methodName * the method name * @param excluded * a list of filters which should be not considered as caller * @return the caller of */ public static StackTraceElement getCallerOf(final String classname, final String methodName, final Pattern... excluded) { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); for (int i = getIndexOf(classname, methodName, stackTrace) + 1; i < stackTrace.length; i++) { if (!matches(stackTrace[i].getMethodName(), excluded)) { return stackTrace[i]; } } throw new NotFoundException(classname + "." + methodName + "()"); }
/** * Gets the caller of a constructor. * * @param clazz * the clazz * @param excluded * the excluded * @return the caller of constructor */ public static StackTraceElement getCallerOfConstructor(final Class<?> clazz, final Pattern... excluded) { try { return getCallerOf(clazz, "<init>", excluded); } catch (NotFoundException nfe) { LOG.trace("Caller of constructor of {} not found:", clazz, nfe); LOG.debug("Looking for adviced init method because of {}", nfe.getMessage()); StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); for (int i = 1; i < stackTrace.length; i++) { if (stackTrace[i].getMethodName().startsWith("init$")) { for (int j = i + 1; j < stackTrace.length; j++) { if (!matches(stackTrace[j].getMethodName(), excluded)) { return stackTrace[j]; } } break; } } throw new NotFoundException( "new " + clazz.getSimpleName() + "(..) not part of " + Converter.toShortString(stackTrace)); } }
/** * Gets the caller stack trace of the method or constructor which calls it. * * @return the caller stack trace * @since 1.4.2 (17.05.2014) */ public static StackTraceElement[] getCallerStackTrace() { return getCallerStackTrace(new Pattern[0]); }
/** * Gets the caller of a constructor. * * @param clazz * the clazz * @return the caller of constructor */ public static StackTraceElement getCallerOfConstructor(final Class<?> clazz) { return getCallerOfConstructor(clazz, new Pattern[0]); }
/** * Find the given method on the stack trace. * * @param classname * the classname * @param methodName * the method name * @return the stack trace element */ public static StackTraceElement find(final String classname, final String methodName) { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); int i = getIndexOf(classname, methodName, stackTrace); return stackTrace[i]; }
private static StackTraceElement find(final ConstructorSignature signature) { return findConstructor(signature.getDeclaringType()); }
/** * Gets the caller stack trace of the method or constructor which calls it. * * @param excludedMethods * the excluded methods * @param excludedClasses * the excluded classes * @return the caller stack trace * @since 1.4.2 (17.05.2014) */ public static StackTraceElement[] getCallerStackTrace(final Pattern[] excludedMethods, final Class<?>... excludedClasses) { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); int i = 2; String scannerClassName = StackTraceScanner.class.getName(); for (; i < stackTrace.length - 1; i++) { if (!scannerClassName.equals(stackTrace[i].getClassName())) { break; } } for (; i < stackTrace.length - 1; i++) { if (!matches(stackTrace[i].getMethodName(), excludedMethods) && !matches(stackTrace[i].getClassName(), excludedClasses)) { break; } } return ArrayUtils.subarray(stackTrace, i, stackTrace.length); }
/** * Gets the caller stack trace of the method or constructor which calls it. * * @param excluded * a list of filters which should be not considered as caller * @return the caller stack trace * @since 1.4.2 (17.05.2014) */ public static StackTraceElement[] getCallerStackTrace(final Pattern... excluded) { return getCallerStackTrace(excluded, new Class<?>[0]); }
private static StackTraceElement getCallerOf(final ConstructorSignature signature, final Pattern... excluded) { return getCallerOfConstructor(signature.getDeclaringType(), excluded); }
/** * Gets the caller of the given signature. * * @param signature * the signature * @return the caller of */ public static StackTraceElement getCallerOf(final Signature signature) { return getCallerOf(signature, new Pattern[0]); }
/** * Find the constructor of the given class on the stack trace. * * @param clazz * the clazz * @return the stack trace element */ public static StackTraceElement findConstructor(final Class<?> clazz) { return find(clazz.getName(), "<init>"); }
/** * Gets the caller class by examing the stacktrace. * * @return the caller class */ public static Class<?> getCallerClass() { return getCallerClass(new Pattern[0]); }
/** * Gets the caller class by examing the stacktrace. * * @param excludedMethods * the excluded methods * @param excludedClasses * the excluded classes * @return the caller class */ public static Class<?> getCallerClass(final Pattern[] excludedMethods, final Class<?>... excludedClasses) { StackTraceElement[] stackTrace = getCallerStackTrace(excludedMethods, excludedClasses); String classname = stackTrace[0].getClassName(); try { return Class.forName(classname); } catch (ClassNotFoundException ex) { throw new NotFoundException(classname, ex); } }
/** * Gets the caller of the given method. * * @param classname * the classname * @param methodName * the method name * @return the caller of */ public static StackTraceElement getCallerOf(final String classname, final String methodName) { return getCallerOf(classname, methodName, new Pattern[0]); }
/** * Find the given signature on the stack trace. * * @param signature * the signature * @return the stack trace element */ public static StackTraceElement find(final Signature signature) { if (signature instanceof MethodSignature) { return find((MethodSignature) signature); } else if (signature instanceof ConstructorSignature) { return find((ConstructorSignature) signature); } else { throw new IllegalArgumentException(signature + " is not a method or ctor signature"); } }
/** * Gets the caller class by examing the stacktrace. * <p> * Sometime the caller of the method is an aspect. With this method you have * the chance to list the aspects which should be ignored. * </p> * * @param excluded * the excluded * @return the caller of a joinpoint */ public static Class<?> getCallerClass(final Class<?>... excluded) { return StackTraceScanner.getCallerClass(ADVICE_PATTERNS, excluded); }
/** * Stops the given 'mon' and logs the given command with the needed time if * debug is enabled. * * @param mon * the mon * @param command * the command * @param returnValue * the return value */ public static void stop(final ProfileMonitor mon, final String command, final Object returnValue) { mon.stop(); if (LOG.isDebugEnabled()) { String msg = '"' + command + "\" returned with " + Converter.toShortString(returnValue) + " after " + mon.getLastTime(); if (LOG.isTraceEnabled()) { StackTraceElement[] stacktrace = StackTraceScanner.getCallerStackTrace(new Pattern[0], SqlStatistic.class, StasiStatement.class, StasiPreparedStatement.class); LOG.trace("{}\n\t{}", msg, Converter.toLongString(stacktrace).trim()); } else { LOG.debug("{}.", msg); } } }