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:gov.nih.nci.cabig.caaers.AbstractTestCase.java

public <T extends CaaersDao<?>> T registerDaoMockFor(Class<T> forClass) {
    List<Method> methods = new LinkedList<Method>(Arrays.asList(forClass.getMethods()));
    for (Iterator<Method> iterator = methods.iterator(); iterator.hasNext();) {
        Method method = iterator.next();
        if ("domainClass".equals(method.getName())) {
            iterator.remove();//from ww w. j a v  a 2s .c o m
        }
    }
    return registerMockFor(forClass, methods.toArray(new Method[methods.size()]));
}

From source file:easyrpc.server.serialization.jsonrpc.JSONCallee.java

@Override
public byte[] matchMethod(Object object, byte[] callInfo) {
    try {// ww  w.j ava2  s  .co  m
        Object returnedObject = null;
        ObjectNode call = (ObjectNode) MAPPER.readTree(callInfo);

        String jsonrpc = call.get("jsonrpc").textValue();
        if (jsonrpc == null || !"2.0".equals(jsonrpc)) {
            throw new SerializationException(
                    "'jsonrpc' value must be '2.0' and actually is: '" + jsonrpc + "'");
        }

        String methodName = call.get("method").textValue();
        if (methodName == null)
            throw new SerializationException("The 'method' field must not be null: " + call.toString());

        Class iface = object.getClass();
        for (Method m : iface.getMethods()) {
            if (methodName.equals(m.getName())) {
                ArrayNode jsParams = (ArrayNode) call.get("params");
                if (jsParams == null || jsParams.size() == 0) {
                    try {
                        returnedObject = m.invoke(object);
                        //                            System.out.println("returnedObject = " + returnedObject);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return returnJsonRpcError(call.get("id"), e);
                    }
                } else {
                    //                        System.out.println("methodName = " + methodName);
                    Object[] params = new Object[jsParams.size()];
                    for (int i = 0; i < params.length; i++) {
                        params[i] = MAPPER.convertValue(jsParams.get(i), m.getParameters()[i].getType());
                        //                            System.out.println("params[i] = " + params[i] + "("+ params[i].getClass().getName() +")");
                    }
                    try {
                        returnedObject = m.invoke(object, params);
                        //                            System.out.println("returnedObject = " + returnedObject);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return returnJsonRpcError(call.get("id"), e);
                    }
                }
                break;
            }
        }
        ObjectNode jsret = JsonNodeFactory.instance.objectNode();
        jsret.put("jsonrpc", "2.0");
        jsret.put("id", call.get("id").toString());
        if (returnedObject != null) {
            addResult(jsret, returnedObject);
        }

        //            System.out.println("jsret.toString() = " + jsret.toString());
        return jsret.toString().getBytes();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.atteo.moonshine.websocket.jsonmessages.HandlerDispatcher.java

public <T> void addHandler(Class<T> klass, Provider<? extends T> provider) {
    for (Method method : klass.getMethods()) {
        if (method.isAnnotationPresent(OnMessage.class)) {
            registerOnMessageMethod(method, provider);
        }//from  w w w. j av a2s . c o m
    }
}

From source file:com.googlecode.jsonschema2pojo.integration.ref.AbsoluteRefIT.java

@Test
public void absoluteRefIsReadSuccessfully() throws ClassNotFoundException, NoSuchMethodException, IOException {

    File schemaWithAbsoluteRef = createSchemaWithAbsoluteRef();

    File generatedOutputDirectory = generate(schemaWithAbsoluteRef.toURI().toURL(), "com.example");
    Class<?> absoluteRefClass = compile(generatedOutputDirectory).loadClass("com.example.AbsoluteRef");

    Class<?> addressClass = absoluteRefClass.getMethod("getAddress").getReturnType();

    assertThat(addressClass.getName(), is("com.example.Address"));
    assertThat(addressClass.getMethods(), hasItemInArray(hasProperty("name", equalTo("getPostal_code"))));

}

From source file:de.micromata.genome.util.matcher.cls.ContainsMethod.java

/**
 * Match this class.//w w w .  j av a  2 s.c o  m
 *
 * @param cls the cls
 * @return true, if successful
 */
public boolean matchThisClass(Class<?> cls) {
    for (Method m : cls.getMethods()) {
        int mods = m.getModifiers();
        if (staticMethod == true && (mods & Modifier.STATIC) != Modifier.STATIC) {
            continue;
        }
        if (staticMethod == false && (mods & Modifier.STATIC) == Modifier.STATIC) {
            continue;
        }
        if (publicMethod == true && (mods & Modifier.PUBLIC) != Modifier.PUBLIC) {
            continue;
        }

        if (name != null) {
            if (StringUtils.equals(m.getName(), name) == false) {
                continue;
            }
        }
        if (returnType != null) {
            if (m.getReturnType() != returnType) {
                continue;
            }
        }
        if (params != null) {
            Class<?>[] pt = m.getParameterTypes();
            if (pt.length != params.length) {
                continue;
            }
            for (int i = 0; i < pt.length; ++i) {
                if (pt[i] != params[i]) {
                    continue;
                }
            }
        }
        return true;
    }
    return false;
}

From source file:org.kantega.dogmaticmvc.web.DogmaticMVCHandler.java

private Method findRequestHandlerMethod(Class clazz) {
    for (Method method : clazz.getMethods()) {
        for (Annotation annotation : method.getAnnotations()) {
            if (annotation instanceof RequestHandler) {
                return method;
            }//from  w  ww. j a v a  2s . c o  m
        }
    }
    return null;
}

From source file:org.jsonschema2pojo.integration.ref.FragmentRefIT.java

@Test
public void refToFragmentOfSelfIsReadSuccessfully() throws NoSuchMethodException {

    Class<?> aClass = fragmentRefsClass.getMethod("getFragmentOfSelf").getReturnType();

    assertThat(aClass.getName(), is("com.example.A"));
    assertThat(aClass.getMethods(), hasItemInArray(hasProperty("name", equalTo("getPropertyOfA"))));
}

From source file:com.anrisoftware.globalpom.reflection.annotations.AnnotationDiscoveryImpl.java

private Set<AnnotationBean> findMethods(Set<AnnotationBean> result, Object bean) {
    Class<? extends Object> type = bean.getClass();
    return findAnnotations(result, bean, asList(type.getMethods()));
}

From source file:de.openknowledge.extensions.jsf.model.ModelMethod.java

private Method findMethod(Class<?> type, String name, Class<?>[] parameters) {
    for (Method method : type.getMethods()) {
        if (method.getName().equals(name)) {
            Class<?>[] parameterTypes = method.getParameterTypes();
            if (parameterTypes.length != parameters.length) {
                continue;
            }//from w  w  w  .j av a  2s  .  c  o m
            boolean match = true;
            for (int i = 0; i < parameters.length; i++) {
                if (parameters[i] != null && !parameterTypes[i].isAssignableFrom(parameters[i])) {
                    match = false;
                    break;
                }
            }
            if (match) {
                // TODO currently we simply take the first match... We should resolve based on java resolution rules...
                return method;
            }
        }
    }
    throw new ELException("method with name '" + name + "' not found in type " + type.getName());
}

From source file:cn.edu.zju.bigdata.controller.JerseyController.java

@GET
@Path("/_list")
@Produces({ "application/json" })
//@ReturnType("java.lang.List<String>")
public Response listAPI() {

    List<String> apiList = new ArrayList<String>();

    Reflections reflections = new Reflections("cn.edu.zju.bigdata.controller");

    Set<Class<?>> allClasses = reflections.getTypesAnnotatedWith(Path.class);

    for (Class<?> controller : allClasses) {
        String apiPath = controller.getAnnotation(Path.class).value();
        for (Method method : controller.getMethods()) {
            // Filter the API with the @RequestMapping Annotation
            String apiMethod = method.isAnnotationPresent(GET.class) ? "GET"
                    : method.isAnnotationPresent(POST.class) ? "POST"
                            : method.isAnnotationPresent(PUT.class) ? "PUT"
                                    : method.isAnnotationPresent(DELETE.class) ? "DELETE"
                                            : method.isAnnotationPresent(HEAD.class) ? "HEAD" : null;
            if (apiMethod == null)
                continue;

            if (method.isAnnotationPresent(Path.class))
                apiPath += method.getAnnotation(Path.class).value();

            // Filter out /_list itself
            if (apiPath.equals("/_list"))
                continue;

            apiList.add(apiMethod + " " + apiPath);
        }//from w  w w  .  ja  v  a2s .  com
    }

    return Response.ok().entity(apiList).build();
}