0N/A/*
2362N/A * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.beans;
0N/A
0N/Aimport java.lang.ref.Reference;
0N/Aimport java.lang.ref.WeakReference;
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.util.List;
0N/Aimport java.util.ArrayList;
0N/A
0N/A/**
0N/A * A MethodDescriptor describes a particular method that a Java Bean
0N/A * supports for external access from other components.
0N/A */
0N/A
0N/Apublic class MethodDescriptor extends FeatureDescriptor {
0N/A
0N/A private Reference<Method> methodRef;
0N/A
0N/A private String[] paramNames;
0N/A
0N/A private List params;
0N/A
0N/A private ParameterDescriptor parameterDescriptors[];
0N/A
0N/A /**
0N/A * Constructs a <code>MethodDescriptor</code> from a
0N/A * <code>Method</code>.
0N/A *
0N/A * @param method The low-level method information.
0N/A */
0N/A public MethodDescriptor(Method method) {
0N/A this(method, null);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Constructs a <code>MethodDescriptor</code> from a
0N/A * <code>Method</code> providing descriptive information for each
0N/A * of the method's parameters.
0N/A *
0N/A * @param method The low-level method information.
0N/A * @param parameterDescriptors Descriptive information for each of the
0N/A * method's parameters.
0N/A */
0N/A public MethodDescriptor(Method method,
0N/A ParameterDescriptor parameterDescriptors[]) {
0N/A setName(method.getName());
0N/A setMethod(method);
0N/A this.parameterDescriptors = parameterDescriptors;
0N/A }
0N/A
0N/A /**
0N/A * Gets the method that this MethodDescriptor encapsualtes.
0N/A *
0N/A * @return The low-level description of the method
0N/A */
0N/A public synchronized Method getMethod() {
0N/A Method method = getMethod0();
0N/A if (method == null) {
0N/A Class cls = getClass0();
2523N/A String name = getName();
2523N/A if ((cls != null) && (name != null)) {
0N/A Class[] params = getParams();
0N/A if (params == null) {
0N/A for (int i = 0; i < 3; i++) {
0N/A // Find methods for up to 2 params. We are guessing here.
0N/A // This block should never execute unless the classloader
0N/A // that loaded the argument classes disappears.
2788N/A method = Introspector.findMethod(cls, name, i, null);
0N/A if (method != null) {
0N/A break;
0N/A }
0N/A }
0N/A } else {
2788N/A method = Introspector.findMethod(cls, name, params.length, params);
0N/A }
0N/A setMethod(method);
0N/A }
0N/A }
0N/A return method;
0N/A }
0N/A
0N/A private synchronized void setMethod(Method method) {
0N/A if (method == null) {
0N/A return;
0N/A }
0N/A if (getClass0() == null) {
0N/A setClass0(method.getDeclaringClass());
0N/A }
0N/A setParams(getParameterTypes(getClass0(), method));
0N/A this.methodRef = getSoftReference(method);
0N/A }
0N/A
0N/A private Method getMethod0() {
0N/A return (this.methodRef != null)
0N/A ? this.methodRef.get()
0N/A : null;
0N/A }
0N/A
0N/A private synchronized void setParams(Class[] param) {
0N/A if (param == null) {
0N/A return;
0N/A }
0N/A paramNames = new String[param.length];
0N/A params = new ArrayList(param.length);
0N/A for (int i = 0; i < param.length; i++) {
0N/A paramNames[i] = param[i].getName();
0N/A params.add(new WeakReference(param[i]));
0N/A }
0N/A }
0N/A
0N/A // pp getParamNames used as an optimization to avoid method.getParameterTypes.
0N/A String[] getParamNames() {
0N/A return paramNames;
0N/A }
0N/A
0N/A private synchronized Class[] getParams() {
0N/A Class[] clss = new Class[params.size()];
0N/A
0N/A for (int i = 0; i < params.size(); i++) {
0N/A Reference ref = (Reference)params.get(i);
0N/A Class cls = (Class)ref.get();
0N/A if (cls == null) {
0N/A return null;
0N/A } else {
0N/A clss[i] = cls;
0N/A }
0N/A }
0N/A return clss;
0N/A }
0N/A
0N/A /**
0N/A * Gets the ParameterDescriptor for each of this MethodDescriptor's
0N/A * method's parameters.
0N/A *
0N/A * @return The locale-independent names of the parameters. May return
0N/A * a null array if the parameter names aren't known.
0N/A */
0N/A public ParameterDescriptor[] getParameterDescriptors() {
0N/A return parameterDescriptors;
0N/A }
0N/A
0N/A /*
0N/A * Package-private constructor
0N/A * Merge two method descriptors. Where they conflict, give the
0N/A * second argument (y) priority over the first argument (x).
0N/A * @param x The first (lower priority) MethodDescriptor
0N/A * @param y The second (higher priority) MethodDescriptor
0N/A */
0N/A
0N/A MethodDescriptor(MethodDescriptor x, MethodDescriptor y) {
0N/A super(x,y);
0N/A
0N/A methodRef = x.methodRef;
0N/A if (y.methodRef != null) {
0N/A methodRef = y.methodRef;
0N/A }
0N/A params = x.params;
0N/A if (y.params != null) {
0N/A params = y.params;
0N/A }
0N/A paramNames = x.paramNames;
0N/A if (y.paramNames != null) {
0N/A paramNames = y.paramNames;
0N/A }
0N/A
0N/A parameterDescriptors = x.parameterDescriptors;
0N/A if (y.parameterDescriptors != null) {
0N/A parameterDescriptors = y.parameterDescriptors;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Package-private dup constructor
0N/A * This must isolate the new object from any changes to the old object.
0N/A */
0N/A MethodDescriptor(MethodDescriptor old) {
0N/A super(old);
0N/A
0N/A methodRef = old.methodRef;
0N/A params = old.params;
0N/A paramNames = old.paramNames;
0N/A
0N/A if (old.parameterDescriptors != null) {
0N/A int len = old.parameterDescriptors.length;
0N/A parameterDescriptors = new ParameterDescriptor[len];
0N/A for (int i = 0; i < len ; i++) {
0N/A parameterDescriptors[i] = new ParameterDescriptor(old.parameterDescriptors[i]);
0N/A }
0N/A }
0N/A }
0N/A
2172N/A void appendTo(StringBuilder sb) {
2172N/A appendTo(sb, "method", this.methodRef);
2172N/A if (this.parameterDescriptors != null) {
2172N/A sb.append("; parameterDescriptors={");
2172N/A for (ParameterDescriptor pd : this.parameterDescriptors) {
2172N/A sb.append(pd).append(", ");
2172N/A }
2172N/A sb.setLength(sb.length() - 2);
2172N/A sb.append("}");
2172N/A }
2172N/A }
0N/A}