1 /* 2 * Copyright (c) 2011, 2014, 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 30 import sun.reflect.misc.ReflectUtil; 31 32 /** 33 */ 34 public class JavaBeanPropertyBuilderHelper { 35 36 private static final String IS_PREFIX = "is"; 37 private static final String GET_PREFIX = "get"; 38 private static final String SET_PREFIX = "set"; 39 40 private String propertyName; 41 private Class<?> beanClass; 42 private Object bean; 43 private String getterName; 44 private String setterName; 45 private Method getter; 46 private Method setter; 47 private PropertyDescriptor descriptor; 48 49 public void name(String propertyName) { 50 if ((propertyName == null)? this.propertyName != null : !propertyName.equals(this.propertyName)) { 51 this.propertyName = propertyName; 52 this.descriptor = null; 53 } 54 } 55 56 public void beanClass(Class<?> beanClass) { 57 if ((beanClass == null)? this.beanClass != null : !beanClass.equals(this.beanClass)) { 58 ReflectUtil.checkPackageAccess(beanClass); 59 this.beanClass = beanClass; 60 this.descriptor = null; 61 } 62 } 63 64 public void bean(Object bean) { 65 this.bean = bean; 66 if (bean != null) { 67 Class<?> newClass = bean.getClass(); 68 if ((beanClass == null) || !beanClass.isAssignableFrom(newClass)) { 69 ReflectUtil.checkPackageAccess(newClass); 70 this.beanClass = newClass; 71 this.descriptor = null; 72 } 73 } 74 } 75 76 public Object getBean() { 77 return bean; 78 } 79 80 public void getterName(String getterName) { 81 if ((getterName == null)? this.getterName != null : !getterName.equals(this.getterName)) { 82 this.getterName = getterName; 83 this.descriptor = null; 84 } 85 } 86 87 public void setterName(String setterName) { 88 if ((setterName == null)? this.setterName != null : !setterName.equals(this.setterName)) { 89 this.setterName = setterName; 90 this.descriptor = null; 91 } 92 } 93 94 public void getter(Method getter) { 95 if ((getter == null)? this.getter != null : !getter.equals(this.getter)) { 96 this.getter = getter; 97 this.descriptor = null; 98 } 99 } 100 101 public void setter(Method setter) { 102 if ((setter == null)? this.setter != null : !setter.equals(this.setter)) { 103 this.setter = setter; 104 this.descriptor = null; 105 } 106 } 107 108 public PropertyDescriptor getDescriptor() throws NoSuchMethodException { 109 if (descriptor == null) { 110 if (propertyName == null) { 111 throw new NullPointerException("Property name has to be specified"); 112 } 113 if (propertyName.isEmpty()) { 114 throw new IllegalArgumentException("Property name cannot be empty"); 115 } 116 final String capitalizedName = ReadOnlyPropertyDescriptor.capitalizedName(propertyName); 117 Method getterMethod = getter; 118 if (getterMethod == null) { 119 if ((getterName != null) && !getterName.isEmpty()) { 120 getterMethod = beanClass.getMethod(getterName); 121 } else { 122 try { 123 getterMethod = beanClass.getMethod(IS_PREFIX + capitalizedName); 124 } catch (NoSuchMethodException e) { 125 getterMethod = beanClass.getMethod(GET_PREFIX + capitalizedName); 126 } 127 } 128 } 129 Method setterMethod = setter; 130 if (setterMethod == null) { 131 final Class<?> type = getterMethod.getReturnType(); 132 if ((setterName != null) && !setterName.isEmpty()) { 133 setterMethod = beanClass.getMethod(setterName, type); 134 } else { 135 setterMethod = beanClass.getMethod(SET_PREFIX + capitalizedName, type); 136 } 137 } 138 descriptor = new PropertyDescriptor(propertyName, beanClass, getterMethod, setterMethod); 139 } 140 return descriptor; 141 } 142 143 }