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 ReadOnlyJavaBeanPropertyBuilderHelper { 34 35 private static final String IS_PREFIX = "is"; 36 private static final String GET_PREFIX = "get"; 37 38 private String propertyName; 39 private Class<?> beanClass; 40 private Object bean; 41 private String getterName; 42 private Method getter; 43 private ReadOnlyPropertyDescriptor descriptor; 44 45 public void name(String propertyName) { 46 if ((propertyName == null)? this.propertyName != null : !propertyName.equals(this.propertyName)) { 47 this.propertyName = propertyName; 48 this.descriptor = null; 49 } 50 } 51 52 public void beanClass(Class<?> beanClass) { 53 if ((beanClass == null)? this.beanClass != null : !beanClass.equals(this.beanClass)) { 54 ReflectUtil.checkPackageAccess(beanClass); 55 this.beanClass = beanClass; 56 this.descriptor = null; 57 } 58 } 59 60 public void bean(Object bean) { 61 this.bean = bean; 62 if (bean != null) { 63 Class<?> newClass = bean.getClass(); 64 if ((beanClass == null) || !beanClass.isAssignableFrom(newClass)) { 65 ReflectUtil.checkPackageAccess(newClass); 66 this.beanClass = bean.getClass(); 67 this.descriptor = null; 68 } 69 } 70 } 71 72 public Object getBean() { 73 return bean; 74 } 75 76 public void getterName(String getterName) { 77 if ((getterName == null)? this.getterName != null : !getterName.equals(this.getterName)) { 78 this.getterName = getterName; 79 this.descriptor = null; 80 } 81 } 82 83 public void getter(Method getter) { 84 if ((getter == null)? this.getter != null : !getter.equals(this.getter)) { 85 this.getter = getter; 86 this.descriptor = null; 87 } 88 } 89 90 public ReadOnlyPropertyDescriptor getDescriptor() throws NoSuchMethodException { 91 if (descriptor == null) { 92 if ((propertyName == null) || (bean == null)) { 93 throw new NullPointerException("Bean and property name have to be specified"); 94 } 95 if (propertyName.isEmpty()) { 96 throw new IllegalArgumentException("Property name cannot be empty"); 97 } 98 final String capitalizedName = ReadOnlyPropertyDescriptor.capitalizedName(propertyName); 99 if (getter == null) { 100 if ((getterName != null) && !getterName.isEmpty()) { 101 getter = beanClass.getMethod(getterName); 102 } else { 103 try { 104 getter = beanClass.getMethod(IS_PREFIX + capitalizedName); 105 } catch (NoSuchMethodException e) { 106 getter = beanClass.getMethod(GET_PREFIX + capitalizedName); 107 } 108 } 109 } 110 descriptor = new ReadOnlyPropertyDescriptor(propertyName, beanClass, getter); 111 } 112 return descriptor; 113 } 114 115 }