Example usage for java.beans Introspector getBeanInfo

List of usage examples for java.beans Introspector getBeanInfo

Introduction

In this page you can find the example usage for java.beans Introspector getBeanInfo.

Prototype

public static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException 

Source Link

Document

Introspect on a Java Bean and learn about all its properties, exposed methods, and events.

Usage

From source file:org.rimudb.DataObjectNode.java

public DataObjectNode(CompoundDatabase cdb, DataObjectNode parentNode, String name, Table table) {

    this.cdb = cdb;
    this.name = name;
    this.table = table;
    this.parentNode = parentNode;

    try {/*from w  ww  .ja  va  2 s.co  m*/
        // get property descriptors to use
        doClass = table.getDataObjectClass();
        pdescMap = new TreeMap<String, PropertyDescriptor>();
        BeanInfo info = Introspector.getBeanInfo(doClass);
        PropertyDescriptor pdesc[] = info.getPropertyDescriptors();
        for (int i = 0; i < pdesc.length; i++) {
            String propname = pdesc[i].getName();
            TableMetaData tableMetaData = table.getTableMetaData();
            ColumnMetaData columnMetaData = tableMetaData.getMetaDataByPropertyName(propname);
            if (columnMetaData != null) {
                // these are properties to keep
                pdescMap.put(propname, pdesc[i]);
            }
        }

        doConst1 = doClass.getConstructor(new Class[] { CompoundDatabase.class });

    } catch (Exception e) {
        log.error("in DataObjectNode.DataObjectNode", e);

    } catch (Error err) {
        log.info("name=" + name + " table=" + table + " doClass=" + doClass);
        log.error("in DataObjectNode.DataObjectNode", err);
        throw err;
    }
}

From source file:EventTracerTest.java

/**
 * Adds event tracers for all events to which this component and its children can listen
 * @param c a component//from w  w  w.  j a  v  a2  s  .  c om
 */
public void add(Component c) {
    try {
        // get all events to which this component can listen
        BeanInfo info = Introspector.getBeanInfo(c.getClass());

        EventSetDescriptor[] eventSets = info.getEventSetDescriptors();
        for (EventSetDescriptor eventSet : eventSets)
            addListener(c, eventSet);
    } catch (IntrospectionException e) {
    }
    // ok not to add listeners if exception is thrown

    if (c instanceof Container) {
        // get all children and call add recursively
        for (Component comp : ((Container) c).getComponents())
            add(comp);
    }
}

From source file:stroom.util.AbstractCommandLineTool.java

private void doTraceArguments(final PrintStream printStream) throws Exception {
    final BeanInfo beanInfo = Introspector.getBeanInfo(this.getClass());
    for (final PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
        // Only do properties with getters
        if (pd.getWriteMethod() != null) {
            // Simple getter ?
            String suffix = " (default)";
            if (map.containsKey(pd.getName())) {
                suffix = " (arg)";
            }//w  ww . j av  a 2s.com
            String value = "";

            if (pd.getReadMethod() != null && pd.getReadMethod().getParameterTypes().length == 0) {
                value = String.valueOf(pd.getReadMethod().invoke(this));
            } else {
                // No simple getter
                Field field = null;
                try {
                    field = this.getClass().getDeclaredField(pd.getName());
                } catch (final NoSuchFieldException nsfex) {
                    // Ignore
                }
                if (field != null) {
                    field.setAccessible(true);
                    value = String.valueOf(field.get(this));
                } else {
                    value = "?";

                }
            }
            printStream.println(StringUtils.rightPad(pd.getName(), maxPropLength) + " = " + value + suffix);
        }
    }
}

From source file:BeanUtility.java

public static Object getProperty(Object o, String propertyName) throws Exception {
    PropertyDescriptor[] pds = Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors();
    for (int i = 0; i < pds.length; i++) {
        if (pds[i].getName().equals(propertyName)) {
            return pds[i].getReadMethod().invoke(o);
        }//from   ww  w  .  j av a 2 s. c  o m
    }
    throw new Exception("Property not found.");
}

From source file:java2typescript.jackson.module.visitors.TSJsonObjectFormatVisitor.java

