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.github.cherimojava.data.mongo.entity.EntityUtils.java

/**
 * returns the getter method belonging to the given setter method
 *
 * @param m setter method for which a matching getter method shall be found
 * @return getter method belonging to the given Setter method
 * @throws java.lang.IllegalArgumentException if the given method has no matching adder method
 *///from w w w. j a v  a2  s .c  o m
static Method getGetterFromSetter(Method m) {
    try {
        Method getter = m.getDeclaringClass().getMethod(m.getName().replaceFirst("s", "g"));
        checkArgument(getter.getReturnType().equals(m.getParameterTypes()[0]),
                "You can only declare setter methods if there's a matching getter. Found %s without getter",
                m.getName());
        return getter;
    } catch (NoSuchMethodException e) {
        try {
            // check if there's a is method available
            Method isser = m.getDeclaringClass().getMethod(m.getName().replaceFirst("set", "is"));
            // check that both have boolean as type
            checkArgument(boolean.class.equals(Primitives.unwrap(isser.getReturnType()))
                    && boolean.class.equals(Primitives.unwrap(m.getParameterTypes()[0])));
            return isser;
        } catch (NoSuchMethodException e1) {
            // if neither get nor is can be found throw an exception
            throw new IllegalArgumentException(
                    format("Method %s has no corresponding get/is method", m.getName()));
        }
    }
}

From source file:nz.co.senanque.validationengine.ValidationUtils.java

private static Method figureSetter(final String name, final Class<?> clazz) {
    final String setter = figureSetter(name);
    for (Method method : clazz.getMethods()) {
        if (method.getName().equals(setter)) {
            return method;
        }/*from w ww . j  av a  2s.c  om*/
    }
    throw new RuntimeException("Could not find method " + setter + " on class " + clazz.getName());
}

From source file:ml.shifu.shifu.util.ClassUtils.java

public static Method getFirstMethodWithName(String name, Class<?> clazz) {
    String key = clazz.getName() + "#" + name;
    Method cacheMethod = METHOD_CACHE.get(key);
    if (cacheMethod != null) {
        return cacheMethod;
    }//  w w  w.  j a  v  a 2s . c o  m
    List<Method> allMethods = ClassUtils.getAllMethods(clazz);
    Method method = null;
    for (Method f : allMethods) {
        if (f.getName().equals(name)) {
            method = f;
            break;
        }
    }
    return method;
}

From source file:io.lavagna.common.QueryFactory.java

private static QueryTypeAndQuery extractQueryAnnotation(Class<?> clazz, String activeDb, Method method) {
    Query q = method.getAnnotation(Query.class);
    QueriesOverride qs = method.getAnnotation(QueriesOverride.class);

    Assert.isTrue(q != null, String.format("missing @Query annotation for method %s in interface %s",
            method.getName(), clazz.getSimpleName()));
    // only one @Query annotation, thus we return the value without checking the database
    if (qs == null) {
        return new QueryTypeAndQuery(q.type(), q.value());
    }/*  www  .  ja  v a 2s.c  o  m*/

    for (QueryOverride query : qs.value()) {
        if (query.db().equals(activeDb)) {
            return new QueryTypeAndQuery(q.type(), query.value());
        }
    }

    return new QueryTypeAndQuery(q.type(), q.value());
}

From source file:com.maiseries.core.bank.web.common.util.Reflections.java

/**
 * ?, ?DeclaredMethod,?. ?Object?, null. ????
 * // w  w  w  .j  a v  a  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, "object can't be null");
    Validate.notEmpty(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;
}

From source file:com.cnksi.core.tools.utils.Reflections.java

/**
 * ?, ?DeclaredMethod,?./*from  w ww.  j  a v  a  2 s. co 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;
}

From source file:nz.co.senanque.validationengine.ValidationUtils.java

public static Method figureGetter(final String name, final Class<?> clazz) {
    final String getter = figureGetter(name);
    for (Method method : clazz.getMethods()) {
        if (method.getName().equals(getter)) {
            return method;
        }// ww w. ja  v a  2s.  c o  m
    }
    final String isGetter = figureIsGetter(name);
    for (Method method : clazz.getMethods()) {
        if (method.getName().equals(isGetter)) {
            return method;
        }
    }
    throw new RuntimeException("Could not find method " + getter + " on class " + clazz.getName());
}

From source file:Main.java

public static Method getGetter(Object bean, String property) {
    Map<String, Method> cache = GETTER_CACHE.get(bean.getClass());
    if (cache == null) {
        cache = new ConcurrentHashMap<String, Method>();
        for (Method method : bean.getClass().getMethods()) {
            if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())
                    && !void.class.equals(method.getReturnType()) && method.getParameterTypes().length == 0) {
                String name = method.getName();
                if (name.length() > 3 && name.startsWith("get")) {
                    cache.put(name.substring(3, 4).toLowerCase() + name.substring(4), method);
                } else if (name.length() > 2 && name.startsWith("is")) {
                    cache.put(name.substring(2, 3).toLowerCase() + name.substring(3), method);
                }//from   www  .j  av  a 2s.co  m
            }
        }
        Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache);
        if (old != null) {
            cache = old;
        }
    }
    return cache.get(property);
}

From source file:Main.java

/**
 * <p>//ww w .j  a  v  a  2 s.  co m
 * Register a VTI.
 * </p>
 */
public static void registerVTI(Method method, String[] columnNames, String[] columnTypes, boolean readsSqlData)
        throws Exception {
    String methodName = method.getName();
    String sqlName = doubleQuote(methodName);
    Class methodClass = method.getDeclaringClass();
    Class[] parameterTypes = method.getParameterTypes();
    int parameterCount = parameterTypes.length;
    int columnCount = columnNames.length;
    StringBuilder buffer = new StringBuilder();

    buffer.append("create function ");
    buffer.append(sqlName);
    buffer.append("\n( ");
    for (int i = 0; i < parameterCount; i++) {
        if (i > 0) {
            buffer.append(", ");
        }

        String parameterType = mapType(parameterTypes[i]);

        buffer.append("arg");
        buffer.append(i);
        buffer.append(" ");
        buffer.append(parameterType);
    }
    buffer.append(" )\n");

    buffer.append("returns table\n");
    buffer.append("( ");
    for (int i = 0; i < columnCount; i++) {
        if (i > 0) {
            buffer.append(", ");
        }

        buffer.append("\"" + columnNames[i] + "\"");
        buffer.append(" ");
        buffer.append(columnTypes[i]);
    }
    buffer.append(" )\n");

    buffer.append("language java\n");
    buffer.append("parameter style DERBY_JDBC_RESULT_SET\n");
    if (readsSqlData) {
        buffer.append("reads sql data\n");
    } else {
        buffer.append("no sql\n");
    }

    buffer.append("external name ");
    buffer.append("'");
    buffer.append(methodClass.getName());
    buffer.append(".");
    buffer.append(methodName);
    buffer.append("'\n");

    executeDDL(buffer.toString());
}

From source file:com.lpm.fanger.commons.util.Reflections.java

/**
 * ?, ?DeclaredMethod,?.//  w  w w . ja  va  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");
    Validate.notNull(methodName, "method can't be null");
    Validate.notEmpty(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;
}