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

link-intersystems / lis-commons / #221

21 Aug 2023 11:39AM UTC coverage: 89.884% (-0.07%) from 89.952%
#221

push

renelink
Added PropertyCopier.

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

7277 of 8096 relevant lines covered (89.88%)

0.9 hits per line

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

89.47
/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.*;
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 DefaultProperty 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(bean::getBeanObject, 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
    /**
88
     * The name of this {@link JavaProperty}.
89
     *
90
     * @return name of this {@link JavaProperty}.
91
     * @since 1.2.0;
92
     */
93
    public String getName() {
94
        return getPropertyDesc().getName();
1✔
95
    }
96

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

107
        PropertyDescriptor javaPropertyDescriptor = getJavaPropertyDescriptor();
1✔
108

109
        PropertyEditor propertyEditor = javaPropertyDescriptor.createPropertyEditor(bean);
1✔
110

111
        if (propertyEditor == null) {
1✔
112
            Class<?> propertyType = getPropertyDesc().getType();
1✔
113
            propertyEditor = PropertyEditorManager.findEditor(propertyType);
1✔
114
        }
115

116
        if (propertyEditor == null) {
1✔
117
            String msg = MessageFormat.format("No property editor available for {0}", this);
×
118
            throw new PropertyEditorNotAvailableException(msg);
×
119
        }
120

121
        return propertyEditor;
1✔
122
    }
123

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

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

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

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

183
    /**
184
     * Encapsulation of a method invocation for better testing.
185
     */
186
    protected Object invoke(Method method, Object target, Object... args)
187
            throws IllegalAccessException, InvocationTargetException {
188
        Object beanValue = method.invoke(target, args);
1✔
189
        return beanValue;
1✔
190
    }
191

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

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

210
    /**
211
     * The name of this property.
212
     */
213
    @Override
214
    public String toString() {
215
        return getName();
1✔
216
    }
217

218
    /**
219
     * The declaring class of a {@link JavaProperty} is the class that first mentions
220
     * the property. So it can be either the read or write method's declaring class.
221
     * If the read and write methods are defined in different classes the uppermost
222
     * class in the hierarchy is returned.
223
     */
224
    public Class<?> getDeclaringClass() {
225
        return getPropertyDesc().getDeclaringClass();
1✔
226
    }
227
}
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