1 17 package org.apache.log.util; 18 19 import java.io.PrintWriter ; 20 import java.io.StringWriter ; 21 22 30 public final class StackIntrospector 31 { 32 37 private static final class CallStack 38 extends SecurityManager 39 { 40 47 public Class [] get() 48 { 49 return getClassContext(); 50 } 51 } 52 53 private static CallStack c_callStack; 55 56 60 private StackIntrospector() 61 { 62 } 63 64 71 private static synchronized CallStack getCallStack() 72 throws SecurityException 73 { 74 if( null == c_callStack ) 75 { 76 c_callStack = new CallStack(); 78 } 79 80 return c_callStack; 81 } 82 83 92 public static final Class getCallerClass( final Class clazz ) 93 throws SecurityException 94 { 95 return getCallerClass( clazz, 0 ); 96 } 97 98 108 public static final Class getCallerClass( final Class clazz, int stackDepthOffset ) 109 { 110 final Class [] stack = getCallStack().get(); 111 112 for( int i = stack.length - 1; i >= 0; i-- ) 114 { 115 if( clazz.isAssignableFrom( stack[ i ] ) ) 116 { 117 return stack[ i + 1 + stackDepthOffset ]; 119 } 120 } 121 122 return null; 124 } 125 126 134 public static final String getCallerMethod( final Class clazz ) 135 { 136 final String className = clazz.getName(); 137 138 final StringWriter sw = new StringWriter (); 140 final Throwable throwable = new Throwable (); 141 throwable.printStackTrace( new PrintWriter ( sw, true ) ); 142 final StringBuffer buffer = sw.getBuffer(); 143 144 final StringBuffer line = new StringBuffer (); 146 final int length = buffer.length(); 147 148 boolean found = false; 150 int state = 0; 151 152 for( int i = 0; i < length; i++ ) 154 { 155 final char ch = buffer.charAt( i ); 156 157 switch( state ) 158 { 159 case 0: 160 if( '\n' == ch ) 162 { 163 state = 1; 164 } 165 break; 166 167 case 1: 168 if( 't' == ch ) 170 { 171 state = 2; 172 } 173 break; 174 175 case 2: 176 line.setLength( 0 ); 178 state = 3; 179 break; 180 181 case 3: 182 if( '\n' != ch ) 184 { 185 line.append( ch ); 186 } 187 else 188 { 189 final String method = line.toString(); 192 193 final boolean match = method.startsWith( className ); 195 if( !found && match ) 196 { 197 found = true; 200 } 201 else if( found && !match ) 202 { 203 return method; 205 } 206 207 state = 1; 209 } 210 } 211 } 212 213 return ""; 214 } 215 216 229 public static final String getRecentStack( final Class clazz, int entries ) 230 { 231 final String className = clazz.getName(); 232 233 final StringWriter sw = new StringWriter (); 235 final Throwable throwable = new Throwable (); 236 throwable.printStackTrace( new PrintWriter ( sw, true ) ); 237 final StringBuffer buffer = sw.getBuffer(); 238 239 final StringBuffer line = new StringBuffer (); 241 final StringBuffer stack = new StringBuffer (); 242 final int length = buffer.length(); 243 244 boolean found = false; 246 int state = 0; 247 248 for( int i = 0; i < length; i++ ) 250 { 251 final char ch = buffer.charAt( i ); 252 253 switch( state ) 254 { 255 case 0: 256 if( '\n' == ch ) 258 { 259 state = 1; 260 } 261 break; 262 263 case 1: 264 if( 't' == ch ) 266 { 267 state = 2; 268 } 269 break; 270 271 case 2: 272 line.setLength( 0 ); 274 state = 3; 275 break; 276 277 case 3: 278 if( '\n' != ch ) 280 { 281 line.append( ch ); 282 } 283 else 284 { 285 final String method = line.toString(); 288 289 final boolean match = method.startsWith( className ); 291 if( !found && match ) 292 { 293 found = true; 296 } 297 else if( found && !match ) 298 { 299 stack.append( method ); 301 entries--; 302 if( entries == 0 ) 303 { 304 return stack.toString(); 305 } 306 stack.append( "\n" ); 307 } 308 309 state = 1; 311 } 312 } 313 } 314 315 return ""; 316 } 317 } 318 319 | Popular Tags |