Example usage for java.lang StackTraceElement getClassName

List of usage examples for java.lang StackTraceElement getClassName

Introduction

In this page you can find the example usage for java.lang StackTraceElement getClassName.

Prototype

public String getClassName() 

Source Link

Document

Returns the fully qualified name of the class containing the execution point represented by this stack trace element.

Usage

From source file:com.evolveum.midpoint.util.logging.LoggingUtils.java

public static String dumpStackTrace(Class... classesToSkip) {
    StackTraceElement[] fullStack = Thread.currentThread().getStackTrace();
    String immediateClass = null;
    String immediateMethod = null;
    boolean firstFrameLogged = false;
    StringBuilder sb = new StringBuilder();
    OUTER: for (StackTraceElement stackElement : fullStack) {
        if (!firstFrameLogged) {
            if (stackElement.getClassName().equals(Thread.class.getName())) {
                // skip call to thread.getStackTrace();
                continue;
            }/*  ww  w  . j  a v  a 2  s  .co m*/
            if (classesToSkip != null) {
                for (Class classToSkip : classesToSkip) {
                    if (stackElement.getClassName().equals(classToSkip.getName())) {
                        continue OUTER;
                    }
                }
            }
        }
        firstFrameLogged = true;

        sb.append(stackElement.toString());
        sb.append("\n");
    }
    return sb.toString();
}

From source file:com.ikanow.aleph2.logging.utils.Log4JUtils.java

public static String getLog4JMessage(final JsonNode logObject, final Level level, final StackTraceElement stack,
        final String date_field, final Map<String, Object> map, final String hostname) {
    StringBuilder sb = new StringBuilder();
    final String c = stack.getClassName().substring(stack.getClassName().lastIndexOf(".") + 1);
    final String l = Integer.toString(stack.getLineNumber());
    final String thread = Thread.currentThread().getName().isEmpty()
            ? Long.toString(Thread.currentThread().getId())
            : Thread.currentThread().getName();
    sb.append(String.format(message_format, date_format.format(new Date(logObject.get(date_field).asLong())),
            thread, level.name(), c, l, logObject.get("message").asText()));
    sb.append(String.format(field_format, "bucket", logObject.get("bucket").asText()));
    sb.append(String.format(field_format, "hostname", hostname));
    sb.append(String.format(field_format, "subsystem", logObject.get("subsystem").asText()));
    sb.append(String.format(field_format, "command", logObject.get("command").asText()));
    Optional.ofNullable(map).orElse(Collections.emptyMap()).entrySet().stream()
            .forEach(e -> sb.append(String.format(field_format, e.getKey(), e.getValue())));
    return sb.toString();
}

From source file:org.gradle.util.DeprecationLogger.java

private static void logTraceIfNecessary() {
    if (isTraceLoggingEnabled()) {
        StackTraceElement[] stack = StackTraceUtils.sanitize(new Exception()).getStackTrace();
        for (StackTraceElement frame : stack) {
            if (!frame.getClassName().startsWith(DeprecationLogger.class.getName())) {
                LOGGER.warn("    {}", frame.toString());
            }/* w w  w. j  av a 2 s.  com*/
        }
    }
}

From source file:com.dmitrymalkovich.android.githubanalytics.Utils.java

public static void openFeedback(Activity activity) {
    try {//from   w w w .j  av  a2s . c o  m
        throw new IOException();
    } catch (IOException e) {
        ApplicationErrorReport report = new ApplicationErrorReport();
        report.packageName = report.processName = activity.getApplication().getPackageName();
        report.time = System.currentTimeMillis();
        report.type = ApplicationErrorReport.TYPE_CRASH;
        report.systemApp = false;
        ApplicationErrorReport.CrashInfo crash = new ApplicationErrorReport.CrashInfo();
        crash.exceptionClassName = e.getClass().getSimpleName();
        crash.exceptionMessage = e.getMessage();
        StringWriter writer = new StringWriter();
        PrintWriter printer = new PrintWriter(writer);
        e.printStackTrace(printer);
        crash.stackTrace = writer.toString();
        StackTraceElement stack = e.getStackTrace()[0];
        crash.throwClassName = stack.getClassName();
        crash.throwFileName = stack.getFileName();
        crash.throwLineNumber = stack.getLineNumber();
        crash.throwMethodName = stack.getMethodName();
        report.crashInfo = crash;
        Intent intent = new Intent(Intent.ACTION_APP_ERROR);
        intent.putExtra(Intent.EXTRA_BUG_REPORT, report);
        activity.startActivity(intent);
    }
}

