package javaeva.gui; /* * Title: JavaEvA * Description: * Copyright: Copyright (c) 2003 * Company: University of Tuebingen, Computer Architecture * @author Holger Ulmer, Felix Streichert, Hannes Planatscher * @version: $Revision: 202 $ * $Date: 2007-10-25 16:12:49 +0200 (Thu, 25 Oct 2007) $ * $Author: mkron $ */ /*==========================================================================* * IMPORTS *==========================================================================*/ import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Array; import java.lang.reflect.Method; import java.util.List; /* * ==========================================================================* * CLASS DECLARATION * ========================================================================== */ public class BeanInspector { public static boolean TRACE = false; // public static int step = 0; // public static String check(String s) { // // s=s.replace('$','_'); // s=s.replace(';','_'); //// String ret = null; //// try { //// RE r = new RE("\\["); //// ret = r.subst(s,""); //// //ret.substring(); //// //ret //// } catch (Exception e) {e.getMessage();}; //// System.out.println("s="+s+" ret"+ret); // if (s.equals("[D")) return "Double_Array"; // if (s.startsWith("[D")) return s.substring(2); // if (s.startsWith("[L")) return s.substring(2); // // return s; // } public static boolean equalProperties(Object Target_1, Object Target_2) { if (Target_1 == null || Target_2 == null) { System.out.println(""); return false; } System.out.println("equalProperties: " + Target_1.getClass().getName() + " " + Target_2.getClass().getName()); if (Target_1.getClass().getName().equals(Target_2.getClass().getName()) == false) { System.out.println(""); return false; } // compare each of the properties !! BeanInfo Info_1 = null; BeanInfo Info_2 = null; PropertyDescriptor[] Properties_1 = null; PropertyDescriptor[] Properties_2 = null; try { Info_1 = Introspector.getBeanInfo(Target_1.getClass()); Info_2 = Introspector.getBeanInfo(Target_2.getClass()); Properties_1 = Info_1.getPropertyDescriptors(); Properties_2 = Info_2.getPropertyDescriptors(); Info_1.getMethodDescriptors(); } catch (IntrospectionException ex) { System.out.println("BeanTest: Couldn't introspect !!!!!!!!!"); return false; } boolean BeansInside = false; boolean BeansEqual = true; for (int i = 0; i < Properties_1.length; i++) { if (Properties_1[i].isHidden() || Properties_1[i].isExpert()) { continue; } //String name = Properties_1[i].getDisplayName(); //System.out.println("name = "+name ); //Class type = Properties_1[i].getPropertyType(); //System.out.println("type = "+type.getName() ); Method getter_1 = Properties_1[i].getReadMethod(); Method getter_2 = Properties_2[i].getReadMethod(); Method setter_1 = Properties_1[i].getWriteMethod(); // Only display read/write properties. if (getter_1 == null || setter_1 == null) { continue; } System.out.println("getter_1 = " + getter_1.getName() + " getter_2 = " + getter_2.getName()); //System.out.println("type = "+type.getName() ); Object args_1[] = {}; Object args_2[] = {}; //System.out.println("m_Target"+m_Target.toString()); try { Object value_1 = getter_1.invoke(Target_1, args_1); Object value_2 = getter_2.invoke(Target_2, args_2); BeansInside = true; if (BeanInspector.equalProperties(value_1, value_2) == false) { BeansEqual = false; } } catch (Exception e) { System.out.println(" BeanTest.equalProperties " + e.getMessage()); } } if (BeansInside == true) { return BeansEqual; } // here we have Integer or Double ... if (Target_1 instanceof Integer || Target_1 instanceof Boolean || Target_1 instanceof Float || Target_1 instanceof Double || Target_1 instanceof Long || Target_1 instanceof String) { return Target_1.equals(Target_2); } System.out.println(" Attention no match !!!"); return true; } /** * Collect the accessible properties of an object and their values in a string. * * @param Target Description of the Parameter * @return Description of the Return Value */ public static String toString(Object Target) { String ret = ""; if (Target == null) return "null"; // try the object itself if (Target instanceof String) return (String)Target; // directly return a string object Class type = Target.getClass(); if (type.isArray()) { // handle the array case StringBuffer sbuf = new StringBuffer("[ "); int len = Array.getLength(Target); for (int i=0; i