Java Reflection Method Get from Object getMethodWithPrefix(final Object o, final String fieldName, final String prefix)

Here you can find the source of getMethodWithPrefix(final Object o, final String fieldName, final String prefix)

Description

Returns any method whose name starts with given prefix and ends with given fieldname where first letter of the fieldname will be uppercased.

License

Open Source License

Parameter

Parameter Description
o The object containing the desired method.
fieldName The fieldname contained in the name of the desired method.
prefix A prefix string of the method-name

Return

The reflected method. Is empty if method was not found.

Declaration

private static Optional<Method> getMethodWithPrefix(final Object o, final String fieldName,
        final String prefix) 

Method Source Code

//package com.java2s;
/**/*from  w  w  w. j  a va2  s  .  c o  m*/
 * Made by Kay Lerch (https://twitter.com/KayLerch)
 *
 * Attached license applies.
 * This library is licensed under GNU GENERAL PUBLIC LICENSE Version 3 as of 29 June 2007
 */

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;

public class Main {
    /**
     * Returns any method whose name starts with given prefix and ends with given fieldname where
     * first letter of the fieldname will be uppercased.
     * @param o The object containing the desired method.
     * @param fieldName The fieldname contained in the name of the desired method.
     * @param prefix A prefix string of the method-name
     * @return The reflected method. Is empty if method was not found.
     */
    private static Optional<Method> getMethodWithPrefix(final Object o, final String fieldName,
            final String prefix) {
        final String methodName = prefix + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
        return Arrays.stream(o.getClass().getMethods()).filter(method -> method.getName().equals(methodName))
                .findFirst();
    }
}

Related

  1. getMethodValue(Method method, Object instance)
  2. getMethodValue(Object base, Method method)
  3. getMethodValue(Object target, String methodName)
  4. getMethodVector(Object object)
  5. getMethodWithExactName(Class clazz, String name)