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:net.sf.excelutils.ExcelParser.java

/**
 * get properties of the JavaBean/*www .  j  a  v  a2s . c o m*/
 * 
 * @param clazz JavaBean
 * @return fields in the javabean
 */
public static Field[] getBeanProperties(Class clazz) {
    Field[] fields = clazz.getDeclaredFields();
    Method[] methods = clazz.getMethods();
    String m = "";

    for (int i = 0; i < methods.length; i++) {
        m += methods[i].getName() + ",";
    }

    List flist = new ArrayList();
    for (int i = 0; i < fields.length; i++) {
        if (m.indexOf("get" + fields[i].getName().substring(0, 1).toUpperCase()
                + fields[i].getName().substring(1, fields[i].getName().length())) >= 0) {
            flist.add(fields[i]);
        }
    }
    Field[] result = new Field[flist.size()];
    flist.toArray(result);
    return result;
}

From source file:com.kcs.core.utilities.Utility.java

public static Object clone(Object sourceObject, Object tarObject) {
    if (sourceObject != null) {
        Class theSourceClass = sourceObject.getClass();
        Class targetClass = tarObject.getClass();
        Method soruceMethods[] = theSourceClass.getMethods();
        for (int i = 0; i < soruceMethods.length; i++) {
            Method method = soruceMethods[i];
            try {
                if (method.getName().startsWith("get") && !"getClass".equalsIgnoreCase(method.getName())) {
                    if (method.getName().equalsIgnoreCase("getId")) {
                        Object idObj = method.invoke(sourceObject);
                        Class idClass = idObj.getClass();
                        Method idMethods[] = idClass.getMethods();
                        for (int j = 0; j < idMethods.length; j++) {
                            try {
                                Method idMethod = idMethods[j];
                                if (idMethod.getName().startsWith("get")
                                        && !"getClass".equals(idMethod.getName())) {
                                    String setterName = idMethod.getName().substring(3);
                                    setterName = "set" + setterName.substring(0, 1).toUpperCase()
                                            + setterName.substring(1);
                                    Method setter = targetClass.getMethod(setterName, idMethod.getReturnType());
                                    setter.invoke(tarObject, idMethod.invoke(idObj));
                                }/*from  w w  w.ja  v a  2s .c o  m*/
                            } catch (Exception e) {
                            }
                        }
                    } else {
                        String setterName = method.getName().substring(3);
                        setterName = "set" + setterName.substring(0, 1).toUpperCase() + setterName.substring(1);
                        Method setter = targetClass.getMethod(setterName, method.getReturnType());
                        setter.invoke(tarObject, method.invoke(sourceObject));
                    }
                }
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
        return tarObject;
    } else {
        return null;
    }
}

From source file:com.mm.yamingapp.core.MethodDescriptor.java

/** Collects all methods with {@code RPC} annotation from given class. */
public static Collection<MethodDescriptor> collectFrom(Class<? extends WrapperBase> clazz) {
    List<MethodDescriptor> descriptors = new ArrayList<MethodDescriptor>();

    for (Method method : clazz.getMethods()) {
        if (method.isAnnotationPresent(Rpc.class)) {
            descriptors.add(new MethodDescriptor(clazz, method));
        }//from w w w  .java 2  s . c om
    }
    return descriptors;
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static Method findSetter(Class<?> clazz, String propName) {
    String expectedName = "set" + propName.substring(0, 1).toUpperCase(Locale.ROOT) + propName.substring(1);
    for (Method method : clazz.getMethods()) {
        if (!method.getName().equals(expectedName)) {
            continue;
        }/*ww  w.  j  a v a2 s  . c  o m*/
        if (!method.getReturnType().equals(void.class)) {
            continue;
        }
        Type[] argTypes = method.getGenericParameterTypes();
        if (argTypes == null || argTypes.length != 1) {
            continue;
        }
        return method;
    }
    return null;
}

From source file:com.google.code.siren4j.util.ReflectionUtils.java

/**
 * Retrieve the setter for the specified class/field if it exists.
 *
 * @param clazz/* w w  w.  j  av  a 2  s . c o m*/
 * @param f
 * @return
 */
public static Method getSetter(Class<?> clazz, Field f) {
    Method setter = null;
    for (Method m : clazz.getMethods()) {
        if (ReflectionUtils.isSetter(m)
                && m.getName().equals(SETTER_PREFIX + StringUtils.capitalize(f.getName()))) {
            setter = m;
            break;
        }
    }
    return setter;
}

From source file:com.hubrick.vertx.rest.converter.JacksonJsonHttpMessageConverter.java

private static Method getMethodIfAvailable(Class<?> clazz, String methodName, Class<?>... paramTypes) {
    checkNotNull(clazz, "Class must not be null");
    checkNotNull(methodName, "Method name must not be null");
    if (paramTypes != null) {
        try {// w  ww . ja  va2 s  .  co m
            return clazz.getMethod(methodName, paramTypes);
        } catch (NoSuchMethodException ex) {
            return null;
        }
    } else {
        Set<Method> candidates = new HashSet<>(1);
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            if (methodName.equals(method.getName())) {
                candidates.add(method);
            }
        }
        if (candidates.size() == 1) {
            return candidates.iterator().next();
        }
        return null;
    }
}

From source file:com.googlecode.android_scripting.rpc.MethodDescriptor.java

/** Collects all methods with {@code RPC} annotation from given class. */
public static Collection<MethodDescriptor> collectFrom(Class<? extends RpcReceiver> clazz) {
    List<MethodDescriptor> descriptors = new ArrayList<MethodDescriptor>();
    for (Method method : clazz.getMethods()) {
        if (method.isAnnotationPresent(Rpc.class)) {
            descriptors.add(new MethodDescriptor(clazz, method));
        }/*from   w w  w.j a va2s .  com*/
    }
    return descriptors;
}

From source file:com.smart.utils.ReflectionUtils.java

/**
 * ?????,danfo/*w  ww . j a v a  2  s .  co m*/
 * 
 * @param bean
 * @param methodName
 * @param args
 * @return
 */
public static Method getMethodOfBeanByName(Object bean, String methodName, Object[] args) {

    if (bean == null || methodName == null) {
        return null;
    }
    if (args == null) {
        Object[] paras = {};
        args = paras;
    }
    Method method = null;
    Class beanClass = bean.getClass();
    Method[] methods = beanClass.getMethods();
    for (int i = methods.length - 1; i >= 0; i--) {
        Method methodTemp = (Method) methods[i];
        String methodNameTemp = methodTemp.getName();
        // ??,??
        if (methodName.equals(methodNameTemp)) {
            Class[] paras = methodTemp.getParameterTypes();
            // ??
            if (paras.length != args.length) {
                continue;
            }
            // ,???
            boolean isParaTheSame = true;
            for (int j = paras.length - 1; j >= 0; j--) {
                Class paraNeededClass = paras[j];
                Class paraGivenClass = args[j].getClass();
                if (!paraNeededClass.getName().equals(paraGivenClass.getName())) {
                    isParaTheSame = false;
                    break;
                }
            }
            if (isParaTheSame) {
                method = methodTemp;
            }
        }
    }
    return method;
    // end of methodNameargs
}

From source file:pt.webdetails.cpf.SimpleContentGenerator.java

/**
 * Get a map of all public methods with the Exposed annotation.
 * Map is not thread-safe and should be used read-only.
 * @param classe Class where to find methods
 * @param log classe's logger/*from w w  w  . j  a v a2  s.  c o m*/
 * @param lowerCase if keys should be in lower case.
 * @return map of all public methods with the Exposed annotation
 */
protected static Map<String, Method> getExposedMethods(Class<?> classe, boolean lowerCase) {
    HashMap<String, Method> exposedMethods = new HashMap<String, Method>();
    Log log = LogFactory.getLog(classe);
    for (Method method : classe.getMethods()) {
        if (method.getAnnotation(Exposed.class) != null) {
            String methodKey = method.getName().toLowerCase();
            if (exposedMethods.containsKey(methodKey)) {
                log.error("Method " + method + " differs from " + exposedMethods.get(methodKey)
                        + " only in case and will override calls to it!!");
            }
            log.debug("registering " + classe.getSimpleName() + "." + method.getName());
            exposedMethods.put(methodKey, method);
        }
    }
    return exposedMethods;
}

From source file:com.softmotions.commons.bean.BeanUtils.java

/**
 * Sets a property at the given bean.// w w w  . ja v  a  2 s.  c o m
 *
 * @param bean         The bean to set a property at.
 * @param propertyName The name of the property to set.
 * @param value        The value to set for the property.
 * @throws BeanException In case the bean access failed.
 */
public static void setProperty(Object bean, String propertyName, Object value) throws BeanException {
    Class valueClass = null;
    try {
        // getting property object from bean using "setNnnn", where nnnn is parameter name
        Method setterMethod = null;
        // first trying form getPropertyNaae for regular value
        String setterName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
        Class paramClass = bean.getClass();
        if (value != null) {
            valueClass = value.getClass();
            Class[] setterArgTypes = new Class[] { valueClass };
            setterMethod = paramClass.getMethod(setterName, setterArgTypes);
        } else {
            Method[] methods = paramClass.getMethods();
            for (int i = 0; i < methods.length; i++) {
                Method m = methods[i];
                if (m.getName().equals(setterName) && (m.getParameterTypes().length == 1)) {
                    setterMethod = m;
                    break;
                }
            }
        }
        if (setterMethod == null) {
            throw new NoSuchMethodException(setterName);
        }
        Object[] setterArgs = new Object[] { value };
        setterMethod.invoke(bean, setterArgs);
    } catch (NoSuchMethodError | NoSuchMethodException ex) {
        throw new BeanException("No setter method found for property '" + propertyName + "' and type "
                + valueClass + " at given bean from class " + bean.getClass().getName() + ".", ex);
    } catch (InvocationTargetException ex) {
        throw new BeanException("Property '" + propertyName + "' could not be set for given bean from class "
                + bean.getClass().getName() + ".", ex);
    } catch (IllegalAccessException ex) {
        throw new BeanException("Property '" + propertyName
                + "' could not be accessed for given bean from class " + bean.getClass().getName() + ".", ex);
    }
}