Java Reflection Method Get from Object getMethodVector(Object object)

Here you can find the source of getMethodVector(Object object)

Description

This walks all of the super classes and interfaces of an object and creates a vector of declared methods where the first method in the vector is the first method in the super most class

License

Open Source License

Declaration

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Vector<Method> getMethodVector(Object object) 

Method Source Code

//package com.java2s;
/**//  w  w  w .  j  a va  2s. c o  m
Copyright (C) 2012  Delcyon, Inc.
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.Method;

import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    @SuppressWarnings("rawtypes")
    private static volatile ConcurrentHashMap<Class, Method[]> systemClassDeclaredMethodsHashMap = new ConcurrentHashMap<Class, Method[]>();
    @SuppressWarnings("rawtypes")
    private static volatile ConcurrentHashMap<Class, Class[]> systemClassIneterfacesHashMap = new ConcurrentHashMap<Class, Class[]>();
    @SuppressWarnings("rawtypes")
    private static volatile ConcurrentHashMap<Class, Vector<Method>> systemClassMethodVectorHashMap = new ConcurrentHashMap<Class, Vector<Method>>();

    /**
      * This walks all of the super classes and interfaces of an object and
      * creates a vector of declared methods where the first method in the vector is the first
      * method in the super most class
      */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Vector<Method> getMethodVector(Object object) {
        Vector<Class> classVector = new Vector<Class>();

        Class currentClass = object.getClass();
        Vector<Method> methodVector = systemClassMethodVectorHashMap.get(currentClass);
        if (methodVector != null) {
            return methodVector;
        } else {
            methodVector = new Vector<Method>();
        }
        while (currentClass != null) {
            classVector.insertElementAt(currentClass, 0);
            Class[] interfaceClasses = null;

            if (systemClassIneterfacesHashMap.containsKey(currentClass)) {
                interfaceClasses = systemClassIneterfacesHashMap.get(currentClass);
            } else {
                interfaceClasses = currentClass.getInterfaces();
                systemClassIneterfacesHashMap.put(currentClass, interfaceClasses);
            }

            for (Class interfaceClass : interfaceClasses) {
                classVector.insertElementAt(interfaceClass, 0);
            }
            currentClass = currentClass.getSuperclass();
        }

        for (Class clazz : classVector) {
            Method[] methods = null;
            if (systemClassDeclaredMethodsHashMap.containsKey(clazz)) {
                methods = systemClassDeclaredMethodsHashMap.get(clazz);
            } else {
                methods = clazz.getDeclaredMethods();
                systemClassDeclaredMethodsHashMap.put(clazz, methods);
            }
            for (Method method : methods) {
                methodVector.add(method);
            }
        }
        systemClassMethodVectorHashMap.put(object.getClass(), methodVector);
        return methodVector;
    }
}

Related

  1. getMethodsForObject(Object o2, String[] passedMethods)
  2. getMethodsIncludingHierarchy(Object comp, Class annotation)
  3. getMethodValue(Method method, Object instance)
  4. getMethodValue(Object base, Method method)
  5. getMethodValue(Object target, String methodName)
  6. getMethodWithExactName(Class clazz, String name)
  7. getMethodWithPrefix(final Object o, final String fieldName, final String prefix)