Search in sources :

Example 36 with BeanInfo

use of java.beans.BeanInfo in project geronimo-xbean by apache.

the class XBeanNamespaceHandler method getPropertyDescriptor.

/**
 * Looks up the property decriptor for the given class and property name
 */
protected PropertyDescriptor getPropertyDescriptor(String className, String localName) {
    BeanInfo beanInfo = qnameHelper.getBeanInfo(className);
    if (beanInfo != null) {
        PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < descriptors.length; i++) {
            PropertyDescriptor descriptor = descriptors[i];
            String name = descriptor.getName();
            if (name.equals(localName)) {
                return descriptor;
            }
        }
    }
    return null;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo)

Example 37 with BeanInfo

use of java.beans.BeanInfo in project geronimo-xbean by apache.

the class XBeanQNameHelper method coerceNamespaceAwarePropertyValues.

/**
 * Any namespace aware property values (such as QNames) need to be coerced
 * while we still have access to the XML Element from which its value comes -
 * so lets do that now before we trash the DOM and just have the bean
 * definition.
 */
public void coerceNamespaceAwarePropertyValues(BeanDefinition definition, Element element) {
    if (definition instanceof AbstractBeanDefinition && isQnameIsOnClassPath()) {
        AbstractBeanDefinition bd = (AbstractBeanDefinition) definition;
        // lets check for any QName types
        BeanInfo beanInfo = getBeanInfo(bd.getBeanClassName());
        if (beanInfo != null) {
            PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < descriptors.length; i++) {
                QNameReflectionHelper.coerceNamespaceAwarePropertyValues(bd, element, descriptors, i);
            }
        }
    }
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo)

Example 38 with BeanInfo

use of java.beans.BeanInfo in project jaffa-framework by jaffa-projects.

the class JmsQueueAdmin method createMessageGraph.

/**
 * Molds the input JMS Message into a MessageGraph.
 */
private static MessageGraph createMessageGraph(Message message, String queueName, PropertyFilter pf) throws JMSException, IntrospectionException {
    MessageGraph graph = new MessageGraph();
    graph.setQueueMetaData(createQueueMetaData(queueName, pf));
    if (pf.isFieldIncluded("type"))
        graph.setType(queueName);
    if (pf.isFieldIncluded("messageId"))
        graph.setMessageId(message.getJMSMessageID());
    if (pf.isFieldIncluded("priority"))
        graph.setPriority(new Long(message.getJMSPriority()));
    if (pf.isFieldIncluded("createdOn"))
        graph.setCreatedOn(message.getJMSTimestamp() != 0 ? new DateTime(message.getJMSTimestamp()) : null);
    if (pf.isFieldIncluded("createdBy"))
        graph.setCreatedBy(message.getStringProperty(JmsBrowser.HEADER_USER_ID));
    if (pf.isFieldIncluded("errorMessage"))
        graph.setErrorMessage(message.getStringProperty(JmsBrowser.HEADER_ERROR_DETAILS));
    if (pf.isFieldIncluded("payload") && message instanceof TextMessage)
        graph.setPayload(((TextMessage) message).getText());
    if (pf.isFieldIncluded("hasAdminAccess"))
        graph.setHasAdminAccess(JmsBrowser.hasAdminMessageAccess(queueName));
    if (pf.isFieldIncluded("applicationFields") || pf.isFieldIncluded("technicalFields")) {
        // Generate a Map of header elements, keyed by the name of each header element
        // Names beginning with JMS and jaffa_ will be treated as techincal, while the rest will be assumed to be application-specific
        Map<String, MessageField> applicationFields = new LinkedHashMap<String, MessageField>();
        Map<String, MessageField> technicalFields = new TreeMap<String, MessageField>();
        for (Enumeration e = message.getPropertyNames(); e.hasMoreElements(); ) {
            String name = (String) e.nextElement();
            String value = Formatter.format(message.getObjectProperty(name));
            MessageField messageField = new MessageField();
            messageField.setName(name);
            messageField.setValue(value);
            if (name.startsWith("JMS") || name.startsWith("jaffa_"))
                technicalFields.put(name, messageField);
            else
                applicationFields.put(name, messageField);
        }
        // Treat all the JMS* properties of the Message as technical
        if (pf.isFieldIncluded("technicalFields")) {
            BeanInfo beanInfo = Introspector.getBeanInfo(Message.class);
            if (beanInfo != null) {
                PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
                if (pds != null) {
                    for (PropertyDescriptor pd : pds) {
                        if (pd.getReadMethod() != null && !pd.getPropertyType().isArray()) {
                            String name = pd.getName();
                            try {
                                Object value = pd.getReadMethod().invoke(message);
                                MessageField messageField = new MessageField();
                                messageField.setName(name);
                                messageField.setValue(Formatter.format(value));
                                technicalFields.put(name, messageField);
                            } catch (Exception e) {
                                if (log.isDebugEnabled())
                                    log.debug("Exception thrown while reading Message property: " + name, e);
                            }
                        }
                    }
                }
            }
            if (!technicalFields.isEmpty())
                graph.setTechnicalFields(technicalFields.values().toArray(new MessageField[technicalFields.size()]));
        }
        // Add labels to the application-fields from the configuration-file
        if (pf.isFieldIncluded("applicationFields")) {
            QueueInfo queueInfo = ConfigurationService.getInstance().getQueueInfo(queueName);
            if (queueInfo != null && queueInfo.getDisplayParam() != null) {
                for (DisplayParam displayParam : queueInfo.getDisplayParam()) {
                    if (displayParam.getLabel() != null && applicationFields.containsKey(displayParam.getName()))
                        applicationFields.get(displayParam.getName()).setLabel(MessageHelper.replaceTokens(displayParam.getLabel()));
                }
            }
            if (!applicationFields.isEmpty())
                graph.setApplicationFields(applicationFields.values().toArray(new MessageField[applicationFields.size()]));
        }
    }
    return graph;
}
Also used : QueueInfo(org.jaffa.modules.messaging.services.configdomain.QueueInfo) Enumeration(java.util.Enumeration) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) TreeMap(java.util.TreeMap) DateTime(org.jaffa.datatypes.DateTime) MessageField(org.jaffa.qm.apis.data.MessageField) JMSException(javax.jms.JMSException) IntrospectionException(java.beans.IntrospectionException) LinkedHashMap(java.util.LinkedHashMap) MessageGraph(org.jaffa.qm.apis.data.MessageGraph) DisplayParam(org.jaffa.modules.messaging.services.configdomain.DisplayParam) TextMessage(javax.jms.TextMessage)

