001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.swing; 005 006 import java.lang.reflect.Field; 007 import java.lang.reflect.InvocationTargetException; 008 import java.lang.reflect.Method; 009 010 import javax.swing.table.DefaultTableModel; 011 import javax.swing.table.TableModel; 012 import javax.swing.tree.DefaultMutableTreeNode; 013 import javax.swing.tree.MutableTreeNode; 014 015 016 /** 017 * @author Pavel Vlasov 018 * 019 * @version $Revision: 1.1 $ 020 */ 021 public class BeanVisualizer { 022 023 public Visualizable toVisualizable(final Object obj) { 024 return new Visualizable() { 025 026 public MutableTreeNode toTree(final String title) { 027 DefaultMutableTreeNode ret=new DefaultMutableTreeNode(obj) { 028 public String toString() { 029 return title+" ["+obj.getClass().getName()+"] "+obj; 030 } 031 }; 032 033 return ret; 034 } 035 036 public TableModel toTable() { 037 int rowCount=2; 038 DefaultTableModel tm=new DefaultTableModel(rowCount,4); 039 tm.setColumnIdentifiers(new String[] {"Property", "Declared type", "Runtime type", "Value"}); 040 041 Class beanClass=obj.getClass(); 042 043 tm.setValueAt("this", 0, 0); 044 tm.setValueAt(beanClass.getName(), 0, 2); 045 tm.setValueAt(obj, 0, 3); 046 047 tm.setValueAt("Hash code", 1, 0); 048 tm.setValueAt("int", 1, 1); 049 tm.setValueAt("int", 1, 2); 050 tm.setValueAt(Integer.toString(obj.hashCode(), Character.MAX_RADIX), 1, 3); 051 052 Method[] methods = beanClass.getMethods(); 053 for (int i=0; i<methods.length; i++) { 054 // getXXX() methods. Object.getClass() is not included. 055 if (!(methods[i].getDeclaringClass().equals(Object.class)) && methods[i].getName().startsWith("get") && methods[i].getParameterTypes().length==0 ) { 056 try { 057 Object value=methods[i].invoke(obj, null); 058 tm.setRowCount(++rowCount); 059 int idx = rowCount-1; 060 tm.setValueAt(methods[i].getName().substring(3), idx, 0); 061 tm.setValueAt(methods[i].getReturnType().getName(), idx, 1); 062 if (value==null) { 063 tm.setValueAt("(null)", idx, 2); 064 tm.setValueAt("(null)", idx, 3); 065 } else { 066 tm.setValueAt(value.getClass().getName(), idx, 2); 067 tm.setValueAt(value, idx, 3); 068 } 069 } catch (IllegalArgumentException e) { 070 e.printStackTrace(); 071 } catch (IllegalAccessException e) { 072 e.printStackTrace(); 073 } catch (InvocationTargetException e) { 074 e.printStackTrace(); 075 } 076 } 077 } 078 079 Field[] fields = beanClass.getFields(); 080 for (int i=0; i<fields.length; i++) { 081 try { 082 Object value=fields[i].get(obj); 083 tm.setRowCount(++rowCount); 084 int idx = rowCount-1; 085 tm.setValueAt(fields[i].getName().substring(3), idx, 0); 086 tm.setValueAt(fields[i].getType().getName(), idx, 1); 087 if (value==null) { 088 tm.setValueAt("(null)", idx, 2); 089 tm.setValueAt("(null)", idx, 3); 090 } else { 091 tm.setValueAt(value.getClass().getName(), idx, 2); 092 tm.setValueAt(value, idx, 3); 093 } 094 } catch (IllegalArgumentException e) { 095 e.printStackTrace(); 096 } catch (IllegalAccessException e) { 097 e.printStackTrace(); 098 } 099 } 100 101 return tm; 102 } 103 104 }; 105 106 } 107 }