Showing the changes to java/pkg/java/beans/MethodDescriptor.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,228 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1996, 2010, 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 Reference<Method> methodRef; | |
| 42 | + | |
| 43 | + private String[] paramNames; | |
| 44 | + | |
| 45 | + private List 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; | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Gets the method that this MethodDescriptor encapsualtes. | |
| 78 | + * | |
| 79 | + * @return The low-level description of the method | |
| 80 | + */ | |
| 81 | + public synchronized Method getMethod() { | |
| 82 | + Method method = getMethod0(); | |
| 83 | + if (method == null) { | |
| 84 | + Class cls = getClass0(); | |
| 85 | + String name = getName(); | |
| 86 | + if ((cls != null) && (name != null)) { | |
| 87 | + Class[] params = getParams(); | |
| 88 | + if (params == null) { | |
| 89 | + for (int i = 0; i < 3; i++) { | |
| 90 | + // Find methods for up to 2 params. We are guessing here. | |
| 91 | + // This block should never execute unless the classloader | |
| 92 | + // that loaded the argument classes disappears. | |
| 93 | + method = Introspector.findMethod(cls, name, i, null); | |
| 94 | + if (method != null) { | |
| 95 | + break; | |
| 96 | + } | |
| 97 | + } | |
| 98 | + } else { | |
| 99 | + method = Introspector.findMethod(cls, name, params.length, params); | |
| 100 | + } | |
| 101 | + setMethod(method); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + return method; | |
| 105 | + } | |
| 106 | + | |
| 107 | + private synchronized void setMethod(Method method) { | |
| 108 | + if (method == null) { | |
| 109 | + return; | |
| 110 | + } | |
| 111 | + if (getClass0() == null) { | |
| 112 | + setClass0(method.getDeclaringClass()); | |
| 113 | + } | |
| 114 | + setParams(getParameterTypes(getClass0(), method)); | |
| 115 | + this.methodRef = getSoftReference(method); | |
| 116 | + } | |
| 117 | + | |
| 118 | + private Method getMethod0() { | |
| 119 | + return (this.methodRef != null) | |
| 120 | + ? this.methodRef.get() | |
| 121 | + : null; | |
| 122 | + } | |
| 123 | + | |
| 124 | + private synchronized void setParams(Class[] param) { | |
| 125 | + if (param == null) { | |
| 126 | + return; | |
| 127 | + } | |
| 128 | + paramNames = new String[param.length]; | |
| 129 | + params = new ArrayList(param.length); | |
| 130 | + for (int i = 0; i < param.length; i++) { | |
| 131 | + paramNames[i] = param[i].getName(); | |
| 132 | + params.add(new WeakReference(param[i])); | |
| 133 | + } | |
| 134 | + } | |
| 135 | + | |
| 136 | + // pp getParamNames used as an optimization to avoid method.getParameterTypes. | |
| 137 | + String[] getParamNames() { | |
| 138 | + return paramNames; | |
| 139 | + } | |
| 140 | + | |
| 141 | + private synchronized Class[] getParams() { | |
| 142 | + Class[] clss = new Class[params.size()]; | |
| 143 | + | |
| 144 | + for (int i = 0; i < params.size(); i++) { | |
| 145 | + Reference ref = (Reference)params.get(i); | |
| 146 | + Class cls = (Class)ref.get(); | |
| 147 | + if (cls == null) { | |
| 148 | + return null; | |
| 149 | + } else { | |
| 150 | + clss[i] = cls; | |
| 151 | + } | |
| 152 | + } | |
| 153 | + return clss; | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Gets the ParameterDescriptor for each of this MethodDescriptor's | |
| 158 | + * method's parameters. | |
| 159 | + * | |
| 160 | + * @return The locale-independent names of the parameters. May return | |
| 161 | + * a null array if the parameter names aren't known. | |
| 162 | + */ | |
| 163 | + public ParameterDescriptor[] getParameterDescriptors() { | |
| 164 | + return parameterDescriptors; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /* | |
| 168 | + * Package-private constructor | |
| 169 | + * Merge two method descriptors. Where they conflict, give the | |
| 170 | + * second argument (y) priority over the first argument (x). | |
| 171 | + * @param x The first (lower priority) MethodDescriptor | |
| 172 | + * @param y The second (higher priority) MethodDescriptor | |
| 173 | + */ | |
| 174 | + | |
| 175 | + MethodDescriptor(MethodDescriptor x, MethodDescriptor y) { | |
| 176 | + super(x,y); | |
| 177 | + | |
| 178 | + methodRef = x.methodRef; | |
| 179 | + if (y.methodRef != null) { | |
| 180 | + methodRef = y.methodRef; | |
| 181 | + } | |
| 182 | + params = x.params; | |
| 183 | + if (y.params != null) { | |
| 184 | + params = y.params; | |
| 185 | + } | |
| 186 | + paramNames = x.paramNames; | |
| 187 | + if (y.paramNames != null) { | |
| 188 | + paramNames = y.paramNames; | |
| 189 | + } | |
| 190 | + | |
| 191 | + parameterDescriptors = x.parameterDescriptors; | |
| 192 | + if (y.parameterDescriptors != null) { | |
| 193 | + parameterDescriptors = y.parameterDescriptors; | |
| 194 | + } | |
| 195 | + } | |
| 196 | + | |
| 197 | + /* | |
| 198 | + * Package-private dup constructor | |
| 199 | + * This must isolate the new object from any changes to the old object. | |
| 200 | + */ | |
| 201 | + MethodDescriptor(MethodDescriptor old) { | |
| 202 | + super(old); | |
| 203 | + | |
| 204 | + methodRef = old.methodRef; | |
| 205 | + params = old.params; | |
| 206 | + paramNames = old.paramNames; | |
| 207 | + | |
| 208 | + if (old.parameterDescriptors != null) { | |
| 209 | + int len = old.parameterDescriptors.length; | |
| 210 | + parameterDescriptors = new ParameterDescriptor[len]; | |
| 211 | + for (int i = 0; i < len ; i++) { | |
| 212 | + parameterDescriptors[i] = new ParameterDescriptor(old.parameterDescriptors[i]); | |
| 213 | + } | |
| 214 | + } | |
| 215 | + } | |
| 216 | + | |
| 217 | + void appendTo(StringBuilder sb) { | |
| 218 | + appendTo(sb, "method", this.methodRef); | |
| 219 | + if (this.parameterDescriptors != null) { | |
| 220 | + sb.append("; parameterDescriptors={"); | |
| 221 | + for (ParameterDescriptor pd : this.parameterDescriptors) { | |
| 222 | + sb.append(pd).append(", "); | |
| 223 | + } | |
| 224 | + sb.setLength(sb.length() - 2); | |
| 225 | + sb.append("}"); | |
| 226 | + } | |
| 227 | + } | |
| 228 | +} | |