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.yahoo.sql4d.sql4ddriver.Util.java

public static List<Method> getAllGetters(Class<?> clazz) {
    Method[] allMethods = clazz.getMethods();
    List<Method> getters = new ArrayList<>();
    for (Method method : allMethods) {
        if (method.getName().startsWith("get") && !method.getName().equals("getClass")) {
            getters.add(method);//from   www  . ja v  a 2s. c o  m
        }
    }
    return getters;
}

From source file:net.daboross.bukkitdev.skywars.util.CrossVersion.java

/**
 * Supports Bukkit earlier than... 1.8?//www  .  j a v  a2  s  .  c  o m
 */
public static Collection<? extends Player> getOnlinePlayers(Server s) {
    try {
        return s.getOnlinePlayers();
    } catch (NoSuchMethodError ignored) {
        Class<? extends Server> theClass = s.getClass();
        try {
            for (Method method : theClass.getMethods()) {
                if ("getOnlinePlayers".equals(method.getName()) && method.getParameterTypes().length == 0
                        && method.getReturnType().isArray()) {
                    return Arrays.asList((Player[]) method.invoke(s));
                }
            }
        } catch (SecurityException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException ex) {
            SkyStatic.getLogger().log(Level.WARNING,
                    "Couldn't use fallback .getOnlinePlayers method of Server! Acting as if there are no online players!!",
                    ex);
        }
        SkyStatic.getLogger().log(Level.WARNING,
                "Couldn't find old fallback .getOnlinePlayers method of Server! Acting as if there are no online players!!");
    }
    return Collections.emptyList();
}

From source file:Main.java

public static void dumpMethod(final Method method) {
    final StringBuilder builder = new StringBuilder();
    builder.append("------------------------------\n");
    builder.append("MethodName: ").append(method.getName()).append("\n");
    builder.append("ParameterTypes:{");
    for (Class<?> cls : method.getParameterTypes()) {
        builder.append(cls.getName()).append(", ");
    }/*from  www  . j av a2  s  .c  o  m*/
    builder.append("}\n");
    builder.append("GenericParameterTypes:{");
    for (Type cls : method.getGenericParameterTypes()) {
        builder.append(cls.getClass()).append(", ");
    }
    builder.append("}\n");
    builder.append("TypeParameters:{");
    for (TypeVariable<Method> cls : method.getTypeParameters()) {
        builder.append(cls.getName()).append(", ");
    }
    builder.append("}\n");
    builder.append("DeclaredAnnotations:{");
    for (Annotation cls : method.getDeclaredAnnotations()) {
        builder.append(cls).append(", ");
    }
    builder.append("}\n");
    builder.append("Annotations:{");
    for (Annotation cls : method.getAnnotations()) {
        builder.append(cls).append(", ");
    }
    builder.append("}\n");
    builder.append("ExceptionTypes:{");
    for (Class<?> cls : method.getExceptionTypes()) {
        builder.append(cls.getName()).append(", ");
        ;
    }
    builder.append("}\n");
    builder.append("ReturnType: ").append(method.getReturnType());
    builder.append("\nGenericReturnType: ").append(method.getGenericReturnType());
    builder.append("\nDeclaringClass: ").append(method.getDeclaringClass());
    builder.append("\n");

    System.out.println(builder.toString());
}

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

/**
 * objnull-?/*from   w ww .  ja v  a2  s  .c  o  m*/
 * @param obj
 * @return
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
public static Object setNullValueWithLine(Object obj)
        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")) {
            propertyName = methodName.substring(methodName.indexOf("set") + 3);
            propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
            if (getProperty(obj, propertyName) == null)
                copyProperty(obj, propertyName, "-");
        }
    }
    return obj;
}

From source file:com.cloudera.csd.validation.references.components.ReflectionHelper.java

/**
 * Returns the name of the property associated with the getter method.
 * If the method is not a a getter, an IllegalStateException is thrown.
 *
 * @param method the method./* w w w.  jav  a2  s .c om*/
 * @return the property name.
 */
public static String propertyNameOfGetter(Method method) {
    Preconditions.checkNotNull(method);
    String name = method.getName();
    for (String prefix : GETTER_PREFIXES) {
        if (name.startsWith(prefix)) {
            return Introspector.decapitalize(name.substring(prefix.length()));
        }
    }
    throw new IllegalStateException("Method is malformed " + method.getName());
}

From source file:pl.bristleback.server.bristle.conf.resolver.action.ActionResolvingUtils.java

public static String resolveActionName(Method actionMethod) {
    Action actionAnnotation = actionMethod.getAnnotation(Action.class);
    if (actionAnnotation == null || StringUtils.isBlank(actionAnnotation.name())) {
        return actionMethod.getName();
    }/*ww w  . ja va 2s . co  m*/
    validateActionNameFromAnnotationValue(actionAnnotation.name());
    return actionAnnotation.name();
}

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

