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");
}
}
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();
}
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);
}
}
}
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
}
}
}
}
}
}
}
}
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);
}
}
}
}
Aggregations