List of utility methods to do Reflection Method Name
Method | getMethod(Class> clazz, String name, Class>... args) get Method for (Method m : clazz.getMethods()) if (m.getName().equals(name) && (args.length == 0 || ClassListEqual(args, m.getParameterTypes()))) { m.setAccessible(true); return m; return null; |
Optional | getMethod(Class> clazz, String name, Class>... params) get Method try { return Optional.of(clazz.getMethod(name, params)); } catch (NoSuchMethodException ignored) { try { return Optional.of(clazz.getDeclaredMethod(name, params)); } catch (NoSuchMethodException ignored) { return Optional.empty(); |
Method | getMethod(Class> clazz, String name, int paramlength) get Method do { for (Method method : clazz.getDeclaredMethods()) { if (method.getName().equals(name) && (method.getParameterTypes().length == paramlength)) { return setAccessible(method); } while ((clazz = clazz.getSuperclass()) != null); throw new RuntimeException("Can't find method " + name + " with params length " + paramlength); ... |
Method | getMethod(Class> clazz, String... names) get Method try { Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { for (String methodName : names) { if (method.getName().equalsIgnoreCase(methodName)) { method.setAccessible(true); return method; } catch (Exception e) { throw e; return null; |
Method | getMethod(Class> cls, String name) Retrieve method with 0 parameter count and with given name from given class return getMethod(cls, name, new Class[0]); |
Method | getMethod(Class> cls, String name) Search for a method in the class and all superclasses. NoSuchMethodException firstExc = null; for (Class<?> c = cls; c != null; c = c.getSuperclass()) { try { return c.getDeclaredMethod(name); } catch (NoSuchMethodException e) { if (firstExc == null) { firstExc = e; throw firstExc; |
Method | getMethod(Class> cls, String name, Class>... types) get Method Method method = null; while (method == null && cls != null) { try { method = cls.getDeclaredMethod(name, types); } catch (NoSuchMethodException ignored) { cls = cls.getSuperclass(); if (method == null) throw new NoSuchMethodException(); return method; |
Method | getMethod(Class> clss, String name, Class>... params) Gets the given method or throw exception. try { return clss.getMethod(name, params); } catch (NoSuchMethodException e) { fatal("Method " + name + " does not exist", e); return null; |
Method | getMethod(Class> clz, String name) get Method return clz.getMethod("get" + name); |
Method | getMethod(Class> klass, String methodName) get Method try { return klass.getMethod(methodName); } catch (Exception e) { throw new RuntimeException(e); |