Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/hoobsoft/portal/myClass.java


1   /*
2    * myClass.java
3    *
4    * Created on August 10, 2001, 10:18 AM
5    */
6   
7   package com.hoobsoft.portal;
8   import java.lang.reflect.*;
9   import java.util.*;
10  /**
11   * Uses reflection to call methods of other classes
12   * @author Net@Work (Igor Patishman)
13   * @version 1.0
14   */
15  public class myClass{
16  
17      /**Default Constractor. Creates new myClass */
18      public myClass() {
19      }
20  
21     /**
22      *Method invokes entered method on entered class.
23      *@param classname - Class Name where method will be invoked
24      *@param methodname - Method Name that will be invoked
25      *@param arg - array of parameters for invoking method
26     */
27      public static Object doMethod(String classname, String methodname, Object [] arg) throws Exception{
28          Object objReturn = null;
29          try{
30              Class someclass = Class.forName(classname);
31              Class [] argType;
32              if(arg!=null){
33                  argType = new Class [arg.length];
34                  for(int i=0;i<arg.length;i++){
35                     argType[i] =  arg[i].getClass();
36                  }            
37              }
38              else argType=null;
39              
40              
41              Method method = someclass.getMethod(methodname, argType);
42              objReturn = method.invoke(someclass.newInstance(),arg);
43          }catch(Exception e){throw e;}
44          return objReturn;      
45      }
46  
47     /**
48      *Method invokes entered method on entered class.
49      *@param classname - Class Name where method will be invoked
50      *@param methodname - Method Name that will be invoked
51      *@param argString - string of parameters with "," delimiter for invoking method. 
52     */
53  
54      public String doMethod(String classname, String methodname, String argString) throws Exception{
55          String sReturn = null;
56          Object [] arg = null;
57          int methodIndex = -1;
58  
59          StringTokenizer st = new StringTokenizer(argString, ",");
60          String [] paramValue = new String [st.countTokens()];
61          int j=0;
62          while (st.hasMoreTokens()){ 
63               paramValue[j]=st.nextToken();
64               j++;
65          }
66          
67          try{
68              Class someclass = Class.forName(classname);
69              Method [] methods = someclass.getDeclaredMethods();
70              //here we are looking for matching method
71              for(int i=0;i<methods.length;i++){
72                  //here our method must return only String and takes same number of parameters
73                  // as we passing as String
74                  //System.out.println("Methods Returns: "+methods[i].getReturnType().getName());
75                  if(methods[i].getReturnType().getName().equals("java.lang.String") && 
76                      methods[i].getParameterTypes().length == paramValue.length){
77                     methodIndex=i;
78                     break;
79                  }
80              }
81              if(methodIndex==-1) throw new Exception("No matching methods found!!!");
82              
83              Class [] parameters = methods[methodIndex].getParameterTypes();
84              
85              arg = new Object[paramValue.length];
86              for(int i=0;i<parameters.length;i++){
87                  
88                  if(parameters[i].getName().equals("java.lang.String")){ arg[i]=paramValue[i];}
89                  if(parameters[i].getName().equals("int")){ arg[i]=new Integer(paramValue[i]);}
90                  if(parameters[i].getName().equals("double")){ arg[i]=new Double(paramValue[i]);}
91                  if(parameters[i].getName().equals("long")){ arg[i]=new Long(paramValue[i]);}
92                  if(parameters[i].getName().equals("java.lang.Integer")){ arg[i]=new Integer(paramValue[i]);}
93                  if(parameters[i].getName().equals("java.lang.Double")){ arg[i]=new Double(paramValue[i]);}
94                 
95              }
96              
97              sReturn = (String) methods[methodIndex].invoke(someclass.newInstance(),arg);
98          }catch(Exception e){
99              System.out.println("couldn't call method from doMethod");
100             e.printStackTrace();
101             throw e;
102         }
103         return sReturn;      
104     }
105 
106     
107     
108     
109     public String doMethod(String classname, String methodname) throws Exception{
110         String sReturn = null;
111         Object [] arg = null;
112         int methodIndex = -1;
113         String [] paramValue = new String [0];
114         arg = new Object[0];
115 
116         try{
117             Class someclass = Class.forName(classname);
118             Method [] methods = someclass.getDeclaredMethods();
119             //here we are looking for matching method
120             for(int i=0;i<methods.length;i++){
121                 //here our method must return only String and takes same number of parameters
122                 // as we passing as String
123                 if(methods[i].getReturnType().getName().equals("java.lang.String")){
124                    methodIndex=i;
125                    break;
126                 }
127             }
128             if(methodIndex==-1) throw new Exception("No matching methods found!!!");
129             sReturn = (String) methods[methodIndex].invoke(someclass.newInstance(),arg);
130         }catch(Exception e){
131             System.out.println("Something couldn't call method from doMethod");
132             e.printStackTrace();
133             throw e;
134         }
135         return sReturn;      
136     }
137 
138     
139     //--------------------------------------------------------
140     public static void main(String [] arg){
141         myClass mc = new myClass();
142         try{
143             //System.out.println((String)myClass.doMethod("com.netatwork.portal.xmlTest","getXMLString",args));
144             //System.out.println((String)mc.doMethod("com.hoobsoft.portal.xmlTest","someMethod","test-test,8,1.3"));
145            // System.out.println((String)myClass.doMethod("com.netatwork.portal.xmlTest","getXMLString","another test,31"));
146            // System.out.println((String)mc.doMethod("com.hoobsoft.portal.TempClient","getTemp","11204"));
147             System.out.println((String)mc.doMethod("com.hoobsoft.portal.TempClient","getNews",""));
148             //System.out.println((String)mc.doMethod("com.hoobsoft.portal.Calendar","createCalendarHTML","2001,8,1"));
149            // System.out.println(mc.doMethod("com.hoobsoft.portal.Calendar","getToday"));
150         }catch(Exception e){System.out.println(e);}
151     }     
152 }