KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > bytecode > VirtualInvocation


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm.bytecode;
20
21 import gov.nasa.jpf.jvm.ClassInfo;
22 import gov.nasa.jpf.jvm.KernelState;
23 import gov.nasa.jpf.jvm.MethodInfo;
24 import gov.nasa.jpf.jvm.SystemState;
25 import gov.nasa.jpf.jvm.ThreadInfo;
26
27
28 /**
29  * a base class for virtual call instructions
30  */

31 public abstract class VirtualInvocation extends InvokeInstruction {
32   
33   protected VirtualInvocation () {}
34   
35   protected VirtualInvocation (MethodInfo mi, String JavaDoc cname, String JavaDoc mname, String JavaDoc signature,
36                                int offset, int position) {
37     super(mi, cname, mname, signature, offset, position);
38   }
39   
40   
41   public boolean isDeterministic (SystemState ss, KernelState ks, ThreadInfo ti) {
42     int objRef = getCalleeThis(ti);
43     MethodInfo mi = getInvokedMethod( ks, objRef);
44     
45     if ((objRef == -1) || (mi == null)) {
46       return true; // doesn't really matter, just make sure we execute
47
}
48     
49     return mi.isDeterministic(ti);
50   }
51   
52   public boolean isExecutable (SystemState ss, KernelState ks, ThreadInfo ti) {
53     int objRef = getCalleeThis(ti);
54     MethodInfo mi = getInvokedMethod( ks, objRef);
55     
56     if ((objRef == -1) || (mi == null)) {
57       return true; // make sure we execute so that we get the exception
58
}
59     
60     return mi.isExecutable(ti);
61   }
62   
63   public Instruction execute (SystemState ss, KernelState ks, ThreadInfo ti) {
64     int objRef = getCalleeThis(ti);
65     
66     if (objRef == -1) { // NPE
67
return ti.createAndThrowException("java.lang.NullPointerException",
68                                         "calling '" + mname + "' on null object");
69     }
70     
71     MethodInfo mi = getInvokedMethod(ks, objRef);
72     if (mi == null) {
73       return ti.createAndThrowException("java.lang.NoSuchMethodException",
74                                         getCalleeClassInfo(ks, objRef).getName() + "." + mname);
75     }
76     
77     return mi.execute(ti, false);
78   }
79   
80   int getCalleeThis (ThreadInfo ti) {
81     return ti.getCalleeThis( getArgSize());
82   }
83   
84   ClassInfo getCalleeClassInfo (KernelState ks, int objRef) {
85     return ks.da.get(objRef).getClassInfo();
86   }
87   
88   /**
89    * cache the actual callee class MethodInfo
90    */

91   MethodInfo getInvokedMethod (KernelState ks, int objRef) {
92     if (objRef != -1) {
93       
94       // <?> - can this be ambiguous? probably yes if we had a backtrace
95
// with different heap state where the heap symmetry didn't work
96
if (lastObj != objRef) { // it's good we are not multithreaded
97

98         ClassInfo mci = getCalleeClassInfo(ks, objRef);
99         invokedMethod = mci.getMethod(mname, true);
100         
101         // here we could catch the NoSuchMethodError
102
if (invokedMethod == null) {
103           lastObj = -1;
104         } else {
105           lastObj = objRef;
106         }
107       }
108     } else {
109       lastObj = -1;
110     }
111     
112     return invokedMethod;
113   }
114   
115   public boolean isSchedulingRelevant (SystemState ss, KernelState ks, ThreadInfo ti) {
116     
117     // if it's not synchronized, we can just go on
118
int objRef = getCalleeThis(ti);
119     MethodInfo mi = getInvokedMethod( ks, objRef);
120
121     if ((objRef == -1) || (mi == null)) {
122       return false; // we are going to have an exception, force
123
}
124
125     return mi.isSchedulingRelevant(ti, ks.da.get(objRef));
126   }
127 }
128
Popular Tags