Example usage for java.lang.reflect Method getName

List of usage examples for java.lang.reflect Method getName

Introduction

In this page you can find the example usage for java.lang.reflect Method getName.

Prototype

@Override
public String getName() 

Source Link

Document

Returns the name of the method represented by this Method object, as a String .

Usage

From source file:Main.java

public static Map toMap(Object javaBean) {

    Map result = new HashMap();
    Method[] methods = javaBean.getClass().getDeclaredMethods();
    for (Method method : methods) {
        try {//from  www  .  j  a va  2 s.  c o  m

            if (method.getName().startsWith("get")) {

                String field = method.getName();
                field = field.substring(field.indexOf("get") + 3);
                field = field.toLowerCase().charAt(0) + field.substring(1);

                Object value = method.invoke(javaBean, (Object[]) null);
                result.put(field, null == value ? "" : value.toString());

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    return result;

}

From source file:com.mobius.software.mqtt.parser.test.StaticData.java

public static boolean isGetter(Method method) {
    if (!method.getName().startsWith("get"))
        return false;
    if (method.getParameterTypes().length != 0)
        return false;
    if (void.class.equals(method.getReturnType()))
        return false;
    return true;//w ww. jav a2  s .  co m
}

From source file:Main.java

static boolean isSuperMethodAnnotated(Class<?> superClass, Method method,
        Class<? extends Annotation> annotationClass) {
    try {/*from   ww  w.j a  va 2 s .c o m*/
        return superClass.getMethod(method.getName(), method.getParameterTypes())
                .isAnnotationPresent(annotationClass);
    } catch (NoSuchMethodException ignored) {
        return false;
    }
}

From source file:ReflectionUtils.java

/**
 * Attempt to find a {@link Method} on the supplied class with the supplied name
 * and parameter types. Searches all superclasses up to <code>Object</code>.
 * <p>Returns <code>null</code> if no {@link Method} can be found.
 * @param clazz the class to introspect/*from  w ww.j a  v a 2 s  .c om*/
 * @param name the name of the method
 * @param paramTypes the parameter types of the method
 * @return the Method object, or <code>null</code> if none found
 */
public static Method findMethod(Class clazz, String name, Class[] paramTypes) {

    Class searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (name.equals(method.getName()) && Arrays.equals(paramTypes, method.getParameterTypes())) {
                return method;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:com.bfd.harpc.main.ConfigHelper.java

/**
 * ??/* w ww  . j  a  v  a  2s .  co m*/
 * <p>
 * 
 * @param configObject
 *            ?
 * @param configPrefix
 *            ??
 * @param configuration
 *            {@link PropertiesConfiguration}
 * @throws RpcException
 */
public static void initConfig(Object configObject, String configPrefix, PropertiesConfiguration configuration)
        throws RpcException {
    Method[] methods = configObject.getClass().getMethods();
    for (Method method : methods) {
        if (method.getName().length() > 3 && method.getName().startsWith("set")
                && method.getParameterTypes().length == 1) {
            String attribute = method.getName().substring(3);
            char ch = attribute.charAt(0);
            attribute = Character.toLowerCase(ch) + attribute.substring(1);
            String value = configuration.getProperty(configPrefix + attribute, "");

            try {
                if (StringUtils.isNotEmpty(value)) {
                    Type type = method.getParameterTypes()[0];
                    if (type == boolean.class) {
                        method.invoke(configObject, Boolean.valueOf(value));
                    } else if (type == int.class) {
                        method.invoke(configObject, Integer.valueOf(value));
                    } else if (type == long.class) {
                        method.invoke(configObject, Long.valueOf(value));
                    } else {
                        method.invoke(configObject, value);
                    }
                }
            } catch (Exception e) {
                LOGGER.error("Init config error", e);
                throw new RpcException(RpcException.CONFIG_EXCEPTION, e);
            }
        }
    }
}

From source file:com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.BasicDataSourceWrapper.java

private static Object invokeMethod(Object object, String methodName, Object arg1) throws Exception {
    Method[] methods = object.getClass().getMethods();
    for (Method method : methods) {
        if (method.getName().equals(methodName)) {
            if (arg1 == null)
                return method.invoke(object);
            else/*  ww  w .ja  v  a  2 s  .  co m*/
                return method.invoke(object, arg1);
        }
    }
    throw new Exception("Class " + object.getClass().getName() + " Invalid Method: " + methodName);
}

From source file:Main.java

private static Set<Method> findMethodsCompatibleWith(boolean staticMethod, Set<Method> methods,
        String methodName, Class<?>[] argTypes) {
    final Set<Method> list = new HashSet<Method>(methods);
    for (final Iterator<Method> it = list.iterator(); it.hasNext();) {
        final Method method = it.next();
        if (!methodName.equals(method.getName())) {
            it.remove();/*www .  j  av a2s  .c om*/
            continue;
        }
        if (argTypes.length != method.getParameterTypes().length) {
            it.remove();
            continue;
        }
        if (Modifier.isAbstract(method.getModifiers())
                || Modifier.isStatic(method.getModifiers()) != staticMethod) {
            it.remove();
            continue;
        }
        if (!isMethodArgCompatible(method, argTypes)) {
            it.remove();
            continue;
        }

    }
    return list;
}

From source file:MainClass.java

public static void printMethods(Class cl) {
    Method[] methods = cl.getDeclaredMethods();

    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        Class retType = m.getReturnType();
        Class[] paramTypes = m.getParameterTypes();
        String name = m.getName();
        System.out.print(Modifier.toString(m.getModifiers()));
        System.out.print(" " + retType.getName() + " " + name + "(");
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getName());
        }//from  ww w  .  j  a v a 2  s.c om
        System.out.println(");");
    }
}

From source file:Main.java

public static void runMain(Object main, String[] args, String defCommand) throws Exception {
    if (args.length > 0) {
        String command = args[0];
        Method[] methods = main.getClass().getMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method.getName().equals(command)) {
                String[] remaining = new String[args.length - 1];
                System.arraycopy(args, 1, remaining, 0, remaining.length);
                try {
                    method.invoke(main, bindParameters(method, remaining));
                } catch (InvocationTargetException e) {
                    Throwable cause = e.getCause();
                    if (cause instanceof IllegalArgumentException) {
                        System.err.println("Syntax error: " + cause.getMessage());
                    } else if (cause instanceof Exception) {
                        throw (Exception) cause;
                    } else {
                        throw e;
                    }//from   w  w  w . jav a  2  s. c  o  m
                }
                return;
            }
        }
    }
    if (defCommand != null) {
        runMain(main, new String[] { defCommand }, null);
    }
}

From source file:com.weibo.motan.demo.client.DemoRpcClient.java

public static DubboBenchmark.BenchmarkMessage prepareArgs()
        throws InvocationTargetException, IllegalAccessException {

    boolean b = true;
    int i = 100000;
    String s = "??";

    DubboBenchmark.BenchmarkMessage.Builder builder = DubboBenchmark.BenchmarkMessage.newBuilder();

    Method[] methods = builder.getClass().getDeclaredMethods();
    for (Method m : methods) {
        if (m.getName().startsWith("setField") && ((m.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC)) {
            Parameter[] params = m.getParameters();
            if (params.length == 1) {
                String n = params[0].getParameterizedType().getTypeName();
                m.setAccessible(true);//from   w  w w .  j  av  a  2 s .co m
                if (n.endsWith("java.lang.String")) {
                    m.invoke(builder, new Object[] { s });
                } else if (n.endsWith("int")) {
                    m.invoke(builder, new Object[] { i });
                } else if (n.equals("boolean")) {
                    m.invoke(builder, new Object[] { b });
                }

            }
        }
    }

    return builder.build();

}