/**
 * ???//from   w w  w. ja v  a 2s. com
 * @param bean
 * @param property
 * @return
 */
public static boolean isContainProperty(Object bean, String property) {

    Method[] methods = bean.getClass().getMethods();

    String fieldName = null;

    for (Method method : methods) {

        fieldName = method.getName().substring(3);

        fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);

        if (fieldName.equals(property)) {
            return true;
        }
    }
    return false;
}

From source file:com.fengduo.bee.commons.util.ObjectUtils.java

/**
 * ????//from w w  w .  j  a v  a 2  s . c o  m
 * 
 * @param annotation
 * @param object
 */
public static void annotationToObject(Object annotation, Object object) {
    if (annotation != null) {
        Class<?> annotationClass = annotation.getClass();
        Class<?> objectClass = object.getClass();
        for (Method m : objectClass.getMethods()) {
            if (StringUtils.startsWith(m.getName(), "set")) {
                try {
                    String s = StringUtils.uncapitalize(StringUtils.substring(m.getName(), 3));
                    Object obj = annotationClass.getMethod(s).invoke(annotation);
                    if (obj != null && !"".equals(obj.toString())) {
                        if (object == null) {
                            object = objectClass.newInstance();
                        }
                        m.invoke(object, obj);
                    }
                } catch (Exception e) {
                    // 
                }
            }
        }
    }
}

From source file:edu.cornell.mannlib.vedit.util.OperationUtils.java

/**
 * Takes a bean and clones it using reflection. Any fields without standard
 * getter/setter methods will not be copied.
 *//*from w w  w  .  j a  v  a 2 s .c o  m*/
public static Object cloneBean(final Object bean, final Class<?> beanClass, final Class<?> iface) {
    if (bean == null) {
        throw new NullPointerException("bean may not be null.");
    }
    if (beanClass == null) {
        throw new NullPointerException("beanClass may not be null.");
    }
    if (iface == null) {
        throw new NullPointerException("iface may not be null.");
    }

    class CloneBeanException extends RuntimeException {
        public CloneBeanException(String message, Throwable cause) {
            super(message + " <" + cause.getClass().getSimpleName() + ">: bean=" + bean + ", beanClass="
                    + beanClass.getName() + ", iface=" + iface.getName(), cause);
        }
    }

    Object newBean;
    try {
        newBean = beanClass.getConstructor().newInstance();
    } catch (NoSuchMethodException e) {
        throw new CloneBeanException("bean has no 'nullary' constructor.", e);
    } catch (InstantiationException e) {
        throw new CloneBeanException("tried to create instance of an abstract class.", e);
    } catch (IllegalAccessException e) {
        throw new CloneBeanException("bean constructor is not accessible.", e);
    } catch (InvocationTargetException e) {
        throw new CloneBeanException("bean constructor threw an exception.", e);
    } catch (Exception e) {
        throw new CloneBeanException("failed to instantiate a new bean.", e);
    }

    for (Method beanMeth : iface.getMethods()) {
        String methName = beanMeth.getName();
        if (!methName.startsWith("get")) {
            continue;
        }
        if (beanMeth.getParameterTypes().length != 0) {
            continue;
        }
        String fieldName = methName.substring(3, methName.length());
        Class<?> returnType = beanMeth.getReturnType();

        Method setterMethod;
        try {
            setterMethod = iface.getMethod("set" + fieldName, returnType);
        } catch (NoSuchMethodException nsme) {
            continue;
        }

        Object fieldVal;
        try {
            fieldVal = beanMeth.invoke(bean, (Object[]) null);
        } catch (Exception e) {
            throw new CloneBeanException("failed to invoke " + beanMeth, e);
        }

        try {
            Object[] setArgs = new Object[1];
            setArgs[0] = fieldVal;
            setterMethod.invoke(newBean, setArgs);
        } catch (Exception e) {
            throw new CloneBeanException("failed to invoke " + setterMethod, e);
        }
    }
    return newBean;
}

From source file:net.minder.config.impl.DefaultConfigurationInjector.java

private static String getConfigName(Method method) {
    String methodName = method.getName();
    StringBuilder name = new StringBuilder(methodName.length());
    if (methodName != null && methodName.length() > 3 && methodName.startsWith("set")
            && Character.isUpperCase(methodName.charAt(3))) {
        name.append(methodName.substring(3));
        name.setCharAt(0, Character.toLowerCase(name.charAt(0)));
    } else {//w ww.ja  va2 s  . c  o  m
        name.append(name);
    }
    return name.toString();
}