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.arpnetworking.jackson.BuilderDeserializer.java

private static void addTo(final Set<Type> visited, final Map<Class<?>, JsonDeserializer<?>> deserializerMap,
        final Type targetType) {
    if (visited.contains(targetType)) {
        return;//from  ww w.j  a v a 2s  . c  o m
    }
    visited.add(targetType);

    if (targetType instanceof Class<?>) {
        @SuppressWarnings("unchecked")
        final Class<Object> targetClass = (Class<Object>) targetType;
        try {
            // Look-up and register the builder for this class
            final Class<? extends Builder<? extends Object>> builderClass = getBuilderForClass(targetClass);
            deserializerMap.put(targetClass, BuilderDeserializer.of(builderClass));

            LOGGER.info("Registered builder for class; builderClass=" + builderClass + " targetClass="
                    + targetClass);

            // Process all setters on the builder
            for (final Method method : builderClass.getMethods()) {
                if (isSetterMethod(builderClass, method)) {
                    final Type setterArgumentType = method.getGenericParameterTypes()[0];
                    // Recursively register builders for each setter's type
                    addTo(visited, deserializerMap, setterArgumentType);
                }
            }
        } catch (final ClassNotFoundException e) {
            // Log that the class is not build-able
            LOGGER.debug("Ignoring class without builder; targetClass=" + targetClass);
        }

        // Support for JsonSubTypes annotation
        if (targetClass.isAnnotationPresent(JsonSubTypes.class)) {
            final JsonSubTypes.Type[] subTypes = targetClass.getAnnotation(JsonSubTypes.class).value();
            for (final JsonSubTypes.Type subType : subTypes) {
                addTo(visited, deserializerMap, subType.value());
            }
        }

        // Support for JsonTypeInfo annotation
        // TODO(vkoskela): Support JsonTypeInfo classpath scan [MAI-116]
    }

    if (targetType instanceof ParameterizedType) {
        // Recursively register builders for each parameterized type
        final ParameterizedType targetParameterizedType = (ParameterizedType) targetType;
        final Type rawType = targetParameterizedType.getRawType();
        addTo(visited, deserializerMap, rawType);
        for (final Type argumentActualType : targetParameterizedType.getActualTypeArguments()) {
            addTo(visited, deserializerMap, argumentActualType);
        }
    }
}

From source file:com.medallia.spider.api.StRenderer.java

/** @return the action method of the given class; throws AssertionError if no such method exists */
private static Method findActionMethod(Class<?> clazz) {
    Method am = ACTION_METHOD_MAP.get(clazz);
    if (am != null)
        return am;

    for (Method m : CollUtils.concat(Arrays.asList(clazz.getMethods()),
            Arrays.asList(clazz.getDeclaredMethods()))) {
        if (m.getName().equals("action")) {
            int modifiers = m.getModifiers();
            if (Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers))
                continue;
            m.setAccessible(true);/*from w ww .  j a v a  2 s. co  m*/
            ACTION_METHOD_MAP.put(clazz, m);
            return m;
        }
    }
    throw new AssertionError("No action method found in " + clazz);
}

From source file:org.openmrs.module.webservices.docs.ResourceDocCreator.java

/**
 * Generates {@link ResourceOperation}s corresponding to the supported http methods
 * /* w w  w.j a v a 2  s .co m*/
 * @param url
 * @param clazz
 * @param supportsSearching specified if the controller the method belongs supports searching
 * @return
 */
private static List<ResourceOperation> getResourceOperations(String url, Class<?> clazz,
        boolean supportsSearching) {
    List<ResourceOperation> resourceOperations = new ArrayList<ResourceOperation>();
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        RequestMapping antn = (RequestMapping) method.getAnnotation(RequestMapping.class);
        if (antn == null)
            continue;

        String requestMethod = antn.method()[0].name();

        if (requestMethod.equals("TRACE")) {
            //Skip TRACE, which is used to disable a method in HL7MessageController.
            continue;
        }

        String operationUrl = requestMethod + " " + url;

        if (antn.value().length > 0)
            operationUrl += antn.value()[0];

        String paramString = null;
        for (String param : antn.params()) {
            if (paramString == null)
                paramString = param;
            else
                paramString += ("&" + param);
        }

        if (paramString != null)
            operationUrl += "?" + paramString;

        resourceOperations.add(new ResourceOperation(operationUrl,
                getMethodDescription(antn, requestMethod, method, supportsSearching)));
    }

    Collections.sort(resourceOperations);

    return resourceOperations;
}

From source file:com.wxxr.nirvana.json.JSONUtil.java

/**
 * List visible methods carrying the/*from   w w  w . j a va  2 s  . co  m*/
 *
 * @SMDMethod annotation
 *
 * @param ignoreInterfaces
 *            if true, only the methods of the class are examined. If false,
 *            annotations on every interfaces' methods are examined.
 */
@SuppressWarnings("unchecked")
public static Method[] listSMDMethods(Class clazz, boolean ignoreInterfaces) {
    final List<Method> methods = new LinkedList<Method>();
    if (ignoreInterfaces) {
        for (Method method : clazz.getMethods()) {
            SMDMethod smdMethodAnnotation = method.getAnnotation(SMDMethod.class);
            if (smdMethodAnnotation != null) {
                methods.add(method);
            }
        }
    } else {
        // recurse the entire superclass/interface hierarchy and add in
        // order encountered
        JSONUtil.visitInterfaces(clazz, new JSONUtil.ClassVisitor() {
            public boolean visit(Class aClass) {
                for (Method method : aClass.getMethods()) {
                    SMDMethod smdMethodAnnotation = method.getAnnotation(SMDMethod.class);
                    if ((smdMethodAnnotation != null) && !methods.contains(method)) {
                        methods.add(method);
                    }
                }
                return true;
            }
        });
    }

    Method[] methodResult = new Method[methods.size()];
    return methods.toArray(methodResult);
}

