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:ei.ne.ke.cassandra.cql3.EntitySpecificationUtils.java

/**
 *
 *
 * @param entityClazz/*from  w w  w .java2s. c om*/
 * @param propertyName
 * @return
 */
public static <T> Method findGetterMethod(Class<T> entityClazz, String propertyName) {
    for (Method methodCandidate : entityClazz.getDeclaredMethods()) {
        String methodCandidateName = methodCandidate.getName();
        if ((methodCandidateName.startsWith("get") || methodCandidateName.startsWith("is"))
                && methodCandidateName.endsWith(propertyName)) {
            return methodCandidate;
        }
    }
    return null;
}

From source file:com.mine.core.util.ReflectUtils.java

/**
 * ?, ?DeclaredMethod,?. ?Object?, null. ????
 * <p>/*  ww  w. java 2  s .c o  m*/
 * ?. ?Method,?Method.invoke(Object obj, Object...
 * args)
 */
public static Method getAccessibleMethodByName(final Object obj, final String methodName) {
    Validate.notNull(obj);
    Validate.notBlank(methodName);

    for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType
            .getSuperclass()) {
        Method[] methods = searchType.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                makeAccessible(method);
                return method;
            }
        }
    }
    return null;
}

From source file:com.alibaba.rocketmq.common.MixAll.java

/**
 * PropertiesObject/*from   w  w w  .j  av a2 s .  c om*/
 */
public static void properties2Object(final Properties p, final Object object) {
    Method[] methods = object.getClass().getMethods();
    for (Method method : methods) {
        String mn = method.getName();
        if (mn.startsWith("set")) {
            try {
                String tmp = mn.substring(4);
                String first = mn.substring(3, 4);

                String key = first.toLowerCase() + tmp;
                String property = p.getProperty(key);
                if (property != null) {
                    Class<?>[] pt = method.getParameterTypes();
                    if (pt != null && pt.length > 0) {
                        String cn = pt[0].getSimpleName();
                        Object arg = null;
                        if (cn.equals("int")) {
                            arg = Integer.parseInt(property);
                        } else if (cn.equals("long")) {
                            arg = Long.parseLong(property);
                        } else if (cn.equals("double")) {
                            arg = Double.parseDouble(property);
                        } else if (cn.equals("boolean")) {
                            arg = Boolean.parseBoolean(property);
                        } else if (cn.equals("String")) {
                            arg = property;
                        } else {
                            continue;
                        }
                        method.invoke(object, new Object[] { arg });
                    }
                }
            } catch (Throwable e) {
            }
        }
    }
}

From source file:podd.util.PoddWebappTestUtil.java

@SuppressWarnings({ "unchecked" })
public static <T> void checkGetterMethods(T obj1, T obj2, Set<String> skipList) {
    try {/*from w w  w.  j  a v a 2s . c om*/
        final Method[] methods = obj1.getClass().getMethods();
        for (Method m : methods) {
            if (m.getName().startsWith("get") && !skipList.contains(m.getName())) {
                final Object value1 = m.invoke(obj1);
                final Object value2 = m.invoke(obj2);
                if (value1 instanceof Collection && value2 instanceof Collection) {
                    checkMembers((Collection) value1, ((Collection) value2).toArray());
                } else if (!(value1 instanceof Collection || value2 instanceof Collection)) {
                    assertEquals("Method: " + m.getName(), value1, value2);
                } else {
                    throw new IllegalArgumentException("One is collection while the other is not.");
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

private static Method findMethod(Class<?> clazz, String methodName) {
    Method[] methods = clazz.getMethods();
    Method result = null;/*from  w  ww .  j a v  a  2  s  . c o  m*/
    for (Method method : methods) {
        if (method.getName().equals(methodName)) {
            result = method;
        }
    }
    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));
                                }/* w ww  .  j  a  v a2  s  .co  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.github.dozermapper.core.util.ReflectionUtils.java

private static Method findPreferablyNonSyntheticMethod(String methodName, Class<?> clazz) {
    Method[] methods = clazz.getMethods();
    Method syntheticMethod = null;
    for (Method method : methods) {
        if (method.getName().equals(methodName)) {
            if (!method.isBridge() && !method.isSynthetic()) {
                return method;
            } else {
                syntheticMethod = method;
            }/*  w ww  .  ja va 2  s. c  o m*/
        }
    }
    return syntheticMethod;
}

From source file:com.predic8.membrane.annot.bean.MCUtil.java

