List of usage examples for java.lang StackTraceElement getMethodName
public String getMethodName()
From source file:org.apache.cxf.common.logging.LogUtils.java
private static void doLog(Logger log, Level level, String msg, Throwable t) { LogRecord record = new LogRecord(level, msg); record.setLoggerName(log.getName()); record.setResourceBundleName(log.getResourceBundleName()); record.setResourceBundle(log.getResourceBundle()); if (t != null) { record.setThrown(t);/* w ww .ja v a2 s . c o m*/ } //try to get the right class name/method name - just trace //back the stack till we get out of this class StackTraceElement stack[] = (new Throwable()).getStackTrace(); String cname = LogUtils.class.getName(); for (int x = 0; x < stack.length; x++) { StackTraceElement frame = stack[x]; if (!frame.getClassName().equals(cname)) { record.setSourceClassName(frame.getClassName()); record.setSourceMethodName(frame.getMethodName()); break; } } log.log(record); }
From source file:org.sonatype.flexmojos.tests.AbstractFlexMojosTests.java
private static String getTestName() { StackTraceElement[] stackTrace = new Exception().getStackTrace(); for (StackTraceElement stack : stackTrace) { Class<?> testClass; try {//from www.j a v a 2 s . com testClass = Class.forName(stack.getClassName()); } catch (ClassNotFoundException e) { // nvm, and should never happen continue; } for (Method method : testClass.getMethods()) { if (method.getName().equals(stack.getMethodName())) { if (method.getAnnotation(Test.class) != null) { return method.getName(); } } } } return null; }
From source file:com.linkedin.whiteelephant.mapreduce.lib.job.StagedOutputJob.java
private static Class<?> getCallersClass() { StackTraceElement[] stack = Thread.currentThread().getStackTrace(); boolean foundSelf = false; for (StackTraceElement element : stack) { if (foundSelf && !StagedOutputJob.class.getName().equals(element.getClassName())) { try { return Class.forName(element.getClassName()); } catch (ClassNotFoundException e) { throw new RuntimeException(e); }/*from w ww. j a va 2s. com*/ } else if (StagedOutputJob.class.getName().equals(element.getClassName()) && "getCallersClass".equals(element.getMethodName())) { foundSelf = true; } } return StagedOutputJob.class; }
From source file:com.simiacryptus.util.io.MarkdownNotebookOutput.java
/** * Get markdown notebook output.//from w w w .j av a2s .c o m * * @param sourceClass the source class * @param absoluteUrl the absolute url * @param suffix the suffix * @return the markdown notebook output */ @javax.annotation.Nonnull public static com.simiacryptus.util.io.MarkdownNotebookOutput get( @javax.annotation.Nonnull Class<?> sourceClass, @Nullable String absoluteUrl, @javax.annotation.Nonnull String... suffix) { try { final StackTraceElement callingFrame = Thread.currentThread().getStackTrace()[2]; final String methodName = callingFrame.getMethodName(); final String className = sourceClass.getCanonicalName(); @javax.annotation.Nonnull File path = new File(Util.mkString(File.separator, "reports", className.replaceAll("\\.", "/").replaceAll("\\$", "/"))); for (int i = 0; i < suffix.length - 1; i++) path = new File(path, suffix[i]); String testName = suffix.length == 0 ? methodName : suffix[suffix.length - 1]; path = new File(path, testName + ".md"); path.getParentFile().mkdirs(); @javax.annotation.Nonnull com.simiacryptus.util.io.MarkdownNotebookOutput notebookOutput = new com.simiacryptus.util.io.MarkdownNotebookOutput( path, testName); if (null != absoluteUrl) { try { String url = new URI(absoluteUrl + "/" + path.toPath().toString().replaceAll("\\\\", "/")) .normalize().toString(); notebookOutput.setAbsoluteUrl(url); } catch (URISyntaxException e) { throw new RuntimeException(e); } } return notebookOutput; } catch (@javax.annotation.Nonnull final FileNotFoundException e) { throw new RuntimeException(e); } }
From source file:cascading.util.Util.java
public static Class findMainClass(Class defaultType) { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); for (StackTraceElement stackTraceElement : stackTrace) { if (stackTraceElement.getMethodName().equals("main") && !stackTraceElement.getClassName().startsWith("org.apache.hadoop")) { try { LOG.info("resolving application jar from found main method on: " + stackTraceElement.getClassName()); return Thread.currentThread().getContextClassLoader() .loadClass(stackTraceElement.getClassName()); } catch (ClassNotFoundException exception) { LOG.warn("unable to load class while discovering application jar: " + stackTraceElement.getClassName(), exception); }/*from w w w .j a v a2 s . c o m*/ } } LOG.info("using default application jar, may cause class not found exceptions on the cluster"); return defaultType; }
From source file:com.simiacryptus.util.io.MarkdownNotebookOutput.java
/** * Get markdown notebook output.//from w w w. j a v a2s . co m * * @return the markdown notebook output */ public static com.simiacryptus.util.io.MarkdownNotebookOutput get() { try { final StackTraceElement callingFrame = Thread.currentThread().getStackTrace()[2]; final String className = callingFrame.getClassName(); final String methodName = callingFrame.getMethodName(); @javax.annotation.Nonnull final String fileName = methodName + ".md"; @javax.annotation.Nonnull File path = new File(Util.mkString(File.separator, "reports", className.replaceAll("\\.", "/").replaceAll("\\$", "/"))); path = new File(path, fileName); path.getParentFile().mkdirs(); return new com.simiacryptus.util.io.MarkdownNotebookOutput(path, methodName); } catch (@javax.annotation.Nonnull final FileNotFoundException e) { throw new RuntimeException(e); } }
From source file:org.openspaces.test.client.executor.ExecutorUtils.java
/** * Strip stack trace of provided {@link Throwable} object up to supplied classname <b>and</b> * method name. This method is useful when you wish to hide internal stackTrace.<br> This method * scans the {@link Throwable#getStackTrace()} and removes all {@link StackTraceElement} up to * supplied classname and methodName. The Throwable's cause is also scanned in a * <i>recursive</i> manner. <p> <u>NOTE</u>: If the provided classname and method was not found * in the stackTrace, the {@link Throwable} stackTrace stays unmodified. * * <pre>/*from w ww.j ava2s.c o m*/ * For example: * Original stackTrace: * java.lang.Exception: Example stack trace... * at test.TestSTrace.applCall3(Test.java:926) * at test.TestSTrace.applCall2(Test.java:921) * at test.TestSTrace.applCall1(Test.java:916) * at test.TestSTrace.startApplCall(Test.java:912) * at test.TestSTrace.internalCall3(Test.java:941) * at test.TestSTrace.internalCall2(Test.java:936) * at test.TestSTrace.internalCall1(Test.java:931) * at test.TestSTrace.main(Test.java:977) * * TGridHelper.stripStackTrace(throwable, "test.TestSTrace", "startApplCall"); * throwable.printStackTrace(); * * After: * java.lang.Exception: Example stack trace... * at test.TestSTrace.applCall3(Test.java:941) * at test.TestSTrace.applCall2(Test.java:936) * at test.TestSTrace.applCall1(Test.java:931) * at test.TestSTrace.startApplCall(Test.java:912) * </pre> * * @param ex the throwable object to strip stack trace. * @param className strip stackTrace up to classname. * @param methodName strip stackTrace up to methodName. */ public static void stripStackTrace(Throwable ex, String className, String methodName) { boolean found = false; StackTraceElement[] ste = ex.getStackTrace(); List<StackTraceElement> stList = asList(ste); for (int i = stList.size() - 1; i >= 0; i--) { StackTraceElement traceList = stList.get(i); String traceClassName = traceList.getClassName(); String traceMethodName = traceList.getMethodName(); /* only if classname and method are equals with StackTraceElement */ if (!(traceClassName.equals(className) && traceMethodName.equals(methodName))) stList.remove(i); else { found = true; break; } } /* replace only if provided classname and method was found the the stackTrace */ if (found) ex.setStackTrace(stList.toArray(new StackTraceElement[stList.size()])); /* recursive call on all causes */ Throwable cause = ex.getCause(); if (cause != null) stripStackTrace(cause, className, methodName); }
From source file:test.jamocha.languages.clips.SystemTest.java
/** * Get the calling method name. <br /> Utility function * * @return method name/*from www . j a v a2 s. c o m*/ */ public static String getMethodName() { // index 0 is getStackTrace, index 1 is getMethodName, index 2 is invoking method final StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[2]; return stackTraceElement.getClassName() + "::" + stackTraceElement.getMethodName(); }
From source file:sf.net.experimaestro.utils.JSUtils.java
/** * @see org.mozilla.javascript.RhinoException#getScriptStack() * @param ex/*from w w w . jav a 2s . c o m*/ * @return A stack */ static public ScriptStackElement[] getScriptStackTrace(Throwable ex) { List<ScriptStackElement> list = new ArrayList<>(); ScriptStackElement[][] interpreterStack = null; int interpreterStackIndex = 0; StackTraceElement[] stack = ex.getStackTrace(); // Pattern to recover function name from java method name - // see Codegen.getBodyMethodName() // kudos to Marc Guillemot for coming up with this Pattern pattern = Pattern.compile("_c_(.*)_\\d+"); for (StackTraceElement e : stack) { String fileName = e.getFileName(); if (e.getMethodName().startsWith("_c_") && e.getLineNumber() > -1 && fileName != null && !fileName.endsWith(".java")) { String methodName = e.getMethodName(); Matcher match = pattern.matcher(methodName); // the method representing the main script is always "_c_script_0" - // at least we hope so methodName = !"_c_script_0".equals(methodName) && match.find() ? match.group(1) : null; list.add(new ScriptStackElement(fileName, methodName, e.getLineNumber())); } else if ("org.mozilla.javascript.Interpreter".equals(e.getClassName()) && "interpretLoop".equals(e.getMethodName()) && interpreterStack != null && interpreterStack.length > interpreterStackIndex) { for (ScriptStackElement elem : interpreterStack[interpreterStackIndex++]) { list.add(elem); } } } return list.toArray(new ScriptStackElement[list.size()]); }
From source file:com.simiacryptus.util.io.MarkdownNotebookOutput.java
/** * Get markdown notebook output./*from ww w . j av a 2s.c o m*/ * * @param source the source * @return the markdown notebook output */ public static MarkdownNotebookOutput get(Object source) { try { StackTraceElement callingFrame = Thread.currentThread().getStackTrace()[2]; String className = null == source ? callingFrame.getClassName() : source.getClass().getCanonicalName(); String methodName = callingFrame.getMethodName(); String fileName = methodName + ".md"; File path = new File(Util.mkString(File.separator, "reports", className.replaceAll("\\.", "/").replaceAll("\\$", "/"), fileName)); path.getParentFile().mkdirs(); return new MarkdownNotebookOutput(path, methodName); } catch (FileNotFoundException e) { throw new RuntimeException(e); } }