Path: blob/master/src/java.desktop/share/classes/com/sun/beans/introspect/MethodInfo.java
41171 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.beans.introspect;2627import java.lang.reflect.Method;28import java.lang.reflect.Modifier;29import java.lang.reflect.Type;30import java.util.ArrayList;31import java.util.Collections;32import java.util.Comparator;33import java.util.List;3435import com.sun.beans.TypeResolver;36import com.sun.beans.finder.MethodFinder;3738final class MethodInfo {39final Method method;40final Class<?> type;4142MethodInfo(Method method, Class<?> type) {43this.method = method;44this.type = type;45}4647MethodInfo(Method method, Type type) {48this.method = method;49this.type = resolve(method, type);50}5152boolean isThrow(Class<?> exception) {53for (Class<?> type : this.method.getExceptionTypes()) {54if (type == exception) {55return true;56}57}58return false;59}6061static Class<?> resolve(Method method, Type type) {62return TypeResolver.erase(TypeResolver.resolveInClass(method.getDeclaringClass(), type));63}6465static List<Method> get(Class<?> type) {66List<Method> list = null;67if (type != null) {68boolean inaccessible = !Modifier.isPublic(type.getModifiers());69for (Method method : type.getMethods()) {70if (method.getDeclaringClass().equals(type)) {71if (inaccessible) {72try {73method = MethodFinder.findAccessibleMethod(method);74if (!method.getDeclaringClass().isInterface()) {75method = null; // ignore methods from superclasses76}77} catch (NoSuchMethodException exception) {78// commented out because of 697657779// method = null; // ignore inaccessible methods80}81}82if (method != null) {83if (list == null) {84list = new ArrayList<>();85}86list.add(method);87}88}89}90}91if (list != null) {92list.sort(MethodOrder.instance);93return Collections.unmodifiableList(list);94}95return Collections.emptyList();96}9798/**99* A comparator that defines a total order so that methods have the same100* name and identical signatures appear next to each others. The methods are101* sorted in such a way that methods which override each other will sit next102* to each other, with the overridden method last - e.g. is Integer getFoo()103* placed before Object getFoo().104**/105private static final class MethodOrder implements Comparator<Method> {106107/*108* Code particularly was copied from com.sun.jmx.mbeanserver.MethodOrder109*/110@Override111public int compare(final Method a, final Method b) {112int cmp = a.getName().compareTo(b.getName());113if (cmp != 0) {114return cmp;115}116final Class<?>[] aparams = a.getParameterTypes();117final Class<?>[] bparams = b.getParameterTypes();118if (aparams.length != bparams.length) {119return aparams.length - bparams.length;120}121for (int i = 0; i < aparams.length; ++i) {122final Class<?> aparam = aparams[i];123final Class<?> bparam = bparams[i];124if (aparam == bparam) {125continue;126}127cmp = aparam.getName().compareTo(bparam.getName());128if (cmp != 0) {129return cmp;130}131}132final Class<?> aret = a.getReturnType();133final Class<?> bret = b.getReturnType();134if (aret == bret) {135return 0;136}137138// Super type comes last: Integer, Number, Object139if (aret.isAssignableFrom(bret)) {140return 1;141}142if (bret.isAssignableFrom(aret)) {143return -1;144}145return aret.getName().compareTo(bret.getName());146}147148static final MethodOrder instance = new MethodOrder();149}150}151152153