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

private static Method findMethod(final Class<?> beanType, final Type[] paramTypeHolder,
        final String methodName) {
    for (final Method m : beanType.getMethods()) {
        if (methodName.equals(m.getName()) && !Modifier.isStatic(m.getModifiers())) {
            final Type[] paramTypes = m.getGenericParameterTypes();
            if (paramTypes.length == 1) {
                paramTypeHolder[0] = paramTypes[0];
                return m;
            }//w ww.  j  a  va  2  s  .c o m
        }
    }
    return null;
}

From source file:com.github.wnameless.factorextractor.FactorExtractor.java

/**
 * Returns a Map{@literal <String, Object>} which is extracted from annotated
 * methods of input objects./*from  ww w  . j  a va 2s  .co m*/
 * 
 * @param objects
 *          an array of objects
 * @return a Map{@literal <String, Object>}
 */
public static Map<String, Object> extract(Object... objects) {
    for (Object o : objects) {
        if (o == null)
            throw new NullPointerException("Null object is not allowed");
    }

    Map<String, Object> factors = new HashMap<String, Object>();
    for (Object o : objects) {
        Class<?> testClass = o.getClass();
        for (Method m : testClass.getMethods()) {
            Factor factor = AnnotationUtils.findAnnotation(m, Factor.class);
            if (factor != null) {
                try {
                    factors.put(factor.value(), m.invoke(o));
                } catch (InvocationTargetException wrappedExc) {
                    Throwable exc = wrappedExc.getCause();
                    Logger.getLogger(FactorExtractor.class.getName()).log(Level.SEVERE, m + " failed: " + exc,
                            wrappedExc);
                } catch (Exception exc) {
                    Logger.getLogger(FactorExtractor.class.getName()).log(Level.SEVERE,
                            "INVALID @Factor: " + m + exc, exc);
                }
            }
        }
    }

    return factors;
}

From source file:com.codeabovelab.dm.common.utils.PojoBeanUtils.java

/**
 * Copy properties into lombok-style builders (it builder do not follow java bean convention)
 * @param src source bean object// w  ww .java 2s . c  om
 * @param dst destination builder object
 * @return dst object
 */
public static <T> T copyToBuilder(Object src, T dst) {
    PojoClass srcpojo = new PojoClass(src.getClass());
    Class<?> builderClass = dst.getClass();
    Method[] methods = builderClass.getMethods();
    for (Method method : methods) {
        boolean isBuilderSetter = method.getReturnType().equals(builderClass)
                && method.getParameterCount() == 1;
        if (!isBuilderSetter) {
            continue;
        }
        String propertyName = method.getName();
        Property property = srcpojo.getProperties().get(propertyName);
        if (property == null) {
            continue;
        }
        Object val = property.get(src);
        if (val == null) {
            continue;
        }
        try {
            method.invoke(dst, val);
        } catch (Exception e) {
            //nothing
        }
    }
    return dst;
}

From source file:net.mindengine.blogix.utils.BlogixUtils.java

private static Method findMethodInClass(Class<?> controllerClass, String methodName) {
    Method[] methods = controllerClass.getMethods();
    for (Method method : methods) {
        if (Modifier.isStatic(method.getModifiers()) && method.getName().equals(methodName)) {
            return method;
        }/*w  ww.  jav a  2s .  com*/
    }
    return null;
}

From source file:Main.java

