/* * Created by LuaView. * Copyright (c) 2017, Alibaba Group. All rights reserved. * * This source code is licensed under the MIT. * For the full copyright and license information,please view the LICENSE file in the root directory of this source tree. */ package com.taobao.luaview.util; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * 反射相关类封装 * * @author song * @date 15/8/18 * 下午4:39 song XXX */ public class ReflectionUtil { /** * get methods of given annotation * * @param type * @param annotation * @return */ public static List<Method> getMethodsAnnotatedWith(final Class<?> type, final Class<? extends Annotation> annotation) { final List<Method> methods = new ArrayList<Method>(); Class<?> klass = type; while (klass != Object.class) { // need to iterated thought hierarchy in order to retrieve methods from above the current instance // iterate though the list of methods declared in the class represented by klass variable, and add those annotated with the specified annotation final List<Method> allMethods = new ArrayList<Method>(Arrays.asList(klass.getDeclaredMethods())); for (final Method method : allMethods) { if (annotation == null || method.isAnnotationPresent(annotation)) { Annotation annotInstance = method.getAnnotation(annotation); // TODO process annotInstance methods.add(method); } } // move to the upper class in the hierarchy in search for more methods klass = klass.getSuperclass(); } return methods; } }