Search in sources :

Example 16 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project jdk8u_jdk by JetBrains.

the class Test6194788 method main.

public static void main(String[] args) throws IntrospectionException {
    test(Grand.class, new PropertyDescriptor("index", Grand.class));
    test(Grand.class, new IndexedPropertyDescriptor("name", Grand.class, null, null, "getName", "setName"));
    test(Parent.class, new PropertyDescriptor("parentIndex", Parent.class));
    test(Parent.class, new IndexedPropertyDescriptor("parentName", Parent.class));
    test(Child.class, new PropertyDescriptor("childIndex", Child.class));
    test(Child.class, new IndexedPropertyDescriptor("childName", Child.class));
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor)

Example 17 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project jdk8u_jdk by JetBrains.

the class Test6660539 method main.

public static void main(String[] args) throws Exception {
    for (PropertyDescriptor pd : getPropertyDescriptors()) {
        pd.setDisplayName(NAME);
    }
    ThreadGroup group = new ThreadGroup(NAME);
    Thread thread = new Thread(group, new Test6660539());
    thread.start();
    thread.join();
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor)

Example 18 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project jdk8u_jdk by JetBrains.

the class Test6707231 method test.

private static void test(Class<?> actual, Class<?> expected) {
    PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(actual, "value");
    Class<?> getter = pd.getReadMethod().getDeclaringClass();
    Class<?> setter = pd.getWriteMethod().getDeclaringClass();
    if ((getter != expected) || (setter != expected)) {
        throw new Error(actual.getName());
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor)

Example 19 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project beam by apache.

the class PipelineOptionsFactory method getPropertyDescriptors.

/**
   * This method is meant to emulate the behavior of {@link Introspector#getBeanInfo(Class, int)}
   * to construct the list of {@link PropertyDescriptor}.
   *
   * <p>TODO: Swap back to using Introspector once the proxy class issue with AppEngine is
   * resolved.
   */
private static List<PropertyDescriptor> getPropertyDescriptors(Set<Method> methods, Class<? extends PipelineOptions> beanClass) throws IntrospectionException {
    SortedMap<String, Method> propertyNamesToGetters = new TreeMap<>();
    for (Map.Entry<String, Method> entry : PipelineOptionsReflector.getPropertyNamesToGetters(methods).entries()) {
        propertyNamesToGetters.put(entry.getKey(), entry.getValue());
    }
    List<PropertyDescriptor> descriptors = Lists.newArrayList();
    List<TypeMismatch> mismatches = new ArrayList<>();
    Set<String> usedDescriptors = Sets.newHashSet();
    /*
     * Add all the getter/setter pairs to the list of descriptors removing the getter once
     * it has been paired up.
     */
    for (Method method : methods) {
        String methodName = method.getName();
        if (!methodName.startsWith("set") || method.getParameterTypes().length != 1 || method.getReturnType() != void.class) {
            continue;
        }
        String propertyName = Introspector.decapitalize(methodName.substring(3));
        Method getterMethod = propertyNamesToGetters.remove(propertyName);
        // Validate that the getter and setter property types are the same.
        if (getterMethod != null) {
            Type getterPropertyType = getterMethod.getGenericReturnType();
            Type setterPropertyType = method.getGenericParameterTypes()[0];
            if (!getterPropertyType.equals(setterPropertyType)) {
                TypeMismatch mismatch = new TypeMismatch();
                mismatch.propertyName = propertyName;
                mismatch.getterPropertyType = getterPropertyType;
                mismatch.setterPropertyType = setterPropertyType;
                mismatches.add(mismatch);
                continue;
            }
        }
        // getter and setter).
        if (!usedDescriptors.contains(propertyName)) {
            descriptors.add(new PropertyDescriptor(propertyName, getterMethod, method));
            usedDescriptors.add(propertyName);
        }
    }
    throwForTypeMismatches(mismatches);
    // Add the remaining getters with missing setters.
    for (Map.Entry<String, Method> getterToMethod : propertyNamesToGetters.entrySet()) {
        descriptors.add(new PropertyDescriptor(getterToMethod.getKey(), getterToMethod.getValue(), null));
    }
    return descriptors;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) TreeMap(java.util.TreeMap)

Example 20 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project lucene-solr by apache.

the class MetricUtils method addMXBeanMetrics.

/**
   * Creates a set of metrics (gauges) that correspond to available bean properties for the provided MXBean.
   * @param obj an instance of MXBean
   * @param intf MXBean interface, one of {@link PlatformManagedObject}-s
   * @param consumer consumer for created names and metrics
   * @param <T> formal type
   */
public static <T extends PlatformManagedObject> void addMXBeanMetrics(T obj, Class<? extends T> intf, String prefix, BiConsumer<String, Metric> consumer) {
    if (intf.isInstance(obj)) {
        BeanInfo beanInfo;
        try {
            beanInfo = Introspector.getBeanInfo(intf, intf.getSuperclass(), Introspector.IGNORE_ALL_BEANINFO);
        } catch (IntrospectionException e) {
            LOG.warn("Unable to fetch properties of MXBean " + obj.getClass().getName());
            return;
        }
        for (final PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
            final String name = desc.getName();
            // test if it works at all
            try {
                desc.getReadMethod().invoke(obj);
                // worked - consume it
                final Gauge<?> gauge = () -> {
                    try {
                        return desc.getReadMethod().invoke(obj);
                    } catch (InvocationTargetException ite) {
                        // ignore (some properties throw UOE)
                        return null;
                    } catch (IllegalAccessException e) {
                        return null;
                    }
                };
                String metricName = MetricRegistry.name(prefix, name);
                consumer.accept(metricName, gauge);
            } catch (Exception e) {
            // didn't work, skip it...
            }
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)841 Method (java.lang.reflect.Method)400 BeanInfo (java.beans.BeanInfo)342 IntrospectionException (java.beans.IntrospectionException)191 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)171 SimpleBeanInfo (java.beans.SimpleBeanInfo)142 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)141 InvocationTargetException (java.lang.reflect.InvocationTargetException)109 ArrayList (java.util.ArrayList)90 HashMap (java.util.HashMap)66 Field (java.lang.reflect.Field)60 Map (java.util.Map)52 Test (org.junit.Test)39 IOException (java.io.IOException)35 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)34 List (java.util.List)33 Type (java.lang.reflect.Type)21 HashSet (java.util.HashSet)21 Set (java.util.Set)20 LinkedHashMap (java.util.LinkedHashMap)19