private static <T> String toXml(T t, String head) {
    if (null == t) {
        return "";
    }/*from w ww. ja va 2  s .c  o m*/
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("<%s>", head)).append("\n");
    if (t instanceof Collection<?>) {
        Collection<?> collection = (Collection<?>) t;
        for (Object object : collection) {
            sb.append(toXml(object, "item"));
        }
    } else {
        Class<?> clazz = t.getClass();
        Method[] methods = clazz.getMethods();
        try {
            for (Method method : methods) {
                String methodName = method.getName();
                if (methodName.startsWith("get")) {
                    String key = methodName.substring(3);
                    Method setMethod = null;
                    try {
                        setMethod = clazz.getMethod("set" + key, method.getReturnType());
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                    if (null != setMethod) {
                        Object value = method.invoke(t);
                        if (method.getReturnType() == String.class) {
                            try {
                                String stringValue = (String) method.invoke(t);
                                if (null != stringValue) {
                                    try {
                                        Integer.parseInt(stringValue);
                                        sb.append(String.format("<%s>%s</%s>\n", key, stringValue, key));
                                    } catch (Exception e) {
                                        sb.append(String.format("<%s><![CDATA[%s]]></%s>\n", key, stringValue,
                                                key));

                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            sb.append(toXml(value, key));
                        }
                    }
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
    sb.append(String.format("</%s>\n", head));
    return sb.toString();
}

From source file:Main.java

/**
 * Attempt to find a {@link Method} on the supplied class with the supplied name
 * and parameter types. Searches all superclasses up to <code>Object</code>.
 * <p>Returns <code>null</code> if no {@link Method} can be found.
 *
 * @param clazz      the class to introspect
 * @param name       the name of the method
 * @param paramTypes the parameter types of the method
 *                   (may be <code>null</code> to indicate any signature)
 * @return the Method object, or <code>null</code> if none found
 *//*from www. j  ava 2  s .  c  om*/
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
    Class<?> searchType = clazz;
    while (searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (Method method : methods) {
            if (name.equals(method.getName())
                    && (paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
                return method;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:cn.hxh.springside.test.groups.GroupsTestRunner.java

/**
 * ???.// ww  w  .  j  a  v a 2s.  co  m
 * ??Groupstrue.
 */
public static boolean shouldRun(Class<?> testClass) {
    Method[] methods = testClass.getMethods();
    for (Method method : methods) {
        if (method.getAnnotation(Test.class) != null && method.getAnnotation(Ignore.class) == null
                && shouldRun(method)) {
            return true;
        }
    }

    return false;
}

From source file:com.mirth.connect.client.core.api.util.OperationUtil.java

private static void addOperationName(String permissionName, Class<?> servletInterface,
        Set<String> operationNames) {
    for (Method method : servletInterface.getMethods()) {
        MirthOperation operationAnnotation = method.getAnnotation(MirthOperation.class);
        if (operationAnnotation != null && operationAnnotation.permission().equals(permissionName)) {
            operationNames.add(operationAnnotation.name());
        }/*from   w  w w .j  a  v a2s .  c o  m*/
    }
}

From source file:Main.java

public static boolean checkIfClassHasMatchingGetMethod(Class<?> clazz, String columnname) {
    String neededMethodename = "get" + (Character.toUpperCase(columnname.charAt(0)) + columnname.substring(1));
    for (Method aMethod : clazz.getMethods()) {
        if (neededMethodename.equals(aMethod.getName())) {
            return true;
        }//from   ww w .ja va2 s . co  m
    }
    return false;
}

From source file:com.dukescript.presenters.androidapp.test.Knockout.java

public static Map<String, Method> assertMethods(Class<?> test) throws Exception {
    Map<String, Method> all = new HashMap<String, Method>();
    StringBuilder errors = new StringBuilder();
    int cnt = 0;//  w ww.  jav  a  2  s.  c om
    final Class<?>[] classes = testClasses();
    for (Class<?> c : classes) {
        for (Method method : c.getMethods()) {
            if (method.getAnnotation(KOTest.class) != null) {
                cnt++;
                String name = method.getName();
                if (!name.startsWith("test")) {
                    name = "test" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
                }
                try {
                    Method m = test.getMethod(name);
                    all.put(name, method);
                } catch (NoSuchMethodException ex) {
                    errors.append("\n").append(name);
                }
            }
        }
    }
    if (errors.length() > 0) {
        errors.append("\nTesting classes: ").append(Arrays.toString(classes));
        fail("Missing method: " + errors);
    }
    assert cnt > 0 : "Some methods found";
    return all;
}