1 27 package ch.ethz.inf.iks.jvmai.jvmdi; 28 import ch.ethz.jvmai.JoinPoint; 29 import java.lang.reflect.Modifier ; 30 import java.lang.reflect.Method ; 31 32 import java.util.*; 34 import java.io.*; 35 36 46 public 47 class JoinPointContext 48 { 49 private static int UNDEFINED_DEPTH=-1; 50 51 protected Thread thread; protected JoinPointLocation location; protected int depth; protected int height; 56 private int state; private static int SLOT_THIS_OBJ; 58 private static int STATE_GOT_NOTHING = 0x0; 59 private static int STATE_GOT_ARGS = 0x02; 60 private static int STATE_GOT_THIS = 0x01; 61 private static int STATE_GOT_INFO = 0x04; 62 63 private Object thisObject; 64 private Object [] argValues; 65 private Class [] argTypes; 66 private String [] argNames; 67 private int[] argSlots; 68 69 protected JoinPointContext() 70 { 71 this(Thread.currentThread(),UNDEFINED_DEPTH,0); 72 } 73 74 private JoinPointContext(Thread t, int dpth,int hght) 75 { 76 this.location = new JoinPointLocation(); 77 this.depth = dpth; 78 this.height = hght; this.thread = t; 80 this.state = 0; 81 } 82 83 protected JoinPointContext callerContext() 84 { 85 JoinPointContext newContext = new JoinPointContext(thread,depth-1,height + 1); 86 doGetCallerLocationData(newContext.location,thread,depth-1,height+1); 87 return newContext; 88 } 89 90 protected void invalidate() 91 { 92 state = 0; 93 location.executingMethod = null; 94 } 95 96 protected boolean isValid() 97 { 98 return location.executingMethod == null; 99 } 100 101 102 103 104 protected Object [] getArgs() 105 { 106 if ( (state & STATE_GOT_ARGS) != 0) 107 return argValues; 108 109 110 doGetLocalVariableInfo(); 111 112 for (int i = 0; i < argValues.length; i++) 113 argValues[i]=getLocalValue(argTypes[i],argSlots[i]); 114 115 state |= STATE_GOT_ARGS; 116 return argValues; 117 118 } 119 120 protected Object getThis() 121 { 122 if ( (state & STATE_GOT_THIS) != 0) 123 return thisObject; 124 125 126 if ( ( location.executingMethod.getModifiers() & Modifier.STATIC ) != 0) 127 thisObject = null; 128 else 129 thisObject = getLocalValue(java.lang.Object .class,0); 130 131 state |= STATE_GOT_THIS; 132 return thisObject; 133 } 134 135 protected String [] getParameterNames() 136 { 137 doGetLocalVariableInfo(); 138 return argNames; 139 } 140 141 142 private Object [][] preAllocatedArgs = new Object [][]{ new Object [0], 143 new Object [1], 144 new Object [2], 145 new Object [3], 146 new Object [4], 147 new Object [5]}; 148 149 150 private int[][] preAllocatedSlots = new int[][]{ new int[0], 151 new int[1], 152 new int[2], 153 new int[3], 154 new int[4], 155 new int[5]}; 156 157 158 private String [][] preAllocatedNames = new String [][]{ new String [0], 159 new String [1], 160 new String [2], 161 new String [3], 162 new String [4], 163 new String [5]}; 164 165 private void doGetLocalVariableInfo() 166 { 167 if ((state & STATE_GOT_INFO) != 0) 168 return; 169 170 argTypes = location.executingMethod.getParameterTypes(); 172 if (argTypes.length <5) 173 { 174 argValues = preAllocatedArgs[argTypes.length]; 175 argSlots = preAllocatedSlots[argTypes.length]; 176 argNames = preAllocatedNames[argTypes.length]; 177 } 178 else 179 { 180 argValues = new Object [argTypes.length]; 181 argSlots = new int[argTypes.length]; 182 argNames = new String [argTypes.length]; 183 } 184 185 186 try 187 { 188 189 doGetLocalVariables(thread,location.executingMethod,location.executingMethod.getDeclaringClass(),argNames,argSlots); 191 } 192 catch(AbsentInformationException e) 193 { 194 int slot = 0; 196 if ( (location.executingMethod.getModifiers() & Modifier.STATIC) == 0) 197 slot++; 198 199 for(int i = 0; i<argTypes.length; i++) 200 { 201 argNames[i] = null; 202 argSlots[i] = slot; 203 if ( Double .class.isAssignableFrom(argTypes[i]) || Long .class.isAssignableFrom(argTypes[i]) ) 204 slot+=2; 205 else 206 slot+=1; 207 } 208 } 209 state |= STATE_GOT_INFO; 210 } 211 212 213 214 223 protected 224 void setLocalValue(Object v, int index) throws ClassCastException 225 { 226 doGetLocalVariableInfo(); 227 Class cls = argTypes[index]; 228 Thread crtThread = thread; 229 if (cls.isAssignableFrom(Boolean.TYPE)) 230 { 231 setLocalBoolean(crtThread,depth,height,argSlots[index],((Boolean )v).booleanValue()); 232 return; 233 } 234 if (cls.isAssignableFrom(Byte.TYPE)) 235 { 236 setLocalInt(crtThread,depth,height,argSlots[index],((Byte )v).byteValue()); 237 return; 238 } 239 240 if (cls.isAssignableFrom(Character.TYPE)) 241 { 242 setLocalInt(crtThread,depth,height,argSlots[index],((Character )v).charValue()); 243 return; 244 } 245 246 if (cls.isAssignableFrom(Short.TYPE)) 247 { 248 setLocalInt(crtThread,depth,height,argSlots[index],((Short )v).shortValue()); 249 return; 250 } 251 252 if (cls.isAssignableFrom(Integer.TYPE)) 253 { 254 setLocalInt(crtThread,depth,height,argSlots[index],((Integer )v).intValue()); 255 return; 256 } 257 258 if (cls.isAssignableFrom(Long.TYPE)) 259 { 260 setLocalLong(crtThread,depth,height,argSlots[index],((Long )v).longValue()); 261 return; 262 } 263 if (cls.isAssignableFrom (Double.TYPE)) 264 { 265 setLocalDouble(crtThread,depth,height,argSlots[index],((Double )v).doubleValue()); 266 return; 267 } 268 269 if (cls.isAssignableFrom(Float.TYPE)) 270 { 271 setLocalFloat(crtThread,depth,height,argSlots[index],((Float )v).floatValue()); 272 return; 273 } 274 275 276 setLocalObject(crtThread,depth,height,argSlots[index],v); 277 } 278 279 286 287 protected Object getLocalValue(Class cls, int slot) 288 { 289 Thread crtThread = thread; 290 291 if (cls.isAssignableFrom(Boolean.TYPE)) 292 { 293 return getLocalBoolean(crtThread,depth,height,slot)?Boolean.TRUE:Boolean.FALSE; 294 } 295 296 if (cls.isAssignableFrom(Byte.TYPE)) 297 return new Byte ((byte)getLocalInt(crtThread,depth,height,slot)); 298 299 if (cls.isAssignableFrom(Character.TYPE)) 300 return new Character ((char)getLocalInt(crtThread,depth,height,slot)); 301 302 if (cls.isAssignableFrom(Short.TYPE)) 303 return new Short ((short)getLocalInt(crtThread,depth,height,slot)); 304 305 if (cls.isAssignableFrom(Integer.TYPE)) 306 return new Integer ((int)getLocalInt(crtThread,depth,height,slot)); 307 308 if (cls.isAssignableFrom(Long.TYPE)) 309 return new Long ((int)getLocalLong(crtThread,depth,height,slot)); 310 311 if (cls.isAssignableFrom(Double.TYPE)) 312 return new Double ((int)getLocalDouble(crtThread,depth,height,slot)); 313 314 if (cls.isAssignableFrom (Float.TYPE)) 315 return new Float (getLocalFloat(crtThread,depth,height,slot)); 316 317 return getLocalObject(crtThread,depth,height,slot); 318 319 } 320 321 private static native void doGetLocalVariables(Thread t, Method meth, Class cls, String [] aNames, int[] aSlots); 322 323 329 private static native Object getLocalObject(Thread t, int depth, int height, int slot); 330 331 private static native int getLocalInt(Thread t, int depth, int height, int slot); 332 333 private static native byte getLocalByte(Thread t, int depth, int height, int slot); 334 335 private static native char getLocalChar(Thread t, int depth, int height, int slot); 336 337 private static native short getLocalShort(Thread t, int depth, int height, int slot); 338 339 private static native long getLocalLong(Thread t, int depth, int height, int slot); 340 341 private static native boolean getLocalBoolean(Thread t, int depth, int height, int slot); 342 343 private static native double getLocalDouble(Thread t, int depth, int height, int slot); 344 345 private static native float getLocalFloat(Thread t, int depth, int height, int slot); 346 347 private static native void setLocalObject(Thread t, int depth, int height, int slot, Object value); 348 349 private static native void setLocalInt(Thread t, int depth, int height, int slot, int value); 350 351 private static native void setLocalLong(Thread t, int depth, int height, int slot, long value); 352 353 private static native void setLocalBoolean(Thread t, int depth, int height, int slot, boolean value); 354 355 358 private static native void setLocalDouble(Thread t, int depth, int height, int slot, double value); 359 360 363 private static native void setLocalFloat(Thread t, int depth, int height, int slot, float value); 364 365 private native void doGetCallerLocationData(JoinPointLocation jpl, Thread t, int depth,int height); 366 367 } 368 369 370 | Popular Tags |