Here you can find the source of getMethodInstace(Class> c, Object inst, String name, Class>[] types, boolean access)
Parameter | Description |
---|---|
c | the class (can be null for objects) |
inst | the instance, null for static context |
name | the name of the method |
types | the types required for the method |
access | true if the method should be made accessible |
public static Object getMethodInstace(Class<?> c, Object inst, String name, Class<?>[] types, boolean access)
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Method; public class Main { /**//from www . j ava 2 s. com * same as callMethod, but only returns a reference * @param c the class (can be null for objects) * @param inst the instance, null for static context * @param name the name of the method * @param types the types required for the method * @param access true if the method should be made accessible * @return a reflective instance of the requested method, or null if it does not exist */ public static Object getMethodInstace(Class<?> c, Object inst, String name, Class<?>[] types, boolean access) { if (c == null) c = inst.getClass(); try { Method method = null; while (method == null) { try { method = c.getDeclaredMethod(name, types); } catch (NoSuchMethodException e) { c = c.getSuperclass(); if (c == null) throw new NoSuchMethodException(name); } } if (access) method.setAccessible(true); return method; } catch (Exception e) { e.printStackTrace(); return null; } } }