Search in sources :

Example 66 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project abstools by abstools.

the class Bean method init.

private void init() {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(dataClass);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor property = propertyDescriptors[i];
            if (property.getWriteMethod() != null && property.getWriteMethod().isAnnotationPresent(Adjustable.class)) {
                String key = property.getWriteMethod().getAnnotation(Adjustable.class).key();
                if (key.equals("")) {
                    key = norm(property.getName());
                }
                order.put(key, norm(property.getName()));
                properties.put(norm(property.getName()), property);
                if (getValue(property) == null) {
                    // may not be null when called in the process of deserialization
                    // (see readObject)
                    setValue(property, NullValueProvider.getNullValue(property.getPropertyType()));
                }
            }
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Throwable t) {
        t.printStackTrace();
        throw new IllegalStateException("FATAL: data class introspection was not successful");
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo)

Example 67 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project abstools by abstools.

the class Bean method toString.

public String toString() {
    StringBuffer buffer = new StringBuffer();
    for (PropertyDescriptor property : getProperties()) {
        buffer.append(property.getName() + "=");
        buffer.append(getValue(property.getName()));
        buffer.append("\n");
    }
    return buffer.toString();
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor)

Example 68 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project abstools by abstools.

the class BeanConverter method setElementValue.

private void setElementValue(Element element) {
    String name = element.getAttribute("name");
    PropertyDescriptor property = bean.getProperty(name);
    if (property == null) {
        System.err.println("Warning: " + name + " is not a property");
    } else {
        Object value = null;
        Class<?> type = property.getPropertyType();
        if (type.equals(String.class)) {
            value = element.getTextContent();
        } else if (type.equals(Integer.TYPE)) {
            value = parseInt(element.getAttribute("value"));
        } else if (type.equals(Boolean.TYPE)) {
            value = parseBool(element.getAttribute("value"));
        } else if (type.equals(Font.class)) {
            String family = element.getAttribute("family");
            int style = parseInt(element.getAttribute("style"));
            int size = parseInt(element.getAttribute("size"));
            if (size == 0) {
                size = 12;
            }
            value = new Font(family, style, size);
        } else if (type.equals(File.class)) {
            String fileName = element.getAttribute("value");
            value = new File(fileName);
        } else if (type.equals(Color.class)) {
            String rgb = element.getAttribute("value");
            value = new Color(Integer.parseInt(rgb));
        }
        if (value == null) {
            System.err.println("Warning: a value for property " + name + " could not be created");
        } else {
            bean.setValue(property, value);
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Color(java.awt.Color) File(java.io.File) Font(java.awt.Font)

Example 69 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project qiuyj-code by qiuyuanjun.

the class BeanUtils method copyProperties.

public static void copyProperties(Object src, Object dest) {
    if (Objects.isNull(src) || Objects.isNull(dest)) {
        throw new NullPointerException();
    } else {
        CachedIntrospectorResults srcIntrospectorResults = CachedIntrospectorResults.forClass(src.getClass());
        CachedIntrospectorResults destIntrospectorResults = CachedIntrospectorResults.forClass(dest.getClass());
        PropertyDescriptor[] pds = srcIntrospectorResults.getBeanInfo().getPropertyDescriptors();
        if (Objects.nonNull(pds)) {
            Method readMethod, writeMethod;
            PropertyDescriptor destPd;
            for (PropertyDescriptor srcPd : pds) {
                readMethod = srcPd.getReadMethod();
                if (Objects.nonNull(readMethod) && readMethod.getDeclaringClass() != Object.class) {
                    destPd = destIntrospectorResults.getPropertyDescriptor(srcPd.getName());
                    if (Objects.nonNull(destPd)) {
                        writeMethod = destPd.getWriteMethod();
                        if (Objects.nonNull(writeMethod) && (readMethod.getReturnType() == writeMethod.getParameterTypes()[0] || writeMethod.getParameterTypes()[0].isAssignableFrom(readMethod.getReturnType()))) {
                            ReflectionUtils.makeAccessible(readMethod);
                            ReflectionUtils.makeAccessible(writeMethod);
                            try {
                                // ReflectionUtils.invokeMethod(dest, writeMethod, ReflectionUtils.invokeMethod(src, readMethod));
                                writeMethod.invoke(dest, readMethod.invoke(src));
                            } catch (Exception e) {
                            // ignore
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method)

Example 70 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project qiuyj-code by qiuyuanjun.

the class BeanWrapperImpl method doSetPropertyValue.

@Override
protected void doSetPropertyValue(String property, Object value) {
    PropertyDescriptor pd = getPropertyDescriptor(property);
    if (Objects.isNull(pd)) {
        throw new ReflectionException("Can not found property: " + property + " in class: " + wrappedClass);
    } else {
        if (Objects.nonNull(value)) {
            validateType(pd.getPropertyType(), value.getClass());
        }
        if (pd instanceof NoGetterSetterPropertyDescriptor) {
            Field propertyField = ((NoGetterSetterPropertyDescriptor) pd).getPropertyField();
            ReflectionUtils.makeAccessible(propertyField);
            try {
                propertyField.set(wrappedInstance, value);
            } catch (IllegalAccessException e) {
            // ignore
            }
        } else {
            Method writeMethod = pd.getWriteMethod();
            if (Objects.isNull(writeMethod)) {
                if (fieldOperationSupport) {
                    Field propertyField = ReflectionUtils.getDeclaredField(wrappedClass, property);
                    ReflectionUtils.makeAccessible(propertyField);
                    try {
                        propertyField.set(wrappedInstance, value);
                    } catch (IllegalAccessException e) {
                    // ingore
                    }
                } else {
                    throw new IllegalStateException("Property '" + property + "' is an readonly property.");
                }
            } else {
                ReflectionUtils.makeAccessible(writeMethod);
                ReflectionUtils.invokeMethod(wrappedInstance, writeMethod, value);
            }
        }
    }
}
Also used : ReflectionException(com.qiuyj.commons.bean.exception.ReflectionException) Field(java.lang.reflect.Field) PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)836 Method (java.lang.reflect.Method)396 BeanInfo (java.beans.BeanInfo)339 IntrospectionException (java.beans.IntrospectionException)188 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)108 ArrayList (java.util.ArrayList)90 HashMap (java.util.HashMap)66 Field (java.lang.reflect.Field)59 Map (java.util.Map)52 Test (org.junit.Test)39 IOException (java.io.IOException)34 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)34 List (java.util.List)33 HashSet (java.util.HashSet)21 Type (java.lang.reflect.Type)20 Set (java.util.Set)20 LinkedHashMap (java.util.LinkedHashMap)19