• 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

83.78
/lis-commons-beans/src/main/java/com/link_intersystems/beans/java/JavaProperty.java
1
/**
2
 * Copyright 2011 Link Intersystems GmbH <rene.link@link-intersystems.com>
3
 * <p>
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 * <p>
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 * <p>
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.link_intersystems.beans.java;
17

18
import com.link_intersystems.beans.*;
19

20
import java.beans.PropertyDescriptor;
21
import java.beans.PropertyEditor;
22
import java.beans.PropertyEditorManager;
23
import java.io.Serializable;
24
import java.lang.reflect.InvocationTargetException;
25
import java.lang.reflect.Method;
26
import java.text.MessageFormat;
27

28
import static java.util.Objects.requireNonNull;
29

30
/**
31
 * Encapsulates access to a java bean property.
32
 *
33
 * <pre>
34
 * public class SomeBean {
35
 *
36
 *    private String internalName;
37
 *
38
 *    public void setName(String name){
39
 *      this.internalName = name;
40
 *    }
41
 *
42
 *    public String getName(){
43
 *            return internalName;
44
 *    }
45
 *
46
 * }
47
 *
48
 * ...
49
 * SomeBean someBean = new SomeBean();
50
 *
51
 * Bean<SomeBean> bean = new Bean<SomeBean>();
52
 * Property<String> nameProp = bean.getProperty("name");
53
 * nameProp.setValue("Hello");
54
 *
55
 * assertEquals("Hello", someBean.getName());
56
 * </pre>
57
 *
58
 * @author René Link
59
 * <a href="mailto:rene.link@link-intersystems.com">[rene.link@link-
60
 * intersystems.com]</a>
61
 * <p>
62
 * the {@link JavaProperty}'s type.
63
 * @since 1.2.0;
64
 */
65
public class JavaProperty extends AbstractProperty implements Serializable, Property {
66

67
    private static final long serialVersionUID = -6759158627808430975L;
68
    private final JavaBean<?> bean;
69

70
    JavaProperty(JavaBean<?> bean, JavaPropertyDesc propertyDescriptor) {
71
        super(propertyDescriptor);
1✔
72
        this.bean = requireNonNull(bean);
1✔
73
    }
1✔
74

75
    protected PropertyDescriptor getJavaPropertyDescriptor() {
76
        return ((JavaPropertyDesc) getPropertyDesc()).getJavaPropertyDescriptor();
1✔
77
    }
78

79
    /**
80
     * @return the bean object of this {@link JavaProperty}.
81
     * @since 1.2.0;
82
     */
83
    protected final JavaBean<?> getBean() {
84
        return bean;
1✔
85
    }
86

87
    @Override
88
    protected Object getBeanObject() {
89
        return getBean().getBeanObject();
×
90
    }
91

92
    /**
93
     * The name of this {@link JavaProperty}.
94
     *
95
     * @return name of this {@link JavaProperty}.
96
     * @since 1.2.0;
97
     */
98
    public String getName() {
99
        return getPropertyDesc().getName();
1✔
100
    }
101

102
    /**
103
     * Creates a {@link PropertyEditor} for this {@link JavaProperty}.
104
     *
105
     * @return the property editor for this property.
106
     * @since 1.2.0;
107
     */
108
    public PropertyEditor createPropertyEditor() throws PropertyEditorNotAvailableException {
109
        JavaBean<?> beanObj = getBean();
1✔
110
        Object bean = beanObj.getBeanObject();
1✔
111

112
        PropertyDescriptor javaPropertyDescriptor = getJavaPropertyDescriptor();
1✔
113

114
        PropertyEditor propertyEditor = javaPropertyDescriptor.createPropertyEditor(bean);
1✔
115

116
        if (propertyEditor == null) {
1✔
117
            Class<?> propertyType = getPropertyDesc().getType();
1✔
118
            propertyEditor = PropertyEditorManager.findEditor(propertyType);
1✔
119
        }
120

121
        if (propertyEditor == null) {
1✔
122
            String msg = MessageFormat.format("No property editor available for {0}", this);
×
123
            throw new PropertyEditorNotAvailableException(msg);
×
124
        }
125

126
        return propertyEditor;
1✔
127
    }
128

129
    /**
130
     * Get this {@link JavaProperty}'s value as text, according to the associated
131
     * {@link PropertyEditor}'s return value.
132
     *
133
     * @since 1.2.0;
134
     */
135
    public String getValueAsText() throws PropertyEditorNotAvailableException {
136
        try {
137
            PropertyEditor propertyEditor = createPropertyEditor();
1✔
138
            propertyEditor.setValue(getValue());
1✔
139
            return propertyEditor.getAsText();
1✔
140
        } catch (PropertyEditorNotAvailableException e) {
×
141
            return null;
×
142
        }
143
    }
144

145
    /**
146
     * Set this {@link JavaProperty}'s value by the {@link PropertyEditor}'s text
147
     * representation.
148
     *
149
     * @since 1.2.0;
150
     */
151
    public void setValueAsText(String text) throws PropertyEditorNotAvailableException {
152
        PropertyEditor propertiyEditor = createPropertyEditor();
1✔
153
        propertiyEditor.setAsText(text);
1✔
154
        Object value = propertiyEditor.getValue();
1✔
155
        setValue(value);
1✔
156
    }
1✔
157

158
    /**
159
     * Gets the value of this {@link JavaProperty}.
160
     *
161
     * @return the value of this property.
162
     * @throws PropertyReadException if the property could not be accessed for any reason. If the
163
     *                               thrown {@link PropertyReadException} has no cause this property
164
     *                               is not readable (has no property getter method).
165
     * @since 1.2.0;
166
     */
167
    @Override
168
    public <T> T getValue() {
169
        PropertyDesc propertyDesc = getPropertyDesc();
1✔
170
        return propertyDesc.getPropertyValue(getBean().getBeanObject());
1✔
171
    }
172

173
    /**
174
     * Sets the value of this {@link JavaProperty}.
175
     *
176
     * @param propertyValue the value to set.
177
     * @throws PropertyReadException if this {@link JavaProperty}'s value could not be set. If the thrown
178
     *                               {@link PropertyWriteException} has no cause this property is not
179
     *                               writable (has no property setter method).
180
     * @since 1.2.0;
181
     */
182
    @Override
183
    public void setValue(Object propertyValue) {
184
        PropertyDesc propertyDesc = getPropertyDesc();
1✔
185
        propertyDesc.setPropertyValue(getBean().getBeanObject(), propertyValue);
1✔
186
    }
1✔
187

188
    /**
189
     * Encapsulation of a method invocation for better testing.
190
     */
191
    protected Object invoke(Method method, Object target, Object... args)
192
            throws IllegalAccessException, InvocationTargetException {
UNCOV
193
        return method.invoke(target, args);
×
194
    }
195

196
    /**
197
     * Returns true if this property is readable (has a getter method).
198
     *
199
     * @return true if this property is readable (has a getter method).
200
     */
201
    public boolean isReadable() {
202
        return getPropertyDesc().isReadable();
1✔
203
    }
204

205
    /**
206
     * Returns if this property is writable (has a setter method).
207
     *
208
     * @return true if this property is writable (has a setter method).
209
     */
210
    public boolean isWritable() {
211
        return getPropertyDesc().isWritable();
1✔
212
    }
213

214
    /**
215
     * The name of this property.
216
     */
217
    @Override
218
    public String toString() {
219
        return getName();
1✔
220
    }
221
}
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