JavaFXPropertyExtractor.java
01 /*
02  * Copyright 2014-2016 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
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 griffon.plugins.glazedlists.javafx.gui;
17 
18 import griffon.exceptions.InstanceMethodInvocationException;
19 import javafx.beans.property.Property;
20 
21 import javax.annotation.Nonnull;
22 
23 import static griffon.util.GriffonClassUtils.getGetterName;
24 import static griffon.util.GriffonClassUtils.invokeExactInstanceMethod;
25 import static griffon.util.GriffonNameUtils.requireNonBlank;
26 import static java.util.Objects.requireNonNull;
27 
28 /**
29  @author Andres Almiray
30  */
31 final class JavaFXPropertyExtractor {
32     private static final String PROPERTY_SUFFIX = "Property";
33 
34     @Nonnull
35     @SuppressWarnings("ConstantConditions")
36     public static <B> Property getProperty(@Nonnull B bean, @Nonnull String propertyName) {
37         requireNonNull(bean, "Argument 'bean' must not be null");
38         requireNonBlank(propertyName, "Argument 'propertyName' must not be null");
39 
40         if (!propertyName.endsWith(PROPERTY_SUFFIX)) {
41             propertyName += PROPERTY_SUFFIX;
42         }
43 
44         InstanceMethodInvocationException imie;
45         try {
46             // 1. try <columnName>Property() first
47             return (Property<?>invokeExactInstanceMethod(bean, propertyName);
48         catch (InstanceMethodInvocationException e) {
49             imie = e;
50         }
51 
52         // 2. fallback to get<columnName>Property()
53         try {
54             return (Property<?>invokeExactInstanceMethod(bean, getGetterName(propertyName));
55         catch (InstanceMethodInvocationException e) {
56             throw imie;
57         }
58     }
59 }