测试两个方法是否具有相同的名称,即返回类型,参数计数和参数类型。
/** * @file AnnotationUtil.java * * AnnotationUtil is a utility to get all annotations of a class, its * methods, * and the method parameters. Returned annotations include all annotations * of * the classes interfaces and super classes. * Requested classes are cached, so requesting a classes annotations * repeatedly * is fast. * * Example usage: * * AnnotatedClass annotatedClass = AnnotationUtil.get(MyClass.class); * List<AnnotatedMethod> methods = annotatedClass.getMethods(); * for (AnnotatedMethod method : methods) { * System.out.println("Method: " + method.getName()); * List<Annotation> annotations = method.getAnnotations(); * for (Annotation annotation : annotations) { * System.out.println(" Annotation: " + annotation.toString()); * } * } * * @brief * AnnotationUtil is a utility to retrieve merged annotations from a * class * including all its superclasses and interfaces. * * @license * Licensed under the Apache License, Version 2.0 (the "License"); you * may not * use this file except in compliance with the License. You may obtain * a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See * the * License for the specific language governing permissions and * limitations under * the License. * * Copyright (c) 2013 Almende B.V. * * @author Jos de Jong, <jos@almende.org> * @date 2013-01-21 */ //package com.nowjava; /*来自 N o w J a v a . c o m - 时 代 Java*/ import java.lang.reflect.Field; import java.lang.reflect.Method; public class Main { /** * Test if two methods have equal names, return type, param count, * and param types. * * @param a * the a * @param b * the b * @return true, if successful */ private static boolean areEqual(final Method a, final Method b) { // http://stackoverflow.com/q/10062957/1262753 if (!a.getName().equals(b.getName())) { return false; } if (a.getReturnType() != b.getReturnType()) { return false;/** 来自 NowJava.com - 时 代 Java**/ } final Class<?>[] paramsa = a.getParameterTypes(); final Class<?>[] paramsb = b.getParameterTypes(); if (paramsa.length != paramsb.length) { return false; } for (int i = 0; i < paramsa.length; i++) { if (paramsa[i] != paramsb[i]) { return false; } } return true; }