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:io.stallion.reflection.PropertyUtils.java

private static boolean containsSetterForGetter(Class clazz, Method method) {
    String methodName = method.getName();
    String setterName;//  ww w.  j  av  a 2s  .  co  m

    if (methodName.startsWith("get"))
        setterName = "set" + methodName.substring(3);
    else if (methodName.startsWith("is"))
        setterName = "set" + methodName.substring(2);
    else
        throw new PropertyException(
                "method '" + methodName + "' is not a getter, thereof no setter can be found");

    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method1 = methods[i];
        if (method1.getName().equals(setterName))
            return true;
    }
    return false;
}

From source file:io.stallion.reflection.PropertyUtils.java

public static Method getGetter(Object target, String propertyName) {
    String cacheKey = "getGetter" + "|" + target.getClass().getCanonicalName() + "|" + propertyName;
    if (target instanceof BaseJavascriptModel) {
        cacheKey = "getGetter" + "|jsModel" + ((BaseJavascriptModel) target).getBucketName() + "|"
                + propertyName;/*  w  w  w  .j a  va2  s.  c o  m*/
    }
    if (lookupCache.containsKey(cacheKey)) {
        return (Method) lookupCache.get(cacheKey);
    }
    String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    Method[] methods = target.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if ((method.getName().equals(getterName) || method.getName().equals(getterIsName))
                && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) {
            lookupCache.put(cacheKey, method);
            return method;
        }
    }
    lookupCache.put(cacheKey, null);
    throw new PropertyException(
            "no readable property '" + propertyName + "' in class '" + target.getClass().getName() + "'");
}

From source file:com.diversityarrays.kdxplore.stats.StatsUtil.java

static public void initCheck() {
    if (checkDone) {
        return;//ww w  . j a  v  a2 s  .c o  m
    }
    Set<String> okIfExhibitMissing = new HashSet<>();
    Collections.addAll(okIfExhibitMissing, "getFormat", "getValueClass", "getStatsName", "getLowOutliers",
            "getHighOutliers");
    List<String> errors = new ArrayList<String>();

    Set<String> unMatchedStatNameValues = new HashSet<String>();
    for (SimpleStatistics.StatName sname : StatName.values()) {
        unMatchedStatNameValues.add(sname.displayName);
    }

    for (Method m : SimpleStatistics.class.getDeclaredMethods()) {

        if (!Modifier.isPublic(m.getModifiers())) {
            continue; // shouldn't happen!
        }

        ExhibitColumn ec = m.getAnnotation(ExhibitColumn.class);

        if (ec == null) {
            if (okIfExhibitMissing.contains(m.getName())) {
                //               if ("getQuartiles".equals(m.getName())) {
                //                  System.err.println("%TODO: @ExhibitColumn for 'SimpleStatistics.getQuartiles()'");
                //               }
            } else {
                errors.add("Missing @ExhibitColumn: " + m.getName());
            }
        } else {
            String ecValue = ec.value();
            boolean found = false;
            for (SimpleStatistics.StatName sname : StatName.values()) {
                if (sname.displayName.equals(ecValue)) {
                    unMatchedStatNameValues.remove(sname.displayName);
                    METHOD_BY_STATNAME.put(sname, m);
                    found = true;
                    break;
                }
            }
            if (!found) {
                errors.add("Doesn't match any StatName: '" + ecValue + "', method=" + m.getName());
            }
        }
    }

    if (!unMatchedStatNameValues.isEmpty()) {
        errors.add(StringUtil.join("Unmatched StatName values: ", " ", unMatchedStatNameValues));
    }

    if (!errors.isEmpty()) {
        throw new RuntimeException(StringUtil.join("Problems in SimpleStatistics config: ", "\n", errors));
    }

    checkDone = true;
}

From source file:io.stallion.reflection.PropertyUtils.java

public static List<String> getPropertyNames(Class clazz) throws PropertyException {
    Method[] methods = clazz.getMethods();
    List<String> names = list();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String name = method.getName();
        if (method.getModifiers() == Modifier.PUBLIC && method.getParameterTypes().length == 0
                && (name.startsWith("get") || name.startsWith("is"))
                && containsSetterForGetter(clazz, method)) {
            String propertyName;/*from  ww  w . j av  a2s .  co  m*/
            if (name.startsWith("get"))
                propertyName = Character.toLowerCase(name.charAt(3)) + name.substring(4);
            else
                propertyName = Character.toLowerCase(name.charAt(2)) + name.substring(3);
            names.add(propertyName);
        }
    }
    return names;
}

From source file:com.liferay.cli.support.util.ReflectionUtils.java

