History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/beans/finder/MethodFinder.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/beans/finder/MethodFinder.java+231−0
@@ -0,0 +1,231 @@
1/*
2 * Copyright (c) 2008, 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 */
25package com.sun.beans.finder;
26
27import com.sun.beans.TypeResolver;
28import com.sun.beans.WeakCache;
29
30import java.lang.reflect.Method;
31import java.lang.reflect.Modifier;
32import java.lang.reflect.ParameterizedType;
33import java.lang.reflect.Type;
34import java.util.Arrays;
35
36/**
37 * This utility class provides {@code static} methods
38 * to find a public method with specified name and parameter types
39 * in specified class.
40 *
41 * @since 1.7
42 *
43 * @author Sergey A. Malenkov
44 */
45public final class MethodFinder extends AbstractFinder<Method> {
46 private static final WeakCache<Signature, Method> CACHE = new WeakCache<Signature, Method>();
47
48 /**
49 * Finds public method (static or non-static)
50 * that is accessible from public class.
51 *
52 * @param type the class that can have method
53 * @param name the name of method to find
54 * @param args parameter types that is used to find method
55 * @return object that represents found method
56 * @throws NoSuchMethodException if method could not be found
57 * or some methods are found
58 */
59 public static Method findMethod(Class<?> type, String name, Class<?>...args) throws NoSuchMethodException {
60 if (name == null) {
61 throw new IllegalArgumentException("Method name is not set");
62 }
63 PrimitiveWrapperMap.replacePrimitivesWithWrappers(args);
64 Signature signature = new Signature(type, name, args);
65
66 Method method = CACHE.get(signature);
67 if (method != null) {
68 return method;
69 }
70 method = findAccessibleMethod(new MethodFinder(name, args).find(type.getMethods()));
71 CACHE.put(signature, method);
72 return method;
73 }
74
75 /**
76 * Finds public non-static method
77 * that is accessible from public class.
78 *
79 * @param type the class that can have method
80 * @param name the name of method to find
81 * @param args parameter types that is used to find method
82 * @return object that represents found method
83 * @throws NoSuchMethodException if method could not be found
84 * or some methods are found
85 */
86 public static Method findInstanceMethod(Class<?> type, String name, Class<?>... args) throws NoSuchMethodException {
87 Method method = findMethod(type, name, args);
88 if (Modifier.isStatic(method.getModifiers())) {
89 throw new NoSuchMethodException("Method '" + name + "' is static");
90 }
91 return method;
92 }
93
94 /**
95 * Finds public static method
96 * that is accessible from public class.
97 *
98 * @param type the class that can have method
99 * @param name the name of method to find
100 * @param args parameter types that is used to find method
101 * @return object that represents found method
102 * @throws NoSuchMethodException if method could not be found
103 * or some methods are found
104 */
105 public static Method findStaticMethod(Class<?> type, String name, Class<?>...args) throws NoSuchMethodException {
106 Method method = findMethod(type, name, args);
107 if (!Modifier.isStatic(method.getModifiers())) {
108 throw new NoSuchMethodException("Method '" + name + "' is not static");
109 }
110 return method;
111 }
112
113 /**
114 * Finds method that is accessible from public class or interface through class hierarchy.
115 *
116 * @param method object that represents found method
117 * @return object that represents accessible method
118 * @throws NoSuchMethodException if method is not accessible or is not found
119 * in specified superclass or interface
120 */
121 public static Method findAccessibleMethod(Method method) throws NoSuchMethodException {
122 Class<?> type = method.getDeclaringClass();
123 if (Modifier.isPublic(type.getModifiers())) {
124 return method;
125 }
126 if (Modifier.isStatic(method.getModifiers())) {
127 throw new NoSuchMethodException("Method '" + method.getName() + "' is not accessible");
128 }
129 for (Type generic : type.getGenericInterfaces()) {
130 try {
131 return findAccessibleMethod(method, generic);
132 }
133 catch (NoSuchMethodException exception) {
134 // try to find in superclass or another interface
135 }
136 }
137 return findAccessibleMethod(method, type.getGenericSuperclass());
138 }
139
140 /**
141 * Finds method that accessible from specified class.
142 *
143 * @param method object that represents found method
144 * @param generic generic type that is used to find accessible method
145 * @return object that represents accessible method
146 * @throws NoSuchMethodException if method is not accessible or is not found
147 * in specified superclass or interface
148 */
149 private static Method findAccessibleMethod(Method method, Type generic) throws NoSuchMethodException {
150 String name = method.getName();
151 Class<?>[] params = method.getParameterTypes();
152 if (generic instanceof Class) {
153 Class<?> type = (Class<?>) generic;
154 return findAccessibleMethod(type.getMethod(name, params));
155 }
156 if (generic instanceof ParameterizedType) {
157 ParameterizedType pt = (ParameterizedType) generic;
158 Class<?> type = (Class<?>) pt.getRawType();
159 for (Method m : type.getMethods()) {
160 if (m.getName().equals(name)) {
161 Class<?>[] pts = m.getParameterTypes();
162 if (pts.length == params.length) {
163 if (Arrays.equals(params, pts)) {
164 return findAccessibleMethod(m);
165 }
166 Type[] gpts = m.getGenericParameterTypes();
167 if (Arrays.equals(params, TypeResolver.erase(TypeResolver.resolve(pt, gpts)))) {
168 return findAccessibleMethod(m);
169 }
170 }
171 }
172 }
173 }
174 throw new NoSuchMethodException("Method '" + name + "' is not accessible");
175 }
176
177
178 private final String name;
179
180 /**
181 * Creates method finder with specified array of parameter types.
182 *
183 * @param name the name of method to find
184 * @param args the array of parameter types
185 */
186 private MethodFinder(String name, Class<?>[] args) {
187 super(args);
188 this.name = name;
189 }
190
191 /**
192 * Returns an array of {@code Class} objects
193 * that represent the formal parameter types of the method
194 * Returns an empty array if the method takes no parameters.
195 *
196 * @param method the object that represents method
197 * @return the parameter types of the method
198 */
199 @Override
200 protected Class<?>[] getParameters(Method method) {
201 return method.getParameterTypes();
202 }
203
204 /**
205 * Returns {@code true} if and only if the method
206 * was declared to take a variable number of arguments.
207 *
208 * @param method the object that represents method
209 * @return {@code true} if the method was declared
210 * to take a variable number of arguments;
211 * {@code false} otherwise
212 */
213 @Override
214 protected boolean isVarArgs(Method method) {
215 return method.isVarArgs();
216 }
217
218 /**
219 * Checks validness of the method.
220 * The valid method should be public and
221 * should have the specified name.
222 *
223 * @param method the object that represents method
224 * @return {@code true} if the method is valid,
225 * {@code false} otherwise
226 */
227 @Override
228 protected boolean isValid(Method method) {
229 return Modifier.isPublic(method.getModifiers()) && method.getName().equals(this.name);
230 }
231}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t