Ignore:
Timestamp:
2016-08-25T23:21:10+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #13318 - Clean up program startup (parameters, logging) - patch by michael2402 - gsoc-core

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/bugreport/BugReport.java

    r10886 r10899  
    66import java.io.StringWriter;
    77import java.util.concurrent.CopyOnWriteArrayList;
     8import java.util.function.Predicate;
    89
    910import org.openstreetmap.josm.actions.ShowStatusReportAction;
     
    185186     */
    186187    public static String getCallingMethod(int offset) {
     188        String className = BugReport.class.getName();
     189        String methodName = "getCallingMethod";
     190        StackTraceElement found = getCallingMethod(offset, className, methodName::equals);
     191        if (found != null) {
     192            return found.getClassName().replaceFirst(".*\\.", "") + '#' + found.getMethodName();
     193        } else {
     194            return "?";
     195        }
     196    }
     197
     198    /**
     199     * Find the method that called the given method on the current stack trace.
     200     * @param offset
     201     *           How many methods to look back in the stack trace. 1 gives the method calling this method, 0 gives you getCallingMethod().
     202     * @param className The name of the class to search for
     203     * @param methodName The name of the method to search for
     204     * @return The class and method name or null if it is unknown.
     205     */
     206    public static StackTraceElement getCallingMethod(int offset, String className, Predicate<String> methodName) {
    187207        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    188         String className = BugReport.class.getName();
    189208        for (int i = 0; i < stackTrace.length - offset; i++) {
    190209            StackTraceElement element = stackTrace[i];
    191             if (className.equals(element.getClassName()) && "getCallingMethod".equals(element.getMethodName())) {
     210            if (className.equals(element.getClassName()) && methodName.test(element.getMethodName())) {
    192211                StackTraceElement toReturn = stackTrace[i + offset];
    193                 return toReturn.getClassName().replaceFirst(".*\\.", "") + '#' + toReturn.getMethodName();
     212                return toReturn;
    194213            }
    195214        }
    196         return "?";
     215        return null;
    197216    }
    198217
Note: See TracChangeset for help on using the changeset viewer.