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:org.dimitrovchi.conf.service.ServiceParameterUtils.java

@SuppressWarnings({ "element-type-mismatch" })
public static <P> P mergeAnnotationParameters() {
    final AnnotationParameters aParameters = annotationParameters();
    return (P) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
            aParameters.annotations.toArray(new Class[aParameters.annotations.size()]),
            new InvocationHandler() {
                @Override//  w ww .  j  a  va 2s. c o m
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if ("toString".equals(method.getName())) {
                        return reflectToString(aParameters.topCaller.getSimpleName(), proxy);
                    }
                    final Class<?> annotationClass = method.getDeclaringClass();
                    final List<Annotation> annotations = aParameters.annotationMap.containsKey(annotationClass)
                            ? aParameters.annotationMap.get(annotationClass)
                            : Collections.<Annotation>emptyList();
                    for (final Annotation annotation : annotations) {
                        final Object value = method.invoke(annotation, args);
                        if (!Objects.deepEquals(method.getDefaultValue(), value)) {
                            return value;
                        }
                    }
                    return method.getDefaultValue();
                }
            });
}

From source file:com.evolveum.midpoint.util.ReflectionUtil.java

public static Method findVarArgsMethod(Object object, String methodName) {
    for (Method method : object.getClass().getMethods()) {
        if (method.getName().equals(methodName) && method.isVarArgs()) {
            return method;
        }//from w  w  w . j a  va  2s  . c  om
    }
    return null;
}

From source file:com.evolveum.midpoint.util.ReflectionUtil.java

public static Method findMethod(Object object, String methodName, int arity) {
    for (Method method : object.getClass().getMethods()) {
        if (method.getName().equals(methodName) && method.getParameterTypes().length == arity
                && !method.isVarArgs()) {
            return method;
        }//from  w  ww. ja v  a 2  s .  co m
    }
    return null;
}

From source file:com.allinfinance.system.util.BeanUtils.java

/**
 * POJONULL?/*from   w  ww .j  av a  2s .com*/
 * @param obj
 * @param val1 
 * @param val2 ?
 * @return
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
public static Object setNullValueWithValue(Object obj, String val1, int val2)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Method[] methods = obj.getClass().getMethods();
    String propertyName = null;
    String methodName = null;
    for (Method method : methods) {
        methodName = method.getName();
        if (methodName.startsWith("set")) {
            String type = method.getGenericParameterTypes()[0].toString();
            propertyName = methodName.substring(methodName.indexOf("set") + 3);
            propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
            if (getProperty(obj, propertyName) == null)
                if ("class java.lang.String".equals(type)) {
                    copyProperty(obj, propertyName, val1);
                } else {
                    copyProperty(obj, propertyName, val2);
                }

        }
    }
    return obj;
}

From source file:com.opensymphony.xwork2.util.AnnotationUtils.java

/**
 * Returns the property name for a method.
 * This method is independent from property fields.
 *
 * @param method The method to get the property name for.
 * @return the property name for given method; null if non could be resolved.
 *///from   w  w  w  .ja  v a  2 s.  c o m
public static String resolvePropertyName(Method method) {

    Matcher matcher = SETTER_PATTERN.matcher(method.getName());
    if (matcher.matches() && method.getParameterTypes().length == 1) {
        String raw = matcher.group(1);
        return raw.substring(0, 1).toLowerCase() + raw.substring(1);
    }

    matcher = GETTER_PATTERN.matcher(method.getName());
    if (matcher.matches() && method.getParameterTypes().length == 0) {
        String raw = matcher.group(2);
        return raw.substring(0, 1).toLowerCase() + raw.substring(1);
    }

    return null;
}

From source file:cat.albirar.framework.dynabean.impl.DynaBeanImplementationUtils.java

/**
 * Comprova que el mtode representa una propietat correcta. Un propietat correcta s:
 * <ul>//from w w w. j  a  v  a 2s  .  c  o  m
 * <li>Un {@link #isSetter(String) mtode set} amb arguments i sense retorn</li>
 * <li>Un {@link #isGetter(String) mtode get} sense arguments i amb retorn diferent de 'void'.</li>
 * <li>Un {@link #isGetterBoolean(String) mtode 'is'} sense arguments i amb retorn 'boolean'.</li>
 * </ul>
 * 
 * @param method El mtode
 * @return true si s correcta i false en cas contrari
 */
public static final boolean isCorrectProperty(Method method) {
    if (isGetter(method.getName())) {
        if (isGetterBoolean(method.getName())) {
            return (method.getParameterTypes().length == 0 && method.getReturnType().equals(boolean.class));
        }
        return (!method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0);
    } else {
        return (method.getParameterTypes().length == 1 && method.getReturnType().equals(void.class));
    }
}

From source file:org.agiso.core.lang.util.ClassUtils.java

/**
 * Wyszukuje dla wskazanej klasy publiczn metod o okrelonej sygnaturze. Jeli
 * metoda nie zostanie naleziona zwraca {@code null}.
 * <p>W przypadku gdy nie jest okrelona tablica parametrw wywoania, zwraca metod
 * tylko gdy wynik wyszukiwania jest unikatowy, tj. istnieje tylko jedna publiczna
 * metoda o wskazanej nazwie.//from  w  w  w .j  ava  2  s  . co  m
 * 
 * <p>Based on:
 * org.springframework.util.ClassUtils.getMethodIfAvailable(Class<?>, String, Class<?>...)
 * 
 * @param clazz Klasa do sprawdzenia
 * @param methodName Nazwa wyszukiwanej metody
 * @param paramTypes Tablica typw parametrw wywoania metody
 *     (moe by {@code null} w celu wyszukania dowolnej metody o wskazanej nazwie)
 * @return Znaleziona metoda lub @{code null} gdy nie istnieje lub nie jest unikatowa
 * @see Class#getMethod
 */
