Example usage for java.lang Class getMethods

List of usage examples for java.lang Class getMethods

Introduction

In this page you can find the example usage for java.lang Class getMethods.

Prototype

@CallerSensitive
public Method[] getMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

Usage

From source file:Main.java

public static Map<String, Object> getProperties(Object object, boolean includeSuperClasses, boolean deepCopy) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    if (object == null) {
        return map;
    }//  w ww .j a  v  a 2 s .  c  o m

    Class<?> objectClass = object.getClass();
    Method[] methods = includeSuperClasses ? objectClass.getMethods() : objectClass.getDeclaredMethods();
    for (Method method : methods) {
        if (method.getParameterTypes().length == 0 && !method.getReturnType().equals(Void.TYPE)) {
            String methodName = method.getName();
            String propertyName = "";
            if (methodName.startsWith("get")) {
                propertyName = methodName.substring(3);
            } else if (methodName.startsWith("is")) {
                propertyName = methodName.substring(2);
            }
            if (propertyName.length() > 0 && Character.isUpperCase(propertyName.charAt(0))) {
                propertyName = Character.toLowerCase(propertyName.charAt(0)) + propertyName.substring(1);

                Object value = null;
                try {
                    value = method.invoke(object);
                } catch (Exception e) {
                }

                Object value2 = value;

                if (!deepCopy) {
                    map.put(propertyName, value);
                } else {
                    if (isSimpleObject(value)) {
                        map.put(propertyName, value);
                    } else if (value instanceof Map) {
                        Map<String, Object> submap = new HashMap<String, Object>();
                        for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
                            submap.put(String.valueOf(entry.getKey()),
                                    convertObject(entry.getValue(), includeSuperClasses));
                        }
                        map.put(propertyName, submap);
                    } else if (value instanceof Iterable) {
                        List<Object> sublist = new ArrayList<Object>();
                        for (Object v : (Iterable<?>) object) {
                            sublist.add(convertObject(v, includeSuperClasses));
                        }
                        map.put(propertyName, sublist);
                    } else if (value.getClass().isArray()) {
                        List<Object> sublist = new ArrayList<Object>();
                        int length = Array.getLength(value);
                        for (int i = 0; i < length; i++) {
                            sublist.add(convertObject(Array.get(value, i), includeSuperClasses));
                        }
                        map.put(propertyName, sublist);
                    } else {
                        map.put(propertyName, getProperties(value, includeSuperClasses, deepCopy));
                    }
                }
            }
        }
    }

    return map;
}

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

/**
 * Supports Bukkit earlier than... 1.8?//  w  ww  .  j  av  a2 s .  c  om
 */
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:com.infinities.skyport.timeout.ServiceProviderTimeLimiter.java

private static Set<Method> findInterruptibleMethods(Class<?> interfaceType) {
    Set<Method> set = Sets.newHashSet();
    for (Method m : interfaceType.getMethods()) {
        if (declaresInterruptedEx(m)) {
            set.add(m);//from   w w w . ja v  a 2s . co m
        }
    }
    return set;
}

From source file:com.wavemaker.tools.apidocs.tools.parser.util.MethodUtils.java

private static Set<Method> getMethods(Class<?> type, Predicate<? super Method> predicate) {
    final Method[] declaredMethods = type.isInterface() ? type.getMethods() : type.getDeclaredMethods();

    Set<Method> filtered = new LinkedHashSet<>();

    if (declaredMethods != null && declaredMethods.length > 0) {
        for (final Method method : declaredMethods) {
            if (predicate.apply(method)) {
                filtered.add(method);//ww  w.  j a v  a 2s  .  c  om
            }
        }
    }

    return filtered;
}

From source file:com.zauberlabs.commons.mom.NaiveProperties.java

public static Method findMethodOrNull(@NonNull final Class<?> clazz, final int argsCount,
        final Predicate<String> namePredicate) {
    return Streams //
            .cons(clazz.getMethods()) //
            .findOrNull(signature(argsCount, namePredicate));
}

From source file:net.sf.companymanager.qbe.JpaUtil.java

public static boolean isEntityIdManuallyAssigned(final Class<?> type) {
    for (Method method : type.getMethods()) {
        if (isPrimaryKey(method)) {
            return isManuallyAssigned(method);
        }/*from w  w  w. j a  v a  2  s. co m*/
    }
    return false; // no pk found, should not happen
}

From source file:org.openmrs.module.webservices.rest.util.ReflectionUtil.java

/**
 * Find getter method in handler class or any of its superclasses
 * //from  ww w. j  ava 2  s .co  m
 * @param handler
 * @param propName
 * @return
 */
public static <T> Method findPropertyGetterMethod(DelegatingPropertyAccessor<? extends T> handler,
        String propName) {
    String key = handler.getClass().getName().concat(propName);
    Method result = getterMethodCache.get(key);
    if (result != null) {
        return result == nullMethod ? null : result;
    }

    Class<?> clazz = handler.getClass();
    while (clazz != Object.class && result == null) {
        for (Method method : clazz.getMethods()) {
            PropertyGetter ann = method.getAnnotation(PropertyGetter.class);
            if (ann != null && ann.value().equals(propName)) {
                result = method;
                break;
            }
        }
        clazz = clazz.getSuperclass();
    }

    getterMethodCache.put(key, result == null ? nullMethod : result);

    return result;
}

From source file:org.openmrs.module.webservices.rest.util.ReflectionUtil.java

/**
 * Find setter method in handler class or any of its superclasses
 * /*from w  w w  .  j a  v  a2s  .  c o  m*/
 * @param handler
 * @param propName
 * @return
 */
public static <T> Method findPropertySetterMethod(DelegatingPropertyAccessor<? extends T> handler,
        String propName) {
    String key = handler.getClass().getName().concat(propName);
    Method result = setterMethodCache.get(key);
    if (result != null) {
        return result == nullMethod ? null : result;
    }

    Class<?> clazz = handler.getClass();
    while (clazz != Object.class && result == null) {
        for (Method method : clazz.getMethods()) {
            PropertySetter ann = method.getAnnotation(PropertySetter.class);
            if (ann != null && ann.value().equals(propName)) {
                result = method;
                break;
            }
        }
        clazz = clazz.getSuperclass();
    }

    setterMethodCache.put(key, result == null ? nullMethod : result);

    return result;
}

From source file:Main.java

public static Method findMethodForField(String methodName, Class<?> objectClass, Class<?> paramClass)
        throws NoSuchMethodException {
    try {//from   w ww . j  av  a 2s. c o  m
        return objectClass.getMethod(methodName, new Class[] { paramClass });
    } catch (NoSuchMethodException ex) {
        Method[] methods = objectClass.getMethods();
        for (Method method : methods) {
            if (method.getName().equalsIgnoreCase(methodName)) {
                return method;
            }
        }
        throw ex;
    }
}

From source file:Main.java

public static synchronized Method getSetter(final Class targetClass, final String propertyName) {
    String setterName = "set" + Character.toUpperCase(propertyName.charAt(0));
    if (setterName.length() > 1)
        setterName += propertyName.substring(1);

    final Method[] methods = targetClass.getMethods();

    for (Method m : methods) {
        if (m.getName().equals(setterName) && m.getParameterTypes().length == 1)
            return m;
    }//w w  w. j a  va2 s .co  m
    return null;
}