void addPublicMethods() {

    for (Method method : this.clazz.getDeclaredMethods()) {

        // Only public
        if (!isPublic(method.getModifiers())) {
            continue;
        }//  w w w.  j  a  va 2 s.c o  m

        // Exclude accessors
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
            if (isAccessorMethod(method, beanInfo)) {
                continue;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        if (conf.isIgnoredMethod(method)) {
            continue;
        }

        addMethod(method);
    }
}

From source file:de.unentscheidbar.validation.internal.Beans.java

public static PropertyDescriptor property(Class<?> beanClass, String propertyName) {

    try {//w  ww. ja  v  a 2s .  c  o  m
        BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
        PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors();
        if (propDescriptors == null) {
            throw new IllegalArgumentException("Class " + beanClass.getName()
                    + " does not provide property descriptors in its bean info.");
        }
        for (PropertyDescriptor pd : propDescriptors) {
            if (pd.getName().equals(propertyName)) {
                return pd;
            }
        }
        return null;
    } catch (IntrospectionException e) {
        throw Throwables.propagate(e.getCause());
    }
}

From source file:org.apache.tapestry.enhance.ComponentClassFactory.java

private void buildBeanProperties() {
    BeanInfo info = null;//from  ww w  .  j a  v a  2  s . c  o m

    try {
        info = Introspector.getBeanInfo(_componentClass);

    } catch (IntrospectionException ex) {
        throw new ApplicationRuntimeException(
                Tapestry.format("ComponentClassFactory.unable-to-introspect-class", _componentClass.getName()),
                ex);
    }

    PropertyDescriptor[] descriptors = info.getPropertyDescriptors();

    for (int i = 0; i < descriptors.length; i++) {
        _beanProperties.put(descriptors[i].getName(), descriptors[i]);
    }
}

From source file:com.cloudbees.plugins.credentials.matchers.BeanPropertyMatcher.java

/**
 * {@inheritDoc}/*  ww w.j  a  v  a 2s .  co  m*/
 */
@Override
public boolean matches(@NonNull Credentials item) {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(item.getClass());
        for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
            if (name.equals(pd.getName())) {
                Method readMethod = pd.getReadMethod();
                if (readMethod == null) {
                    return false; // we cannot read it therefore it cannot be a match
                }
                try {
                    Object actual = readMethod.invoke(item);
                    return expected == null ? actual == null : expected.equals(actual);
                } catch (IllegalAccessException e) {
                    return false; // if we cannot access it then it's not a match
                } catch (InvocationTargetException e) {
                    return false; // if we cannot access it then it's not a match
                }
            }
        }
        return false; // if there is no corresponding property then it cannot be a match
    } catch (IntrospectionException e) {
        return false; // if we cannot introspect it then it cannot be a match
    }
}

From source file:com.heren.turtle.server.utils.TransUtils.java

public Object toTransObject(Object obj) {
    if (obj == null)
        return null;
    try {//  w  w w  . j a v  a 2 s .co m
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (!key.equals("class")) {
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);
                if (value instanceof String) {
                    Method setter = property.getWriteMethod();
                    Object newValue = toTrans(value);
                    setter.invoke(obj, newValue);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:gov.nih.nci.cabig.caaers.domain.expeditedfields.ExpeditedReportTreeTest.java

@SuppressWarnings({ "RawUseOfParameterizedType" })
private void assertChildPropertiesExist(TreeNode node, Class nodePropertyClass) throws IntrospectionException {
    BeanInfo beanInfo = Introspector.getBeanInfo(nodePropertyClass);
    for (TreeNode child : node.getChildren()) {
        String childPropName = child.getPropertyName();
        if (childPropName == null) {
            // this child does not map to a property -- push down
            assertChildPropertiesExist(child, nodePropertyClass);

        } else {//from w  ww. jav  a  2s .c o m
            if (childPropName.indexOf('[') >= 0) {
                childPropName = childPropName.substring(0, childPropName.indexOf('['));
            }

            if (childPropName.indexOf(".") > 0) {
                childPropName = childPropName.substring(0, childPropName.indexOf("."));
            }
            // look for matching property
            boolean found = false;
            for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
                if (descriptor.getName().equals(childPropName)) {
                    // found matching descriptor; recurse
                    assertChildPropertiesExist(child, getPropertyType(descriptor));
                    found = true;
                    break;
                }
            }
            if (!found) {
                fail("Did not find property " + childPropName + " in " + nodePropertyClass.getSimpleName()
                        + ".  Properties: " + listNames(beanInfo.getPropertyDescriptors()));
            }
            // check for "other" property, if applicable
            if (child instanceof CodedOrOtherPropertyNode) {
                boolean otherFound = false;
                CodedOrOtherPropertyNode codedOrOther = ((CodedOrOtherPropertyNode) child);
                for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
                    otherFound = pd.getName().equals(codedOrOther.getOtherPropertyName());
                    if (otherFound)
                        break;
                }
                if (!otherFound) {
                    fail("Did not find property " + codedOrOther.getOtherPropertyName() + " ('other' for coded "
                            + childPropName + ')' + " in " + nodePropertyClass.getSimpleName()
                            + ".  Properties: " + listNames(beanInfo.getPropertyDescriptors()));
                }
            }
        }
    }
}