Example usage for java.lang Class getMethods

List of usage examples for java.lang Class getMethods

Introduction

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

Prototype

@CallerSensitive
public Method[] getMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

Usage

From source file:Main.java

public static void main(String[] args) {

    Class cls = String.class;

    Method[] m = cls.getMethods();
    for (int i = 0; i < m.length; i++) {
        // returns te declaring class
        Class dec = m[i].getDeclaringClass();
        // displays all methods
        System.out.println("Method = " + m[i].toString());
        System.out.println(" Declaring class: " + dec.toString());
    }//from   w  ww  .  j  a v a 2  s  . c  o  m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class cls = java.lang.String.class;
    Method[] methods = cls.getMethods();
    for (int i = 0; i < methods.length; i++) {
        Class returnType = methods[i].getReturnType();
        Class[] paramTypes = methods[i].getParameterTypes();
        System.out.println(methods[i]);
    }//from  www  .  j a  v  a 2  s. c  o m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];

    field.setAccessible(true);/*  w  w w.  ja  va2s. co m*/
    constructor.setAccessible(true);
    method.setAccessible(true);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;//from  w  w  w  .  ja va  2 s . co  m

    name = cls.getName();
    System.out.println(name);
    name = cls.getName() + "." + field.getName();
    System.out.println(name);
    name = constructor.getName();
    System.out.println(name);
    name = cls.getName() + "." + method.getName();
    System.out.println(name);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;// w  w w .jav  a  2  s .co  m

    name = cls.getName().substring(cls.getPackage().getName().length() + 1);
    System.out.println(name);
    name = field.getName();
    System.out.println(name);
    name = constructor.getName().substring(cls.getPackage().getName().length() + 1);
    System.out.println(name);
    name = method.getName();
    System.out.println(name);

}

From source file:PrimsDotClass.java

public static void main(String[] args) {
    Class c = int.class;
    System.out.println(c.getName());
    Method[] methods = c.getMethods();
    System.out.println(c.getName() + " has " + methods.length + " methods");
}

From source file:GetMethods.java

public static void main(String[] args) throws Exception {
    GetMethods object = new GetMethods();
    Class clazz = object.getClass();

    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        System.out.println("Method name        = " + method.getName());
        System.out.println("Method return type = " + method.getReturnType().getName());

        Class[] paramTypes = method.getParameterTypes();
        for (Class c : paramTypes) {
            System.out.println("Param type         = " + c.getName());
        }//from   w ww. j  av a 2s .  c  o m
    }
    Method method = clazz.getMethod("add", new Class[] { int.class, int.class });
    System.out.println("Method name: " + method.getName());
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Class cls = Class.forName("java.awt.Label");
    System.out.println("Methods =");

    Method m[] = cls.getMethods();
    for (int i = 0; i < m.length; i++) {
        System.out.println(m[i]);
    }/*w ww  . j a  v a2  s.c  o  m*/
}

From source file:MyClass.java

public static void main(String[] argv) {
    Class<MyClass> cls = MyClass.class;
    for (Method m : cls.getMethods()) {
        System.out.println(m.getName());
        System.out.println(getModifiers(m));
        System.out.println(getParameters(m));
        System.out.println(getExceptionList(m));
    }/*from www  . j  a  va  2 s .  c o  m*/
}

From source file:ShowMethods.java

public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println(usage);
        System.exit(0);/*from   www. jav  a  2s.  c  om*/
    }
    int lines = 0;
    try {
        Class c = Class.forName(args[0]);
        Method[] m = c.getMethods();
        Constructor[] ctor = c.getConstructors();
        if (args.length == 1) {
            for (int i = 0; i < m.length; i++)
                System.out.println(p.matcher(m[i].toString()).replaceAll(""));
            for (int i = 0; i < ctor.length; i++)
                System.out.println(p.matcher(ctor[i].toString()).replaceAll(""));
            lines = m.length + ctor.length;
        } else {
            for (int i = 0; i < m.length; i++)
                if (m[i].toString().indexOf(args[1]) != -1) {
                    System.out.println(p.matcher(m[i].toString()).replaceAll(""));
                    lines++;
                }
            for (int i = 0; i < ctor.length; i++)
                if (ctor[i].toString().indexOf(args[1]) != -1) {
                    System.out.println(p.matcher(ctor[i].toString()).replaceAll(""));
                    lines++;
                }
        }
    } catch (ClassNotFoundException e) {
        System.out.println("No such class: " + e);
    }
}