updated code for kona libs, not quite working yet.
[feisty_meow.git] / kona / src / org / feistymeow / utility / Naming.java
1 package org.feistymeow.utility;\r
2 \r
3 /*\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
8  * \r
9  * @author Chris Koeritz\r
10  */\r
11 public class Naming {\r
12 \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
19                 // function.\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
25                         }\r
26                 }\r
27                 return "unknown";\r
28         }\r
29 \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
36 \r
37                 String meth = trace[0].getMethodName(); // this should be *this*\r
38                 // function.\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
46                         }\r
47                 }\r
48                 return "unknown";\r
49         }\r
50 \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
55 \r
56                 String meth = trace[0].getMethodName(); // this should be *this*\r
57                 // function.\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
64                                 return packageName;\r
65                         }\r
66                 }\r
67                 return "unknown";\r
68         }\r
69 \r
70         public static String extractPackageName(String fullClassName) {\r
71                 if ((null == fullClassName) || ("".equals(fullClassName)))\r
72                         return "";\r
73 \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
77 \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
80                 if (0 >= lastDot)\r
81                         return "";\r
82 \r
83                 // Otherwise, extract the package name.\r
84                 return fullClassName.substring(0, lastDot);\r
85         }\r
86 \r
87         public static String extractSimpleClassName(String fullClassName) {\r
88                 if ((null == fullClassName) || ("".equals(fullClassName)))\r
89                         return "";\r
90 \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
94                 if (0 > lastDot)\r
95                         return fullClassName;\r
96 \r
97                 // Otherwise, extract the class name.\r
98                 return fullClassName.substring(++lastDot);\r
99         }\r
100 \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
108         }\r
109 };\r