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.snaplogic.snaps.firstdata.Create.java

static boolean isGetter(Method method) {
    if (Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0) {
        String methodName = method.getName();
        if (methodName.matches(GET_REGEX) && !method.getReturnType().equals(void.class))
            return true;
        if (methodName.matches(IS_REGEX) && method.getReturnType().equals(boolean.class))
            return true;
    }// w w  w  .  ja v a 2  s  .com
    return false;
}

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

public static boolean isSetter(Method method) {
    Type returnType = method.getGenericReturnType();
    if (!returnType.equals(void.class)) {
        return false; //should not return anything
    }// w w w .  j av a 2  s . c  o m
    Type[] argumentTypes = method.getGenericParameterTypes();
    if (argumentTypes == null || argumentTypes.length != 1) {
        return false; //should accept exactly one argument
    }
    String name = method.getName();
    if (name.startsWith("set")) {
        if (name.length() < 4) {
            return false; //setSomething
        }
        String fourthChar = name.substring(3, 4);
        return fourthChar.toUpperCase(Locale.ROOT).equals(fourthChar); //setSomething (upper case)
    }
    return false;
}

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

private static Class<?> getListItemClass(Method declaredMethod, Class<?> returnType) {
    Type genericReturnType = declaredMethod.getGenericReturnType();
    if (genericReturnType instanceof ParameterizedType) {
        Type type = ((ParameterizedType) genericReturnType).getActualTypeArguments()[0];
        if (type instanceof Class<?>) {
            returnType = ((Class) type);
        } else {//  www.  j  a va 2 s .c o m
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("List returned by " + declaredMethod.getName() + " isn't using generic types.");
            }
            returnType = String.class;
        }
    }
    return returnType;
}

From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java

/**
 * Determine whether the given method is originally declared by {@link java.lang.Object}.
 *///from w  w  w.ja v  a2  s .c  o m
public static boolean isObjectMethod(Method method) {
    if (method == null) {
        return false;
    }
    try {
        Object.class.getDeclaredMethod(method.getName(), method.getParameterTypes());
        return true;
    } catch (Exception ex) {
        return false;
    }
}

From source file:net.sourceforge.stripes.integration.spring.SpringHelper.java

/**
 * Fetches the methods on a class that are annotated with SpringBean. The first time it
 * is called for a particular class it will introspect the class and cache the results.
 * All non-overridden methods are examined, including protected and private methods.
 * If a method is not public an attempt it made to make it accessible - if it fails
 * it is removed from the collection and an error is logged.
 *
 * @param clazz the class on which to look for SpringBean annotated methods
 * @return the collection of methods with the annotation
 *///from   w w  w .j a v a2  s  . com
protected static Collection<Method> getMethods(Class<?> clazz) {
    Collection<Method> methods = methodMap.get(clazz);
    if (methods == null) {
        methods = ReflectUtil.getMethods(clazz);
        Iterator<Method> iterator = methods.iterator();

        while (iterator.hasNext()) {
            Method method = iterator.next();
            if (!method.isAnnotationPresent(SpringBean.class)) {
                iterator.remove();
            } else {
                // If the method isn't public, try to make it accessible
                if (!method.isAccessible()) {
                    try {
                        method.setAccessible(true);
                    } catch (SecurityException se) {
                        throw new StripesRuntimeException("Method " + clazz.getName() + "." + method.getName()
                                + "is marked " + "with @SpringBean and is not public. An attempt to call "
                                + "setAccessible(true) resulted in a SecurityException. Please "
                                + "either make the method public or modify your JVM security "
                                + "policy to allow Stripes to setAccessible(true).", se);
                    }
                }

                // Ensure the method has only the one parameter
                if (method.getParameterTypes().length != 1) {
                    throw new StripesRuntimeException(
                            "A method marked with @SpringBean must have exactly one parameter: "
                                    + "the bean to be injected. Method [" + method.toGenericString() + "] has "
                                    + method.getParameterTypes().length + " parameters.");
                }
            }
        }

        methodMap.put(clazz, methods);
    }

    return methods;
}

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

