View Javadoc
1 /* ------------------------------------------------------------------- 2 * Java source file for the class ThreadTask 3 * 4 * Copyright (c), 2002, Masahiro Takatsuka. 5 * All Rights Researved. 6 * 7 * Original Author: Masahiro Takatsuka (masa@jbeans.net) 8 * $Author: takatsukam $ 9 * 10 * $Date: 2003/07/25 04:51:46 $ 11 * 12 * $Id: ThreadTask.java,v 1.1.1.1 2003/07/25 04:51:46 takatsukam Exp $ 13 * 14 * Reference: Document no: 15 * ___ ___ 16 * 17 * To Do: 18 * ___ 19 * 20 ------------------------------------------------------------------- */ 21 22 /* --------------------------- Package ---------------------------- */ 23 package net.jbeans.lang; 24 25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */ 26 import java.lang.reflect.*; 27 28 /*==================================================================== 29 Implementation of class ThreadTask 30 ====================================================================*/ 31 /*** 32 * Task object is an object to hold a method name and argument objects. 33 * This object is used(handled) by ThreadObjectImpl. 34 * 35 * @version $Revision: 1.1.1.1 $ 36 * @author Masahiro Takatsuka (masa@jbeans.net) 37 * @see Runnable 38 */ 39 40 public class ThreadTask implements Runnable { 41 /*** 42 * The object executing the specified. 43 */ 44 protected Object object; 45 46 /*** 47 * The method to be executed. 48 */ 49 protected Method method; 50 51 /*** 52 * Arguments for the method. 53 */ 54 protected Object[] args; 55 56 /*** 57 * Construct a new ThreadTask object. 58 */ 59 public ThreadTask() { 60 super(); 61 this.object = null; 62 this.method = null; 63 this.args = null; 64 } 65 66 /*** 67 * Construct a new ThreadTask object. 68 * 69 * @param methodName a name of the method to be executed. 70 * @param args an array of objects holding arguments for the method. 71 */ 72 public ThreadTask(Object object, Method method, Object[] args) { 73 this(); 74 this.object = object; 75 this.method = method; 76 this.args = args; 77 } 78 79 /*** 80 * Construct a new ThreadTask object. 81 * 82 * @param methodName a name of the method to be executed. 83 * @param args an array of objects holding arguments for the method. 84 */ 85 public void setThreadTask(Object object, Method method, Object[] args) { 86 this.object = object; 87 this.method = method; 88 this.args = args; 89 } 90 91 public void setObject(Object object) { 92 this.object = object; 93 } 94 95 /*** 96 * Returns the object. 97 */ 98 public Object getObject() { 99 return this.object; 100 } 101 102 public void setMethod(Method method) { 103 this.method = method; 104 } 105 106 /*** 107 * Returns the method object. 108 */ 109 public Method getMethod() { 110 return this.method; 111 } 112 113 public void setArgs(Object[] args) { 114 this.args = args; 115 } 116 117 /*** 118 * Returns the arguments. 119 */ 120 public Object[] getArgs() { 121 return this.args; 122 } 123 124 /*** 125 * The core of threaded version of makeObjectsPerform methods. 126 */ 127 public void run() { 128 if (this.object == null || this.method == null) { 129 return; 130 } 131 /*** 132 * I decided not to do following sanity check. 133 * Masahiro Takatsuka/2000-May-12 134 135 Class c = this.object.getClass(); // get the Class object. 136 Class[] parameters; 137 138 if (this.args == null) { // no parameter... 139 parameters = new Class[0]; 140 } else { // get fully-qualified names of parameters 141 parameters = new Class[this.args.length]; 142 for (int i = 0; i < this.args.length; i++) { 143 parameters[i] = this.args[i].getClass(); 144 } 145 } 146 String methodname = this.method.getName(); 147 try{ 148 Method m = c.getMethod(methodname, parameters); // get the instance of the method 149 }catch(NoSuchMethodException e){ // want to catch this exception in order to return false. 150 e.printStackTrace(); 151 return; 152 } 153 */ 154 try { 155 if (this.args == null) { 156 this.args = new Object[0]; // set up arguments. 157 } 158 this.method.invoke(this.object, this.args); // invoke the method. 159 } catch(IllegalAccessException ex) { 160 System.err.println("IllegalAccessException: "+ex+" from: "+this+"("+this.method.getName()+")"); 161 ex.printStackTrace(); 162 } catch(IllegalArgumentException ex) { 163 System.err.println("IllegalArgumentException: "+ex+" from: "+this+"("+this.method.getName()+")"); 164 ex.printStackTrace(); 165 } catch(InvocationTargetException ex) { 166 System.err.println("InvocationTargetException: "+ex+" from: "+this+"("+this.method.getName()+")"); 167 ex.printStackTrace(); 168 } 169 } 170 }

This page was automatically generated by Maven