jdk/src/share/classes/java/beans/MethodDescriptor.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 4960 99ac74ca2f2f
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A MethodDescriptor describes a particular method that a Java Bean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * supports for external access from other components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class MethodDescriptor extends FeatureDescriptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private Reference<Method> methodRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private String[] paramNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private List params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private ParameterDescriptor parameterDescriptors[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Constructs a <code>MethodDescriptor</code> from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * <code>Method</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param method    The low-level method information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public MethodDescriptor(Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this(method, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Constructs a <code>MethodDescriptor</code> from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <code>Method</code> providing descriptive information for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * of the method's parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param method    The low-level method information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param parameterDescriptors  Descriptive information for each of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *                          method's parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public MethodDescriptor(Method method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                ParameterDescriptor parameterDescriptors[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        setName(method.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        setMethod(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.parameterDescriptors = parameterDescriptors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Gets the method that this MethodDescriptor encapsualtes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @return The low-level description of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public synchronized Method getMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        Method method = getMethod0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Class cls = getClass0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (cls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                Class[] params = getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        // Find methods for up to 2 params. We are guessing here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        // This block should never execute unless the classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        // that loaded the argument classes disappears.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        method = Introspector.findMethod(cls, getName(), i, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        if (method != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    method = Introspector.findMethod(cls, getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                                     params.length, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                setMethod(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private synchronized void setMethod(Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (getClass0() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            setClass0(method.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        setParams(getParameterTypes(getClass0(), method));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.methodRef = getSoftReference(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private Method getMethod0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return (this.methodRef != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                ? this.methodRef.get()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private synchronized void setParams(Class[] param) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (param == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        paramNames = new String[param.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        params = new ArrayList(param.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        for (int i = 0; i < param.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            paramNames[i] = param[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            params.add(new WeakReference(param[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // pp getParamNames used as an optimization to avoid method.getParameterTypes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    String[] getParamNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return paramNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private synchronized Class[] getParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Class[] clss = new Class[params.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        for (int i = 0; i < params.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            Reference ref = (Reference)params.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            Class cls = (Class)ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (cls == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                clss[i] = cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return clss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Gets the ParameterDescriptor for each of this MethodDescriptor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * method's parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return The locale-independent names of the parameters.  May return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *          a null array if the parameter names aren't known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public ParameterDescriptor[] getParameterDescriptors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return parameterDescriptors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        String message = "name=" + getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Class cls = getClass0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (cls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            message += ", class=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            message += cls.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        String[] names = getParamNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                message += ", param=" + names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Package-private constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Merge two method descriptors.  Where they conflict, give the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * second argument (y) priority over the first argument (x).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param x  The first (lower priority) MethodDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param y  The second (higher priority) MethodDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    MethodDescriptor(MethodDescriptor x, MethodDescriptor y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        super(x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        methodRef = x.methodRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (y.methodRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            methodRef = y.methodRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        params = x.params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (y.params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            params = y.params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        paramNames = x.paramNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (y.paramNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            paramNames = y.paramNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        parameterDescriptors = x.parameterDescriptors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (y.parameterDescriptors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            parameterDescriptors = y.parameterDescriptors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Package-private dup constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * This must isolate the new object from any changes to the old object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    MethodDescriptor(MethodDescriptor old) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        super(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        methodRef = old.methodRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        params = old.params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        paramNames = old.paramNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (old.parameterDescriptors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            int len = old.parameterDescriptors.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            parameterDescriptors = new ParameterDescriptor[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            for (int i = 0; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                parameterDescriptors[i] = new ParameterDescriptor(old.parameterDescriptors[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
}