From source file:com.squarespace.gibson.GibsonUtils.java

private static void append(MessageDigest md, StackTraceElement element) {
    if (element != null) {
        append(md, element.getClassName());
        append(md, element.getMethodName());
        append(md, element.getFileName());
        append(md, element.getLineNumber());
    }//from   w w  w  .  ja va  2  s  .c om
}

From source file:Main.java

public static boolean invokeIn(StackTraceElement[] stackTraceElements, Class<?> cla, String methodName) {
    if (stackTraceElements == null || stackTraceElements.length == 0) {
        return false;
    }//  ww  w. j  av  a  2s . c om

    String targetClassName = cla.getName();
    StackTraceElement element;
    String elementClassName;
    String elementMethodName;
    for (StackTraceElement stackTraceElement : stackTraceElements) {
        element = stackTraceElement;

        elementClassName = element.getClassName();
        elementMethodName = element.getMethodName();
        if (targetClassName.equals(elementClassName) && methodName.equals(elementMethodName)) {
            return true;
        }
    }

    return false;
}

From source file:org.wso2.carbon.registry.core.pagination.PaginationUtils.java

/**
 * Check the resource get method is paginated or not.
 * @param annotation method annotation  name
 * @return true if the method id paginated.
 *//*w ww.ja v a2  s  .  co m*/
public static boolean isPaginationAnnotationFound(String annotation) {
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    for (StackTraceElement element : stackTrace) {
        try {
            Method[] methods = Class.forName(element.getClassName()).getMethods();
            for (Method method : methods) {
                if (method.getName().equals(element.getMethodName())
                        && method.isAnnotationPresent(Paginate.class)
                        && method.getAnnotation(Paginate.class).value().equals(annotation)) {
                    return true;
                }
            }
        } catch (ClassNotFoundException ignore) {
        }
    }
    return false;
}

From source file:acromusashi.stream.resource.ResourceResolver.java

/**
 * Return caller class calling this class's method.
 *
 * @return Caller class/*from  www .  j av a  2s. co  m*/
 */
public static Class<?> resolveCaller() {
    Class<?> callerClass = null;

    StackTraceElement[] stackTraces = Thread.currentThread().getStackTrace();
    int stackSize = stackTraces.length;

    // StackTrace is below. So check from 3rd element.
    // 1st:java.lang.Thread
    // 2nd:ResourceResolver#resolveCaller(this method)
    for (int stackIndex = 2; stackIndex < stackSize; stackIndex++) {
        StackTraceElement stackTrace = stackTraces[stackIndex];
        String callerClassName = stackTrace.getClassName();
        if (StringUtils.equals(ResourceResolver.class.getName(), callerClassName) == false) {
            try {
                callerClass = Class.forName(callerClassName);
                break;
            } catch (ClassNotFoundException ex) {
                return null;
            }
        }
    }

    return callerClass;
}

From source file:com.cmsz.cloudplatform.utils.StringUtils.java

public static String getExceptionStackInfo(Throwable e) {
    StringBuffer sb = new StringBuffer();

    sb.append(e.toString()).append("\n");
    StackTraceElement[] elemnents = e.getStackTrace();
    for (StackTraceElement element : elemnents) {
        sb.append(element.getClassName()).append(".");
        sb.append(element.getMethodName()).append("(");
        sb.append(element.getFileName()).append(":");
        sb.append(element.getLineNumber()).append(")");
        sb.append("\n");
    }//  w  ww  .jav a2s  . com

    return sb.toString();
}

From source file:org.kuali.kfs.module.tem.web.JstlFunctions.java

/**
 * Get the stack trace from a <code>{@link Throwable}</code> and create a log message from it for tracing purposes
 *
 * @param thrownObj/*w w w .j a  va2s  .  c  om*/
 * @return String log message
 */
private static String buildTraceMessage(Throwable thrownObj) {
    StackTraceElement stackTraceElement = thrownObj.getStackTrace()[0];
    return new StringBuilder(stackTraceElement.getClassName()).append("#")
            .append(stackTraceElement.getMethodName()).append(":").append(stackTraceElement.getLineNumber())
            .append(" ").append(thrownObj.getClass().getSimpleName()).append("\n")
            .append(thrownObj.getMessage()).toString();
}