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.enonic.cms.business.portal.datasource.methodcall.MethodCallFactory.java

private static Method resolveMethod(Class targetClass, String methodName, int numParams, boolean useContext) {
    final String localMethodName = resolveLocalMethodName(methodName);

    for (Method method : targetClass.getMethods()) {
        if (localMethodName.equals(method.getName()) && (method.getParameterTypes().length == numParams)) {
            return method;
        }//from w ww . ja v  a  2 s.  c o  m
    }
    throw new VerticalRenderException("Method [" + localMethodName + "] with ["
            + (useContext ? numParams - 1 : numParams) + "] parameters does not exist");
}

From source file:com.agileapes.couteau.context.spring.event.impl.GenericTranslationScheme.java

static boolean isGetter(Method method) {
    return Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())
            && method.getParameterTypes().length == 0 && !method.getReturnType().equals(void.class)
            && (method.getName().matches("get[A-Z].*")
                    || (method.getName().matches("is[A-Z].*") && method.getReturnType().equals(boolean.class)));
}

From source file:com.wrmsr.search.dsl.util.DerivedSuppliers.java

private static void cloneParameterAnnotation(MethodDefinition methodDefinition, int parameterIndex,
        Annotation annotation) throws ReflectiveOperationException {
    Class<?> annotationInterface = getOnlyElement(ImmutableList.copyOf(annotation.getClass().getInterfaces()));
    AnnotationDefinition annotationDefinition = methodDefinition.declareParameterAnnotation(annotationInterface,
            parameterIndex);/*w ww  .  j a va2 s.com*/
    for (java.lang.reflect.Method interfaceMethod : annotationInterface.getDeclaredMethods()) {
        String name = interfaceMethod.getName();
        Object value = interfaceMethod.invoke(annotation);

        // :|
        java.lang.reflect.Method setValueMethod = AnnotationDefinition.class.getDeclaredMethod("setValue",
                String.class, value.getClass());
        setValueMethod.invoke(annotationDefinition, name, value);
    }
}

From source file:cz.jirutka.validator.collection.internal.AnnotationUtils.java

/**
 * Returns annotation's attributes (name and value) as map.
 *///from   www.j  a  v  a  2 s.c  o m
public static Map<String, Object> readAllAttributes(Annotation annotation) {
    Map<String, Object> attributes = new HashMap<>();

    for (Method method : annotation.annotationType().getDeclaredMethods()) {
        try {
            Object value = method.invoke(annotation);
            attributes.put(method.getName(), value);

        } catch (IllegalAccessException | InvocationTargetException ex) {
            throw new IllegalStateException(ex);
        }
    }
    return attributes;
}

From source file:com.github.cherimojava.data.mongo.entity.EntityUtils.java

/**
 * returns the adder method matching the given Getter method or throws an exception if no such method exists
 *
 * @param m getter method for which a matching adder method shall be found
 * @return adder method belongign to the given Getter method
 * @throws java.lang.IllegalArgumentException if the given method has no matching adder method
 *//*from w  w w  .  j av a2 s .co m*/
static Method getAdderFromGetter(Method m) {
    try {
        return m.getDeclaringClass().getMethod(m.getName().replaceFirst("get", "add"),
                (Class) ((ParameterizedType) m.getGenericReturnType()).getActualTypeArguments()[0]);
    } catch (Exception e) {
        try {
            // if we had no hit, try if there's a vararg adder
            return m.getDeclaringClass().getMethod(m.getName().replaceFirst("get", "add"),
                    Array.newInstance(
                            (Class) ((ParameterizedType) m.getGenericReturnType()).getActualTypeArguments()[0],
                            0).getClass());
        } catch (Exception e1) {
            throw new IllegalArgumentException(
                    format("Method %s has no corresponding adder method", m.getName()));
        }
    }
}

From source file:Utils.java

/**
 * Returns true if the method is a JMX attribute setter method
 */// www. j a v a2s . c  om
public static boolean isAttributeSetter(Method m) {
    if (m == null)
        return false;

    String name = m.getName();
    Class retType = m.getReturnType();
    Class[] params = m.getParameterTypes();
    if (retType == Void.TYPE && params.length == 1 && name.startsWith("set") && name.length() > 3) {
        return true;
    }
    return false;
}

From source file:com.lakala.demo.util.Reflections.java

/**
 * ?, ?DeclaredMethod,?.//from   ww w.j av a 2  s  . c  o  m
 * ?Object?, null.
 * ????
 * 
 * ?. ?Method,?Method.invoke(Object obj, Object... args)
 */
public static Method getAccessibleMethodByName(final Object obj, final String methodName) {
    Validate.notNull(obj, "object can't be null");

    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:io.devcon5.pageobjects.tx.TransactionHelper.java

/**
 * Determines the transaction name of the method. The method must be annotated with {@link Transaction} otherwise
 * the empty optional is returned. The name is derived from the value of the {@link Transaction} annotation or from
 * the mehtod name. If the declaring class denotes a transaction itself, it's name prefixes the method transaction.
 *
 * @param method// w w  w . j a  v  a  2  s .  com
 *         the method for which the transaction name should be determined
 *
 * @return the name of the transaction or the empty optional if the method denotes no transaction
 */
public static Optional<String> getTxName(Object object, final Method method) {

    return Optional.ofNullable(method.getAnnotation(Transaction.class))
            .map(t -> getClassTxName(object.getClass()).map(ctx -> ctx + '_').orElse("")
                    + (isEmpty(t.value()) ? method.getName() : t.value()));
}

From source file:Utils.java

/**
 * Returns true is the given method is a JMX attribute getter method
 *///from   www.  j av a2  s .c  o m
public static boolean isAttributeGetter(Method m) {
    if (m == null)
        return false;

    String name = m.getName();
    Class retType = m.getReturnType();
    Class[] params = m.getParameterTypes();
    if (retType != Void.TYPE && params.length == 0) {
        if (name.startsWith("get") && name.length() > 3)
            return true;
        else if (name.startsWith("is") && name.length() > 2 && retType == Boolean.TYPE)
            return true;
    }
    return false;
}

From source file:com.xsw.utils.Reflections.java

/**
 * ?, ?DeclaredMethod,?./*from   w  w w .java  2s . c o m*/
 * ?Object?, null.
 * ????
 * 
 * ?. ?Method,?Method.invoke(Object obj, Object... args)
 */
public static Method getAccessibleMethodByName(final Object obj, final String methodName) {
    Validate.notNull(obj, "object can't be null");
    //Validate.notBlank(methodName, "methodName can't be blank");

    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;
}