Example 39 with BeanInfo

use of java.beans.BeanInfo in project jaffa-framework by jaffa-projects.

the class TransformerUtils method printGraph.

/**
 * Same as printGraph(Object source), except the objectStack lists all the parent
 * objects its printed, and if this is one of them, it stops. This allows detection
 * of possible infinite recusion.
 *
 * @param source      Javabean who's contents should be printed
 * @param objectStack List of objects already traversed
 * @return multi-line string of this beans properties and their values
 */
public static String printGraph(Object source, List objectStack) {
    if (source == null)
        return null;
    // Prevent infinite object recursion
    if (objectStack != null)
        if (objectStack.contains(source))
            return "Object Already Used. " + source.getClass().getName() + '@' + source.hashCode();
        else
            objectStack.add(source);
    else {
        objectStack = new ArrayList();
        objectStack.add(source);
    }
    StringBuffer out = new StringBuffer();
    out.append(source.getClass().getName());
    out.append("\n");
    try {
        BeanInfo sInfo = Introspector.getBeanInfo(source.getClass());
        PropertyDescriptor[] sDescriptors = sInfo.getPropertyDescriptors();
        if (sDescriptors != null && sDescriptors.length != 0)
            for (int i = 0; i < sDescriptors.length; i++) {
                PropertyDescriptor sDesc = sDescriptors[i];
                Method sm = sDesc.getReadMethod();
                if (sm != null && sDesc.getWriteMethod() != null) {
                    if (!sm.isAccessible())
                        sm.setAccessible(true);
                    Object sValue = sm.invoke(source, (Object[]) null);
                    out.append("  ");
                    out.append(sDesc.getName());
                    if (source instanceof GraphDataObject) {
                        if (((GraphDataObject) source).hasChanged(sDesc.getName()))
                            out.append('*');
                    }
                    out.append('=');
                    if (sValue == null)
                        out.append("<--NULL-->\n");
                    else if (sm.getReturnType().isArray() && !sm.getReturnType().getComponentType().isPrimitive()) {
                        StringBuffer out2 = new StringBuffer();
                        out2.append("Array of ");
                        out2.append(sm.getReturnType().getComponentType().getName());
                        out2.append("\n");
                        // Loop through array
                        Object[] a = (Object[]) sValue;
                        for (int j = 0; j < a.length; j++) {
                            out2.append('[');
                            out2.append(j);
                            out2.append("] ");
                            if (a[j] == null)
                                out2.append("<--NULL-->");
                            else if (GraphDataObject.class.isAssignableFrom(a[j].getClass()))
                                out2.append(((GraphDataObject) a[j]).toString(objectStack));
                            else
                                // out2.append(StringHelper.linePad(a[j].toString(), 4, " ",true));
                                out2.append(a[j].toString());
                        }
                        out.append(StringHelper.linePad(out2.toString(), 4, " ", true));
                    } else {
                        if (GraphDataObject.class.isAssignableFrom(sValue.getClass()))
                            out.append(StringHelper.linePad(((GraphDataObject) sValue).toString(objectStack), 4, " ", true));
                        else {
                            out.append(StringHelper.linePad(sValue.toString(), 4, " ", true));
                            out.append("\n");
                        }
                    }
                }
            }
    } catch (IllegalAccessException e) {
        TransformException me = new TransformException(TransformException.ACCESS_ERROR, "???", e.getMessage());
        log.error(me.getLocalizedMessage(), e);
    // throw me;
    } catch (InvocationTargetException e) {
        TransformException me = new TransformException(TransformException.INVOCATION_ERROR, "???", e);
        log.error(me.getLocalizedMessage(), me.getCause());
    // throw me;
    } catch (IntrospectionException e) {
        TransformException me = new TransformException(TransformException.INTROSPECT_ERROR, "???", e.getMessage());
        log.error(me.getLocalizedMessage(), e);
    // throw me;
    }
    return out.toString();
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) ArrayList(java.util.ArrayList) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) GraphDataObject(org.jaffa.soa.graph.GraphDataObject)