/**
 * Attempt to find a {@link Method} on the supplied class with the supplied
 * name and parameter types. Searches all superclasses up to
 * <code>Object</code>./*  www.ja  v  a 2 s .  c om*/
 * <p>
 * Returns <code>null</code> if no {@link Method} can be found.
 * 
 * @param clazz the class to introspect
 * @param name the name of the method
 * @param parameterTypes the parameter types of the method (may be
 *            <code>null</code> to indicate any signature)
 * @return the Method object, or <code>null</code> if none found
 */
public static Method findMethod(final Class<?> clazz, final String name, final Class<?>[] parameterTypes) {
    Validate.notNull(clazz, "Class must not be null");
    Validate.notNull(name, "Method name must not be null");
    Class<?> searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
        final Method[] methods = searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods();
        for (final Method method : methods) {
            if (name.equals(method.getName())
                    && (parameterTypes == null || Arrays.equals(parameterTypes, method.getParameterTypes()))) {
                return method;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:com.autobizlogic.abl.util.BeanUtil.java

private static Object getValueWithMethod(Method getterMethod, Object bean) {

    try {/*from  w  w  w .  j a va2 s  . c  om*/
        getterMethod.setAccessible(true);
        return getterMethod.invoke(bean);
    } catch (Exception ex) {
        throw new RuntimeException("Unable to use get method " + getterMethod.getName() + " on instance of "
                + bean.getClass().getName(), ex);
    }
}

From source file:org.openflamingo.uploader.util.ReflectionUtils.java

/**
 * ? Signature ./*ww  w.  j ava  2  s  .c  o m*/
 *
 * @param method 
 * @return ? Signature
 */
private static String getMethodSignature(Method method) {
    String methodName = method.getName();
    Class<?>[] classes = method.getParameterTypes();

    StringBuilder builder = new StringBuilder();
    builder.append("[ ");
    builder.append(methodName);
    builder.append("(");
    for (int i = 0; i < classes.length; i++) {
        Class<?> aClass = classes[i];
        builder.append(aClass.getName());
        if (i != (classes.length - 1)) {
            builder.append(", ");
        }
    }
    builder.append(")");
    builder.append(" ]");
    return builder.toString();
}

From source file:com.offbynull.coroutines.instrumenter.asm.SearchUtils.java

/**
 * Find invocations of a certain method.
 * @param insnList instruction list to search through
 * @param expectedMethod type of method being invoked
 * @return list of invocations (may be nodes of type {@link MethodInsnNode} or {@link InvokeDynamicInsnNode})
 * @throws NullPointerException if any argument is {@code null}
 * @throws NullPointerException if {@code expectedMethodType} isn't of sort {@link Type#METHOD}
 *///from   w ww. ja  v a 2 s .  co m
public static List<AbstractInsnNode> findInvocationsOf(InsnList insnList, Method expectedMethod) {
    Validate.notNull(insnList);
    Validate.notNull(expectedMethod);

    List<AbstractInsnNode> ret = new ArrayList<>();

    Type expectedMethodDesc = Type.getType(expectedMethod);
    Type expectedMethodOwner = Type.getType(expectedMethod.getDeclaringClass());
    String expectedMethodName = expectedMethod.getName();

    Iterator<AbstractInsnNode> it = insnList.iterator();
    while (it.hasNext()) {
        AbstractInsnNode instructionNode = it.next();

        Type methodDesc;
        Type methodOwner;
        String methodName;
        if (instructionNode instanceof MethodInsnNode) {
            MethodInsnNode methodInsnNode = (MethodInsnNode) instructionNode;
            methodDesc = Type.getType(methodInsnNode.desc);
            methodOwner = Type.getObjectType(methodInsnNode.owner);
            methodName = expectedMethod.getName();
        } else {
            continue;
        }

        if (methodDesc.equals(expectedMethodDesc) && methodOwner.equals(expectedMethodOwner)
                && methodName.equals(expectedMethodName)) {
            ret.add(instructionNode);
        }
    }

    return ret;
}

From source file:org.openflamingo.uploader.util.ReflectionUtils.java

/**
 * ? Signature ./*from ww  w.  j a  v a 2 s  .c o m*/
 *
 * @param method 
 * @return ? Signature
 */
private static String getMethodSignature(Method method, Object[] args) {
    String methodName = method.getName();
    Class<?>[] classes = method.getParameterTypes();

    StringBuilder builder = new StringBuilder();
    builder.append("[ ");
    builder.append(methodName);
    builder.append("(");
    builder.append(getMethodParameter(classes, args));
    builder.append(")");
    builder.append(" ]");
    return builder.toString();
}

From source file:com.autobizlogic.abl.util.BeanUtil.java

private static void setValueWithMethod(Method setterMethod, Object bean, Object value) {

    try {//w  ww. jav a  2  s.  c  o m
        setterMethod.setAccessible(true);
        setterMethod.invoke(bean, value);
    } catch (Exception ex) {
        throw new RuntimeException("Unable to use set method " + setterMethod.getName() + " on instance of "
                + bean.getClass().getName(), ex);
    }
}