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