00001
00005 package edu.mit.csail.sdg.squander.utils;
00006
00007 import java.io.BufferedOutputStream;
00008 import java.io.FileOutputStream;
00009 import java.io.IOException;
00010 import java.lang.annotation.Annotation;
00011 import java.lang.reflect.Constructor;
00012 import java.lang.reflect.Method;
00013 import java.util.Set;
00014
00015 import com.thoughtworks.paranamer.BytecodeReadingParanamer;
00016 import com.thoughtworks.paranamer.Paranamer;
00017
00018 import forge.program.ForgeDomain;
00019 import forge.program.ForgeProgram;
00020 import forge.program.ForgeType;
00021 import forge.program.ForgeVariable;
00022 import forge.program.GlobalVariable;
00023 import forge.program.InstanceDomain;
00024 import forge.program.InstanceLiteral;
00025 import forge.solve.BooleanAtom;
00026 import forge.solve.ForgeConstant;
00027 import forge.solve.IntegerAtom;
00028
00034 public class Utils {
00035
00040 public static boolean isPrimitive(Class<?> cls) {
00041 if (cls.isPrimitive())
00042 return true;
00043 if (Number.class.isAssignableFrom(cls))
00044 return true;
00045 if (Boolean.class == cls)
00046 return true;
00047 return false;
00048 }
00049
00056 public static InstanceDomain findDomain(final Class<?> cls, final ForgeProgram program) {
00057 if (cls == null)
00058 return null;
00059 String clsName = cls.getName();
00060 if (cls.isArray())
00061 clsName = cls.getComponentType().getName() + "[]";
00062 for (final InstanceDomain d : program.instanceDomains()) {
00063 if (clsName.equals(d.name()))
00064 return d;
00065 }
00066 for (Class<?> s : ReflectionUtils.getImmParents(cls)) {
00067 InstanceDomain idom = findDomain(s, program);
00068 if (idom != null)
00069 return idom;
00070 }
00071 return null;
00072 }
00073
00080 public static GlobalVariable findGlobalVar(String varName, ForgeProgram program) {
00081 if (varName == null)
00082 return null;
00083 for (final GlobalVariable v : program.globalVariables()) {
00084 if (varName.equals(v.name()))
00085 return v;
00086 }
00087 return null;
00088 }
00089
00096 public static InstanceLiteral findLiteral(final String litName, final ForgeProgram program) {
00097 if (litName == null)
00098 return null;
00099 for (final InstanceLiteral l : program.instanceLiterals()) {
00100 if (litName.equals(l.name()))
00101 return l;
00102 }
00103 return null;
00104 }
00105
00110 public static ForgeDomain getArrayDomain(ForgeVariable g) {
00111 ForgeType type = g.type();
00112 Set<? extends forge.program.ForgeType.Tuple> tupleTypes = type.tupleTypes();
00113 for (forge.program.ForgeType.Tuple t : tupleTypes) {
00114 ForgeDomain d;
00115 if (t.domains().size() == 1) {
00116 d = t.domains().get(0);
00117 } else {
00118 d = t.domains().get(1);
00119 }
00120 if (!d.name().endsWith("[]"))
00121 continue;
00122 return d;
00123 }
00124 return null;
00125 }
00126
00127 public static String getArrayType(final ForgeVariable var) {
00128 forge.program.ForgeType.Tuple t = var.type().tupleTypes().iterator().next();
00129 if (var.arity() == 1) {
00130 t = t.domains().get(0);
00131 } else if (var.arity() >= 2) {
00132 t = t.domains().get(1);
00133 }
00134 InstanceDomain instanceDomain = (InstanceDomain) t;
00135 String clsName = instanceDomain.name();
00136 if (!clsName.endsWith("[]"))
00137 throw new RuntimeException("Return type is not an array type: " + clsName);
00138 return clsName.substring(0, clsName.length() - 2);
00139 }
00140
00141
00142 public static Class<?> classForName(final String clsName) throws ClassNotFoundException {
00143 if ("int".equals(clsName))
00144 return int.class;
00145 if ("boolean".equals(clsName))
00146 return boolean.class;
00147 return Class.forName(clsName);
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00186
00187 public static Annotation[] getMethodAnnotations(String className, String methodName, Class<?>[] parameterTypes) {
00188 try {
00189 Class<?> cls = Class.forName(className);
00190 if ("<init>".equals(methodName)) {
00191 Constructor<?> c = cls.getDeclaredConstructor(parameterTypes);
00192 c.setAccessible(true);
00193 return c.getAnnotations();
00194 } else {
00195 try {
00196
00197 final Method m = cls.getDeclaredMethod(methodName, parameterTypes);
00198 m.setAccessible(true);
00199 return m.getAnnotations();
00200 } catch (final NoSuchMethodException e) {
00201
00202 final Method m = cls.getMethod(methodName, parameterTypes);
00203 m.setAccessible(true);
00204 return m.getAnnotations();
00205 }
00206 }
00207 } catch (NoSuchMethodException e) {
00208 String msg = String.format("couldn't find a method named %s.%s to extract annotations from", className, methodName);
00209 throw new RuntimeException(msg, e);
00210 } catch (Exception e) {
00211 throw new RuntimeException("error getting annotations", e);
00212 }
00213 }
00214
00215 public static boolean boolValue(ForgeConstant fConst) {
00216 if (fConst.tuples().size() == 0)
00217 return false;
00218 assert fConst instanceof BooleanAtom : "must be a BooleanAtom in order to extract a boolean value from it; fc = " + fConst;
00219 return ((BooleanAtom) fConst).value();
00220 }
00221
00222 public static int intValue(ForgeConstant fConst) {
00223 assert fConst instanceof IntegerAtom : "must be an IntegerAtom in order to extract an int value from it; fc = " + fConst;
00224 return ((IntegerAtom) fConst).value();
00225 }
00226
00227 public static String getMethodParamName(Method method, int i) {
00228 Paranamer pn = new BytecodeReadingParanamer();
00229 method.setAccessible(true);
00230 try {
00231 return pn.lookupParameterNames(method, true)[i];
00232 } catch (Exception e) {
00233 return "@arg(" + i + ")";
00234 }
00235 }
00236
00237 public static void writeToFile(String text, String fileName) {
00238 try {
00239 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
00240 bos.write(text.getBytes());
00241 bos.flush();
00242 bos.close();
00243 } catch (IOException e) {
00244 throw new RuntimeException(e);
00245 }
00246 }
00247
00248 }