1 4 package com.tctest; 5 6 import com.tc.exception.TCRuntimeException; 7 import com.tc.object.TCObject; 8 import com.tc.object.bytecode.ByteCodeUtil; 9 import com.tc.simulator.app.ApplicationConfig; 10 import com.tc.simulator.listener.ListenerProvider; 11 import com.tctest.runner.AbstractTransparentApp; 12 13 import java.lang.reflect.Constructor ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.lang.reflect.Method ; 16 import java.lang.reflect.Modifier ; 17 import java.math.BigInteger ; 18 import java.util.Random ; 19 20 public class BigIntegerTestApp extends AbstractTransparentApp { 21 private final static BigInteger refInt = new BigInteger ("100"); 22 23 public BigIntegerTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 24 super(appId, cfg, listenerProvider); 25 } 26 27 public void run() { 28 invokeAllBigIntegerConstructors(); 29 invokeAllBigIntegerMethods(); 30 } 31 32 private void invokeAllBigIntegerMethods() { 33 Class bClazz = BigInteger .class; 34 Method [] methods = bClazz.getDeclaredMethods(); 35 for (int i=0; i<methods.length; i++) { 36 try { 37 System.out.println("Method name: " + methods[i].getName()); 38 if (!Modifier.isPrivate(methods[i].getModifiers()) && 39 !methods[i].getName().startsWith(ByteCodeUtil.TC_METHOD_PREFIX) && 40 !methods[i].getName().endsWith("class$")) { 41 System.out.println("Executing method: " + methods[i].getName()); 42 if (!Modifier.isPublic(methods[i].getModifiers())) { 43 methods[i].setAccessible(true); 44 } 45 invokeMethod(methods[i]); 46 } 47 } catch (IllegalArgumentException e) { 48 throw new TCRuntimeException(e); 49 } catch (IllegalAccessException e) { 50 throw new TCRuntimeException(e); 51 } catch (InvocationTargetException e) { 52 throw new TCRuntimeException(e); 53 } 54 } 55 } 56 57 private void invokeMethod(Method method) throws IllegalArgumentException , IllegalAccessException , InvocationTargetException { 58 BigInteger bigInteger = new BigInteger ("101"); 59 method.invoke(bigInteger, getMethodArguments(method)); 60 } 61 62 private Object [] getMethodArguments(Method method) { 63 Object [] arguments = new Object [method.getParameterTypes().length]; 64 String methodName = method.getName(); 65 if ("add".equals(methodName) || "and".equals(methodName) || "andNot".equals(methodName)) { 66 arguments[0] = new BigInteger ("200"); 67 } else if ("clearBit".equals(methodName)) { 68 arguments[0] = new Integer (1); 69 } else if ("compareTo".equals(methodName) || "divide".equals(methodName) || 70 "divideAndRemainder".equals(methodName) || "equals".equals(methodName) || 71 "gcd".equals(methodName) || "max".equals(methodName) || "min".equals(methodName) || 72 "mod".equals(methodName) || "modInverse".equals(methodName) || 73 "multiply".equals(methodName) || "or".equals(methodName) || "remainder".equals(methodName) || 74 "subtract".equals(methodName) || "xor".equals(methodName)) { 75 arguments[0] = refInt; 76 } else if ("flipBit".equals(methodName) || "isProbablePrime".equals(methodName) || 77 "pow".equals(methodName) || "setBit".equals(methodName) || "shiftLeft".equals(methodName) || 78 "shiftRight".equals(methodName) || "testBit".equals(methodName) || "trailingZeroCnt".equals(methodName) || 79 "bitLen".equals(methodName) || "primeToCertainty".equals(methodName) || "bitCnt".equals(methodName)) { 80 arguments[0] = new Integer (1); 81 } else if ("modPow".equals(methodName)) { 82 arguments[0] = refInt; 83 arguments[1] = refInt; 84 } else if ("probablePrime".equals(methodName)) { 85 arguments[0] = new Integer (5); 86 arguments[1] = new Random (); 87 } else if ("toString".equals(methodName) && arguments.length == 1) { 88 arguments[0] = new Integer (1); 89 } else if ("valueOf".equals(methodName)) { 90 arguments[0] = new Long (1); 91 } else if ("mulAdd".equals(methodName)) { 92 arguments[0] = new int[]{ 1 }; 93 arguments[1] = new int[]{ 1 }; 94 arguments[2] = new Integer (0); 95 arguments[3] = new Integer (1); 96 arguments[4] = new Integer (1); 97 } else if ("addOne".equals(methodName)) { 98 arguments[0] = new int[]{ 1 }; 99 arguments[1] = new Integer (0); 100 arguments[2] = new Integer (0); 101 arguments[3] = new Integer (1); 102 } else if ("primitiveLeftShift".equals(methodName) || "primitiveRightShift".equals(methodName)) { 103 arguments[0] = new int[]{ 1 }; 104 arguments[1] = new Integer (1); 105 arguments[2] = new Integer (1); 106 } else if ("javaIncrement".equals(methodName)) { 107 arguments[0] = new int[]{ 1 }; 108 } else if ("jacobiSymbol".equals(methodName)) { 109 arguments[0] = new Integer (1); 110 arguments[1] = refInt; 111 } 112 113 return arguments; 114 } 115 116 private void invokeAllBigIntegerConstructors() { 117 Class bClazz = BigInteger .class; 118 Constructor [] constructors = bClazz.getConstructors(); 119 for (int i=0; i<constructors.length; i++) { 120 try { 121 invokeConstructor(constructors[i]); 122 } catch (IllegalArgumentException e) { 123 throw new TCRuntimeException(e); 124 } catch (InstantiationException e) { 125 throw new TCRuntimeException(e); 126 } catch (IllegalAccessException e) { 127 throw new TCRuntimeException(e); 128 } catch (InvocationTargetException e) { 129 throw new TCRuntimeException(e); 130 } 131 } 132 } 133 134 private void invokeConstructor(Constructor constructor) throws IllegalArgumentException , InstantiationException , IllegalAccessException , InvocationTargetException { 135 Class [] parameterTypes = constructor.getParameterTypes(); 136 if (parameterTypes[0] != TCObject.class) { 137 constructor.newInstance(getArguments(parameterTypes)); 138 } 139 } 140 141 private Object [] getArguments(Class [] parameterTypes) { 142 Object [] arguments = new Object [parameterTypes.length]; 143 switch (parameterTypes.length) { 144 case 1: 145 if (parameterTypes[0] == String .class) { 146 arguments[0] = "100"; 147 } 148 else { 149 arguments[0] = new byte[]{(byte) 100}; 150 } 151 break; 152 case 2: 153 if (parameterTypes[0] == String .class) { 154 arguments[0] = "100"; 155 arguments[1] = new Integer (10); 156 } 157 else if (parameterTypes[1] == Random .class) { 158 arguments[0] = new Integer (10); 159 arguments[1] = new Random (); 160 } 161 else { 162 arguments[0] = new Integer (refInt.signum()); 163 arguments[1] = new byte[]{(byte) 100}; 164 } 165 break; 166 case 3: 167 arguments[0] = new Integer (refInt.bitLength()); 168 arguments[1] = new Integer (5); 169 arguments[2] = new Random (); 170 break; 171 } 172 173 return arguments; 174 } 175 } 176 | Popular Tags |