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