00001 package gov.nasa.arc.ase.jpf.jvm.reflection;
00002
00003 import gov.nasa.arc.ase.jpf.*;
00004 import gov.nasa.arc.ase.util.*;
00005 import gov.nasa.arc.ase.jpf.jvm.*;
00006 import de.fub.bytecode.generic.InstructionHandle;
00007 import gov.nasa.arc.ase.util.Debug;
00008 import de.fub.bytecode.Constants;
00009
00010 public class Reflection implements Cloneable, Constants {
00011 protected int objref;
00012 protected int classref;
00013 protected SystemState ss;
00014 protected KernelState ks;
00015 protected ThreadInfo th;
00016 protected DynamicArea da;
00017 protected StaticArea sa;
00018 protected ClassInfo ci;
00019
00020 public long callMethod(String name, int[] args) {
00021 MethodInfo mi = ci.getMethodInfo(name);
00022
00023 if(mi.getClassInfo() != ci)
00024 return ci.callMethod(ss, ks, th, name, args);
00025
00026 for(int i = 0, s = args.length; i < s; i++)
00027 th.push(args[i]);
00028
00029 InstructionHandle pc = th.getPC();
00030 executeMethod(mi, true);
00031 th.setPC(pc);
00032
00033 switch(mi.getMethodGen().getReturnType().getType()) {
00034 case T_BOOLEAN:
00035 case T_BYTE:
00036 case T_CHAR:
00037 case T_FLOAT:
00038 case T_INT:
00039 case T_REFERENCE:
00040 case T_SHORT:
00041 return (long)th.pop();
00042
00043 case T_DOUBLE:
00044 case T_LONG:
00045 return th.pop2();
00046 }
00047
00048 return 0;
00049 }
00050 public long callStaticMethod(String name, int[] args) {
00051 MethodInfo mi = ci.getMethodInfo(name);
00052
00053 for(int i = 0, s = args.length; i < s; i++)
00054 th.push(args[i]);
00055
00056 InstructionHandle pc = th.getPC();
00057 executeStaticMethod(mi, true);
00058 th.setPC(pc);
00059
00060 switch(mi.getMethodGen().getReturnType().getType()) {
00061 case T_BOOLEAN:
00062 case T_BYTE:
00063 case T_CHAR:
00064 case T_FLOAT:
00065 case T_INT:
00066 case T_REFERENCE:
00067 case T_SHORT:
00068 return (long)th.pop();
00069
00070 case T_DOUBLE:
00071 case T_LONG:
00072 return th.pop2();
00073 }
00074
00075 return 0;
00076 }
00077 public Object clone() {
00078 try {
00079 return super.clone();
00080 } catch(CloneNotSupportedException e) {
00081 e.printStackTrace();
00082 System.exit(128);
00083 }
00084
00085 return null;
00086 }
00087 public InstructionHandle executeMethod(MethodInfo mi, boolean atomic) {
00088 if(mi.getMethodGen().isNative()) {
00089 throw new NativeMethodException(mi.getFullName());
00090 }
00091
00092 if(mi.getMethodGen().isSynchronized())
00093 if(!da.monitorLock(objref, th))
00094 return th.getPC();
00095
00096 if(atomic)
00097 th.executeAtomicMethod();
00098
00099 return th.getPC();
00100 }
00101 public InstructionHandle executeStaticMethod(MethodInfo mi, boolean atomic) {
00102 if(mi.getMethodGen().isNative()) {
00103 throw new NativeMethodException(mi.getFullName());
00104 }
00105
00106 if(mi.getMethodGen().isSynchronized())
00107 if(!sa.monitorLock(classref, th))
00108 return th.getPC();
00109
00110 if(atomic)
00111 th.executeAtomicMethod();
00112
00113 return th.getPC();
00114 }
00115
00116
00117
00118
00119
00120
00121 protected InstructionHandle exit() {
00122 MethodInfo mi = th.getCurrentMethod();
00123 th.popFrame();
00124 th.removeArguments(mi);
00125
00126 return th.getPC().getNext();
00127 }
00128 protected InstructionHandle exit(int retval) {
00129 MethodInfo mi = th.getCurrentMethod();
00130 th.popFrame();
00131 th.removeArguments(mi);
00132 th.push(retval);
00133
00134 return th.getPC().getNext();
00135 }
00136 protected InstructionHandle exit2(long retval) {
00137 MethodInfo mi = th.getCurrentMethod();
00138 th.popFrame();
00139 th.removeArguments(mi);
00140 th.push2(retval);
00141
00142 return th.getPC().getNext();
00143 }
00144 protected InstructionHandle exitRef(int retval) {
00145 MethodInfo mi = th.getCurrentMethod();
00146 th.popFrame();
00147 th.removeArguments(mi);
00148 th.push(retval);
00149
00150 th.setOperandRef();
00151
00152
00153 return th.getPC().getNext();
00154 }
00155 public long getField(String name) { return da.getValue(objref, name); }
00156 public int getObjectReference() {
00157 return objref;
00158 }
00159 public long getStaticField(String name) { return sa.getValue(classref, name); }
00160 public boolean isMethodDeterministic(MethodInfo mi) {
00161 return true;
00162 }
00163 public boolean isMethodExecutable(MethodInfo mi) {
00164 if(mi.getMethodGen().isSynchronized())
00165 return da.monitorCheckLock(objref, th);
00166
00167 return true;
00168 }
00169 public boolean isStaticMethodDeterministic(MethodInfo mi) {
00170 return true;
00171 }
00172 public boolean isStaticMethodExecutable(MethodInfo mi) {
00173 if(mi.getMethodGen().isSynchronized())
00174 return sa.monitorCheckLock(classref, th);
00175
00176 return true;
00177 }
00178 protected InstructionHandle repeat() {
00179 th.popFrame();
00180
00181 return th.getPC();
00182 }
00183 public void setClassInfo(ClassInfo c) {
00184 ci = c;
00185 }
00186 public void setClassRef(int c) {
00187 classref = c;
00188 }
00189 public void setField(String name, long value) { da.setValue(objref, name, value); }
00190 public void setObjectReference(int o) {
00191 objref = o;
00192 }
00193 public void setState(SystemState s, KernelState k, ThreadInfo t) {
00194 ss = s;
00195 ks = k;
00196 th = t;
00197 da = ks.dynamic_area;
00198 sa = ks.static_area;
00199 }
00200 public void setStaticField(String name, long value) { sa.setValue(classref, name, value); }
00201 }