List of usage examples for java.lang StackTraceElement getClassName
public String getClassName()
From source file:org.acmsl.commons.utils.ToStringUtils.java
/** * Checks whether given stack trace contains recursive calls to given class' method. * @param <T> the class type.// w ww. j a va 2s .co m * @param stackTrace the stack trace. * @param clazz the class. * @param methodName the method name. * @return {@code true} in such case. */ protected <T> boolean stackTraceContainsRecursiveCalls(@NotNull final StackTraceElement[] stackTrace, @NotNull final Class<T> clazz, @NotNull final String methodName) { boolean result = false; int count = 0; for (@Nullable final StackTraceElement stackEntry : stackTrace) { if (stackEntry != null) { @Nullable final String className = stackEntry.getClassName(); if ((className != null) && (className.equals(clazz.getName())) && (methodName.equals(stackEntry.getMethodName()))) { count++; } if (count == 2) { result = true; break; } } } return result; }
From source file:org.sakaiproject.status.StatusServlet.java
protected void reportThreadDetails(HttpServletResponse response) throws Exception { PrintWriter pw = response.getWriter(); for (Thread thread : findAllThreads()) { if (thread != null) { String threadLocation = ""; try { StackTraceElement ste = thread.getStackTrace()[0]; StackTraceElement ste2 = thread.getStackTrace()[1]; threadLocation = ste.getClassName() + "." + ste.getMethodName() + "()," + ste.getFileName() + ":" + ste.getLineNumber() + "," + ste2.getClassName() + "." + ste2.getMethodName() + "()," + ste2.getFileName() + ":" + ste2.getLineNumber(); } catch (Exception e) { threadLocation = "?,?,?,?"; }//from w ww . j ava2 s.c om pw.print(thread.getThreadGroup().getName() + "," + thread.getId() + "," + thread.getName() + "," + thread.getPriority() + "," + thread.getState().name() + "," + (thread.isAlive() ? "" : "notalive") + "," + (thread.isDaemon() ? "daemon" : "") + "," + (thread.isInterrupted() ? "interrupted" : "") + "," + threadLocation + "\n"); } } }
From source file:de.micromata.genome.tpsb.CommonTestBuilder.java
protected T failImpl(AssertionFailedException ex) { ex.setTestBuilder(this); StackTraceElement el = AssertUtils.getStackAbove(this.getClass()); if (el != null) { String line = StringUtils.trim(AssertUtils.getCodeLine(el)); ex.setClassName(el.getClassName()); ex.setMethodName(el.getMethodName()); ex.setLineNo(el.getLineNumber()); ex.setCodeLine(line);/*w w w .j a va2 s.c o m*/ } throw ex; }
From source file:de.micromata.genome.tpsb.CommonTestBuilder.java
protected T failImpl(Exception ex) { AssertionFailedException nex = new AssertionFailedException(ex); nex.setTestBuilder(this); StackTraceElement el = AssertUtils.getStackAbove(this.getClass()); if (el != null) { String line = StringUtils.trim(AssertUtils.getCodeLine(el)); nex.setClassName(el.getClassName()); nex.setMethodName(el.getMethodName()); nex.setLineNo(el.getLineNumber()); nex.setCodeLine(line);//from w ww.jav a 2 s . co m } throw nex; }
From source file:com.gargoylesoftware.htmlunit.WebTestCase.java
/** * Finds from the call stack the active running JUnit test case * @return the test case method/*from w ww .j a v a 2 s . co m*/ * @throws RuntimeException if no method could be found */ private Method findRunningJUnitTestMethod() { final Class<?> cl = getClass(); final Class<?>[] args = new Class[] {}; // search the initial junit test final Throwable t = new Exception(); for (int i = t.getStackTrace().length - 1; i >= 0; i--) { final StackTraceElement element = t.getStackTrace()[i]; if (element.getClassName().equals(cl.getName())) { try { final Method m = cl.getMethod(element.getMethodName(), args); if (isPublicTestMethod(m)) { return m; } } catch (final Exception e) { // can't access, ignore it } } } throw new RuntimeException("No JUnit test case method found in call stack"); }
From source file:org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.Launcher.java
/** * /*w ww.j a va 2 s . c om*/ * @param stackTraceLine An array of strings that represent {@link Throwable#printStackTrace() printStackTrace} * output, split by newline * @return An exception object whose string representation of printStackTrace is the input stackTrace * @throws Exception */ private Throwable getExceptionFromStrings(String[] stackTraceLines, int startingLineNum) throws Exception { /* * parse the array of string and throw the appropriate exception * first: from the line startingLineNum extract the exception name extract the message if any * fourth: create the appropriate exception and return it * An example of the stack trace: org.apache.pig.backend.executionengine.ExecException: ERROR 1075: Received a bytearray from the UDF. Cannot determine how to convert the bytearray to int. at org.apache.pig.backend.hadoop.executionengine.physicalLayer.expressionOperators.POCast.getNext(POCast.java:152) at org.apache.pig.backend.hadoop.executionengine.physicalLayer.expressionOperators.LessThanExpr.getNext(LessThanExpr.java:85) at org.apache.pig.backend.hadoop.executionengine.physicalLayer.relationalOperators.POFilter.getNext(POFilter.java:148) at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigMapBase.runPipeline(PigMapBase.java:184) at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigMapBase.map(PigMapBase.java:174) at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigMapOnly$Map.map(PigMapOnly.java:65) at org.apache.hadoop.mapred.MapRunner.run(MapRunner.java:47) at org.apache.hadoop.mapred.MapTask.run(MapTask.java:227) at org.apache.hadoop.mapred.TaskTracker$Child.main(TaskTracker.java:2207) */ int prevStartingLineNum = startingLineNum; if (stackTraceLines.length > 0 && startingLineNum < (stackTraceLines.length - 1)) { //the regex for matching the exception class name; note the use of the $ for matching nested classes String exceptionNameDelimiter = "(\\w+(\\$\\w+)?\\.)+\\w+"; Pattern exceptionNamePattern = Pattern.compile(exceptionNameDelimiter); //from the first line extract the exception name and the exception message Matcher exceptionNameMatcher = exceptionNamePattern.matcher(stackTraceLines[startingLineNum]); String exceptionName = null; String exceptionMessage = null; if (exceptionNameMatcher.find()) { exceptionName = exceptionNameMatcher.group(); /* * note that the substring is from end + 2 * the regex matcher ends at one position beyond the match * in this case it will end at colon (:) * the exception message will have a preceding space (after the colon (:)) */ if (exceptionName.contains(OOM_ERR)) { outOfMemory = true; } if (stackTraceLines[startingLineNum].length() > exceptionNameMatcher.end()) { exceptionMessage = stackTraceLines[startingLineNum].substring(exceptionNameMatcher.end() + 2); } ++startingLineNum; } //the exceptionName should not be null if (exceptionName != null) { ArrayList<StackTraceElement> stackTraceElements = new ArrayList<StackTraceElement>(); //Create stack trace elements for the remaining lines String stackElementRegex = "\\s+at\\s+(\\w+(\\$\\w+)?\\.)+(\\<)?\\w+(\\>)?"; Pattern stackElementPattern = Pattern.compile(stackElementRegex); String pigExceptionRegex = "org\\.apache\\.pig\\."; Pattern pigExceptionPattern = Pattern.compile(pigExceptionRegex); String moreElementRegex = "\\s+\\.\\.\\.\\s+\\d+\\s+more"; Pattern moreElementPattern = Pattern.compile(moreElementRegex); String pigPackageRegex = "org.apache.pig"; int lineNum = startingLineNum; for (; lineNum < (stackTraceLines.length - 1); ++lineNum) { Matcher stackElementMatcher = stackElementPattern.matcher(stackTraceLines[lineNum]); if (stackElementMatcher.find()) { StackTraceElement ste = getStackTraceElement(stackTraceLines[lineNum]); stackTraceElements.add(ste); String className = ste.getClassName(); Matcher pigExceptionMatcher = pigExceptionPattern.matcher(className); if (pigExceptionMatcher.find()) { pigException = true; } } else { Matcher moreElementMatcher = moreElementPattern.matcher(stackTraceLines[lineNum]); if (moreElementMatcher.find()) { ++lineNum; } break; } } startingLineNum = lineNum; //create the appropriate exception; setup the stack trace and message Object object = PigContext.instantiateFuncFromSpec(exceptionName); if (object instanceof PigException) { //extract the error code and message the regex for matching the custom format of ERROR <ERROR CODE>: String errMessageRegex = "ERROR\\s+\\d+:"; Pattern errMessagePattern = Pattern.compile(errMessageRegex); Matcher errMessageMatcher = errMessagePattern.matcher(exceptionMessage); if (errMessageMatcher.find()) { String errMessageStub = errMessageMatcher.group(); /* * extract the actual exception message sans the ERROR <ERROR CODE>: * again note that the matcher ends at the space following the colon (:) * the exception message appears after the space and hence the end + 1 */ exceptionMessage = exceptionMessage.substring(errMessageMatcher.end() + 1); //the regex to match the error code wich is a string of numerals String errCodeRegex = "\\d+"; Pattern errCodePattern = Pattern.compile(errCodeRegex); Matcher errCodeMatcher = errCodePattern.matcher(errMessageStub); String code = null; if (errCodeMatcher.find()) { code = errCodeMatcher.group(); } //could receive a number format exception here but it will be propagated up the stack int errCode; if (code != null) errCode = Integer.parseInt(code); else errCode = 2998; //create the exception with the message and then set the error code and error source FuncSpec funcSpec = new FuncSpec(exceptionName, exceptionMessage); object = PigContext.instantiateFuncFromSpec(funcSpec); ((PigException) object).setErrorCode(errCode); ((PigException) object).setErrorSource(PigException.determineErrorSource(errCode)); } else { //else for if(errMessageMatcher.find()) /* * did not find the error code which means that the PigException or its * subclass is not returning the error code * highly unlikely: should never be here */ FuncSpec funcSpec = new FuncSpec(exceptionName, exceptionMessage); object = PigContext.instantiateFuncFromSpec(funcSpec); ((PigException) object).setErrorCode(2997);//generic error code ((PigException) object).setErrorSource(PigException.BUG); } } else { //else for if(object instanceof PigException) //its not PigException; create the exception with the message object = PigContext.instantiateFuncFromSpec(new FuncSpec(exceptionName, exceptionMessage)); } StackTraceElement[] steArr = new StackTraceElement[stackTraceElements.size()]; ((Throwable) object).setStackTrace(stackTraceElements.toArray(steArr)); if (startingLineNum < (stackTraceLines.length - 1)) { Throwable e = getExceptionFromStrings(stackTraceLines, startingLineNum); ((Throwable) object).initCause(e); } return (Throwable) object; } else { //else for if(exceptionName != null) int errCode = 2055; String msg = "Did not find exception name to create exception from string: " + Arrays.toString(stackTraceLines); throw new ExecException(msg, errCode, PigException.BUG); } } else { //else for if(lines.length > 0) int errCode = 2056; String msg = "Cannot create exception from empty string."; throw new ExecException(msg, errCode, PigException.BUG); } }
From source file:be.agiv.security.demo.Main.java
private void showException(Exception e) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(e.getMessage()); stringBuffer.append("\n"); for (StackTraceElement stackTraceElement : e.getStackTrace()) { stringBuffer.append(stackTraceElement.getClassName()); stringBuffer.append("."); stringBuffer.append(stackTraceElement.getMethodName()); stringBuffer.append(":"); stringBuffer.append(stackTraceElement.getLineNumber()); stringBuffer.append("\n"); }//from ww w .j ava2 s .com Throwable cause = e.getCause(); while (null != cause) { stringBuffer.append("\n"); stringBuffer.append("Caused by: "); stringBuffer.append(cause.getMessage()); stringBuffer.append(" - "); stringBuffer.append(cause.getClass().getName()); stringBuffer.append("\n"); for (StackTraceElement stackTraceElement : e.getStackTrace()) { stringBuffer.append(stackTraceElement.getClassName()); stringBuffer.append("."); stringBuffer.append(stackTraceElement.getMethodName()); stringBuffer.append(":"); stringBuffer.append(stackTraceElement.getLineNumber()); stringBuffer.append("\n"); } cause = cause.getCause(); } JTextArea textArea = new JTextArea(stringBuffer.toString(), 10, 40); JScrollPane scrollPane = new JScrollPane(textArea); scrollPane.setAutoscrolls(true); JOptionPane.showMessageDialog(this, scrollPane, e.getClass().getSimpleName(), JOptionPane.ERROR_MESSAGE); }
From source file:org.apache.pig.backend.hadoop.executionengine.Launcher.java
/** * * @param stackTraceLines/* w w w . j a v a 2 s .c o m*/ * An array of strings that represent * {@link Throwable#printStackTrace() printStackTrace} output, * split by newline * @return An exception object whose string representation of * printStackTrace is the input stackTrace * @throws Exception */ private Throwable getExceptionFromStrings(String[] stackTraceLines, int startingLineNum) throws Exception { /* * parse the array of string and throw the appropriate exception first: * from the line startingLineNum extract the exception name extract the * message if any fourth: create the appropriate exception and return it * An example of the stack trace: * org.apache.pig.backend.executionengine.ExecException: ERROR 1075: * Received a bytearray from the UDF. Cannot determine how to convert * the bytearray to int. at * org.apache.pig.backend.hadoop.executionengine * .physicalLayer.expressionOperators.POCast.getNext(POCast.java:152) at * org.apache.pig.backend.hadoop.executionengine.physicalLayer. * expressionOperators.LessThanExpr.getNext(LessThanExpr.java:85) at * org.apache.pig.backend.hadoop.executionengine.physicalLayer. * relationalOperators.POFilter.getNext(POFilter.java:148) at * org.apache. * pig.backend.hadoop.executionengine.mapReduceLayer.PigMapBase * .runPipeline(PigMapBase.java:184) at * org.apache.pig.backend.hadoop.executionengine * .mapReduceLayer.PigMapBase.map(PigMapBase.java:174) at * org.apache.pig. * backend.hadoop.executionengine.mapReduceLayer.PigMapOnly$Map * .map(PigMapOnly.java:65) at * org.apache.hadoop.mapred.MapRunner.run(MapRunner.java:47) at * org.apache.hadoop.mapred.MapTask.run(MapTask.java:227) at * org.apache.hadoop * .mapred.TaskTracker$Child.main(TaskTracker.java:2207) */ if (stackTraceLines.length > 0 && startingLineNum < (stackTraceLines.length - 1)) { // the regex for matching the exception class name; note the use of // the $ for matching nested classes String exceptionNameDelimiter = "(\\w+(\\$\\w+)?\\.)+\\w+"; Pattern exceptionNamePattern = Pattern.compile(exceptionNameDelimiter); // from the first line extract the exception name and the exception // message Matcher exceptionNameMatcher = exceptionNamePattern.matcher(stackTraceLines[startingLineNum]); String exceptionName = null; String exceptionMessage = null; if (exceptionNameMatcher.find()) { exceptionName = exceptionNameMatcher.group(); /* * note that the substring is from end + 2 the regex matcher * ends at one position beyond the match in this case it will * end at colon (:) the exception message will have a preceding * space (after the colon (:)) */ if (exceptionName.contains(OOM_ERR)) { outOfMemory = true; } if (stackTraceLines[startingLineNum].length() > exceptionNameMatcher.end()) { exceptionMessage = stackTraceLines[startingLineNum].substring(exceptionNameMatcher.end() + 2); } ++startingLineNum; } // the exceptionName should not be null if (exceptionName != null) { ArrayList<StackTraceElement> stackTraceElements = Lists.newArrayList(); // Create stack trace elements for the remaining lines String stackElementRegex = "\\s+at\\s+(\\w+(\\$\\w+)?\\.)+(\\<)?\\w+(\\>)?"; Pattern stackElementPattern = Pattern.compile(stackElementRegex); String pigExceptionRegex = "org\\.apache\\.pig\\."; Pattern pigExceptionPattern = Pattern.compile(pigExceptionRegex); String moreElementRegex = "\\s+\\.\\.\\.\\s+\\d+\\s+more"; Pattern moreElementPattern = Pattern.compile(moreElementRegex); int lineNum = startingLineNum; for (; lineNum < (stackTraceLines.length - 1); ++lineNum) { Matcher stackElementMatcher = stackElementPattern.matcher(stackTraceLines[lineNum]); if (stackElementMatcher.find()) { StackTraceElement ste = getStackTraceElement(stackTraceLines[lineNum]); stackTraceElements.add(ste); String className = ste.getClassName(); Matcher pigExceptionMatcher = pigExceptionPattern.matcher(className); if (pigExceptionMatcher.find()) { pigException = true; } } else { Matcher moreElementMatcher = moreElementPattern.matcher(stackTraceLines[lineNum]); if (moreElementMatcher.find()) { ++lineNum; } break; } } startingLineNum = lineNum; // create the appropriate exception; setup the stack trace and // message Object object = PigContext.instantiateFuncFromSpec(exceptionName); if (object instanceof PigException) { // extract the error code and message the regex for matching // the custom format of ERROR <ERROR CODE>: String errMessageRegex = "ERROR\\s+\\d+:"; Pattern errMessagePattern = Pattern.compile(errMessageRegex); Matcher errMessageMatcher = errMessagePattern.matcher(exceptionMessage); if (errMessageMatcher.find()) { String errMessageStub = errMessageMatcher.group(); /* * extract the actual exception message sans the ERROR * <ERROR CODE>: again note that the matcher ends at the * space following the colon (:) the exception message * appears after the space and hence the end + 1 */ exceptionMessage = exceptionMessage.substring(errMessageMatcher.end() + 1); // the regex to match the error code wich is a string of // numerals String errCodeRegex = "\\d+"; Pattern errCodePattern = Pattern.compile(errCodeRegex); Matcher errCodeMatcher = errCodePattern.matcher(errMessageStub); String code = null; if (errCodeMatcher.find()) { code = errCodeMatcher.group(); } // could receive a number format exception here but it // will be propagated up the stack int errCode; if (code != null) errCode = Integer.parseInt(code); else errCode = 2998; // create the exception with the message and then set // the error code and error source FuncSpec funcSpec = new FuncSpec(exceptionName, exceptionMessage); object = PigContext.instantiateFuncFromSpec(funcSpec); ((PigException) object).setErrorCode(errCode); ((PigException) object).setErrorSource(PigException.determineErrorSource(errCode)); } else { // else for if(errMessageMatcher.find()) /* * did not find the error code which means that the * PigException or its subclass is not returning the * error code highly unlikely: should never be here */ FuncSpec funcSpec = new FuncSpec(exceptionName, exceptionMessage); object = PigContext.instantiateFuncFromSpec(funcSpec); ((PigException) object).setErrorCode(2997);// generic // error // code ((PigException) object).setErrorSource(PigException.BUG); } } else { // else for if(object instanceof PigException) // its not PigException; create the exception with the // message object = PigContext.instantiateFuncFromSpec(new FuncSpec(exceptionName, exceptionMessage)); } StackTraceElement[] steArr = new StackTraceElement[stackTraceElements.size()]; ((Throwable) object).setStackTrace(stackTraceElements.toArray(steArr)); if (startingLineNum < (stackTraceLines.length - 1)) { Throwable e = getExceptionFromStrings(stackTraceLines, startingLineNum); ((Throwable) object).initCause(e); } return (Throwable) object; } else { // else for if(exceptionName != null) int errCode = 2055; String msg = "Did not find exception name to create exception from string: " + Arrays.toString(stackTraceLines); throw new ExecException(msg, errCode, PigException.BUG); } } else { // else for if(lines.length > 0) int errCode = 2056; String msg = "Cannot create exception from empty string."; throw new ExecException(msg, errCode, PigException.BUG); } }
From source file:com.evolveum.icf.dummy.resource.DummyResource.java
private void traceOperation(String opName, long counter) { LOGGER.info("MONITOR dummy '{}' {} ({})", instanceName, opName, counter); if (LOGGER.isDebugEnabled()) { StackTraceElement[] fullStack = Thread.currentThread().getStackTrace(); String immediateClass = null; String immediateMethod = null; StringBuilder sb = new StringBuilder(); for (StackTraceElement stackElement : fullStack) { if (stackElement.getClassName().equals(DummyResource.class.getName()) || stackElement.getClassName().equals(Thread.class.getName())) { // skip our own calls continue; }//from w ww.j a v a2s . co m if (immediateClass == null) { immediateClass = stackElement.getClassName(); immediateMethod = stackElement.getMethodName(); } sb.append(stackElement.toString()); sb.append("\n"); } LOGGER.debug("MONITOR dummy '{}' {} ({}): {} {}", new Object[] { instanceName, opName, counter, immediateClass, immediateMethod }); LOGGER.trace("MONITOR dummy '{}' {} ({}):\n{}", new Object[] { instanceName, opName, counter, sb }); } }