1 /* 2 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.javafx.property.adapter; 27 28 import java.lang.reflect.Method; 29 import com.sun.javafx.reflect.ReflectUtil; 30 31 /** 32 */ 33 public class JavaBeanPropertyBuilderHelper { 34 35 private static final String IS_PREFIX = "is"; 36 private static final String GET_PREFIX = "get"; 37 private static final String SET_PREFIX = "set"; 38 39 private String propertyName; 40 private Class<?> beanClass; 41 private Object bean; 42 private String getterName; 43 private String setterName; 44 private Method getter; 45 private Method setter; 46 private PropertyDescriptor descriptor; 47 48 public void name(String propertyName) { 49 if ((propertyName == null)? this.propertyName != null : !propertyName.equals(this.propertyName)) { 50 this.propertyName = propertyName; 51 this.descriptor = null; 52 } 53 } 54 55 public void beanClass(Class<?> beanClass) { 56 if ((beanClass == null)? this.beanClass != null : !beanClass.equals(this.beanClass)) { 57 ReflectUtil.checkPackageAccess(beanClass); 58 this.beanClass = beanClass; 59 this.descriptor = null; 60 } 61 } 62 63 public void bean(Object bean) { 64 this.bean = bean; 65 if (bean != null) { 66 Class<?> newClass = bean.getClass(); 67 if ((beanClass == null) || !beanClass.isAssignableFrom(newClass)) { 68 ReflectUtil.checkPackageAccess(newClass); 69 this.beanClass = newClass; 70 this.descriptor = null; 71 } 72 } 73 } 74 75 public Object getBean() { 76 return bean; 77 } 78 79 public void getterName(String getterName) { 80 if ((getterName == null)? this.getterName != null : !getterName.equals(this.getterName)) { 81 this.getterName = getterName; 82 this.descriptor = null; 83 } 84 } 85 86 public void setterName(String setterName) { 87 if ((setterName == null)? this.setterName != null : !setterName.equals(this.setterName)) { 88 this.setterName = setterName; 89 this.descriptor = null; 90 } 91 } 92 93 public void getter(Method getter) { 94 if ((getter == null)? this.getter != null : !getter.equals(this.getter)) { 95 this.getter = getter; 96 this.descriptor = null; 97 } 98 } 99 100 public void setter(Method setter) { 101 if ((setter == null)? this.setter != null : !setter.equals(this.setter)) { 102 this.setter = setter; 103 this.descriptor = null; 104 } 105 } 106 107 public PropertyDescriptor getDescriptor() throws NoSuchMethodException { 108 if (descriptor == null) { 109 if (propertyName == null) { 110 throw new NullPointerException("Property name has to be specified"); 111 } 112 if (propertyName.isEmpty()) { 113 throw new IllegalArgumentException("Property name cannot be empty"); 114 } 115 final String capitalizedName = ReadOnlyPropertyDescriptor.capitalizedName(propertyName); 116 Method getterMethod = getter; 117 if (getterMethod == null) { 118 if ((getterName != null) && !getterName.isEmpty()) { 119 getterMethod = beanClass.getMethod(getterName); 120 } else { 121 try { 122 getterMethod = beanClass.getMethod(IS_PREFIX + capitalizedName); 123 } catch (NoSuchMethodException e) { 124 getterMethod = beanClass.getMethod(GET_PREFIX + capitalizedName); 125 } 126 } 127 } 128 Method setterMethod = setter; 129 if (setterMethod == null) { 130 final Class<?> type = getterMethod.getReturnType(); 131 if ((setterName != null) && !setterName.isEmpty()) { 132 setterMethod = beanClass.getMethod(setterName, type); 133 } else { 134 setterMethod = beanClass.getMethod(SET_PREFIX + capitalizedName, type); 135 } 136 } 137 descriptor = new PropertyDescriptor(propertyName, beanClass, getterMethod, setterMethod); 138 } 139 return descriptor; 140 } 141 142 }