public static boolean isGetter(Method method) {
    Type returnType = method.getGenericReturnType();
    if (returnType.equals(void.class)) {
        return false; //should return something
    }/*from   w  ww .j a v  a  2s  .  c o m*/
    Type[] argumentTypes = method.getGenericParameterTypes();
    if (argumentTypes != null && argumentTypes.length != 0) {
        return false; //should not accept any arguments
    }
    String name = method.getName();
    if (name.startsWith("get")) {
        if (name.length() < 4) {
            return false; //has to be getSomething
        }
        String fourthChar = name.substring(3, 4);
        return fourthChar.toUpperCase(Locale.ROOT).equals(fourthChar); //getSomething (upper case)
    } else if (name.startsWith("is")) {
        if (name.length() < 3) {
            return false; //isSomething
        }
        String thirdChar = name.substring(2, 3);
        //noinspection SimplifiableIfStatement
        if (!thirdChar.toUpperCase(Locale.ROOT).equals(thirdChar)) {
            return false; //has to start with uppercase (or something that uppercases to itself, like a number?)
        }
        return returnType.equals(boolean.class) || returnType.equals(Boolean.class);
    } else {
        return false;
    }
}

From source file:org.obiba.opal.rest.client.magma.OpalJavaClient.java

private static Object invokeStaticMethod(Method method, Object... arguments) {
    if (method == null)
        throw new IllegalArgumentException("method cannot be null");

    try {//w ww  .jav  a  2s . c o m
        return method.invoke(null, arguments);
    } catch (RuntimeException | InvocationTargetException | IllegalAccessException e) {
        log.error("Error invoking '{}' method for type {}", method.getName(),
                method.getDeclaringClass().getName(), e);
        throw new RuntimeException("Error invoking '" + method.getName() + "' method for type "
                + method.getDeclaringClass().getName());
    }
}

From source file:com.amazon.carbonado.layout.LayoutFactory.java

/**
 * Returns an annotation hash code using a algorithm similar to the
 * default. The difference is in the handling of class and enum values. The
 * name is chosen for the hash code component instead of the instance
 * because it is stable between invocations of the JVM.
 *//*from  ww  w  .jav  a  2 s  .  c  o m*/
private static int annHashCode(Annotation ann) {
    int hash = 0;

    Method[] methods = ann.getClass().getDeclaredMethods();
    for (Method m : methods) {
        if (m.getReturnType() == null || m.getReturnType() == void.class) {
            continue;
        }
        if (m.getParameterTypes().length != 0) {
            continue;
        }

        String name = m.getName();
        if (name.equals("hashCode") || name.equals("toString") || name.equals("annotationType")) {
            continue;
        }

        Object value;
        try {
            value = m.invoke(ann);
        } catch (InvocationTargetException e) {
            continue;
        } catch (IllegalAccessException e) {
            continue;
        }

        hash += (127 * name.hashCode()) ^ annValueHashCode(value);
    }

    return hash;
}

From source file:com.cloudera.api.model.ApiModelTest.java

private static String getMethodName(Method m) {
    return String.format("%s::%s()", m.getDeclaringClass().getName(), m.getName());
}

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

/**
 * set/*from w  w w  . j av  a  2 s. c om*/
 * 
 * @param bean
 * @param fieldName
 * @param setValue
 */

public static void setValueBySetMethod(Object bean, String fieldName, Object setValue) {
    if (bean == null || fieldName == null || "".equals(fieldName))
        return;
    Field field = getDeclaredField(bean, fieldName);
    if (field == null)
        throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + bean + "]");
    Method setMethod = getDeclaredMethod(bean, getSetMethodName(fieldName), new Class<?>[] { field.getType() });
    if (setMethod != null) {
        try {
            setMethod.invoke(bean, setValue);
        } catch (Exception e) {
            logger.error("error methodName=" + setMethod.getName() + " " + e.getMessage(), e);
        }
    }

}