1 package org.feistymeow.utility;
\r
4 * Some helper methods that retrieve the function and class names where code is executing.
\r
5 * This can be helpful for doing very specific logging, and will work even if the log4j
\r
6 * settings have turned off the function names and such (given that you really want to
\r
7 * see those in a log entry).
\r
9 * @author Chris Koeritz
\r
11 public class Naming {
\r
13 // returns the name of the function invoking this method.
\r
14 public static String thisFunctionName() {
\r
15 Throwable ex = new Throwable();
\r
16 StackTraceElement[] trace = ex.getStackTrace();
\r
17 // StackTrace trace = new StackTrace(0, true);
\r
18 String meth = trace[0].getMethodName(); // this should be *this*
\r
20 for (int i = 1; i < trace.length; i++) {
\r
21 if ((trace[i].getMethodName() != meth)
\r
22 && (!trace[i].getMethodName().contains("formatted_names"))) {
\r
23 // we've gone back far enough.
\r
24 return trace[i].getMethodName();
\r
30 // provides a method to get the current class name and function name without
\r
31 // needing to embed a lot of code into individual functions.
\r
32 public static String thisClassName() {
\r
33 Throwable ex = new Throwable();
\r
34 StackTraceElement[] trace = ex.getStackTrace();
\r
35 // StackTrace trace = new StackTrace(0, true);
\r
37 String meth = trace[0].getMethodName(); // this should be *this*
\r
39 for (int i = 1; i < trace.length; i++) {
\r
40 String currClass = trace[i].getClassName();
\r
41 if ((trace[i].getMethodName() != meth)
\r
42 && (!trace[i].getMethodName().contains("formatted_names"))) {
\r
43 // we've gone back far enough.
\r
44 String simpleClassName = extractSimpleClassName(currClass);
\r
45 return simpleClassName;
\r
51 public String thisPackageName() {
\r
52 Throwable ex = new Throwable();
\r
53 StackTraceElement[] trace = ex.getStackTrace();
\r
54 // StackTrace trace = new StackTrace(0, true);
\r
56 String meth = trace[0].getMethodName(); // this should be *this*
\r
58 for (int i = 1; i < trace.length; i++) {
\r
59 String currClass = trace[i].getClassName();
\r
60 if ((trace[i].getMethodName() != meth)
\r
61 && (!trace[i].getMethodName().contains("formatted_names"))) {
\r
62 // we've gone back far enough.
\r
63 String packageName = extractPackageName(currClass);
\r
70 public static String extractPackageName(String fullClassName) {
\r
71 if ((null == fullClassName) || ("".equals(fullClassName)))
\r
74 // The package name is everything preceding the last dot.
\r
75 // Is there a dot in the name?
\r
76 int lastDot = fullClassName.lastIndexOf('.');
\r
78 // Note that by fiat, I declare that any class name that has been
\r
79 // passed in which starts with a dot doesn't have a package name.
\r
83 // Otherwise, extract the package name.
\r
84 return fullClassName.substring(0, lastDot);
\r
87 public static String extractSimpleClassName(String fullClassName) {
\r
88 if ((null == fullClassName) || ("".equals(fullClassName)))
\r
91 // The simple class name is everything after the last dot.
\r
92 // If there's no dot then the whole thing is the class name.
\r
93 int lastDot = fullClassName.lastIndexOf('.');
\r
95 return fullClassName;
\r
97 // Otherwise, extract the class name.
\r
98 return fullClassName.substring(++lastDot);
\r
101 // returns a nicely formatted string containing the class and function name.
\r
102 // the string also contains a colon and space on the end so other text
\r
103 // can be concatenated right up against it while still being readable.
\r
104 public static String formatted_names() {
\r
105 String class_name = thisClassName();
\r
106 String function_name = thisFunctionName();
\r
107 return class_name + "." + function_name + ": ";
\r