1 package org.jahia.clipbuilder.html.util; 2 3 import java.lang.reflect.*; 4 5 10 public abstract class ClassUtilities { 11 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ClassUtilities.class); 12 13 14 22 public static void synchronize(Object sourceObject, Object targetObject) { 23 Class sourceClass = sourceObject.getClass(); 25 Class targetClass = targetObject.getClass(); 26 27 try { 28 Method[] methods = sourceClass.getMethods(); 30 for (int i = 0; i < methods.length; i++) { 31 Method currentMethod = methods[i]; 32 Class declarationClass = currentMethod.getDeclaringClass(); 33 34 if (declarationClass.equals(sourceClass)) { 36 String label = currentMethod.getName(); 37 logger.debug("[ Method " + label + " is in process. ]"); 38 boolean isGetMethod = label.substring(0, 3).equalsIgnoreCase("get"); 40 logger.debug("[ Method " + label + " is a getMthod: " + isGetMethod + " . ]"); 41 if (isGetMethod) { 42 Object getResult = currentMethod.invoke((Object )sourceObject, (Object [])null); 44 45 String setMethodToCall = "set" + label.substring(3); 47 Object [] values = {getResult}; 48 Class [] paramType = {currentMethod.getReturnType()}; 49 targetClass.getMethod(setMethodToCall, paramType).invoke((Object )targetObject,(Object []) values); 50 } 51 } 52 } 53 } 54 55 catch (NoSuchMethodException 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 ex) { 64 logger.error(" [ Error " + ex.toString() + " ] "); 65 ex.printStackTrace(); 66 } 67 catch (IllegalAccessException ex) { 68 logger.error(" [ Error " + ex.getMessage() + " ] "); 69 ex.printStackTrace(); 70 } 71 catch (SecurityException ex) { 72 logger.error(" [ Error " + ex.getMessage() + " ] "); 73 ex.printStackTrace(); 74 } 75 76 } 77 78 } 79 | Popular Tags |