Skip to content

Commit

Permalink
Added generic index set and get methods to IndexedPropertyDesc.
Browse files Browse the repository at this point in the history
  • Loading branch information
renelink committed May 19, 2024
1 parent fb2d99a commit 58c5cdb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public interface IndexedPropertyDesc extends PropertyDesc {
boolean isIndexedReadable();

boolean isIndexedWritable();


public <T> T getPropertyValue(Object bean, int index) throws PropertyReadException;

public void setPropertyValue(Object bean, int index, Object value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,22 +106,9 @@ public IndexedPropertyDesc getPropertyDesc() {
@Override
@SuppressWarnings("unchecked")
public <T> 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);
}

/**
Expand All @@ -135,22 +121,9 @@ public <T> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -76,4 +79,40 @@ public Class<?> getType() {
}
return type;
}

@SuppressWarnings("unchecked")
@Override
public <T> 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);
}
}
}

0 comments on commit 58c5cdb

Please sign in to comment.