Example usage for java.lang StackTraceElement getClass

List of usage examples for java.lang StackTraceElement getClass

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.codelanx.codelanxlib.listener.ListenerManager.java

/**
 * Returns {@code true} if the passed {@link Listener} has another Listener
 * of the same class type already registered for bukkit. This should not be
 * used with any listeners that are from an anonymous class, as this will
 * return {@code true} for any other anonymous classes as well
 * /*from w ww . jav  a 2  s . c o m*/
 * @since 0.1.0
 * @version 0.1.0
 * 
 * @param p The {@link Plugin} that registers this {@link Listener}
 * @param l The {@link Listener} to check
 * @return {@code true} if registered to bukkit
 */
public static boolean isRegisteredToBukkit(Plugin p, Listener l) {
    if (l.getClass().isAnonymousClass()) {
        StackTraceElement t = Reflections.getCaller();
        Logging.simple().here().print(Level.WARNING, "Passed an anonymous class from %s:%d",
                t.getClass().getName(), t.getLineNumber());
    }
    return HandlerList.getRegisteredListeners(p).stream()
            .anyMatch(r -> r.getListener().getClass() == l.getClass());
}

From source file:org.jboss.loom.actions.AbstractStatefulAction.java

private StackTraceElement getNonActionCallee(StackTraceElement[] stackTrace) {
    //return Thread.currentThread().getStackTrace()[4];
    // 0 - Thread.getStackTrace().
    // 1 - This method.
    // 2 - This constructor.
    // 3 - *Action constructor.
    // 4 - Whatever called new CliCommandAction.
    // Could be better, e.g. first non-constructor after 2.

    for (StackTraceElement elm : stackTrace) {
        if (IMigrationAction.class.isAssignableFrom(elm.getClass()))
            return elm;
    }/*from   w w w .j  av  a 2 s .c  o  m*/
    return stackTrace[4]; // Fallback; will not happen, unless we put main() to this class or so.
}