Example usage for java.lang Class getDeclaredMethods

List of usage examples for java.lang Class getDeclaredMethods

Introduction

In this page you can find the example usage for java.lang Class getDeclaredMethods.

Prototype

@CallerSensitive
public Method[] getDeclaredMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.

Usage

From source file:Main.java

public static HashMap<String, Object> testReflect(Object obj)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    Class c = obj.getClass();
    Method m[] = c.getDeclaredMethods();
    for (int i = 0; i < m.length; i++) {
        if (m[i].getName().indexOf("get") == 0) {
            hashMap.put(m[i].getName(), m[i].invoke(obj, new Object[0]));
        }//  w  w w. ja  va  2 s . c om
    }
    return hashMap;
}

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  .j  a v a2s . c o m*/
            return method;
        }
    }
    return null;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static List<Method> getParentClassMothds(List<Method> list, Class clazz) {

    Method[] methods = clazz.getDeclaredMethods();

    for (Method method : methods) {

        list.add(method);/*from w  w w.  j  a va 2 s  .c  om*/

    }

    if (clazz.getSuperclass() == Object.class) {

        return list;

    }

    getParentClassMothds(list, clazz.getSuperclass());

    return list;

}

From source file:Main.java

public static Method getMethod(Class clazz, String methodName) {
    Method[] methods = clazz.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].getName().equalsIgnoreCase(methodName)
                || methods[i].getName().equalsIgnoreCase("get" + methodName)) {
            return methods[i];
        }//from   ww  w . j  av a2  s.c  o  m
    }
    return null;
}

From source file:Main.java

private static void getAllIMethods(Class<?> c, List<Method> methods) {
    for (Method m : c.getDeclaredMethods())
        methods.add(m);/*from   w  w w  .j a va 2s .c  o  m*/
    for (Class<?> i : c.getInterfaces()) {
        getAllIMethods(i, methods);
    }
}

From source file:Main.java

public static boolean declaredEquals(Class<?> clazz) {
    for (Method declaredMethod : clazz.getDeclaredMethods()) {
        if (EQUALS_METHOD.equals(declaredMethod.getName())
                && Arrays.equals(declaredMethod.getParameterTypes(), EQUALS_PARAMETERS)) {
            return true;
        }//from   w  w  w  .j a  v a  2s . c om
    }
    return false;
}

From source file:MyClass.java

public static ArrayList<String> getDeclaredMethodsList(Class c) {
    Method[] methods = c.getDeclaredMethods();
    ArrayList<String> methodsList = getMethodsDesciption(methods);
    return methodsList;
}

From source file:InheritedMethods.java

private static void printMethods(Class c) {
    out.format("Methods from %s%n", c);
    Method[] meths = c.getDeclaredMethods();
    if (meths.length != 0) {
        for (Method m : meths)
            out.format("  Method:  %s%n", m.toGenericString());
    } else {//from ww w  .j a va 2 s .  c o  m
        out.format("  -- no methods --%n");
    }
    out.format("%n");
}

From source file:Main.java

public static void getTelephonyManagerMethods(Context context) {
    String out;/*from w w  w.java2  s  . co m*/
    try {
        File dir = new File(String.valueOf(context.getFilesDir()));
        // create the file in which we will write the contents
        String fileName = "telephony.txt";
        File file = new File(dir, fileName);
        FileOutputStream os = new FileOutputStream(file);
        Class<?> c = Class.forName(GENERIC);
        Method[] cm = c.getDeclaredMethods();
        for (Method m : cm) {
            out = m.toString() + "\n";
            os.write(out.getBytes());
        }
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ReflectionTest.java

/**
 * Prints all methods of a class//from  ww w .  j  ava2  s.  co m
 * @param cl a class
 */
public static void printMethods(Class cl) {
    Method[] methods = cl.getDeclaredMethods();

    for (Method m : methods) {
        Class retType = m.getReturnType();
        String name = m.getName();

        System.out.print("   ");
        // print modifiers, return type and method name
        String modifiers = Modifier.toString(m.getModifiers());
        if (modifiers.length() > 0)
            System.out.print(modifiers + " ");
        System.out.print(retType.getName() + " " + name + "(");

        // print parameter types
        Class[] paramTypes = m.getParameterTypes();
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getName());
        }
        System.out.println(");");
    }
}