Example 40 with BeanInfo

use of java.beans.BeanInfo in project yamcs-studio by yamcs.

the class DefaultWidgetIntrospector method getBeanInfo.

public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
    Introspector.flushFromCaches(beanClass);
    BeanInfo bi = Introspector.getBeanInfo(beanClass);
    BeanDescriptor bd = bi.getBeanDescriptor();
    MethodDescriptor[] mds = bi.getMethodDescriptors();
    EventSetDescriptor[] esds = bi.getEventSetDescriptors();
    PropertyDescriptor[] pds = bi.getPropertyDescriptors();
    List<PropertyDescriptor> filteredPDList = new ArrayList<PropertyDescriptor>();
    List<String> nonPropList = Arrays.asList(getNonProperties());
    for (PropertyDescriptor pd : pds) {
        if (!nonPropList.contains(pd.getName()) && pd.getWriteMethod() != null && pd.getReadMethod() != null)
            filteredPDList.add(pd);
    }
    int defaultEvent = bi.getDefaultEventIndex();
    int defaultProperty = bi.getDefaultPropertyIndex();
    return new GenericBeanInfo(bd, esds, defaultEvent, filteredPDList.toArray(new PropertyDescriptor[filteredPDList.size()]), defaultProperty, mds, null);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) ArrayList(java.util.ArrayList) MethodDescriptor(java.beans.MethodDescriptor) EventSetDescriptor(java.beans.EventSetDescriptor) BeanDescriptor(java.beans.BeanDescriptor)

Aggregations

BeanInfo (java.beans.BeanInfo)420 PropertyDescriptor (java.beans.PropertyDescriptor)328 Method (java.lang.reflect.Method)217 SimpleBeanInfo (java.beans.SimpleBeanInfo)167 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)161 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)148 IntrospectionException (java.beans.IntrospectionException)115 InvocationTargetException (java.lang.reflect.InvocationTargetException)38 HashMap (java.util.HashMap)33 Test (org.junit.jupiter.api.Test)33 ArrayList (java.util.ArrayList)30 Field (java.lang.reflect.Field)17 Map (java.util.Map)17 Test (org.junit.Test)13 EventSetDescriptor (java.beans.EventSetDescriptor)12 MethodDescriptor (java.beans.MethodDescriptor)12 List (java.util.List)11 BeanDescriptor (java.beans.BeanDescriptor)10 HashSet (java.util.HashSet)8 Introspector (java.beans.Introspector)7