2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.test.mc.basic;
21 import gov.nasa.jpf.ListenerAdapter;
22 import gov.nasa.jpf.jvm.bytecode.INVOKESTATIC;
23 import gov.nasa.jpf.jvm.bytecode.JVMInvokeInstruction;
24 import gov.nasa.jpf.jvm.bytecode.VirtualInvocation;
25 import gov.nasa.jpf.util.test.TestJPF;
26 import gov.nasa.jpf.vm.ElementInfo;
27 import gov.nasa.jpf.vm.Instruction;
28 import gov.nasa.jpf.vm.VM;
29 import gov.nasa.jpf.vm.MethodInfo;
30 import gov.nasa.jpf.vm.ThreadInfo;
32 import org.junit.Test;
36 * testing various aspects of listeners on INVOKE instructions
38 public class InvokeListenerTest extends TestJPF {
40 //--- this is only used outside JPF execution
41 public static class Listener extends ListenerAdapter {
43 void checkArgs (ThreadInfo ti, Instruction insn, boolean isPostExec){
44 if (insn instanceof JVMInvokeInstruction){
45 JVMInvokeInstruction call = (JVMInvokeInstruction)insn;
46 MethodInfo mi = call.getInvokedMethod(ti);
47 String miSignature = mi.getUniqueName();
48 String mname = mi.getName();
50 if (miSignature.equals("testInstanceMethod(DI)D")){
51 Object[] args = call.getArgumentValues(ti);
52 ElementInfo ei = getTarget(ti,call);
53 log(mname, ei, args, isPostExec);
54 assert ((Double)args[0]) == 42.0;
55 assert ((Integer)args[1]) == 1;
57 } else if (miSignature.equals("testStaticMethod(I)I")){
58 Object[] args = call.getArgumentValues(ti);
59 ElementInfo ei = getTarget(ti,call);
60 log(mname, ei, args, isPostExec);
61 assert ((Integer)args[0]) == 42;
63 } else if (miSignature.equals("testNativeInstanceMethod(DI)D")){
64 Object[] args = call.getArgumentValues(ti);
65 ElementInfo ei = getTarget(ti,call);
66 log(mname, ei, args, isPostExec);
67 assert ((Double)args[0]) == 42.0;
68 assert ((Integer)args[1]) == 1;
74 ElementInfo getTarget (ThreadInfo ti, JVMInvokeInstruction call){
75 if (call instanceof VirtualInvocation){
76 int objRef = ((VirtualInvocation)call).getCalleeThis(ti);
77 return ti.getElementInfo(objRef);
78 } else if (call instanceof INVOKESTATIC){
79 return ((INVOKESTATIC)call).getInvokedMethod().getClassInfo().getStaticElementInfo();
85 void log (String mname, ElementInfo ei, Object[] args, boolean isPostExec){
86 System.out.print(isPostExec ? "# instructionExecuted: " : "# executeInstruction: ");
89 System.out.print('.');
90 System.out.print(mname);
92 System.out.print(" (");
93 for (int i=0; i<args.length; i++) {
94 if (i >0) System.out.print(',');
95 System.out.print( args[i]);
97 System.out.println(")");
101 public void executeInstruction (VM vm, ThreadInfo ti, Instruction insnToExecute){
102 checkArgs(ti, insnToExecute, false);
106 public void instructionExecuted (VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn){
107 checkArgs(ti, executedInsn, true);
112 double testInstanceMethod (double d, int c){
115 @Test public void testInstanceMethod (){
116 if (verifyNoPropertyViolation("+listener=.test.mc.basic.InvokeListenerTest$Listener")){
117 testInstanceMethod(42.0, 1);
121 int testStaticMethod (int a){
124 @Test public void testStaticMethod (){
125 if (verifyNoPropertyViolation("+listener=.test.mc.basic.InvokeListenerTest$Listener")){
126 testStaticMethod(42);
130 native double testNativeInstanceMethod (double d, int c);
131 @Test public void testNativeInstanceMethod (){
132 if (verifyNoPropertyViolation("+listener=.test.mc.basic.InvokeListenerTest$Listener")){
133 testNativeInstanceMethod(42.0, 1);