| 1 21 package com.db4o.test.lib; 22 23 import com.db4o.foundation.*; 24 25 30 public class StackRecorder { 31 32 34 private static final int EXCLUDEDEPTH = 2; 35 36 private static StackTrace _curTrace; 37 38 private static Collection4 _traces=new Collection4(); 39 40 private static Collection4 _includes; 41 42 private static Collection4 _excludes; 43 44 45 public static void addCase(String caseInfo) { 46 _curTrace=newTrace(caseInfo); 47 } 48 49 public static void addInclude(String classNameSubstring){ 50 if(_includes == null){ 51 _includes = new Collection4(); 52 } 53 _includes.add(classNameSubstring); 54 } 55 56 public static void addExclude(String classNameSubstring){ 57 if(_excludes == null){ 58 _excludes = new Collection4(); 59 } 60 _excludes.add(classNameSubstring); 61 } 62 63 64 public static void record() { 65 StackTrace trace=newTrace(null); 66 if(!_traces.contains(trace)) { 67 _traces.add(trace); 68 } 69 } 70 71 public static void record(Object obj){ 72 if(obj == null){ 73 return; 74 } 75 Class clazz = (obj instanceof Class ) ? (Class )obj : obj.getClass(); 76 if(recordClass(clazz)){ 77 record(); 78 } 79 } 80 81 private static boolean recordClass(Class clazz){ 82 83 String className = clazz.getName(); 84 85 if(_excludes != null){ 86 Iterator4 i = _excludes.iterator(); 87 while(i.moveNext()){ 88 String name = (String )i.current(); 89 if(className.indexOf(name) >= 0){ 90 return false; 91 } 92 } 93 } 94 95 boolean onNotFound = true; 96 97 if(_includes != null){ 98 onNotFound = false; 99 Iterator4 i = _includes.iterator(); 100 while(i.moveNext()){ 101 String name = (String )i.current(); 102 if(className.indexOf(name) >= 0){ 103 onNotFound = true; 104 } 105 } 106 } 107 108 109 Class claxx = clazz.getSuperclass(); 110 if(claxx != null){ 111 if ( recordClass(claxx)){ 112 return true; 113 } 114 } 115 116 return onNotFound; 117 } 118 119 public static void logAll() { 120 Iterator4 iter=_traces.iterator(); 121 while(iter.moveNext()) { 122 System.out.println(iter.current()); 123 if(iter.moveNext()) { 124 System.out.println("\n---\n"); 125 } 126 } 127 } 128 129 private static StackTrace newTrace(String caseInfo) { 130 return new StackTrace(EXCLUDEDEPTH,caseInfo,_curTrace); 131 } 132 133 public static void main(String [] args) { 134 for(int i=0;i<2;i++) { 135 for(int j=0;j<2;j++) { 136 StackRecorder.addCase("main"+i); 137 foo(); 138 } 139 } 140 for(int i=0;i<2;i++) { 141 for(int j=0;j<2;j++) { 142 StackRecorder.addCase("mainX"+i); 143 foo(); 144 } 145 } 146 StackRecorder.logAll(); 147 } 148 149 public static void foo() { 150 for(int i=0;i<2;i++) { 151 for(int j=0;j<2;j++) { 152 StackRecorder.addCase("foo"+i); 153 bar(); 154 } 155 } 156 } 157 158 public static void bar() { 159 for(int i=0;i<2;i++) { 160 StackRecorder.record(); 161 } 162 } 163 } 164 | Popular Tags |