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:com.jetyun.pgcd.rpc.reflect.ClassAnalyzer.java

/**
 * Analyze a class and create a ClassData object containing all of the
 * public methods (both static and non-static) in the class.
 * /*w w  w  . jav  a 2 s .c om*/
 * @param clazz
 *            class to be analyzed.
 * 
 * @return a ClassData object containing all the public static and
 *         non-static methods that can be invoked on the class.
 */
private static ClassData analyzeClass(Class clazz) {
    log.info("analyzing " + clazz.getName());
    Method methods[] = clazz.getMethods();
    ClassData cd = new ClassData();
    cd.clazz = clazz;

    // Create temporary method map
    HashMap staticMethodMap = new HashMap();
    HashMap methodMap = new HashMap();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (method.getDeclaringClass() == Object.class) {
            continue;
        }
        int mod = methods[i].getModifiers();
        if (!Modifier.isPublic(mod)) {
            continue;
        }
        Class param[] = method.getParameterTypes();

        // don't count locally resolved args
        int argCount = 0;
        for (int n = 0; n < param.length; n++) {
            if (LocalArgController.isLocalArg(param[n])) {
                continue;
            }
            argCount++;
        }

        MethodKey mk = new MethodKey(method.getName(), argCount);
        ArrayList marr = (ArrayList) methodMap.get(mk);
        if (marr == null) {
            marr = new ArrayList();
            methodMap.put(mk, marr);
        }
        marr.add(method);
        if (Modifier.isStatic(mod)) {
            marr = (ArrayList) staticMethodMap.get(mk);
            if (marr == null) {
                marr = new ArrayList();
                staticMethodMap.put(mk, marr);
            }
            marr.add(method);
        }
    }
    cd.methodMap = new HashMap();
    cd.staticMethodMap = new HashMap();
    // Convert ArrayLists to arrays
    Iterator i = methodMap.entrySet().iterator();
    while (i.hasNext()) {
        Map.Entry entry = (Map.Entry) i.next();
        MethodKey mk = (MethodKey) entry.getKey();
        ArrayList marr = (ArrayList) entry.getValue();
        if (marr.size() == 1) {
            cd.methodMap.put(mk, marr.get(0));
        } else {
            cd.methodMap.put(mk, marr.toArray(new Method[0]));
        }
    }
    i = staticMethodMap.entrySet().iterator();
    while (i.hasNext()) {
        Map.Entry entry = (Map.Entry) i.next();
        MethodKey mk = (MethodKey) entry.getKey();
        ArrayList marr = (ArrayList) entry.getValue();
        if (marr.size() == 1) {
            cd.staticMethodMap.put(mk, marr.get(0));
        } else {
            cd.staticMethodMap.put(mk, marr.toArray(new Method[0]));
        }
    }
    return cd;
}

From source file:de.micromata.genome.gwiki.utils.ClassUtils.java

public static boolean hasMethod(Class<?> cls, String method) {
    if (StringUtils.isEmpty(method) == true) {
        return false;
    }/* w ww. j a  va2  s.  c om*/
    for (Method m : cls.getMethods()) {
        if (method.equals(m.getName()) == true) {
            return true;
        }
    }
    return false;
}

From source file:elaborate.jaxrs.JAXUtils.java

/**
 * Returns an API description for each HTTP method in the specified
 * class if it has a <code>Path</code> annotation, or an empty list
 * if the <code>Path</code> annotation is missing.
 *///from ww w .ja v  a 2s . c  om
public static List<API> generateAPIs(Class<?> cls) {
    List<API> list = Lists.newArrayList();

    String basePath = pathValueOf(cls);
    if (!basePath.isEmpty()) {
        for (Method method : cls.getMethods()) {
            Builder<String> builder = ImmutableList.<String>builder();
            if (method.isAnnotationPresent(GET.class)) {
                builder.add(HttpMethod.GET);
            }
            if (method.isAnnotationPresent(POST.class)) {
                builder.add(HttpMethod.POST);
            }
            if (method.isAnnotationPresent(PUT.class)) {
                builder.add(HttpMethod.PUT);
            }
            if (method.isAnnotationPresent(DELETE.class)) {
                builder.add(HttpMethod.DELETE);
            }

            ImmutableList<String> reqs = builder.build();
            if (!reqs.isEmpty()) {
                String subPath = pathValueOf(method);
                String fullPath = subPath.isEmpty() ? basePath : basePath + "/" + subPath;
                fullPath = fullPath.replaceAll("\\{([^:]*):[^}]*\\}", "{$1}");
                list.add(new API(fullPath, reqs, requestContentTypesOf(method), responseContentTypesOf(method),
                        descriptionOf(method)));
            }
        }
    }

    return list;
}

