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 void toXml(Object bean) {
    Method[] declaredMethods = bean.getClass().getDeclaredMethods();
    for (Method declaredMethod : declaredMethods) {
        if (declaredMethod.getName().startsWith("get")) {
            try {
                Object invoke = declaredMethod.invoke(bean);
                System.out.println(declaredMethod.getName() + " = " + invoke);
            } catch (Exception e) {
                e.printStackTrace(); //TODO To change body of catch statement use File | Settings | File Templates.
            }/*from   w ww  .  jav a2  s  . c o  m*/
        }
    }
}

From source file:Main.java

public static Method getMethod(Class<?> clazz, String methodName) {
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.getName().equals(methodName)) {
            method.setAccessible(true);//from   w  ww .ja v  a2s . c o m
            return method;
        }
    }
    return null;
}

From source file:Main.java

public static boolean setProperty(Object object, String fieldName, Object fieldValue) {
    Class<?> clazz = object.getClass();
    String mname = "set" + fieldName;
    try {/*from  ww  w.  j a  v  a 2s  .  c  om*/
        for (Method m : clazz.getMethods()) {
            if (m.getName().equalsIgnoreCase(mname)) {
                m.invoke(object, fieldValue);
                return true;
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }

    return false;
}

From source file:Main.java

public final static Set<String> getMethods(Class<?> clazz) {
    HashSet<String> methodSet = new HashSet<String>();
    Method[] methodArray = clazz.getMethods();
    for (Method method : methodArray) {
        String methodName = method.getName();
        methodSet.add(methodName);// w  ww .j  a  va 2  s.  c  om
    }
    return methodSet;
}

From source file:Main.java

public static String getSetMethod(Class<?> c, String prefix, String postfix) {
    StringWriter sw = new StringWriter();
    if (prefix == null)
        prefix = "";
    if (postfix == null)
        postfix = "";
    Method[] ms = c.getDeclaredMethods();
    if (ms != null && ms.length > 0) {
        for (Method m : ms) {
            String name = m.getName();
            if (name.startsWith("set") && isPublicNoStatic(m.getModifiers())) {
                sw.append(prefix).append(name).append("(").append(postfix).append(");\r\n");
            }/*  ww  w  . j a va2s.  c o m*/
        }
    }
    return sw.toString();
}

From source file:Main.java

public static synchronized Method getSetter(final Class targetClass, final String propertyName) {
    String setterName = "set" + Character.toUpperCase(propertyName.charAt(0));
    if (setterName.length() > 1)
        setterName += propertyName.substring(1);

    final Method[] methods = targetClass.getMethods();

    for (Method m : methods) {
        if (m.getName().equals(setterName) && m.getParameterTypes().length == 1)
            return m;
    }//from   ww  w .  j ava2  s .c o  m
    return null;
}

From source file:Main.java

public static <T> T callMethod(@NonNull Object obj, @NonNull String name, Object... args) throws Exception {
    for (Class cls = obj.getClass(); //
            cls != null && cls != Object.class; //
            cls = cls.getSuperclass()) {
        for (Method m : cls.getDeclaredMethods()) {
            if (m.getName().equals(name)) {
                m.setAccessible(true);/*  ww w .j  ava  2  s  .c o  m*/
                //noinspection unchecked
                return (T) m.invoke(obj, args);
            }
        }
    }
    throw new RuntimeException("no method matching " + name);
}

From source file:Main.java

public static void callMethod(Object obj, String method, Object... params) {
    Method[] ms = obj.getClass().getMethods();
    for (Method m : ms) {
        if (!m.getName().equals(method))
            continue;
        // TODO check type args
        Class<?>[] types = m.getParameterTypes();
        if (params.length != types.length)
            continue;
        for (int i = 0; i < params.length; i++) {

        }/*from  w w  w . j  a  v  a 2s .c o  m*/
        // call
        try {
            m.invoke(obj, params);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    throw new RuntimeException(obj + "." + method + "(" + params.length + ")");
}

From source file:Main.java

public static Method getXMLBeansValueMethod(Class<?> wrapperType) {
    for (Method method : wrapperType.getMethods()) {
        if (method.getName().startsWith("addNew")) {
            return method;
        }/*from ww w . j a  v a2s.  co m*/
    }
    return null;
}

From source file:Main.java

private static Method getMediadataMethod() {
    Method[] methods = MediaPlayer.class.getDeclaredMethods();
    for (Method method : methods) {
        if (TextUtils.equals(method.getName(), "getMetadata")) {
            return method;
        }/* www .j  a v a2 s.  co  m*/
    }
    return null;
}