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

/**
 * Retrieves the pojo name from the given method if this is a valid set/get/add method
 *
 * @param m method to retrieve name from
 * @return propertyname derived from the given method
 *///from   w w  w. j  a  va  2 s.c  om
static String getPojoNameFromMethod(Method m) {
    String methodName = m.getName();
    boolean isMethod = methodName.startsWith("is");
    checkArgument(
            ((methodName.startsWith("set") || methodName.startsWith("get") || methodName.startsWith("add"))
                    && m.getName().length() > 3) || (isMethod && m.getName().length() > 2),
            "Don't know how to retrieve name from this method, got [%s]", m.getName());
    // depending on what type of method it's extract the name
    return decapitalize(m.getName().substring(!isMethod ? 3 : 2));
}

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

/**
 * retrieves the name of the property which is used on mongodb side. Accepts only get methods, will throw an
 * exception for all other methods/*from w w w  .  j  a  va  2 s . co  m*/
 *
 * @param m method to retrieve mongo name from
 * @return mongo name for the given getter method (parameter)
 */
static String getMongoNameFromMethod(Method m) {
    checkArgument(m.getName().startsWith("get") || m.getName().startsWith("is"),
            "Mongo name can only be retrieved from get/is methods, but was %s", m.getName());
    checkArgument(!(m.isAnnotationPresent(Named.class) && m.isAnnotationPresent(Id.class)),
            "You can not annotate a property with @Name and @Id");
    if (m.isAnnotationPresent(Named.class)) {
        checkArgument(!Entity.ID.equals(m.getAnnotation(Named.class).value()),
                "It's not allowed to use @Name annotation to declare id field, instead use @Id annotation");
        return m.getAnnotation(Named.class).value();
    }
    if (m.isAnnotationPresent(Id.class) || "id".equals(getPojoNameFromMethod(m).toLowerCase(Locale.US))) {
        return Entity.ID;
    }
    // differentiate between get and is methods
    return decapitalize(m.getName().substring(m.getName().startsWith("get") ? 3 : 2));
}

From source file:fit.Binding.java

private static Method findMethod(Fixture fixture, String simpleName) {
    Method[] methods = fixture.getTargetClass().getMethods();
    Method method = null;//from w  w  w.  j a  v a2s.  c o  m
    for (int i = 0; i < methods.length; i++) {
        Method possibleMethod = methods[i];
        if (simpleName.equals(possibleMethod.getName().toLowerCase())) {
            method = possibleMethod;
            break;
        }
    }
    return method;
}

From source file:jp.go.nict.langrid.testresource.loader.NodeLoader_1_2.java

protected static void addProperties(Class<?> clazz, Set<String> properties, Set<String> tupleProperties) {
    for (Method m : clazz.getDeclaredMethods()) {
        if (m.getParameterTypes().length != 1)
            continue;
        String name = m.getName();
        if (!name.startsWith("set"))
            continue;
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        String propName = name.substring(3, 4).toLowerCase() + name.substring(4);
        if (m.getParameterTypes()[0].isArray()) {
            tupleProperties.add(propName);
        } else {//  w w w  .  j av  a2s.  c o  m
            properties.add(propName);
        }
    }
}

From source file:com.galenframework.reports.GalenTestInfo.java

public static GalenTestInfo fromMethod(Method method, Object[] arguments) {
    StringBuilder builder = new StringBuilder(
            method.getDeclaringClass().getSimpleName() + "#" + method.getName());
    if (arguments != null && arguments.length > 0) {
        builder.append(" (");
        boolean shouldUseComma = false;
        for (Object argument : arguments) {
            if (shouldUseComma) {
                builder.append(", ");
            }//  www.ja v a  2  s.c  o  m
            builder.append(convertArgumentToString(argument));
            shouldUseComma = true;
        }
        builder.append(")");
    }
    return GalenTestInfo.fromString(builder.toString());
}

From source file:bdi4jade.util.ReflectionUtils.java

private static boolean isGetter(Method method) {
    if (!method.getName().startsWith(GETTER_PREFIX))
        return false;
    if (method.getParameterTypes().length != 0)
        return false;
    if (void.class.equals(method.getReturnType()))
        return false;
    return true;/*from www.j a v  a  2  s.  c  o m*/
}

From source file:bdi4jade.util.ReflectionUtils.java

private static boolean isSetter(Method method) {
    if (!method.getName().startsWith(SETTER_PREFIX))
        return false;
    if (method.getParameterTypes().length != 1)
        return false;
    if (!void.class.equals(method.getReturnType()))
        return false;
    return true;//from w  w  w. java2  s.c  om
}

From source file:de.otto.jsonhome.generator.MethodHelper.java

private static Method methodFromSuperclass(final Method method) {
    final Class<?> superclass = method.getDeclaringClass().getSuperclass();
    if (superclass != null) {
        return ClassUtils.getMethodIfAvailable(superclass, method.getName(), method.getParameterTypes());
    }/*from www . j a  v  a2 s. c  o m*/
    return null;
}

From source file:com.dukescript.presenters.androidapp.test.Knockout.java

public static Map<String, Method> assertMethods(Class<?> test) throws Exception {
    Map<String, Method> all = new HashMap<String, Method>();
    StringBuilder errors = new StringBuilder();
    int cnt = 0;//from  w  w  w  .j  a v a  2  s. c  o m
    final Class<?>[] classes = testClasses();
    for (Class<?> c : classes) {
        for (Method method : c.getMethods()) {
            if (method.getAnnotation(KOTest.class) != null) {
                cnt++;
                String name = method.getName();
                if (!name.startsWith("test")) {
                    name = "test" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
                }
                try {
                    Method m = test.getMethod(name);
                    all.put(name, method);
                } catch (NoSuchMethodException ex) {
                    errors.append("\n").append(name);
                }
            }
        }
    }
    if (errors.length() > 0) {
        errors.append("\nTesting classes: ").append(Arrays.toString(classes));
        fail("Missing method: " + errors);
    }
    assert cnt > 0 : "Some methods found";
    return all;
}

From source file:org.codehaus.groovy.grails.plugins.springsecurity.acl.ProxyUtils.java

/**
 * Finds the method in the unproxied superclass if proxied.
 * @param method  the method//from   ww w. j a  v a  2  s . c  om
 * @return  the method in the unproxied class
 */
public static Method unproxy(final Method method) {
    Class<?> clazz = method.getDeclaringClass();

    if (!AopUtils.isCglibProxyClass(clazz)) {
        return method;
    }

    return ReflectionUtils.findMethod(unproxy(clazz), method.getName(), method.getParameterTypes());
}