• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

link-intersystems / lis-commons / #318

19 May 2024 07:18AM UTC coverage: 90.202% (-0.04%) from 90.246%
#318

push

renelink
Added generic index set and get methods to IndexedPropertyDesc.

16 of 25 new or added lines in 2 files covered. (64.0%)

4 existing lines in 2 files now uncovered.

7669 of 8502 relevant lines covered (90.2%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.57
/lis-commons-beans/src/main/java/com/link_intersystems/beans/java/JavaIndexedPropertyDesc.java
1
package com.link_intersystems.beans.java;
2

3
import com.link_intersystems.beans.IndexedPropertyDesc;
4
import com.link_intersystems.beans.PropertyReadException;
5
import com.link_intersystems.beans.PropertyWriteException;
6

7
import java.beans.IndexedPropertyDescriptor;
8
import java.lang.reflect.Array;
9
import java.lang.reflect.InvocationTargetException;
10
import java.lang.reflect.Method;
11

12
/**
13
 * @author René Link {@literal <rene.link@link-intersystems.com>}
14
 */
15
public class JavaIndexedPropertyDesc extends JavaPropertyDesc implements IndexedPropertyDesc {
16

17
    private static final int SETTER_TYPE_PARAM_INDEX = 0;
18
    private static final int INDEXED_SETTER_TYPE_PARAM_INDEX = 1;
19

20
    private Class<?> type;
21

22
    JavaIndexedPropertyDesc(IndexedPropertyDescriptor propertyDescriptor) {
23
        super(propertyDescriptor);
1✔
24
    }
1✔
25

26
    public IndexedPropertyDescriptor getJavaPropertyDescriptor() {
27
        return (IndexedPropertyDescriptor) super.getJavaPropertyDescriptor();
1✔
28
    }
29

30
    /**
31
     * Returns true if this indexed property can be accessed through an indexed
32
     * getter method, e.g. <code>PropertyType getter(int index);</code>.
33
     *
34
     * @return true if this indexed property can be accessed through an indexed
35
     * getter method.
36
     */
37
    @Override
38
    public boolean isIndexedReadable() {
39
        return getJavaPropertyDescriptor().getIndexedReadMethod() != null;
1✔
40
    }
41

42
    /**
43
     * Returns true if this indexed property can be accessed through an indexed
44
     * setter method, e.g.
45
     * <code>void setter(int index, PropertyType value);</code>.
46
     *
47
     * @return true if this indexed property can be accessed through an indexed
48
     * setter method.
49
     */
50
    @Override
51
    public boolean isIndexedWritable() {
52
        return getJavaPropertyDescriptor().getIndexedWriteMethod() != null;
1✔
53
    }
54

55
    @Override
56
    public Class<?> getType() {
57
        if (this.type == null) {
1✔
58
            IndexedPropertyDescriptor javaPropertyDescriptor = getJavaPropertyDescriptor();
1✔
59
            Method readMethod = javaPropertyDescriptor.getReadMethod();
1✔
60
            if (readMethod != null) {
1✔
61
                type = readMethod.getReturnType();
1✔
62
            } else {
63
                Method writeMethod = javaPropertyDescriptor.getWriteMethod();
1✔
64
                if (writeMethod != null) {
1✔
65
                    type = writeMethod.getParameterTypes()[SETTER_TYPE_PARAM_INDEX];
1✔
66
                } else {
67
                    Method indexedReadMethod = javaPropertyDescriptor.getIndexedReadMethod();
1✔
68
                    Class<?> elementType;
69
                    if (indexedReadMethod != null) {
1✔
70
                        elementType = indexedReadMethod.getReturnType();
1✔
71
                    } else {
72
                        Method indexedWriteMethod = javaPropertyDescriptor.getIndexedWriteMethod();
1✔
73
                        Class<?>[] parameterTypes = indexedWriteMethod.getParameterTypes();
1✔
74
                        elementType = parameterTypes[INDEXED_SETTER_TYPE_PARAM_INDEX];
1✔
75
                    }
76
                    this.type = Array.newInstance(elementType, 0).getClass();
1✔
77
                }
78
            }
79
        }
80
        return type;
1✔
81
    }
82

83
    @SuppressWarnings("unchecked")
84
    @Override
85
    public <T> T getPropertyValue(Object bean, int index) throws PropertyReadException {
86
        Method indexedReadMethod = getJavaPropertyDescriptor().getIndexedReadMethod();
1✔
87
        if (indexedReadMethod == null) {
1✔
88

NEW
89
            throw new PropertyReadException(bean.getClass(), getName());
×
90
        }
91
        try {
92
            Object elementValue = invoke(indexedReadMethod, bean, index);
1✔
93
            return (T) elementValue;
1✔
94
        } catch (InvocationTargetException e) {
1✔
95
            Throwable targetException = e.getTargetException();
1✔
96
            throw new PropertyReadException(bean.getClass(), getName(), targetException);
1✔
NEW
97
        } catch (IllegalAccessException e) {
×
NEW
98
            throw new PropertyReadException(bean.getClass(), getName(), e);
×
99
        }
100
    }
101

102
    @Override
103
    public void setPropertyValue(Object bean, int index, Object value) {
104

105
        Method indexedWriteMethod = getJavaPropertyDescriptor().getIndexedWriteMethod();
1✔
106
        if (indexedWriteMethod == null) {
1✔
NEW
107
            throw new PropertyWriteException(bean.getClass(), getName());
×
108
        }
109
        try {
110
            invoke(indexedWriteMethod, bean, index, value);
1✔
NEW
111
        } catch (InvocationTargetException e) {
×
NEW
112
            Throwable targetException = e.getTargetException();
×
NEW
113
            throw new PropertyWriteException(bean.getClass(), getName(), targetException);
×
NEW
114
        } catch (IllegalAccessException e) {
×
NEW
115
            throw new PropertyWriteException(bean.getClass(), getName(), e);
×
116
        }
1✔
117
    }
1✔
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc