From 58c5cdb01801ae585341d30d9fbf9916869e7b56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Link?= Date: Sun, 19 May 2024 09:18:45 +0200 Subject: [PATCH] Added generic index set and get methods to IndexedPropertyDesc. --- .../beans/IndexedPropertyDesc.java | 5 +++ .../beans/java/JavaIndexedProperty.java | 35 ++--------------- .../beans/java/JavaIndexedPropertyDesc.java | 39 +++++++++++++++++++ 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/lis-commons-beans/src/main/java/com/link_intersystems/beans/IndexedPropertyDesc.java b/lis-commons-beans/src/main/java/com/link_intersystems/beans/IndexedPropertyDesc.java index 50de79d6..d7fcea50 100644 --- a/lis-commons-beans/src/main/java/com/link_intersystems/beans/IndexedPropertyDesc.java +++ b/lis-commons-beans/src/main/java/com/link_intersystems/beans/IndexedPropertyDesc.java @@ -12,4 +12,9 @@ public interface IndexedPropertyDesc extends PropertyDesc { boolean isIndexedReadable(); boolean isIndexedWritable(); + + + public T getPropertyValue(Object bean, int index) throws PropertyReadException; + + public void setPropertyValue(Object bean, int index, Object value); } diff --git a/lis-commons-beans/src/main/java/com/link_intersystems/beans/java/JavaIndexedProperty.java b/lis-commons-beans/src/main/java/com/link_intersystems/beans/java/JavaIndexedProperty.java index 98e74638..f7201d6d 100644 --- a/lis-commons-beans/src/main/java/com/link_intersystems/beans/java/JavaIndexedProperty.java +++ b/lis-commons-beans/src/main/java/com/link_intersystems/beans/java/JavaIndexedProperty.java @@ -18,7 +18,6 @@ import com.link_intersystems.beans.IndexedProperty; import com.link_intersystems.beans.IndexedPropertyDesc; import com.link_intersystems.beans.PropertyReadException; -import com.link_intersystems.beans.PropertyWriteException; import java.beans.IndexedPropertyDescriptor; import java.lang.reflect.InvocationTargetException; @@ -107,22 +106,9 @@ public IndexedPropertyDesc getPropertyDesc() { @Override @SuppressWarnings("unchecked") public T getValue(int index) { + IndexedPropertyDesc propertyDesc = getPropertyDesc(); JavaBean bean = getBean(); - Method indexedReadMethod = getIndexedReadMethod(); - if (indexedReadMethod == null) { - - throw new PropertyReadException(bean.getBeanClass().getType(), getPropertyDesc().getName()); - } - try { - Object target = bean.getBeanObject(); - Object elementValue = invoke(indexedReadMethod, target, index); - return (T) elementValue; - } catch (InvocationTargetException e) { - Throwable targetException = e.getTargetException(); - throw new PropertyReadException(bean.getBeanClass().getType(), getPropertyDesc().getName(), targetException); - } catch (IllegalAccessException e) { - throw new PropertyReadException(bean.getBeanClass().getType(), getPropertyDesc().getName(), e); - } + return propertyDesc.getPropertyValue(bean.getBeanObject(), index); } /** @@ -135,22 +121,9 @@ public T getValue(int index) { */ @Override public void setValue(int index, Object elementValue) { + IndexedPropertyDesc propertyDesc = getPropertyDesc(); JavaBean bean = getBean(); - - Method indexedWriteMethod = getIndexedWriteMethod(); - if (indexedWriteMethod == null) { - - throw new PropertyWriteException(bean.getBeanClass().getType(), getPropertyDesc().getName()); - } - try { - Object target = bean.getBeanObject(); - invoke(indexedWriteMethod, target, index, elementValue); - } catch (InvocationTargetException e) { - Throwable targetException = e.getTargetException(); - throw new PropertyWriteException(bean.getBeanClass().getType(), getPropertyDesc().getName(), targetException); - } catch (IllegalAccessException e) { - throw new PropertyWriteException(bean.getBeanClass().getType(), getPropertyDesc().getName(), e); - } + propertyDesc.setPropertyValue(bean.getBeanObject(), index, elementValue); } /** diff --git a/lis-commons-beans/src/main/java/com/link_intersystems/beans/java/JavaIndexedPropertyDesc.java b/lis-commons-beans/src/main/java/com/link_intersystems/beans/java/JavaIndexedPropertyDesc.java index 52b6fd5a..b51e509a 100644 --- a/lis-commons-beans/src/main/java/com/link_intersystems/beans/java/JavaIndexedPropertyDesc.java +++ b/lis-commons-beans/src/main/java/com/link_intersystems/beans/java/JavaIndexedPropertyDesc.java @@ -1,9 +1,12 @@ package com.link_intersystems.beans.java; import com.link_intersystems.beans.IndexedPropertyDesc; +import com.link_intersystems.beans.PropertyReadException; +import com.link_intersystems.beans.PropertyWriteException; import java.beans.IndexedPropertyDescriptor; import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** @@ -76,4 +79,40 @@ public Class getType() { } return type; } + + @SuppressWarnings("unchecked") + @Override + public T getPropertyValue(Object bean, int index) throws PropertyReadException { + Method indexedReadMethod = getJavaPropertyDescriptor().getIndexedReadMethod(); + if (indexedReadMethod == null) { + + throw new PropertyReadException(bean.getClass(), getName()); + } + try { + Object elementValue = invoke(indexedReadMethod, bean, index); + return (T) elementValue; + } catch (InvocationTargetException e) { + Throwable targetException = e.getTargetException(); + throw new PropertyReadException(bean.getClass(), getName(), targetException); + } catch (IllegalAccessException e) { + throw new PropertyReadException(bean.getClass(), getName(), e); + } + } + + @Override + public void setPropertyValue(Object bean, int index, Object value) { + + Method indexedWriteMethod = getJavaPropertyDescriptor().getIndexedWriteMethod(); + if (indexedWriteMethod == null) { + throw new PropertyWriteException(bean.getClass(), getName()); + } + try { + invoke(indexedWriteMethod, bean, index, value); + } catch (InvocationTargetException e) { + Throwable targetException = e.getTargetException(); + throw new PropertyWriteException(bean.getClass(), getName(), targetException); + } catch (IllegalAccessException e) { + throw new PropertyWriteException(bean.getClass(), getName(), e); + } + } }