Example usage for org.springframework.util ReflectionUtils findMethod

List of usage examples for org.springframework.util ReflectionUtils findMethod

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils findMethod.

Prototype

@Nullable
public static Method findMethod(Class<?> clazz, String name) 

Source Link

Document

Attempt to find a Method on the supplied class with the supplied name and no parameters.

Usage

From source file:org.springframework.web.socket.server.standard.ServerEndpointExporter.java

protected ServerContainer getServerContainer() {
    Class<?> servletContextClass;
    try {/*from ww w.  j av  a  2s.  c  o  m*/
        servletContextClass = ClassUtils.forName("javax.servlet.ServletContext", getClass().getClassLoader());
    } catch (Throwable ex) {
        return null;
    }

    try {
        Method getter = ReflectionUtils.findMethod(this.applicationContext.getClass(), "getServletContext");
        Object servletContext = getter.invoke(this.applicationContext);
        Method attrMethod = ReflectionUtils.findMethod(servletContextClass, "getAttribute", String.class);
        return (ServerContainer) attrMethod.invoke(servletContext, "javax.websocket.server.ServerContainer");
    } catch (Exception ex) {
        throw new IllegalStateException(
                "Failed to get javax.websocket.server.ServerContainer via ServletContext attribute", ex);
    }
}

From source file:org.springframework.yarn.support.compat.NMTokenCacheCompat.java

/**
 * Gets the Hadoop {@code NMTokenCache}. Access token cache via singleton
 * method if exists, otherwise assumes that class is hadoop vanilla
 * implementation with static methods where we can just use default
 * constructor and further access for those static method would
 * got via instance itself.//from  w w  w  . j  a va 2s.co  m
 * <p>
 * This is due to a fact that i.e. cloudera modified this class by
 * removing static methods and made access to it via singleton pattern.
 *
 * @return the token cache
 */
public static NMTokenCache getNMTokenCache() {
    if (nmTokenCache == null) {
        synchronized (lock) {
            if (nmTokenCache == null) {
                Method singletonMethod = ReflectionUtils.findMethod(NMTokenCache.class, "getSingleton");
                if (singletonMethod != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Creating NMTokenCache using NMTokenCache.getSingleton()");
                    }
                    nmTokenCache = (NMTokenCache) ReflectionUtils.invokeMethod(singletonMethod, null);
                } else {
                    log.debug(
                            "Creating NMTokenCache using constructor, further access via instance static methods.");
                    nmTokenCache = new NMTokenCache();
                }
            }
        }
    }
    return nmTokenCache;
}

From source file:org.tdar.core.service.ReflectionService.java

/**
 * Based on the field and the object passed in, call the getter and return the result
 * // w  w w.ja  va  2s .c o m
 * @param obj
 * @param field
 * @return
 */
@SuppressWarnings("unchecked")
public <T> T callFieldGetter(Object obj, Field field) {
    // logger.debug("calling getter on: {} {} ", obj, field.getName());
    logger.trace("{}", field.getDeclaringClass());
    Method method = ReflectionUtils.findMethod(field.getDeclaringClass(), generateGetterName(field));
    if (method.getReturnType() != Void.TYPE) {
        try {
            return (T) method.invoke(obj);
        } catch (Exception e) {
            logger.debug("cannot call field getter for field: {}", field, e);
        }
    }
    return null;
}