1 /*
   2  * Copyright (c) 1996, 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 java.beans;
  27 
  28 import java.lang.ref.Reference;
  29 import java.lang.ref.WeakReference;
  30 import java.lang.reflect.Method;
  31 import java.util.List;
  32 import java.util.ArrayList;
  33 
  34 /**
  35  * A MethodDescriptor describes a particular method that a Java Bean
  36  * supports for external access from other components.
  37  */
  38 
  39 public class MethodDescriptor extends FeatureDescriptor {
  40 
  41     private final MethodRef methodRef = new MethodRef();
  42 
  43     private String[] paramNames;
  44 
  45     private List<WeakReference<Class<?>>> params;
  46 
  47     private ParameterDescriptor parameterDescriptors[];
  48 
  49     /**
  50      * Constructs a <code>MethodDescriptor</code> from a
  51      * <code>Method</code>.
  52      *
  53      * @param method    The low-level method information.
  54      */
  55     public MethodDescriptor(Method method) {
  56         this(method, null);
  57     }
  58 
  59 
  60     /**
  61      * Constructs a <code>MethodDescriptor</code> from a
  62      * <code>Method</code> providing descriptive information for each
  63      * of the method's parameters.
  64      *
  65      * @param method    The low-level method information.
  66      * @param parameterDescriptors  Descriptive information for each of the
  67      *                          method's parameters.
  68      */
  69     public MethodDescriptor(Method method,
  70                 ParameterDescriptor parameterDescriptors[]) {
  71         setName(method.getName());
  72         setMethod(method);
  73         this.parameterDescriptors = (parameterDescriptors != null)
  74                 ? parameterDescriptors.clone()
  75                 : null;
  76     }
  77 
  78     /**
  79      * Gets the method that this MethodDescriptor encapsulates.
  80      *
  81      * @return The low-level description of the method
  82      */
  83     public synchronized Method getMethod() {
  84         Method method = this.methodRef.get();
  85         if (method == null) {
  86             Class<?> cls = getClass0();
  87             String name = getName();
  88             if ((cls != null) && (name != null)) {
  89                 Class<?>[] params = getParams();
  90                 if (params == null) {
  91                     for (int i = 0; i < 3; i++) {
  92                         // Find methods for up to 2 params. We are guessing here.
  93                         // This block should never execute unless the classloader
  94                         // that loaded the argument classes disappears.
  95                         method = Introspector.findMethod(cls, name, i, null);
  96                         if (method != null) {
  97                             break;
  98                         }
  99                     }
 100                 } else {
 101                     method = Introspector.findMethod(cls, name, params.length, params);
 102                 }
 103                 setMethod(method);
 104             }
 105         }
 106         return method;
 107     }
 108 
 109     private synchronized void setMethod(Method method) {
 110         if (method == null) {
 111             return;
 112         }
 113         if (getClass0() == null) {
 114             setClass0(method.getDeclaringClass());
 115         }
 116         setParams(getParameterTypes(getClass0(), method));
 117         this.methodRef.set(method);
 118     }
 119 
 120     private synchronized void setParams(Class<?>[] param) {
 121         if (param == null) {
 122             return;
 123         }
 124         paramNames = new String[param.length];
 125         params = new ArrayList<>(param.length);
 126         for (int i = 0; i < param.length; i++) {
 127             paramNames[i] = param[i].getName();
 128             params.add(new WeakReference<Class<?>>(param[i]));
 129         }
 130     }
 131 
 132     // pp getParamNames used as an optimization to avoid method.getParameterTypes.
 133     String[] getParamNames() {
 134         return paramNames;
 135     }
 136 
 137     private synchronized Class<?>[] getParams() {
 138         Class<?>[] clss = new Class<?>[params.size()];
 139 
 140         for (int i = 0; i < params.size(); i++) {
 141             Reference<? extends Class<?>> ref = (Reference<? extends Class<?>>)params.get(i);
 142             Class<?> cls = ref.get();
 143             if (cls == null) {
 144                 return null;
 145             } else {
 146                 clss[i] = cls;
 147             }
 148         }
 149         return clss;
 150     }
 151 
 152     /**
 153      * Gets the ParameterDescriptor for each of this MethodDescriptor's
 154      * method's parameters.
 155      *
 156      * @return The locale-independent names of the parameters.  May return
 157      *          a null array if the parameter names aren't known.
 158      */
 159     public ParameterDescriptor[] getParameterDescriptors() {
 160         return (this.parameterDescriptors != null)
 161                 ? this.parameterDescriptors.clone()
 162                 : null;
 163     }
 164 
 165     private static Method resolve(Method oldMethod, Method newMethod) {
 166         if (oldMethod == null) {
 167             return newMethod;
 168         }
 169         if (newMethod == null) {
 170             return oldMethod;
 171         }
 172         return !oldMethod.isSynthetic() && newMethod.isSynthetic() ? oldMethod : newMethod;
 173     }
 174 
 175     /*
 176      * Package-private constructor
 177      * Merge two method descriptors.  Where they conflict, give the
 178      * second argument (y) priority over the first argument (x).
 179      * @param x  The first (lower priority) MethodDescriptor
 180      * @param y  The second (higher priority) MethodDescriptor
 181      */
 182 
 183     MethodDescriptor(MethodDescriptor x, MethodDescriptor y) {
 184         super(x, y);
 185 
 186         this.methodRef.set(resolve(x.methodRef.get(), y.methodRef.get()));
 187         params = x.params;
 188         if (y.params != null) {
 189             params = y.params;
 190         }
 191         paramNames = x.paramNames;
 192         if (y.paramNames != null) {
 193             paramNames = y.paramNames;
 194         }
 195 
 196         parameterDescriptors = x.parameterDescriptors;
 197         if (y.parameterDescriptors != null) {
 198             parameterDescriptors = y.parameterDescriptors;
 199         }
 200     }
 201 
 202     /*
 203      * Package-private dup constructor
 204      * This must isolate the new object from any changes to the old object.
 205      */
 206     MethodDescriptor(MethodDescriptor old) {
 207         super(old);
 208 
 209         this.methodRef.set(old.getMethod());
 210         params = old.params;
 211         paramNames = old.paramNames;
 212 
 213         if (old.parameterDescriptors != null) {
 214             int len = old.parameterDescriptors.length;
 215             parameterDescriptors = new ParameterDescriptor[len];
 216             for (int i = 0; i < len ; i++) {
 217                 parameterDescriptors[i] = new ParameterDescriptor(old.parameterDescriptors[i]);
 218             }
 219         }
 220     }
 221 
 222     void appendTo(StringBuilder sb) {
 223         appendTo(sb, "method", this.methodRef.get());
 224         if (this.parameterDescriptors != null) {
 225             sb.append("; parameterDescriptors={");
 226             for (ParameterDescriptor pd : this.parameterDescriptors) {
 227                 sb.append(pd).append(", ");
 228             }
 229             sb.setLength(sb.length() - 2);
 230             sb.append("}");
 231         }
 232     }
 233 }