Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / util / Invocation.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18
19 package gov.nasa.jpf.util;
20
21 import gov.nasa.jpf.vm.ClassInfo;
22 import gov.nasa.jpf.vm.ElementInfo;
23 import gov.nasa.jpf.vm.MJIEnv;
24 import gov.nasa.jpf.vm.VM;
25 import gov.nasa.jpf.vm.MethodInfo;
26 import gov.nasa.jpf.vm.ObjRef;
27
28 /**
29  * a record that includes all information to perform a call
30  */
31 public class Invocation {
32   MethodInfo mi;
33   Object[] args;  // includes 'this' for instance methods
34   Object[] attrs;
35   
36   public Invocation (MethodInfo mi, Object[] args, Object[] attrs){
37     this.mi = mi;
38     this.args = args;
39     this.attrs = attrs;
40   }
41   
42   public MethodInfo getMethodInfo () {
43     return mi;
44   }
45   
46   public Object[] getExplicitArguments () {
47     if (!mi.isStatic()){
48       Object[] a = new Object[args.length-1];
49       System.arraycopy(args,1,a,0,a.length);
50       return a;
51     } else {
52       return args;
53     }
54   }
55   
56   public String[] getArgumentTypeNames() {
57     return mi.getArgumentTypeNames();
58   }
59   
60   public String getArgumentValueLiteral(Object a) {
61     Class<?> cls = a.getClass();
62     
63     if (cls == Boolean.class)   return ((Boolean)a).toString();
64     if (cls == Byte.class)      return ((Byte)a).toString();
65     if (cls == Character.class) return ((Character)a).toString();
66     if (cls == Short.class)     return ((Short)a).toString();
67     if (cls == Integer.class)   return ((Integer)a).toString();
68     if (cls == Long.class)      return ((Long)a).toString();
69     if (cls == Float.class)     return ((Float)a).toString();
70     if (cls == Double.class)    return ((Double)a).toString();
71
72     if (cls == ObjRef.class) {
73       int ref = ((ObjRef)a).getReference();
74       
75       if (ref != MJIEnv.NULL){
76         ElementInfo ei = VM.getVM().getElementInfo(ref);
77         ClassInfo ci = ei.getClassInfo();
78         String cname = ci.getName();
79         if (cname.equals("java.lang.String")){
80           return "\"" + ei.asString() + '"';
81         } 
82         // <2do> we could probably do some more literals for java.lang.Class etc.
83       } else {
84         return "null";
85       }
86     }
87     
88     return null; // no literal representation
89   }
90   
91   public Object[] getArguments() {
92     return args;
93   }
94   
95   public Object[] getAttrs() {
96     return attrs;
97   }
98   
99   @Override
100   public String toString() {
101     StringBuilder sb = new StringBuilder();
102     
103     sb.append("INVOKE[");
104     sb.append(mi.getName());
105     sb.append('(');
106     for (int i=0; i<args.length; i++){
107       if (i>0){
108         sb.append(',');
109       }
110       sb.append(args[i]);
111     }
112     sb.append(")]");
113     
114     return sb.toString();
115   }
116 }