00001 package gov.nasa.arc.ase.jpf.tools;
00002
00003
00004 import edu.ksu.cis.bandera.specification.predicate.analysis.*;
00005 import edu.ksu.cis.bandera.specification.predicate.node.*;
00006 import gov.nasa.arc.ase.jpf.JPFErrorException;
00007 import java.io.*;
00008 import java.lang.reflect.*;
00009
00010 class Expression extends DepthFirstAdapter {
00011 private Translation translation;
00012 private Class currentType;
00013
00014 public Expression(Translation t) {
00015 translation = t;
00016 }
00017 public void caseAColonExp(AColonExp node) {
00018 inAColonExp(node);
00019 node.getExp().apply(this);
00020 outAColonExp(node);
00021 }
00022 public void caseAQualifiedName(AQualifiedName node) {
00023 node.getName().apply(this);
00024 String id = node.getId().getText();
00025
00026 if(currentType.isPrimitive())
00027 throw new JPFErrorException("Can't get field in primitive type");
00028
00029 try {
00030 currentType = getField(currentType, id).getType();
00031 printGetField(id, currentType);
00032 } catch(NoSuchFieldException e) {
00033 throw new JPFErrorException("No field " + id + " in " + currentType.getName());
00034 }
00035 }
00036 public void caseASimpleName(ASimpleName node) {
00037 String id = node.getId().getText();
00038
00039 try {
00040 currentType = Class.forName(id);
00041 translation.out.print("s.getClass(\"" + id + "\")");
00042 } catch(ClassNotFoundException e) {
00043 try {
00044 currentType = Class.forName("java.lang." + id);
00045 translation.out.print("s.getClass(\"" + id + "\")");
00046 } catch(ClassNotFoundException ee) {
00047 if((currentType = (Class)translation.params.get(id)) != null) {
00048 translation.out.print(id);
00049 } else {
00050 translation.out.print(classNameOf(translation.thisType.getName()) + "$this");
00051
00052 try {
00053 currentType = getField(translation.thisType, id).getType();
00054 } catch(NoSuchFieldException eee) {
00055 throw new JPFErrorException("No field " + id + " in " + translation.thisType.getName());
00056 }
00057
00058 printGetField(id, currentType);
00059 }
00060 }
00061 }
00062 }
00063 public void caseTRetVal(TRetVal node) {
00064 translation.out.print("t");
00065
00066 printGetReturnValue(translation.thisMethod.getReturnType());
00067 }
00068 private static String classNameOf(String name) {
00069 if(name.indexOf('.') == -1) return name;
00070
00071 return name.substring(name.lastIndexOf('.')+1);
00072 }
00073 public void defaultCase(Node node) {
00074 translation.out.print(node);
00075 }
00076 private static Field getField(Class c, String f) throws NoSuchFieldException {
00077 while(c != null) {
00078 try {
00079 return c.getDeclaredField(f);
00080 } catch(NoSuchFieldException e) {
00081 c = c.getSuperclass();
00082 }
00083 }
00084
00085 throw new NoSuchFieldException(f);
00086 }
00087 private void printGetField(String id, Class type) {
00088 translation.out.print(".get");
00089 if(type.isPrimitive()) {
00090 if(type == Boolean.TYPE) {
00091 translation.out.print("Boolean");
00092 } else if(type == Character.TYPE) {
00093 translation.out.print("Character");
00094 } else if(type == Byte.TYPE) {
00095 translation.out.print("Byte");
00096 } else if(type == Short.TYPE) {
00097 translation.out.print("Short");
00098 } else if(type == Integer.TYPE) {
00099 translation.out.print("Int");
00100 } else if(type == Long.TYPE) {
00101 translation.out.print("Long");
00102 } else if(type == Float.TYPE) {
00103 translation.out.print("Float");
00104 } else if(type == Double.TYPE) {
00105 translation.out.print("Double");
00106 }
00107 } else
00108 translation.out.print("Object");
00109 translation.out.print("Field(\"" + id + "\")");
00110 }
00111 private void printGetReturnValue(Class type) {
00112 translation.out.print(".get");
00113 if(type.isPrimitive()) {
00114 if(type == Boolean.TYPE) {
00115 translation.out.print("Boolean");
00116 } else if(type == Character.TYPE) {
00117 translation.out.print("Character");
00118 } else if(type == Byte.TYPE) {
00119 translation.out.print("Byte");
00120 } else if(type == Short.TYPE) {
00121 translation.out.print("Short");
00122 } else if(type == Integer.TYPE) {
00123 translation.out.print("Int");
00124 } else if(type == Long.TYPE) {
00125 translation.out.print("Long");
00126 } else if(type == Float.TYPE) {
00127 translation.out.print("Float");
00128 } else if(type == Double.TYPE) {
00129 translation.out.print("Double");
00130 }
00131 } else
00132 translation.out.print("Object");
00133 translation.out.print("ReturnValue()");
00134 }
00135 }