You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArrayAccessReplaceTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. package test.javassist.convert;
  2. import java.net.URL;
  3. import java.net.URLClassLoader;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import javassist.ClassPool;
  7. import javassist.CodeConverter;
  8. import javassist.CtClass;
  9. import junit.framework.TestCase;
  10. public class ArrayAccessReplaceTest extends TestCase {
  11. private static SimpleInterface simple;
  12. public void setUp() throws Exception {
  13. ClassPool pool = new ClassPool(true);
  14. CtClass echoClass = pool.get(ArrayAccessReplaceTest.class.getName() + "$Echo");
  15. CtClass simpleClass = pool.get(ArrayAccessReplaceTest.class.getName() + "$Simple");
  16. CodeConverter converter = new CodeConverter();
  17. converter.replaceArrayAccess(echoClass, new CodeConverter.DefaultArrayAccessReplacementMethodNames());
  18. simpleClass.instrument(converter);
  19. //simpleClass.writeFile("/tmp");
  20. simple = (SimpleInterface) simpleClass.toClass(new URLClassLoader(new URL[0], getClass().getClassLoader()), Class.class.getProtectionDomain()).newInstance();
  21. }
  22. public void testComplex() throws Exception {
  23. ClassPool pool = new ClassPool(true);
  24. CtClass clazz = pool.get(ArrayAccessReplaceTest.class.getName() + "$Complex");
  25. CodeConverter converter = new CodeConverter();
  26. converter.replaceArrayAccess(clazz, new CodeConverter.DefaultArrayAccessReplacementMethodNames());
  27. clazz.instrument(converter);
  28. ComplexInterface instance = (ComplexInterface) clazz.toClass(new URLClassLoader(new URL[0], getClass().getClassLoader()), Class.class.getProtectionDomain()).newInstance();
  29. assertEquals(new Integer(5), instance.complexRead(4));
  30. }
  31. public void testBoolean() throws Exception {
  32. for (int i = 0; i < 100; i++) {
  33. boolean value = i % 5 == 0;
  34. simple.setBoolean(i, value);
  35. }
  36. for (int i = 0; i < 100; i++) {
  37. boolean value = i % 5 == 0;
  38. assertEquals(value, simple.getBoolean(i));
  39. }
  40. }
  41. public void testByte() throws Exception {
  42. for (byte i = 0; i < 100; i++) {
  43. simple.setByte(i, i);
  44. }
  45. for (byte i = 0; i < 100; i++) {
  46. assertEquals(i, simple.getByte(i));
  47. }
  48. }
  49. public void testShort() throws Exception {
  50. for (short i = 0; i < 100; i++) {
  51. simple.setShort(i, i);
  52. }
  53. for (short i = 0; i < 100; i++) {
  54. assertEquals(i, simple.getShort(i));
  55. }
  56. }
  57. public void testChar() throws Exception {
  58. for (char i = 0; i < 100; i++) {
  59. simple.setChar(i, i);
  60. }
  61. for (char i = 0; i < 100; i++) {
  62. assertEquals(i, simple.getChar(i));
  63. }
  64. }
  65. public void testInt() throws Exception {
  66. for (int i = 0; i < 100; i++) {
  67. simple.setInt(i, i);
  68. }
  69. for (int i = 0; i < 100; i++) {
  70. assertEquals(i, simple.getInt(i));
  71. }
  72. }
  73. public void testLong() throws Exception {
  74. for (int i = 0; i < 100; i++) {
  75. simple.setLong(i, i);
  76. }
  77. for (int i = 0; i < 100; i++) {
  78. assertEquals(i, simple.getLong(i));
  79. }
  80. }
  81. public void testFloat() throws Exception {
  82. for (int i = 0; i < 100; i++) {
  83. simple.setFloat(i, i);
  84. }
  85. for (int i = 0; i < 100; i++) {
  86. assertEquals((float)i, simple.getFloat(i), 0);
  87. }
  88. }
  89. public void testDouble() throws Exception {
  90. for (int i = 0; i < 100; i++) {
  91. simple.setDouble(i, i);
  92. }
  93. for (int i = 0; i < 100; i++) {
  94. assertEquals((double)i, simple.getDouble(i), 0);
  95. }
  96. }
  97. public void testObject() throws Exception {
  98. for (int i = 0; i < 100; i++) {
  99. simple.setObject(i, new Integer(i));
  100. }
  101. for (int i = 0; i < 100; i++) {
  102. assertEquals(new Integer(i), simple.getObject(i));
  103. }
  104. }
  105. public void testFoo() throws Exception {
  106. for (int i = 0; i < 100; i++) {
  107. simple.setFoo(i, new Foo(i));
  108. }
  109. for (int i = 0; i < 100; i++) {
  110. assertEquals(new Foo(i), simple.getFoo(i));
  111. }
  112. }
  113. public void testMulti() throws Exception {
  114. for (int i = 2; i < 100; i++) {
  115. simple.setMultiFoo(0, 1, i, new Foo(i));
  116. }
  117. for (int i = 2; i < 100; i++) {
  118. assertEquals(new Foo(i), simple.getMultiFoo(0, 1, i));
  119. }
  120. }
  121. public static class Echo {
  122. public static Map byteMap = new HashMap();
  123. public static Map charMap = new HashMap();
  124. public static Map doubleMap = new HashMap();
  125. public static Map floatMap = new HashMap();
  126. public static Map intMap = new HashMap();
  127. public static Map longMap = new HashMap();
  128. public static Map objectMap = new HashMap();
  129. public static Map shortMap = new HashMap();
  130. public static Object arrayReadObject(Object array, int index) {
  131. return objectMap.get(new Integer(index));
  132. }
  133. public static void arrayWriteObject(Object array, int index, Object element) {
  134. objectMap.put(new Integer(index), element);
  135. }
  136. public static byte arrayReadByteOrBoolean(Object array, int index) {
  137. return ((Byte)byteMap.get(new Integer(index))).byteValue();
  138. }
  139. public static void arrayWriteByteOrBoolean(Object array, int index, byte element) {
  140. byteMap.put(new Integer(index), new Byte(element));
  141. }
  142. public static char arrayReadChar(Object array, int index) {
  143. return ((Character)charMap.get(new Integer(index))).charValue();
  144. }
  145. public static void arrayWriteChar(Object array, int index, char element) {
  146. charMap.put(new Integer(index), new Character(element));
  147. }
  148. public static double arrayReadDouble(Object array, int index) {
  149. return ((Double)doubleMap.get(new Integer(index))).doubleValue();
  150. }
  151. public static void arrayWriteDouble(Object array, int index, double element) {
  152. doubleMap.put(new Integer(index), new Double(element));
  153. }
  154. public static float arrayReadFloat(Object array, int index) {
  155. return ((Float)floatMap.get(new Integer(index))).floatValue();
  156. }
  157. public static void arrayWriteFloat(Object array, int index, float element) {
  158. floatMap.put(new Integer(index), new Float(element));
  159. }
  160. public static int arrayReadInt(Object array, int index) {
  161. return ((Integer)intMap.get(new Integer(index))).intValue();
  162. }
  163. public static void arrayWriteInt(Object array, int index, int element) {
  164. intMap.put(new Integer(index), new Integer(element));
  165. }
  166. public static long arrayReadLong(Object array, int index) {
  167. return ((Long)longMap.get(new Integer(index))).longValue();
  168. }
  169. public static void arrayWriteLong(Object array, int index, long element) {
  170. longMap.put(new Integer(index), new Long(element));
  171. }
  172. public static short arrayReadShort(Object array, int index) {
  173. return ((Short)shortMap.get(new Integer(index))).shortValue();
  174. }
  175. public static void arrayWriteShort(Object array, int index, short element) {
  176. shortMap.put(new Integer(index), new Short(element));
  177. }
  178. }
  179. public static class Foo {
  180. public int bar;
  181. public Foo(int bar) {
  182. this.bar = bar;
  183. }
  184. public int hashCode() {
  185. return bar;
  186. }
  187. public boolean equals(Object o) {
  188. if (! (o instanceof Foo))
  189. return false;
  190. return ((Foo)o).bar == bar;
  191. }
  192. }
  193. public static interface SimpleInterface {
  194. public void setBoolean(int pos, boolean value);
  195. public boolean getBoolean(int pos);
  196. public void setByte(int pos, byte value);
  197. public byte getByte(int pos);
  198. public void setShort(int pos, short value);
  199. public short getShort(int pos);
  200. public void setChar(int pos, char value);
  201. public char getChar(int pos);
  202. public void setInt(int pos, int value);
  203. public int getInt(int pos);
  204. public void setLong(int pos, long value);
  205. public long getLong(int pos);
  206. public void setFloat(int pos, float value);
  207. public float getFloat(int pos);
  208. public void setDouble(int pos, double value);
  209. public double getDouble(int pos);
  210. public void setObject(int pos, Object value);
  211. public Object getObject(int pos);
  212. public void setFoo(int pos, Foo value);
  213. public Foo getFoo(int pos);
  214. public void setMultiFoo(int one, int two, int three, Foo foo);
  215. public Foo getMultiFoo(int one, int two, int three);
  216. }
  217. public static class Simple implements SimpleInterface {
  218. private boolean[] booleans;
  219. private byte[] bytes;
  220. private short[] shorts;
  221. private char[] chars;
  222. private int[] ints;
  223. private long[] longs;
  224. private float[] floats;
  225. private double[] doubles;
  226. private Object[] objects;
  227. private Foo[] foos;
  228. private Foo[][][] multi;
  229. public Simple() {
  230. multi[0] = new Foo[0][0];
  231. multi[0][1] = new Foo[0];
  232. }
  233. public boolean getBoolean(int pos) {
  234. return booleans[pos];
  235. }
  236. public byte getByte(int pos) {
  237. return bytes[pos];
  238. }
  239. public char getChar(int pos) {
  240. return chars[pos];
  241. }
  242. public double getDouble(int pos) {
  243. return doubles[pos];
  244. }
  245. public float getFloat(int pos) {
  246. return floats[pos];
  247. }
  248. public Foo getFoo(int pos) {
  249. return foos[pos];
  250. }
  251. public int getInt(int pos) {
  252. return ints[pos];
  253. }
  254. public long getLong(int pos) {
  255. return longs[pos];
  256. }
  257. public Object getObject(int pos) {
  258. return objects[pos];
  259. }
  260. public short getShort(int pos) {
  261. return shorts[pos];
  262. }
  263. public Foo getMultiFoo(int one, int two, int three) {
  264. return multi[one][two][three];
  265. }
  266. public void setBoolean(int pos, boolean value) {
  267. booleans[pos] = value;
  268. }
  269. public void setByte(int pos, byte value) {
  270. bytes[pos] = value;
  271. }
  272. public void setChar(int pos, char value) {
  273. chars[pos] = value;
  274. }
  275. public void setDouble(int pos, double value) {
  276. doubles[pos] = value;
  277. }
  278. public void setFloat(int pos, float value) {
  279. floats[pos] = value;
  280. }
  281. public void setFoo(int pos, Foo value) {
  282. foos[pos] = value;
  283. }
  284. public void setInt(int pos, int value) {
  285. ints[pos] = value;
  286. }
  287. public void setLong(int pos, long value) {
  288. longs[pos] = value;
  289. }
  290. public void setObject(int pos, Object value) {
  291. objects[pos] = value;
  292. }
  293. public void setShort(int pos, short value) {
  294. shorts[pos] = value;
  295. }
  296. public void setMultiFoo(int one, int two, int three, Foo foo) {
  297. multi[one][two][three] = foo;
  298. }
  299. }
  300. public static interface ComplexInterface {
  301. public Number complexRead(int x);
  302. }
  303. public static class Complex implements ComplexInterface {
  304. private Integer[] nums;
  305. private Long[] longNums;
  306. private static Integer justRead;
  307. public static Object arrayReadObject(Object array, int offset) {
  308. return new Integer(justRead.intValue() + offset);
  309. }
  310. public static void arrayWriteObject(Object array, int offset, Object element) {
  311. justRead = (Integer) element;
  312. }
  313. public Object getInteger(int i) {
  314. return (Object) new Integer(i);
  315. }
  316. public Number complexRead(int x) {
  317. Number[] ns = null;
  318. Number n1, n2, n3, n4;
  319. try {
  320. ((Object[])ns)[1] = getInteger(x);
  321. // We have to throw an error since we can't intercept
  322. // a guaranteed null array read yet (likely never will be able to)
  323. throw new Error("hi");
  324. } catch (Error error) {
  325. ns = nums;
  326. } catch (Exception exception) {
  327. ns = longNums;
  328. } finally {
  329. n1 = ns[1];
  330. n2 = ns[2];
  331. n3 = ns[3];
  332. n4 = ns[4];
  333. n2.intValue();
  334. n3.intValue();
  335. n4.intValue();
  336. }
  337. return n1;
  338. }
  339. }
  340. }