Search in sources :

Example 31 with IntrospectionException

use of java.beans.IntrospectionException in project kotlin by JetBrains.

the class Args method usage.

/**
     * Generate usage information based on the target annotations.
     *
     * @param errStream A {@link java.io.PrintStream} to print the usage information to.
     * @param target    An instance or class.
     */
public static void usage(PrintStream errStream, Object target) {
    Class<?> clazz;
    if (target instanceof Class) {
        clazz = (Class) target;
    } else {
        clazz = target.getClass();
    }
    errStream.println("Usage: " + clazz.getName());
    for (Class<?> currentClazz = clazz; currentClazz != null; currentClazz = currentClazz.getSuperclass()) {
        for (Field field : currentClazz.getDeclaredFields()) {
            fieldUsage(errStream, target, field);
        }
    }
    try {
        BeanInfo info = Introspector.getBeanInfo(clazz);
        for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
            propertyUsage(errStream, target, pd);
        }
    } catch (IntrospectionException e) {
    // If its not a JavaBean we ignore it
    }
}
Also used : Field(java.lang.reflect.Field) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 32 with IntrospectionException

use of java.beans.IntrospectionException in project goci by EBISPOT.

the class EmbeddableDocument method embed.

public void embed(Document document) {
    try {
        Set<String> excludeNames = new HashSet<>();
        BeanInfo objectInfo = Introspector.getBeanInfo(Document.class);
        for (PropertyDescriptor descriptor : objectInfo.getPropertyDescriptors()) {
            excludeNames.add(descriptor.getName());
        }
        try {
            BeanInfo docInfo = Introspector.getBeanInfo(document.getClass());
            for (PropertyDescriptor descriptor : docInfo.getPropertyDescriptors()) {
                if (!excludeNames.contains(descriptor.getName())) {
                    boolean isExcluded = false;
                    Method readMethod = descriptor.getReadMethod();
                    readMethod.setAccessible(true);
                    if (readMethod.isAnnotationPresent(NonEmbeddableField.class)) {
                        isExcluded = true;
                    } else {
                        try {
                            Field field = document.getClass().getDeclaredField(descriptor.getName());
                            field.setAccessible(true);
                            if (field.isAnnotationPresent(NonEmbeddableField.class)) {
                                isExcluded = true;
                            }
                        } catch (NoSuchFieldException e) {
                        // no field with this name, skip
                        }
                    }
                    if (!isExcluded) {
                        try {
                            // determine method to update this document with doc being embedded
                            Method propertyAdderMethod = findPropertyAdder(descriptor);
                            // invoke read method on passed document
                            Object fieldToEmbed = descriptor.getReadMethod().invoke(document);
                            propertyAdderMethod.invoke(this, fieldToEmbed);
                        } catch (InvocationTargetException | IllegalAccessException e) {
                            throw new DocumentEmbeddingException("Failed to read property '" + descriptor.getName() + "'", e);
                        }
                    }
                }
            }
        } catch (IntrospectionException e) {
            throw new DocumentEmbeddingException("Failed to analyse document in preparation for embedding", e);
        }
    } catch (IntrospectionException e) {
        throw new DocumentEmbeddingException("Failed to read Object.class when determining which properties to exclude", e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) DocumentEmbeddingException(uk.ac.ebi.spot.goci.exception.DocumentEmbeddingException) HashSet(java.util.HashSet)

Example 33 with IntrospectionException

use of java.beans.IntrospectionException in project goci by EBISPOT.

the class EmbeddableDocumentTest method testIntrospection.

@Test
public void testIntrospection() {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(studyDoc.getClass());
        for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
            System.out.println("Display name: " + pd.getDisplayName());
            System.out.println("Name: " + pd.getName());
            System.out.println("Read method: " + pd.getReadMethod());
            System.out.println("\t" + pd);
        }
    } catch (IntrospectionException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Test(org.junit.Test)

Example 34 with IntrospectionException

use of java.beans.IntrospectionException 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 35 with IntrospectionException

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

Aggregations

IntrospectionException (java.beans.IntrospectionException)112 PropertyDescriptor (java.beans.PropertyDescriptor)66 Method (java.lang.reflect.Method)47 BeanInfo (java.beans.BeanInfo)45 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)24 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)17 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 Field (java.lang.reflect.Field)9 EventSetDescriptor (java.beans.EventSetDescriptor)7 List (java.util.List)6 Map (java.util.Map)6 MessageFormat (java.text.MessageFormat)5 UUID (java.util.UUID)5 Assert (org.springframework.util.Assert)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Optional (java.util.Optional)4 RecursiveChildrenListManager (cern.gp.explorer.test.helpers.RecursiveChildrenListManager)3 SimpleDemoBean (cern.gp.explorer.test.helpers.SimpleDemoBean)3