From source file:com.qcadoo.view.internal.CustomMethodHolder.java

public static boolean methodExists(final String className, final String methodName,
        final ApplicationContext applicationContext, final Class<?>[] expectedParameterTypes) {
    Preconditions.checkArgument(!StringUtils.isBlank(className), "class name attribute is not specified!");
    Preconditions.checkArgument(!StringUtils.isBlank(methodName), "method name attribute is not specified!");
    Preconditions.checkArgument(expectedParameterTypes != null, "expected parameter types are not specified!");

    try {/*from   w ww.j  a  v a  2 s  .com*/
        final Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);

        for (Method method : clazz.getMethods()) {
            if (method.getName().equals(methodName)
                    && Arrays.deepEquals(method.getParameterTypes(), expectedParameterTypes)) {
                return true;
            }
        }
        return false;
    } catch (ClassNotFoundException e) {
        return false;
    }
}

From source file:com.onesignal.TrackGooglePurchase.java

private static Method getAsInterfaceMethod(Class clazz) {
    for (Method method : clazz.getMethods()) {
        Class<?>[] args = method.getParameterTypes();
        if (args.length == 1 && args[0] == android.os.IBinder.class)
            return method;
    }//from   w  ww .  ja v  a2s  .c  o  m

    return null;
}

From source file:com.onesignal.TrackGooglePurchase.java

private static Method getGetPurchasesMethod(Class clazz) {
    for (Method method : clazz.getMethods()) {
        Class<?>[] args = method.getParameterTypes();
        if (args.length == 4 && args[0] == int.class && args[1] == String.class && args[2] == String.class
                && args[3] == String.class)
            return method;
    }/*  w ww. j av a  2s .c o m*/

    return null;
}

From source file:com.onesignal.TrackGooglePurchase.java

private static Method getGetSkuDetailsMethod(Class clazz) {
    for (Method method : clazz.getMethods()) {
        Class<?>[] args = method.getParameterTypes();
        if (args.length == 4 && args[0] == int.class && args[1] == String.class && args[2] == String.class
                && args[3] == Bundle.class)
            return method;
    }//ww  w .ja  v  a2  s  .  c  om

    return null;
}

From source file:com.manydesigns.portofino.buttons.ButtonsLogic.java

public static List<ButtonInfo> computeButtonsForClass(Class<?> someClass, String list) {
    List<ButtonInfo> buttons = new ArrayList<ButtonInfo>();
    for (Method method : someClass.getMethods()) {
        if (method.isBridge() || method.isSynthetic()) {
            continue;
        }//from   w w  w . j  a v  a2s  .c  o  m
        Button button = getButtonForMethod(method, list);
        if (button != null) {
            ButtonInfo buttonInfo = new ButtonInfo(button, method, someClass);
            buttons.add(buttonInfo);
        }
    }
    Collections.sort(buttons, new ButtonComparatorByOrder());
    //Group together buttons of the same group
    for (int i = 0; i < buttons.size() - 1; i++) {
        ButtonInfo info = buttons.get(i);
        String group = info.getButton().group();
        if (!StringUtils.isBlank(group)) {
            int count = 1;
            for (int j = i + 1; j < buttons.size(); j++) {
                ButtonInfo info2 = buttons.get(j);
                if (info2.getButton().group().equals(group)) {
                    buttons.remove(j);
                    buttons.add(i + count, info2);
                    count++;
                }
            }
        }
    }
    return buttons;
}

From source file:com.puppycrawl.tools.checkstyle.doclets.TokenTypesDocletTest.java

private static Method getMethodGetRootDocImplByReflection() throws ClassNotFoundException {
    Method result = null;/*  w  w  w.  j av  a2s. c o m*/
    final Class<?> javadocToolClass = Class.forName("com.sun.tools.javadoc.JavadocTool");
    final Method[] methods = javadocToolClass.getMethods();
    for (Method method : methods) {
        if ("getRootDocImpl".equals(method.getName())) {
            result = method;
        }
    }
    return result;
}

From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java

/**
 * retrieve candidate setter method/*www  .  j  a v a2 s .c  o  m*/
 *
 * @param clazz
 * @param name
 * @return
 */
@SuppressWarnings({ "rawtypes" })
private static Method getCandidateMethod(Class clazz, String name) {
    for (Method method : clazz.getMethods()) {
        if (name.equals(method.getName()) && method.getParameterTypes().length == 1)
            return method;
    }
    return null;
}