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:com.yahoo.elide.core.EntityBinding.java

/**
 * Returns name of field whether public member or method.
 *
 * @param fieldOrMethod field or method//from  w  w w  . j av  a  2  s.c om
 * @return field or method name
 */
private static String getFieldName(AccessibleObject fieldOrMethod) {
    if (fieldOrMethod instanceof Field) {
        return ((Field) fieldOrMethod).getName();
    } else {
        Method method = (Method) fieldOrMethod;
        String name = method.getName();

        if (name.startsWith("get") && method.getParameterCount() == 0) {
            name = WordUtils.uncapitalize(name.substring("get".length()));
        } else if (name.startsWith("is") && method.getParameterCount() == 0) {
            name = WordUtils.uncapitalize(name.substring("is".length()));
        } else {
            return null;
        }
        return name;
    }
}

From source file:com.asual.summer.core.util.ClassUtils.java

public static Object invokeMethod(Object target, final String methodName, final Object[] parameters) {
    if (target != null) {
        final List<Method> matches = new ArrayList<Method>();
        ReflectionUtils.doWithMethods(target.getClass(), new ReflectionUtils.MethodCallback() {
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                matches.add(method);//from w  w w  . ja  v  a2 s .  co m
            }
        }, new ReflectionUtils.MethodFilter() {
            public boolean matches(Method method) {
                if (method.getName().equals(methodName)) {
                    Class<?>[] types = method.getParameterTypes();
                    if (parameters == null && types.length == 0) {
                        return true;
                    }
                    if (types.length != parameters.length) {
                        return false;
                    }
                    for (int i = 0; i < types.length; i++) {
                        if (!types[i].isInstance(parameters[i])) {
                            return false;
                        }
                    }
                    return true;
                }
                return false;
            }
        });
        if (matches.size() > 0) {
            if (parameters == null) {
                return ReflectionUtils.invokeMethod(matches.get(0), target);
            } else {
                return ReflectionUtils.invokeMethod(matches.get(0), target, parameters);
            }
        }
    }
    return null;
}

From source file:com.sun.socialsite.business.impl.JPAListenerManagerImpl.java

/**
 * Calls any appropriately-annotated methods in listeners which
 * have registered to receive lifecycle events for a class to
 * which the specified entity belongs./*from   ww  w .j  av a 2s  .  co m*/
 * @param entity the entity experiencing a lifecycle event.
 * @param annotationClass the annotation class corresponding to the event.
 */
private static void notifyListeners(Object entity, Class<? extends Annotation> annotationClass) {

    //log.trace(String.format("notifyListeners(%s, %s)", entity, annotationClass.getCanonicalName()));

    Collection<Object> listeners = getListeners(entity);
    for (Object listener : listeners) {
        Method[] methods = listener.getClass().getMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method.getAnnotation(annotationClass) != null) {
                try {
                    log.debug(String.format("calling %s.%s(%s)", listener, method.getName(), entity));
                    // Handle cases where the listener is a nested class
                    if (Modifier.isPublic(method.getModifiers()))
                        method.setAccessible(true);
                    method.invoke(listener, entity);
                } catch (Throwable t) {
                    // TODO: Catch and Handle individual Exception types
                    log.error("Exception", t);
                }
            }
        }
    }
}

From source file:com.wavemaker.tools.javaservice.JavaServiceDefinition.java

/**
 * Get a list of methods, excluding overloaded ones, biased towards the method with the least number of arguments
 * (in other words, if a method is overloaded, the instance with the fewest arguments is included in the return
 * value)./*  w  w w.  j ava  2 s  . c  o  m*/
 * 
 * @param allMethods
 * @return
 */
public static Collection<Method> filterOverloadedMethods(List<Method> allMethods) {

    Map<String, Method> methodsMap = new HashMap<String, Method>();
    for (Method method : allMethods) {
        if (methodsMap.containsKey(method.getName())) {
            if (methodsMap.get(method.getName()).getParameterTypes().length > method
                    .getParameterTypes().length) {
                methodsMap.put(method.getName(), method);
            }
        } else {
            methodsMap.put(method.getName(), method);
        }
    }

    return methodsMap.values();
}

From source file:Main.java

/**
 * Converts a given object to a form encoded map
 * @param objName Name of the object//  w ww .jav a2s  .co m
 * @param obj The object to convert into a map
 * @param objectMap The object map to populate
 * @param processed List of objects hashCodes that are already parsed
 * @throws InvalidObjectException
 */