public static Method getMethodIfAvailable(Class<?> clazz, String methodName, Class<?>... paramTypes) {
    if (clazz == null) {
        throw new NullPointerException("Klasa musi by okrelona");
    }
    if (methodName == null) {
        throw new NullPointerException("Nazwa metody musi by okrelona");
    }

    if (paramTypes != null) {
        try {
            return clazz.getMethod(methodName, paramTypes);
        } catch (NoSuchMethodException ex) {
            return null;
        }
    } else {
        Set<Method> candidates = new HashSet<Method>(1);
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            if (methodName.equals(method.getName())) {
                candidates.add(method);
            }
        }
        if (candidates.size() == 1) {
            return candidates.iterator().next();
        }
        return null;
    }
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

public static LinkedHashSet<Method> getDeclaredMethods(Class<?> theClazz) {
    LinkedHashSet<Method> retVal = new LinkedHashSet<Method>();
    for (Method next : theClazz.getDeclaredMethods()) {
        try {//from   w  w  w .j  a v  a 2 s .co  m
            Method method = theClazz.getMethod(next.getName(), next.getParameterTypes());
            retVal.add(method);
        } catch (NoSuchMethodException e) {
            retVal.add(next);
        } catch (SecurityException e) {
            retVal.add(next);
        }
    }
    return retVal;
}

From source file:edu.umn.msi.tropix.proteomics.parameters.ParameterUtils.java

/**
 * This is the (mostly) inverse of {@link #setParametersFromMap(Map, Object)}. Given a parameter object that is bean where the parameters are the
 * attributes with a simple type (i.e. Double, Integer, Float, or Boolean), a map of the attribute names to values (as strings) is
 * created./*from  ww w .  java 2  s .  c  o  m*/
 * 
 * @param parameters
 *          Parameter object to pull keys and values from.
 * @return
 */
@SuppressWarnings("unchecked")
public static void setMapFromParameters(final Object parameters, final Map parameterMap) {
    final List<Class> simpleTypes = java.util.Arrays.asList(new Class[] { Double.class, Integer.class,
            Float.class, Boolean.class, Long.class, String.class, Short.class, double.class, int.class,
            float.class, boolean.class, long.class, short.class });
    for (final Method method : parameters.getClass().getMethods()) {
        String methodName = method.getName();
        if (method.getParameterTypes().length != 0
                || !(methodName.startsWith("get") || methodName.startsWith("is"))
                || !simpleTypes.contains(method.getReturnType())) {
            continue;
        }
        String attributeName;
        if (methodName.startsWith("get")) {
            attributeName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
        } else { // is method
            attributeName = methodName.substring(2, 3).toLowerCase() + methodName.substring(3);
        }
        Object result;
        try {
            result = method.invoke(parameters);
        } catch (final Exception e) {
            logger.info(e);
            throw new IllegalArgumentException(
                    "Failed to invoke get method on bean, should not happen with simple beans.", e);
        }
        if (result != null) {
            parameterMap.put(attributeName, result.toString());
        } // else null value found in setMapFromParameters, not adding it to the map"
    }
}

From source file:org.agiso.core.lang.util.ClassUtils.java

/**
 * Wyszukuje dla wskazanej klasy publiczn metod o okrelonej sygnaturze. Jeli
 * metoda nie zostanie naleziona wyrzuca wyjtek {@code IllegalStateException}.
 * <p>W przypadku gdy nie jest okrelona tablica parametrw wywoania, zwraca metod
 * tylko gdy wynik wyszukiwania jest unikatowy, tj. istnieje tylko jedna publiczna
 * metoda o wskazanej nazwie./*w  w w. j  av  a2  s . c o m*/
 * 
 * <p>Based on:
 * org.springframework.util.ClassUtils.getMethod(Class<?>, String, Class<?>...)
 * 
 * @param clazz Klasa do sprawdzenia
 * @param methodName Nazwa wyszukiwanej metody
 * @param paramTypes Tablica typw parametrw wywoania metody
 *     (moe by {@code null} w celu wyszukania dowolnej metody o wskazanej nazwie)
 * @return Znaleziona metoda (niegdy {@code null})
 * @throws IllegalStateException jeli nie znaleziono metody lub nie jest unikatowa
 * @see Class#getMethod
 */
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
    if (clazz == null) {
        throw new NullPointerException("Klasa musi by okrelona");
    }
    if (methodName == null) {
        throw new NullPointerException("Nazwa metody musi by okrelona");
    }

    if (paramTypes != null) {
        try {
            return clazz.getMethod(methodName, paramTypes);
        } catch (NoSuchMethodException ex) {
            throw new IllegalStateException("Expected method not found: " + ex);
        }
    } else {
        Set<Method> candidates = new HashSet<Method>(1);
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            if (methodName.equals(method.getName())) {
                candidates.add(method);
            }
        }
        if (candidates.size() == 1) {
            return candidates.iterator().next();
        } else if (candidates.isEmpty()) {
            throw new IllegalStateException("Expected method not found: " + clazz + "." + methodName);
        } else {
            throw new IllegalStateException("No unique method found: " + clazz + "." + methodName);
        }
    }
}