List of utility methods to do Reflection Method Get from Object
Collection | getMethodsAnnotatedWith(Class extends Annotation> ann, Object o) get Methods Annotated With Class<?> c; if (o instanceof Class<?>) c = (Class<?>) o; else c = o.getClass(); HashSet<Method> methods = new HashSet<>(); for (Method m : c.getMethods()) { if (m.getAnnotation(ann) != null) ... |
Collection | getMethodsAnnotatedWith(Object target, Class extends Annotation> annotation) get Methods Annotated With Set<Method> methods = new HashSet<Method>(); for (Method method : target.getClass().getDeclaredMethods()) { if (method.isAnnotationPresent(annotation)) { methods.add(method); return methods; |
List | getMethodsAnnotatedWithValue(Class extends Annotation> anno, Object o, String name, Object value) get Methods Annotated With Value ArrayList<Method> methods = new ArrayList<Method>(); List<Method> all = Arrays.asList(o.getClass().getDeclaredMethods()); for (Iterator<Method> iterator = all.iterator(); iterator.hasNext();) { Method method = (Method) iterator.next(); if (value != null && value.equals(getAnnotationValue(anno, method, name))) methods.add(method); return methods; ... |
Collection | getMethodsByStartsWithName(String name, Object o) this will get all mehods whose names start with the expressed string Collection<Method> methods = new ArrayList<Method>(); Class c = o.getClass(); java.lang.reflect.Method[] theMethods = c.getMethods(); for (int i = 0; i < theMethods.length; i++) { String methodString = theMethods[i].getName(); if (methodString.startsWith(name)) { methods.add(theMethods[i]); Class[] parameterTypes = theMethods[i].getParameterTypes(); ... |
Method[] | getMethodsForObject(Object o2, String[] passedMethods) get Methods For Object Method methods[] = new Method[passedMethods.length]; Class clazz = o2.getClass(); for (int i = 0; i < passedMethods.length; i++) { methods[i] = clazz.getMethod(passedMethods[i], (Class[]) null); return methods; |
List | getMethodsIncludingHierarchy(Object comp, Class extends Annotation> annotation) same as getMethod but uses while loop to check hierarchy LinkedList<Method> result = new LinkedList<Method>(); Class<?> current = comp.getClass(); while (current != null) { result.addLast(getMethod(current, annotation)); current = current.getSuperclass(); return result; |
Object | getMethodValue(Method method, Object instance) get Method Value Object result; boolean accessible = method.isAccessible(); if (!accessible) { method.setAccessible(true); try { result = method.invoke(instance); } catch (Exception ex) { ... |
Object | getMethodValue(Object base, Method method) get Method Value try { return method.invoke(base); } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException ex) { throw new IllegalArgumentException(ex); |
Object | getMethodValue(Object target, String methodName) get method value by name. Method method = target.getClass().getDeclaredMethod(methodName);
return method.invoke(target);
|
Vector | getMethodVector(Object object) 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 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; |