public String getProperty ( String key ) { // Try to get the callers class name and method name by examing the stack. String className = null ; String methodName = null ; // Java 1.4 onwards only. /*try { throw new Exception(); } catch (Exception e) { StackTraceElement[] stack = e.getStackTrace(); // Check that the stack trace contains at least two elements, one for this method and one for the caller. if (stack.length >= 2) { className = stack[1].getClassName(); methodName = stack[1].getMethodName(); } }*/ // Java 1.5 onwards only. StackTraceElement [ ] stack = Thread . currentThread ( ) . getStackTrace ( ) ; // Check that the stack trace contains at least two elements, one for this method and one for the caller. if ( stack . length >= 2 ) { className = stack [ 1 ] . getClassName ( ) ; methodName = stack [ 1 ] . getMethodName ( ) ; } // Java 1.3 and before? Not sure, some horrible thing that parses the text spat out by printStackTrace? return getProperty ( className , methodName , key ) ; }
Looks up a property value relative to the environment callers class and method . The default environment will be checked for a matching property if defaults are being used . In order to work out the callers class and method this method throws an exception and then searches one level up its stack frames .