| 1 19 20 package jode.jvm; 21 import jode.AssertError; 22 import jode.bytecode.Reference; 23 import jode.bytecode.TypeSignature; 24 25 import java.lang.reflect.Array ; 26 import java.lang.reflect.Constructor ; 27 import java.lang.reflect.Field ; 28 import java.lang.reflect.Method ; 29 import java.lang.reflect.InvocationTargetException ; 30 31 public class SimpleRuntimeEnvironment implements RuntimeEnvironment { 32 33 public static Object fromReflectType(String typeSig, Object value) { 34 switch(typeSig.charAt(0)) { 35 case 'Z': 36 return new Integer (((Boolean ) value).booleanValue() ? 1 : 0); 37 case 'B': 38 case 'S': 39 return new Integer (((Number ) value).intValue()); 40 case 'C': 41 return new Integer (((Character ) value).charValue()); 42 default: 43 return value; 44 } 45 } 46 47 public static Object toReflectType(String typeSig, Object value) { 48 switch(typeSig.charAt(0)) { 49 case 'Z': 50 return new Boolean (((Integer )value).intValue() != 0); 51 case 'B': 52 return new Byte (((Integer )value).byteValue()); 53 case 'S': 54 return new Short (((Integer )value).shortValue()); 55 case 'C': 56 return new Character ((char) ((Integer )value).intValue()); 57 default: 58 return value; 59 } 60 } 61 62 public Object getField(Reference ref, Object obj) 63 throws InterpreterException { 64 Field f; 65 try { 66 Class clazz = TypeSignature.getClass(ref.getClazz()); 67 try { 68 f = clazz.getField(ref.getName()); 69 } catch (NoSuchFieldException ex) { 70 f = clazz.getDeclaredField(ref.getName()); 71 } 72 } catch (ClassNotFoundException ex) { 73 throw new InterpreterException 74 (ref+": Class not found"); 75 } catch (NoSuchFieldException ex) { 76 throw new InterpreterException 77 ("Constructor "+ref+" not found"); 78 } catch (SecurityException ex) { 79 throw new InterpreterException 80 (ref+": Security exception"); 81 } 82 try { 83 return fromReflectType(ref.getType(), f.get(obj)); 84 } catch (IllegalAccessException ex) { 85 throw new InterpreterException 86 ("Field " + ref + " not accessible"); 87 } 88 } 89 90 public void putField(Reference ref, Object obj, Object value) 91 throws InterpreterException { 92 Field f; 93 try { 94 Class clazz = TypeSignature.getClass(ref.getClazz()); 95 try { 96 f = clazz.getField(ref.getName()); 97 } catch (NoSuchFieldException ex) { 98 f = clazz.getDeclaredField(ref.getName()); 99 } 100 } catch (ClassNotFoundException ex) { 101 throw new InterpreterException 102 (ref+": Class not found"); 103 } catch (NoSuchFieldException ex) { 104 throw new InterpreterException 105 ("Constructor "+ref+" not found"); 106 } catch (SecurityException ex) { 107 throw new InterpreterException 108 (ref+": Security exception"); 109 } 110 try { 111 f.set(obj, toReflectType(ref.getType(), value)); 112 } catch (IllegalAccessException ex) { 113 throw new InterpreterException 114 ("Field " + ref + " not accessible"); 115 } 116 } 117 118 public Object invokeConstructor(Reference ref, Object [] params) 119 throws InterpreterException, InvocationTargetException { 120 Constructor c; 121 try { 122 String [] paramTypeSigs 123 = TypeSignature.getParameterTypes(ref.getType()); 124 Class clazz = TypeSignature.getClass(ref.getClazz()); 125 Class [] paramTypes = new Class [paramTypeSigs.length]; 126 for (int i=0; i< paramTypeSigs.length; i++) { 127 params[i] = toReflectType(paramTypeSigs[i], params[i]); 128 paramTypes[i] = TypeSignature.getClass(paramTypeSigs[i]); 129 } 130 try { 131 c = clazz.getConstructor(paramTypes); 132 } catch (NoSuchMethodException ex) { 133 c = clazz.getDeclaredConstructor(paramTypes); 134 } 135 } catch (ClassNotFoundException ex) { 136 throw new InterpreterException 137 (ref+": Class not found"); 138 } catch (NoSuchMethodException ex) { 139 throw new InterpreterException 140 ("Constructor "+ref+" not found"); 141 } catch (SecurityException ex) { 142 throw new InterpreterException 143 (ref+": Security exception"); 144 } 145 146 try { 147 return c.newInstance(params); 148 } catch (IllegalAccessException ex) { 149 throw new InterpreterException 150 ("Constructor " + ref + " not accessible"); 151 } catch (InstantiationException ex) { 152 throw new InterpreterException 153 ("InstantiationException in " + ref + "."); 154 } 155 } 156 157 public Object invokeMethod(Reference ref, boolean isVirtual, 158 Object cls, Object [] params) 159 throws InterpreterException, InvocationTargetException { 160 if (!isVirtual && cls != null) 161 throw new InterpreterException 162 ("Can't invoke nonvirtual Method " + ref + "."); 163 164 Method m; 165 try { 166 String [] paramTypeSigs 167 = TypeSignature.getParameterTypes(ref.getType()); 168 Class clazz = TypeSignature.getClass(ref.getClazz()); 169 Class [] paramTypes = new Class [paramTypeSigs.length]; 170 for (int i=0; i< paramTypeSigs.length; i++) { 171 params[i] = toReflectType(paramTypeSigs[i], params[i]); 172 paramTypes[i] = TypeSignature.getClass(paramTypeSigs[i]); 173 } 174 try { 175 m = clazz.getMethod(ref.getName(), paramTypes); 176 } catch (NoSuchMethodException ex) { 177 m = clazz.getDeclaredMethod(ref.getName(), paramTypes); 178 } 179 } catch (ClassNotFoundException ex) { 180 throw new InterpreterException 181 (ref+": Class not found"); 182 } catch (NoSuchMethodException ex) { 183 throw new InterpreterException 184 ("Method "+ref+" not found"); 185 } catch (SecurityException ex) { 186 throw new InterpreterException 187 (ref+": Security exception"); 188 } 189 String retType = TypeSignature.getReturnType(ref.getType()); 190 try { 191 return fromReflectType(retType, m.invoke(cls, params)); 192 } catch (IllegalAccessException ex) { 193 throw new InterpreterException 194 ("Method " + ref + " not accessible"); 195 } 196 } 197 198 public boolean instanceOf(Object obj, String className) 199 throws InterpreterException { 200 Class clazz; 201 try { 202 clazz = Class.forName(className); 203 } catch (ClassNotFoundException ex) { 204 throw new InterpreterException 205 ("Class "+ex.getMessage()+" not found"); 206 } 207 return obj != null && !clazz.isInstance(obj); 208 } 209 210 public Object newArray(String type, int[] dimensions) 211 throws InterpreterException, NegativeArraySizeException { 212 Class clazz; 213 try { 214 215 clazz = TypeSignature.getClass(type.substring(dimensions.length)); 216 } catch (ClassNotFoundException ex) { 217 throw new InterpreterException 218 ("Class "+ex.getMessage()+" not found"); 219 } 220 return Array.newInstance(clazz, dimensions); 221 } 222 223 public void enterMonitor(Object obj) 224 throws InterpreterException { 225 throw new InterpreterException("monitor not implemented"); 226 } 227 public void exitMonitor(Object obj) 228 throws InterpreterException { 229 throw new InterpreterException("monitor not implemented"); 230 } 231 } 232 | Popular Tags |