Search in sources :

Example 76 with BeanInfo

use of java.beans.BeanInfo in project opennars by opennars.

the class DefaultBeanInfoResolver method getBeanInfo.

public BeanInfo getBeanInfo(Class clazz) {
    if (clazz == null) {
        return null;
    }
    String classname = clazz.getName();
    // look for .impl.basic., remove it and call getBeanInfo(class)
    int index = classname.indexOf(".impl.basic");
    if (index != -1 && classname.endsWith("Basic")) {
        classname = classname.substring(0, index) + classname.substring(index + ".impl.basic".length(), classname.lastIndexOf("Basic"));
        try {
            return getBeanInfo(Class.forName(classname));
        } catch (ClassNotFoundException e) {
            return null;
        }
    } else {
        try {
            BeanInfo beanInfo = (BeanInfo) Class.forName(classname + "BeanInfo").newInstance();
            return beanInfo;
        } catch (Exception e) {
            return null;
        }
    }
}
Also used : BeanInfo(java.beans.BeanInfo)

Example 77 with BeanInfo

use of java.beans.BeanInfo in project cosmic by MissionCriticalCloud.

the class ReflectUtil method flattenProperties.

private static List<String> flattenProperties(final Object target, final Class<?> clazz, final ImmutableSet<String> excludedProperties) {
    assert clazz != null;
    if (target == null) {
        return emptyList();
    }
    assert clazz.isAssignableFrom(target.getClass());
    try {
        final BeanInfo beanInfo = getBeanInfo(clazz);
        final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
        final List<String> serializedProperties = new ArrayList<>();
        for (final PropertyDescriptor descriptor : descriptors) {
            if (excludedProperties.contains(descriptor.getName())) {
                continue;
            }
            serializedProperties.add(descriptor.getName());
            final Object value = descriptor.getReadMethod().invoke(target);
            serializedProperties.add(value != null ? value.toString() : "null");
        }
        return unmodifiableList(serializedProperties);
    } catch (final IntrospectionException e) {
        s_logger.warn("Ignored IntrospectionException when serializing class " + target.getClass().getCanonicalName(), e);
    } catch (final IllegalArgumentException e) {
        s_logger.warn("Ignored IllegalArgumentException when serializing class " + target.getClass().getCanonicalName(), e);
    } catch (final IllegalAccessException e) {
        s_logger.warn("Ignored IllegalAccessException when serializing class " + target.getClass().getCanonicalName(), e);
    } catch (final InvocationTargetException e) {
        s_logger.warn("Ignored InvocationTargetException when serializing class " + target.getClass().getCanonicalName(), e);
    }
    return emptyList();
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Introspector.getBeanInfo(java.beans.Introspector.getBeanInfo) BeanInfo(java.beans.BeanInfo) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 78 with BeanInfo

use of java.beans.BeanInfo in project shiro by apache.

the class PrincipalTag method getPrincipalProperty.

private String getPrincipalProperty(Object principal, String property) throws JspTagException {
    String strValue = null;
    try {
        BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
        // Loop through the properties to get the string value of the specified property
        boolean foundProperty = false;
        for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
            if (pd.getName().equals(property)) {
                Object value = pd.getReadMethod().invoke(principal, (Object[]) null);
                strValue = String.valueOf(value);
                foundProperty = true;
                break;
            }
        }
        if (!foundProperty) {
            final String message = "Property [" + property + "] not found in principal of type [" + principal.getClass().getName() + "]";
            if (log.isErrorEnabled()) {
                log.error(message);
            }
            throw new JspTagException(message);
        }
    } catch (Exception e) {
        final String message = "Error reading property [" + property + "] from principal of type [" + principal.getClass().getName() + "]";
        if (log.isErrorEnabled()) {
            log.error(message, e);
        }
        throw new JspTagException(message, e);
    }
    return strValue;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) JspTagException(javax.servlet.jsp.JspTagException) JspException(javax.servlet.jsp.JspException) IOException(java.io.IOException) JspTagException(javax.servlet.jsp.JspTagException)

Example 79 with BeanInfo

use of java.beans.BeanInfo in project typescript-generator by vojtechhabarta.

the class Jackson2Parser method parseEnumOrObjectEnum.

private DeclarationModel parseEnumOrObjectEnum(SourceType<Class<?>> sourceClass) {
    final JsonFormat jsonFormat = sourceClass.type.getAnnotation(JsonFormat.class);
    if (jsonFormat != null && jsonFormat.shape() == JsonFormat.Shape.OBJECT) {
        return parseBean(sourceClass);
    }
    final boolean isNumberBased = jsonFormat != null && (jsonFormat.shape() == JsonFormat.Shape.NUMBER || jsonFormat.shape() == JsonFormat.Shape.NUMBER_FLOAT || jsonFormat.shape() == JsonFormat.Shape.NUMBER_INT);
    final List<EnumMemberModel> enumMembers = new ArrayList<>();
    if (sourceClass.type.isEnum()) {
        final Class<?> enumClass = (Class<?>) sourceClass.type;
        try {
            Method valueMethod = null;
            final BeanInfo beanInfo = Introspector.getBeanInfo(enumClass);
            for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
                final Method readMethod = propertyDescriptor.getReadMethod();
                if (readMethod.isAnnotationPresent(JsonValue.class)) {
                    valueMethod = readMethod;
                }
            }
            int index = 0;
            for (Field field : enumClass.getFields()) {
                if (field.isEnumConstant()) {
                    if (isNumberBased) {
                        final Number value = getNumberEnumValue(field, valueMethod, index++);
                        enumMembers.add(new EnumMemberModel(field.getName(), value, null));
                    } else {
                        final String value = getStringEnumValue(field, valueMethod);
                        enumMembers.add(new EnumMemberModel(field.getName(), value, null));
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(String.format("Cannot get enum values for '%s' enum", enumClass.getName()));
            e.printStackTrace(System.out);
        }
    }
    return new EnumModel(sourceClass.type, isNumberBased ? EnumKind.NumberBased : EnumKind.StringBased, enumMembers, null);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method) EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) Field(java.lang.reflect.Field) JsonFormat(com.fasterxml.jackson.annotation.JsonFormat)

Example 80 with BeanInfo

use of java.beans.BeanInfo in project fabric8 by fabric8io.

the class UserConfigurationCompare method configEqualKubernetesDTO.

/**
 * Compares 2 instances of the given Kubernetes DTO class to see if the user has changed their configuration.
 * <p/>
 * This method will ignore properties {@link #ignoredProperties} such as status or timestamp properties
 */
protected static boolean configEqualKubernetesDTO(@NotNull Object entity1, @NotNull Object entity2, @NotNull Class<?> clazz) {
    // lets iterate through the objects making sure we've not
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(clazz);
    } catch (IntrospectionException e) {
        LOG.warn("Failed to get beanInfo for " + clazz.getName() + ". " + e, e);
        return false;
    }
    try {
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            String name = propertyDescriptor.getName();
            if (ignoredProperties.contains(name)) {
                continue;
            }
            Method readMethod = propertyDescriptor.getReadMethod();
            if (readMethod != null) {
                Object value1 = invokeMethod(entity1, readMethod);
                Object value2 = invokeMethod(entity2, readMethod);
                if (!configEqual(value1, value2)) {
                    return false;
                }
            }
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) 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