38 39 private Class<?> cl; 40 private Object instance; 41 42 /** 43 * Constructor. 44 * @param o Object to work with. 45 */ 46 public ClassReference(Object o) { 47 super(); 48 instance = o; 49 cl = o.getClass(); 50 } 51 52 /** 53 * Contructor. 54 * The object created by this constructor can be used 55 * to access static methods and fields only. 56 * 57 * @param className name of class 58 * @exception ClassNotFoundException 59 */ 60 public ClassReference(String className) 61 throws ClassNotFoundException { 62 super(); 63 cl = Class.forName(className); 64 instance = null; 65 } 66 67 /** 68 * Executes class's <code>main(java.lang.String[])</code> method 69 * with a zero-length <code>java.lang.String</code> array 70 * as a parameter. 71 * 72 * @exception NoSuchMethodException 73 * @exception InvocationTargetException 74 */ 75 public void startApplication() 76 throws InvocationTargetException, NoSuchMethodException { 77 String[] params = new String[0]; 78 startApplication(params); 79 } 80 81 /** 82 * Executes class's <code>main(java.lang.String[])</code> method. 83 * 84 * @param params The <code>java.lang.String</code> array to pass 85 * to <code>main(java.lang.String[])</code>. 86 * @exception NoSuchMethodException 87 * @exception InvocationTargetException 88 */ 89 public void startApplication(String[] params) 90 throws InvocationTargetException, NoSuchMethodException { 91 String[] real_params; 92 if (params == null) { 93 real_params = new String[0]; 94 } else { 95 real_params = params; 96 } 97 String[][] methodParams = {real_params}; 98 Class[] classes = {real_params.getClass()}; 99 try { 100 invokeMethod("main", methodParams, classes); 101 } catch (IllegalAccessException e) { 102 e.printStackTrace(); 103 } catch (IllegalStateException e) { 104 e.printStackTrace(); 105 } 106 } 107 108 /** 109 * Locates method by name and parameter types and executes it. 110 * 111 * @param method_name Name of method. 112 * @param params Method parameters. 113 * @param params_classes Method parameters types. 114 * @return the return value from an invocation of the Method.<BR> 115 * If <code>method_name</code> method is void, <code>null</code> is returned.<BR> 116 * If <code>method_name</code> method returns a primitive type, then 117 * return wrapper class instance. 118 * @throws InvocationTargetException when the invoked method throws an exception. 119 * @throws NoSuchMethodException when the method cannot be found. 120 * @throws IllegalAccessException when access to the class or method is lacking. 121 * @throws SecurityException if access to the package or method is denied. 122 */ 123 public Object invokeMethod(String method_name, Object[] params, Class<?>[] params_classes) 124 throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { 125 if (params == null) { 126 params = new Object[0]; 127 } 128 if (params_classes == null) { 129 params_classes = new Class<?>[0]; 130 } 131 Method method = cl.getMethod(method_name, 132 params_classes); 133 return (method.invoke(instance, params)); 134 } 135 | 38 39 private Class<?> cl; 40 private Object instance; 41 42 /** 43 * Constructor. 44 * @param o Object to work with. 45 */ 46 public ClassReference(Object o) { 47 super(); 48 instance = o; 49 cl = o.getClass(); 50 } 51 52 /** 53 * Contructor. 54 * The object created by this constructor can be used 55 * to access static methods and fields only. 56 * 57 * @param className name of class 58 * @throws ClassNotFoundException todo document 59 */ 60 public ClassReference(String className) 61 throws ClassNotFoundException { 62 super(); 63 cl = Class.forName(className); 64 instance = null; 65 } 66 67 /** 68 * Executes class's <code>main(java.lang.String[])</code> method 69 * with a zero-length <code>java.lang.String</code> array 70 * as a parameter. 71 * 72 * @throws NoSuchMethodException when the method cannot be found. 73 * @throws InvocationTargetException when the invoked method throws an exception. 74 */ 75 public void startApplication() 76 throws InvocationTargetException, NoSuchMethodException { 77 String[] params = new String[0]; 78 startApplication(params); 79 } 80 81 /** 82 * Executes class's <code>main(java.lang.String[])</code> method. 83 * 84 * @param params The <code>java.lang.String</code> array to pass 85 * to <code>main(java.lang.String[])</code>. 86 * @throws NoSuchMethodException when the method cannot be found. 87 * @throws InvocationTargetException when the invoked method throws an exception. 88 */ 89 public void startApplication(String[] params) 90 throws InvocationTargetException, NoSuchMethodException { 91 String[] real_params; 92 if (params == null) { 93 real_params = new String[0]; 94 } else { 95 real_params = params; 96 } 97 String[][] methodParams = {real_params}; 98 Class[] classes = {real_params.getClass()}; 99 try { 100 invokeMethod("main", methodParams, classes); 101 } catch (IllegalAccessException e) { 102 e.printStackTrace(); 103 } catch (IllegalStateException e) { 104 e.printStackTrace(); 105 } 106 } 107 108 /** 109 * Locates method by name and parameter types and executes it. 110 * 111 * @param method_name Name of method. 112 * @param params Method parameters. 113 * @param params_classes Method parameters types. 114 * @return the return value from an invocation of the Method.<br> 115 * If <code>method_name</code> method is void, <code>null</code> is returned.<br> 116 * If <code>method_name</code> method returns a primitive type, then 117 * return wrapper class instance. 118 * @throws InvocationTargetException when the invoked method throws an exception. 119 * @throws NoSuchMethodException when the method cannot be found. 120 * @throws IllegalAccessException when access to the class or method is lacking. 121 * @throws SecurityException if access to the package or method is denied. 122 */ 123 public Object invokeMethod(String method_name, Object[] params, Class<?>[] params_classes) 124 throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { 125 if (params == null) { 126 params = new Object[0]; 127 } 128 if (params_classes == null) { 129 params_classes = new Class<?>[0]; 130 } 131 Method method = cl.getMethod(method_name, 132 params_classes); 133 return (method.invoke(instance, params)); 134 } 135 |