Search in sources :

Example 6 with BeanInfo

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

the class Test4520754 method test4168475.

/**
     * This is a regression test to ensure that 4168475 does not regress.
     */
private static void test4168475(Class type) {
    String[] newPath = { "infos" };
    String[] oldPath = Introspector.getBeanInfoSearchPath();
    Introspector.setBeanInfoSearchPath(newPath);
    BeanInfo info = getBeanInfo(Boolean.TRUE, type);
    Introspector.setBeanInfoSearchPath(oldPath);
    PropertyDescriptor[] pds = info.getPropertyDescriptors();
    if (pds.length != 1) {
        throw new Error("could not find custom BeanInfo for " + type);
    }
    Introspector.flushCaches();
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ComponentBeanInfo(infos.ComponentBeanInfo) BeanInfo(java.beans.BeanInfo)

Example 7 with BeanInfo

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

the class Test4520754 method getBeanInfo.

private static BeanInfo getBeanInfo(Boolean mark, Class type) {
    System.out.println("test=" + mark + " for " + type);
    BeanInfo info;
    try {
        info = Introspector.getBeanInfo(type);
    } catch (IntrospectionException exception) {
        throw new Error("unexpected exception", exception);
    }
    if (info == null) {
        throw new Error("could not find BeanInfo for " + type);
    }
    if (mark != info.getBeanDescriptor().getValue("test")) {
        throw new Error("could not find marked BeanInfo for " + type);
    }
    return info;
}
Also used : ComponentBeanInfo(infos.ComponentBeanInfo) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 8 with BeanInfo

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

the class TestBeanInfo method test.

private static void test(Class<?> type, Class<? extends BeanInfo> expected) {
    BeanInfo actual;
    try {
        actual = Introspector.getBeanInfo(type);
        type = actual.getClass();
        // NON-NLS: method name
        Method method = type.getDeclaredMethod("getTargetBeanInfo");
        method.setAccessible(true);
        actual = (BeanInfo) method.invoke(actual);
    } catch (Exception exception) {
        throw new Error("unexpected error", exception);
    }
    if ((actual == null) && (expected != null)) {
        throw new Error("expected info is not found");
    }
    if ((actual != null) && !actual.getClass().equals(expected)) {
        throw new Error("found unexpected info");
    }
}
Also used : ThirdBeanBeanInfo(infos.ThirdBeanBeanInfo) BeanInfo(java.beans.BeanInfo) SecondBeanBeanInfo(infos.SecondBeanBeanInfo) FirstBeanBeanInfo(beans.FirstBeanBeanInfo) Method(java.lang.reflect.Method)

Example 9 with BeanInfo

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

the class Test5102804 method getReference.

private static Reference getReference() {
    try {
        ClassLoader loader = new Loader();
        Class type = Class.forName(BEAN_NAME, true, loader);
        if (!type.getClassLoader().equals(loader)) {
            throw new Error("Wrong class loader");
        }
        BeanInfo info = Introspector.getBeanInfo(type);
        if (0 != info.getDefaultPropertyIndex()) {
            throw new Error("Wrong bean info found");
        }
        return new WeakReference<Class>(type);
    } catch (IntrospectionException exception) {
        throw new Error("Introspection Error", exception);
    } catch (ClassNotFoundException exception) {
        throw new Error("Class Not Found", exception);
    }
}
Also used : BeanInfo(java.beans.BeanInfo) SimpleBeanInfo(java.beans.SimpleBeanInfo) WeakReference(java.lang.ref.WeakReference) IntrospectionException(java.beans.IntrospectionException) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader)

Example 10 with BeanInfo

use of java.beans.BeanInfo 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

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