1 17 18 package org.objectweb.jac.aspects.tracing; 19 20 import org.objectweb.jac.core.*; 21 import java.util.*; 22 import org.objectweb.jac.core.rtti.ClassRepository; 23 24 27 28 public class Recorder { 29 30 static Recorder recorder; 31 Vector calls = new Vector(); 32 Vector newObjectsClasses = new Vector(); 33 Hashtable newObjectsIndexes = new Hashtable(); 34 boolean recording = false; 35 int newObjectsCount = 0; 36 37 public void printRecordState() { 38 Iterator it = calls.iterator(); 39 while( it.hasNext() ) { 40 Object [] call = (Object [])it.next(); 41 System.out.println("- "+call[0]+"."+call[1]+ 42 Arrays.asList((Object [])call[2])); 43 } 44 } 45 46 public static Recorder get() { 47 return recorder; 48 } 49 50 public boolean isRecording() { 51 return recording; 52 } 53 54 public void start() { 55 calls.clear(); 56 newObjectsClasses.clear(); 57 newObjectsIndexes.clear(); 58 newObjectsCount = 0; 59 recording = true; 60 } 61 62 public void stop() { 63 recording = false; 64 } 65 66 public Vector getCalls() { 67 return calls; 68 } 69 70 public Vector getNewObjectsClasses() { 71 return newObjectsClasses; 72 } 73 74 public void replay( Vector newObjectsClasses, Vector calls ) { 75 if( recording ) return; 76 Vector createdObjects = new Vector(); 79 Iterator it = newObjectsClasses.iterator(); 80 while( it.hasNext() ) { 81 Class newObjectClass = (Class )it.next(); 82 try { 83 Object o = newObjectClass.newInstance(); 84 createdObjects.add(o); 85 System.out.println("REPLAY: creating new object "+o); 86 } catch( Exception e ) { 87 System.out.println("FATAL ERROR: replay failed!!"); 88 e.printStackTrace(); 89 return; 90 } 91 } 92 it = calls.iterator(); 95 while( it.hasNext() ) { 96 Object [] call = (Object [])it.next(); 97 Wrappee o; 98 if( call[0] instanceof Integer ) { 99 o = (Wrappee)createdObjects.get( ((Integer )call[0]).intValue() ); 101 } else { 102 o = (Wrappee)NameRepository.get().getObject( (String )call[0] ); 104 } 105 System.out.println("REPLAY: calling recorded "+o+"."+ 106 (String )call[1]+Arrays.asList((Object [])call[2])); 107 ClassRepository.get().getClass(o).getMethod((String )call[1]).invoke( 108 o, (Object [])call[2] ); 109 } 110 } 111 112 public void recordMethodCall(Object o, String methodName, Object [] args) { 113 if( isNewObject(o) ) { 114 calls.add( new Object [] { newObjectsIndexes.get(o), 115 methodName, args } ); 116 } else { 117 calls.add( new Object [] { NameRepository.get().getName(o), 118 methodName, args } ); 119 } 120 } 121 122 public void recordNewObject(Object o) { 123 newObjectsClasses.add(o.getClass()); 124 newObjectsIndexes.put(o,new Integer (newObjectsCount++)); 125 } 126 127 boolean isNewObject(Object o) { 128 if( newObjectsIndexes.get(o) == null ) { 129 return false; 130 } else { 131 return true; 132 } 133 } 134 135 } 136 137 138 139 140 141 142 143 144 | Popular Tags |