private static void addXML(Object object, String id, XMLStreamWriter xew, SerializationContext sc)
        throws XMLStreamException {
    if (object == null)
        throw new InvalidParameterException("'object' must not be null.");

    Class<? extends Object> clazz = object.getClass();

    MCElement e = clazz.getAnnotation(MCElement.class);
    if (e == null)
        throw new IllegalArgumentException("'object' must be @MCElement-annotated.");

    BeanWrapperImpl src = new BeanWrapperImpl(object);

    xew.writeStartElement(e.name());//from  w  ww .  ja va 2s  . co  m

    if (id != null)
        xew.writeAttribute("id", id);

    HashSet<String> attributes = new HashSet<String>();
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        MCAttribute a = m.getAnnotation(MCAttribute.class);
        if (a != null) {
            Object value = src.getPropertyValue(propertyName);
            String str;
            if (value == null)
                continue;
            else if (value instanceof String)
                str = (String) value;
            else if (value instanceof Boolean)
                str = ((Boolean) value).toString();
            else if (value instanceof Integer)
                str = ((Integer) value).toString();
            else if (value instanceof Long)
                str = ((Long) value).toString();
            else if (value instanceof Enum<?>)
                str = value.toString();
            else {
                MCElement el = value.getClass().getAnnotation(MCElement.class);
                if (el != null) {
                    str = defineBean(sc, value, null, true);
                } else {
                    str = "?";
                    sc.incomplete = true;
                }
            }

            if (a.attributeName().length() > 0)
                propertyName = a.attributeName();

            attributes.add(propertyName);
            xew.writeAttribute(propertyName, str);
        }
    }
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        MCOtherAttributes o = m.getAnnotation(MCOtherAttributes.class);
        if (o != null) {
            Object value = src.getPropertyValue(propertyName);
            if (value instanceof Map<?, ?>) {
                Map<?, ?> map = (Map<?, ?>) value;
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    Object key = entry.getKey();
                    Object val = entry.getValue();
                    if (!(key instanceof String) || !(val instanceof String)) {
                        sc.incomplete = true;
                        key = "incompleteAttributes";
                        val = "?";
                    }
                    if (attributes.contains(key))
                        continue;
                    attributes.add((String) key);
                    xew.writeAttribute((String) key, (String) val);
                }
            } else {
                xew.writeAttribute("incompleteAttributes", "?");
                sc.incomplete = true;
            }
        }
    }

    List<Method> childElements = new ArrayList<Method>();
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));

        MCChildElement c = m.getAnnotation(MCChildElement.class);
        if (c != null) {
            childElements.add(m);
        }
        MCTextContent t = m.getAnnotation(MCTextContent.class);
        if (t != null) {
            Object value = src.getPropertyValue(propertyName);
            if (value == null) {
                continue;
            } else if (value instanceof String) {
                xew.writeCharacters((String) value);
            } else {
                xew.writeCharacters("?");
                sc.incomplete = true;
            }
        }
    }

    Collections.sort(childElements, new Comparator<Method>() {

        @Override
        public int compare(Method o1, Method o2) {
            MCChildElement c1 = o1.getAnnotation(MCChildElement.class);
            MCChildElement c2 = o2.getAnnotation(MCChildElement.class);
            return c1.order() - c2.order();
        }
    });

    for (Method m : childElements) {
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));

        Object value = src.getPropertyValue(propertyName);
        if (value != null) {
            if (value instanceof Collection<?>) {
                for (Object item : (Collection<?>) value)
                    addXML(item, null, xew, sc);
            } else {
                addXML(value, null, xew, sc);
            }
        }
    }

    xew.writeEndElement();
}

From source file:com.amalto.core.metadata.ClassRepository.java

private static boolean isClassMethod(Class clazz, Method declaredMethod) {
    Class superClass = clazz.getSuperclass();
    if (!Object.class.equals(superClass)) {
        try {//from ww  w .  j ava2 s. c  om
            return superClass.getMethod(declaredMethod.getName(), declaredMethod.getParameterTypes()) != null;
        } catch (NoSuchMethodException e) {
            return true;
        }
    }
    return true;
}

From source file:com.ocs.dynamo.utils.ClassUtils.java

/**
 * Check if the object has a (public) method that has the specified name
 * /*from w w w. ja  va  2 s  .com*/
 * @param obj
 * @param methodName
 * @return
 */
public static boolean hasMethod(Object obj, String methodName) {
    Method[] methods = obj.getClass().getMethods();
    for (Method method : methods) {
        if (method.getName().equals(methodName) && (Modifier.isPublic(method.getModifiers()))) {
            return true;
        }
    }
    return false;
}