Prints the values of java bean properties to the console. - Java java.lang

Java examples for java.lang:Throwable

Description

Prints the values of java bean properties to the console.

Demo Code

/*******************************************************************************
 * Copyright (c) 2006, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   ww w  .  jav a2s  .c  o m
 *     Mike Kucera (IBM Corporation) - initial API and implementation
 *******************************************************************************/
//package com.java2s;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

public class Main {
    /**
     * Prints the values of javabean properties to the console.
     * This method is not recursive, it does not print nested properties.
     * 
     * Example of usage: 
     * 
     * IResource resource = ...;
     * DebugUtil.printObjectProperties(resource);
     * DebugUtil.printObjectProperties(resource.getResourceAttributes());
     * @since 5.1
     */
    public static void printObjectProperties(Object obj) {
        try {
            System.out.println("Object: " + obj);
            BeanInfo info = Introspector.getBeanInfo(obj.getClass());

            for (PropertyDescriptor propertyDescriptor : info
                    .getPropertyDescriptors()) {
                Method getter = propertyDescriptor.getReadMethod();
                try {
                    System.out.println("  " + getter.getName() + "="
                            + getter.invoke(obj, new Object[0]));
                } catch (Exception e) {
                }
            }
        } catch (IntrospectionException e) {
        }
    }
}

Related Tutorials