From source file:com.igormaznitsa.upom.UPomModel.java

private static Method findMethod(final Class klazz, final String methodName, final boolean onlyPublic) {
    Method result = null;// w ww  . j  av a 2  s  . c  om
    for (final Method m : klazz.getMethods()) {
        if (onlyPublic && !Modifier.isPublic(m.getModifiers())) {
            continue;
        }
        if (m.getName().equalsIgnoreCase(methodName)) {
            result = m;
            break;
        }
    }
    return result;
}

From source file:org.callimachusproject.webdriver.helpers.BrowserFunctionalTestCase.java

public static TestSuite suite(Class<? extends BrowserFunctionalTestCase> testcase) throws Exception {
    TestSuite suite = new TestSuite(testcase.getName());
    for (Method method : testcase.getMethods()) {
        if (method.getName().startsWith("test") && method.getParameterTypes().length == 0
                && method.getReturnType().equals(Void.TYPE)) {
            addTests(testcase, suite, method);
        }//from  w  w w  .  j a  v a  2 s  .com
    }
    if (suite.countTestCases() == 0) {
        suite.addTest(TestSuite.warning(testcase.getName() + " has no public test methods"));
    }
    return suite;
}

From source file:com.struts2ext.json.JSONUtil.java

/**
 * List visible methods carrying the/*  ww  w.  ja v  a  2s  .  c om*/
 * 
 * @SMDMethod annotation
 * @param ignoreInterfaces
 *            if true, only the methods of the class are examined. If false,
 *            annotations on every interfaces' methods are examined.
 */
public static Method[] listSMDMethods(Class<?> clazz, boolean ignoreInterfaces) {
    final List<Method> methods = new LinkedList<Method>();
    if (ignoreInterfaces) {
        for (Method method : clazz.getMethods()) {
            SMDMethod smdMethodAnnotation = method.getAnnotation(SMDMethod.class);
            if (smdMethodAnnotation != null) {
                methods.add(method);
            }
        }
    } else {
        // recurse the entire superclass/interface hierarchy and add in
        // order encountered
        JSONUtil.visitInterfaces(clazz, new JSONUtil.ClassVisitor() {
            public boolean visit(Class<?> aClass) {
                for (Method method : aClass.getMethods()) {
                    SMDMethod smdMethodAnnotation = method.getAnnotation(SMDMethod.class);
                    if ((smdMethodAnnotation != null) && !methods.contains(method)) {
                        methods.add(method);
                    }
                }
                return true;
            }
        });
    }

    Method[] methodResult = new Method[methods.size()];
    return methods.toArray(methodResult);
}

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

private static boolean isInterfaceWithAnnotatedMethods(final Class<?> iface) {
    synchronized (ANNOTATED_INTERFACE_CACHE) {
        final Boolean flag = ANNOTATED_INTERFACE_CACHE.get(iface);
        if (flag != null) {
            return flag;
        }/*from  ww w .j  a v  a2s  .c o  m*/
        boolean found = false;
        for (final Method ifcMethod : iface.getMethods()) {
            if (ifcMethod.getAnnotations().length > 0) {
                found = true;
                break;
            }
        }
        ANNOTATED_INTERFACE_CACHE.put(iface, found);
        return found;
    }
}

From source file:org.paxml.util.ReflectUtils.java

/**
 * Call a method on an object./*  www . j  a  v  a2 s .co m*/
 * 
 * @param obj
 *            the object
 * @param method
 *            the method name
 * @param args
 *            the arguments
 * @return the return value
 */
public static Object callMethod(Object obj, String method, Object[] args) {

    Class clazz = obj.getClass();

    for (Method m : clazz.getMethods()) {
        if (!m.getName().equals(method)) {
            continue;
        }
        Class[] argTypes = m.getParameterTypes();
        if (argTypes.length == args.length) {
            Object[] actualArgs = new Object[args.length];
            for (int i = args.length - 1; i >= 0; i--) {
                actualArgs[i] = ReflectUtils.coerceType(args[i], argTypes[i]);
            }
            try {
                return m.invoke(obj, actualArgs);
            } catch (Exception e) {
                throw new PaxmlRuntimeException(e);
            }
        }
    }
    throw new PaxmlRuntimeException("No method named '" + method + "' has " + args.length
            + " parameters from class: " + clazz.getName());
}

From source file:com.revolsys.record.io.format.xml.XmlProcessor.java

/**
 * Create the cache of process methods from the specified class.
 *
 * @param processorClass The XmlPorcessor class.
 * @return The map of method names to process methods.
 *//*w ww .  j  a  v a 2s. c o  m*/
private static Map<String, Method> getMethodCache(final Class<?> processorClass) {
    Map<String, Method> methodCache = PROCESSOR_METHOD_CACHE.get(processorClass);
    if (methodCache == null) {
        methodCache = new HashMap<>();
        PROCESSOR_METHOD_CACHE.put(processorClass, methodCache);
        final Method[] methods = processorClass.getMethods();
        for (final Method method : methods) {
            final String methodName = method.getName();
            if (methodName.startsWith("process")) {
                if (Arrays.equals(method.getParameterTypes(), PROCESS_METHOD_ARGS)) {
                    final String name = methodName.substring(7);
                    methodCache.put(name, method);
                }
            }
        }
    }
    return methodCache;
}