KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > util > ClassUtilities


1 package org.jahia.clipbuilder.html.util;
2
3 import java.lang.reflect.*;
4
5 /**
6  * Provide usefull method for Class Object
7  *
8  *@author Tlili Khaled
9  */

10 public abstract class ClassUtilities {
11     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ClassUtilities.class);
12
13
14     /**
15      * Synchronize values of source/target objects. Warning: the 2 objects have
16      * to got exactly same peer of get and set methods. Only set/get methods
17      * declared in the class of sourceObject are processed.
18      *
19      *@param sourceObject Description of Parameter
20      *@param targetObject Description of Parameter
21      */

22     public static void synchronize(Object JavaDoc sourceObject, Object JavaDoc targetObject) {
23         //Class name
24
Class JavaDoc sourceClass = sourceObject.getClass();
25         Class JavaDoc targetClass = targetObject.getClass();
26
27         try {
28             // Get all the get..() methode of configuration
29
Method[] methods = sourceClass.getMethods();
30             for (int i = 0; i < methods.length; i++) {
31                 Method currentMethod = methods[i];
32                 Class JavaDoc declarationClass = currentMethod.getDeclaringClass();
33
34                 // don't process method in the super classes
35
if (declarationClass.equals(sourceClass)) {
36                     String JavaDoc label = currentMethod.getName();
37                     logger.debug("[ Method " + label + " is in process. ]");
38                     // process only the get method
39
boolean isGetMethod = label.substring(0, 3).equalsIgnoreCase("get");
40                     logger.debug("[ Method " + label + " is a getMthod: " + isGetMethod + " . ]");
41                     if (isGetMethod) {
42                         //execute the getMethod in order to get parameter value
43
Object JavaDoc getResult = currentMethod.invoke((Object JavaDoc)sourceObject, (Object JavaDoc[])null);
44
45                                                 //execute the correponding set method of the current object
46
String JavaDoc setMethodToCall = "set" + label.substring(3);
47                         Object JavaDoc[] values = {getResult};
48                         Class JavaDoc[] paramType = {currentMethod.getReturnType()};
49                         targetClass.getMethod(setMethodToCall, paramType).invoke((Object JavaDoc)targetObject,(Object JavaDoc[]) values);
50                     }
51                 }
52             }
53         }
54
55         catch (NoSuchMethodException JavaDoc ex) {
56             logger.error(" [ Error " + ex.toString() + " ] ");
57             ex.printStackTrace();
58         }
59         catch (InvocationTargetException ex) {
60             logger.error(" [ Error " + ex.toString() + " ] ");
61             ex.printStackTrace();
62         }
63         catch (IllegalArgumentException JavaDoc ex) {
64             logger.error(" [ Error " + ex.toString() + " ] ");
65             ex.printStackTrace();
66         }
67         catch (IllegalAccessException JavaDoc ex) {
68             logger.error(" [ Error " + ex.getMessage() + " ] ");
69             ex.printStackTrace();
70         }
71         catch (SecurityException JavaDoc ex) {
72             logger.error(" [ Error " + ex.getMessage() + " ] ");
73             ex.printStackTrace();
74         }
75
76     }
77
78 }
79
Popular Tags