private static void objectToMap(String objName, Object obj, Map<String, Object> objectMap,
        HashSet<Integer> processed) throws InvalidObjectException {
    //null values need not to be processed
    if (obj == null)
        return;

    //wrapper types are autoboxed, so reference checking is not needed
    if (!isWrapperType(obj.getClass())) {
        //avoid infinite recursion
        if (processed.contains(obj.hashCode()))
            return;
        processed.add(obj.hashCode());
    }

    //process arrays
    if (obj instanceof Collection<?>) {
        //process array
        if ((objName == null) || (objName.isEmpty()))
            throw new InvalidObjectException("Object name cannot be empty");

        Collection<?> array = (Collection<?>) obj;
        //append all elements in the array into a string
        int index = 0;
        for (Object element : array) {
            //load key value pair
            String key = String.format("%s[%d]", objName, index++);
            loadKeyValuePairForEncoding(key, element, objectMap, processed);
        }
    } else if (obj instanceof Map) {
        //process map
        Map<?, ?> map = (Map<?, ?>) obj;
        //append all elements in the array into a string            
        for (Map.Entry<?, ?> pair : map.entrySet()) {
            String attribName = pair.getKey().toString();
            String key = attribName;
            if ((objName != null) && (!objName.isEmpty())) {
                key = String.format("%s[%s]", objName, attribName);
            }
            loadKeyValuePairForEncoding(key, pair.getValue(), objectMap, processed);
        }
    } else {
        //process objects
        // invoke getter methods
        Method[] methods = obj.getClass().getMethods();
        for (Method method : methods) {
            //is a getter?
            if ((method.getParameterTypes().length != 0) || (!method.getName().startsWith("get")))
                continue;

            //get json attribute name
            Annotation getterAnnotation = method.getAnnotation(JsonGetter.class);
            if (getterAnnotation == null)
                continue;

            //load key name
            String attribName = ((JsonGetter) getterAnnotation).value();
            String key = attribName;
            if ((objName != null) && (!objName.isEmpty())) {
                key = String.format("%s[%s]", objName, attribName);
            }

            try {
                //load key value pair
                Object value = method.invoke(obj);
                loadKeyValuePairForEncoding(key, value, objectMap, processed);
            } catch (Exception ex) {
            }
        }
        // load fields
        Field[] fields = obj.getClass().getFields();
        for (Field field : fields) {
            //load key name
            String attribName = field.getName();
            String key = attribName;
            if ((objName != null) && (!objName.isEmpty())) {
                key = String.format("%s[%s]", objName, attribName);
            }

            try {
                //load key value pair
                Object value = field.get(obj);
                loadKeyValuePairForEncoding(key, value, objectMap, processed);
            } catch (Exception ex) {
            }
        }
    }
}

From source file:lite.flow.util.ActivityInspector.java

static public EntryPoint[] getEntryPoints(Class<?> clazz) {

    EntryPoint[] entryPoints = getEntryAnnotatedMethods(clazz);
    if (entryPoints != null && entryPoints.length > 0)
        return entryPoints;

    ArrayList<Method> entryMethods = getEntryMethods(clazz);
    entryPoints = new EntryPoint[entryMethods.size()];

    int i = 0;/* ww w.  j av  a2 s.  c o m*/
    for (Method entryMethod : entryMethods) {
        String[] inputNames = getArgumentNames(entryMethod);
        EntryPoint entryPoint = new EntryPoint(entryMethod, inputNames, entryMethod.getName());
        entryPoints[i++] = entryPoint;
    }
    return entryPoints;
}

From source file:cn.webwheel.ActionSetter.java

public static String isGetter(Method method) {
    String name = method.getName();
    if (!name.startsWith("get"))
        return null;
    if (name.length() < 4)
        return null;
    if (method.getParameterTypes().length != 0)
        return null;
    if (method.getReturnType() == Void.class)
        return null;
    name = name.substring(3);/*from   ww  w.j  av  a 2 s. c  o m*/
    if (name.length() > 1 && name.equals(name.toUpperCase()))
        return name;
    return name.substring(0, 1).toLowerCase() + name.substring(1);
}

From source file:com.espertech.esper.event.vaevent.PropertyUtility.java

public static PropertyAccessException getInvocationTargetException(Method method, InvocationTargetException e) {
    Class declaring = method.getDeclaringClass();
    String message = "Failed to invoke method " + method.getName() + " on class "
            + JavaClassHelper.getClassNameFullyQualPretty(declaring) + ": "
            + e.getTargetException().getMessage();
    throw new PropertyAccessException(message, e);
}

From source file:org.fusesource.meshkeeper.util.internal.IntrospectionSupport.java

public static boolean getProperties(Object target, Map<String, Object> props, String optionPrefix) {

    boolean rc = false;
    if (target == null) {
        throw new IllegalArgumentException("target was null.");
    }//  w ww .ja va2s  .c o  m
    if (props == null) {
        throw new IllegalArgumentException("props was null.");
    }

    if (optionPrefix == null) {
        optionPrefix = "";
    }

    Class<?> clazz = target.getClass();
    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String name = method.getName();
        Class<?> type = method.getReturnType();
        Class<?> params[] = method.getParameterTypes();
        if ((name.startsWith("is") || name.startsWith("get")) && params.length == 0 && type != null
                && isSettableType(type)) {

            try {

                Object value = method.invoke(target, new Object[] {});
                if (value == null) {
                    continue;
                }

                String strValue = convertToString(value, type);
                if (strValue == null) {
                    continue;
                }
                if (name.startsWith("get")) {
                    name = name.substring(3, 4).toLowerCase() + name.substring(4);
                } else {
                    name = name.substring(2, 3).toLowerCase() + name.substring(3);
                }
                props.put(optionPrefix + name, strValue);
                rc = true;

            } catch (Throwable ignore) {
            }

        }
    }

    return rc;
}

From source file:io.dyn.core.handler.Handlers.java

public static String findEventName(Method method) {
    On on = find(On.class, method);
    if (null != on) {
        return on.value();
    } else if (null != AnnotationUtils.findAnnotation(method.getDeclaringClass(), Handler.class)) {
        return method.getName();
    }/*from  w w w.j  a  v a  2 s  .co  m*/
    return null;
}