Example usage for java.lang.reflect Method getName

List of usage examples for java.lang.reflect Method getName

Introduction

In this page you can find the example usage for java.lang.reflect Method getName.

Prototype

@Override
public String getName() 

Source Link

Document

Returns the name of the method represented by this Method object, as a String .

Usage

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 . j  a  v a  2s  .c  o  m
    }
    return null;
}

From source file:io.servicecomb.swagger.generator.core.utils.ParamUtils.java

public static String generateBodyParameterName(Method method) {
    return method.getName() + "Body";
}

From source file:it.unibas.spicy.persistence.object.ClassUtility.java

public static void invokeSetMethod(String methodName, Object object, Object arg) {
    try {/*ww  w. j  a  v a2 s  .  co  m*/
        Method[] methods = object.getClass().getMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                method.invoke(object, arg);
                return;
            }
        }
    } catch (Exception ex) {
        logger.error(ex);
    }
}

From source file:asia.gkc.vneedu.utils.FilterUtil.java

/**
 * //w w w.j a va2s .  c  o  m
 * @param list -  
 * @param object - ?
 * @param <T> - 
 * @return ?
 */
public static <T> T exclude(List<String> list, T object) {
    Class<?> c = object.getClass();

    for (String fieldName : list) {
        Method[] methods = c.getDeclaredMethods();

        for (Method method : methods) {
            if (method.getName().equals(getMethodName(fieldName, "set"))) {
                Class<?> param_class = method.getParameterTypes()[0];
                try {
                    logger.info("Invoking: " + method.getName());
                    method.invoke(object, param_class.cast(null));
                    logger.info("Invoked: " + method.getName());
                } catch (IllegalAccessException e) {
                    logger.warn("IllegalAccessException");
                } catch (InvocationTargetException e) {
                    logger.warn("InvocationTargetException");
                }
            }
        }
    }

    return object;
}

From source file:Main.java

private static <T> String toXml(T t, String head) {
    if (null == t) {
        return "";
    }// w w  w  .j a  va2s  . c om
    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:org.jdal.annotation.AnnotatedElementAccessor.java

public static Object getValue(AnnotatedElement element, Object target) {
    if (element instanceof Field) {
        ReflectionUtils.makeAccessible((Field) element);
        return ReflectionUtils.getField((Field) element, target);
    } else if (element instanceof Method) {
        Method method = (Method) element;
        String name = method.getName();

        if (name.startsWith("set")) {
            return PropertyAccessorFactory.forBeanPropertyAccess(target)
                    .getPropertyValue(getPropertyName(name));
        }//from   w w w . j a  va 2 s. c o  m
    }

    return null;
}

From source file:com.zauberlabs.commons.mom.NaiveProperties.java

/** function that answers method name */
@Constant//from   w  ww.ja  v a 2s.  c  o  m
public static AbstractFunction<Method, String> methodName() {
    return new AbstractFunction<Method, String>() {
        public String apply(final Method arg0) {
            return arg0.getName();
        }
    };
}

From source file:Util.java

private static void fillSetterMethods(Class<?> pojoClass, Map<String, Method> baseMap) {
    if (pojoClass.getSuperclass() != Object.class)
        fillSetterMethods(pojoClass.getSuperclass(), baseMap);

    Method[] methods = pojoClass.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        if (!Modifier.isStatic(m.getModifiers()) && m.getParameterTypes().length == 1
                && m.getName().startsWith(SET) && Modifier.isPublic(m.getModifiers())) {
            baseMap.put(toProperty(SET.length(), m.getName()), m);
        }/*ww  w.  j a v a2  s.  co  m*/
    }
}

From source file:com.zhangyue.zeus.util.BeanUtils.java

/**
 * map??BeanBean??mapkey??mapkey?OMIT_REG?
 * //  w  w  w  .  ja va2 s  .  com
 * @param <E>
 * @param cla
 * @param map
 * @return
 */
@SuppressWarnings({ "rawtypes" })
public static <E> E toBean(Class<E> cla, Map<String, Object> map) {

    // 
    E obj = null;
    try {
        obj = cla.newInstance();
        if (obj == null) {
            throw new Exception();
        }
    } catch (Exception e) {
        LOG.error(",:" + cla);
        return null;
    }

    // ?mapkey
    Map<String, Object> newmap = new HashMap<String, Object>();
    for (Map.Entry<String, Object> en : map.entrySet()) {
        newmap.put("set" + en.getKey().trim().replaceAll(OMIT_REG, "").toLowerCase(), en.getValue());
    }
    // 
    Method[] ms = cla.getMethods();
    for (Method method : ms) {
        String mname = method.getName().toLowerCase();
        if (mname.startsWith("set")) {

            Class[] clas = method.getParameterTypes();
            Object v = newmap.get(mname);

            if (v != null && clas.length == 1) {
                try {
                    method.invoke(obj, v);
                } catch (Exception e) {
                    LOG.error("," + cla + "." + method.getName()
                            + ".?" + clas[0] + ";:" + v.getClass());
                }
            }
        }
    }

    return obj;
}

From source file:Utils.java

public static boolean isJavaBeanCompliantSetter(Method method) {
    if (method == null)
        return false;

    if (method.getReturnType() != Void.TYPE)
        return false;

    if (!method.getName().startsWith("set"))
        return false;

    if (method.getParameterTypes().length != 1)
        return false;

    return true;/*from ww  w  . j a v  a2 s.  c o  m*/
}