Example usage for java.lang Class getDeclaredMethods

List of usage examples for java.lang Class getDeclaredMethods

Introduction

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

Prototype

@CallerSensitive
public Method[] getDeclaredMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.

Usage

From source file:cf.spring.servicebroker.AccessorUtils.java

/**
 * Finds a method annotated with the specified annotation. This method can
 * be defined in the specified class, or one of its parents.
 *
 * @return the matching method, or <tt>null</tt> if any
 */// w  ww .j av  a  2  s .  c  om
public static <T extends Annotation> Method findMethodWithAnnotation(Class<?> clazz, Class<T> annotationType) {
    Method annotatedMethod = null;
    for (Method method : clazz.getDeclaredMethods()) {
        T annotation = AnnotationUtils.findAnnotation(method, annotationType);
        if (annotation != null) {
            if (annotatedMethod != null) {
                throw new BeanCreationException("Only ONE method with @" + annotationType.getName()
                        + " is allowed on " + clazz.getName() + ".");
            }
            annotatedMethod = method;
        }
    }

    if ((annotatedMethod != null) || clazz.equals(Object.class)) {
        return annotatedMethod;
    } else {
        return findMethodWithAnnotation(clazz.getSuperclass(), annotationType);
    }
}

From source file:com.outcastgeek.traversal.TraverseUtils.java

/**
 * @param pojo is the POJO to be traversed
 * @param pathSteps is traversal path//  w  w  w  . j a  va  2 s . c o  m
 * @return the object a the end of the path
 * @throws TraverseException
 */
public static Object getPath(Object pojo, String... pathSteps) throws TraverseException {

    Object value = null;

    try {
        Class pojoClass = pojo.getClass();
        Method[] declaredMethods = pojoClass.getDeclaredMethods();
        int pathStepLength = pathSteps.length;
        logger.debug("Traversing {}...", pojo);
        for (int i = 0; i < pathStepLength; i++) {
            String step = pathSteps[i];
            logger.debug("Step: {}", step);
            for (Method method : declaredMethods) {
                String methodName = method.getName();
                if (StringUtils.containsIgnoreCase(methodName, step)) {
                    value = pojoClass.getDeclaredMethod(methodName).invoke(pojo);
                    break;
                }
            }
            if (i == pathStepLength - 1) {
                break;
            } else {
                String[] followingSteps = ArrayUtils.removeElement(pathSteps, step);
                return getPath(value, followingSteps);
            }
        }
    } catch (Exception e) {
        throw new TraverseException(e);
    }

    return value;
}

From source file:Main.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>.
 * <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 paramTypes 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
 *//* w ww. ja  v a  2 s.co  m*/
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
    Class<?> searchType = clazz;
    while (searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (Method method : methods) {
            if (name.equals(method.getName())
                    && (paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
                return method;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:com.sonatype.security.ldap.api.DeepEqualsBuilder.java

private static boolean classHasEqualsMethod(Class clazz) {
    if (clazz != null) {
        Method[] methods = clazz.getDeclaredMethods();
        for (int ii = 0; ii < methods.length; ii++) {
            Method method = methods[ii];
            if ("equals".equals(method.getName()) && method.getParameterTypes().length == 1
                    && method.getParameterTypes()[0].equals(Object.class)) {
                return true;
            }/*from w  ww .  jav  a2  s .  c om*/
        }
    }
    return false;
}

From source file:com.outcastgeek.traversal.TraverseUtils.java

/**
 * @param pojo is the POJO to be traversed
 * @param pathSteps is traversal path//from   w  w  w .j a va  2 s. c o m
 * @return true or false
 * @throws TraverseException
 */
public static boolean isNullPath(Object pojo, String... pathSteps) throws TraverseException {

    boolean isNullPath = false;

    try {
        Class pojoClass = pojo.getClass();
        Method[] declaredMethods = pojoClass.getDeclaredMethods();
        int pathStepLength = pathSteps.length;
        logger.debug("Traversing {}...", pojo);
        for (int i = 0; i < pathStepLength; i++) {
            String step = pathSteps[i];
            logger.debug("Step: {}", step);
            Object value = null;
            for (Method method : declaredMethods) {
                String methodName = method.getName();
                if (StringUtils.containsIgnoreCase(methodName, step)) {
                    value = pojoClass.getDeclaredMethod(methodName).invoke(pojo);
                    break;
                }
            }
            if (value != null) {
                if (i == pathStepLength - 1) {
                    break;
                } else {
                    String[] followingSteps = ArrayUtils.removeElement(pathSteps, step);
                    return isNullPath(value, followingSteps);
                }
            } else {
                isNullPath = true;
                break;
            }
        }
    } catch (Exception e) {
        throw new TraverseException(e);
    }

    return isNullPath;
}

From source file:Main.java

static Method getMethod(String fieldName, Class<?> objectClass) throws NoSuchFieldException {
    Method finalMethod = null;/*from   ww w . ja v a  2 s.  com*/
    while (objectClass != null && finalMethod == null) {
        for (Method method : objectClass.getDeclaredMethods()) {
            if (method.getName().equals(fieldName)) {
                Class<?>[] paramsType = method.getParameterTypes();
                if (paramsType.length == 0) {
                    finalMethod = method;
                    break;
                } else if (paramsType.length == 1) {
                    if (paramsType[0].equals(Context.class) || View.class.isAssignableFrom(paramsType[0])) {
                        finalMethod = method;
                        break;
                    }
                }

            }
        }
        if (finalMethod == null) {
            objectClass = objectClass.getSuperclass();
        }
    }
    if (finalMethod == null) {
        throw new NoSuchFieldException(fieldName);
    }
    return finalMethod;
}

From source file:asia.gkc.vneedu.utils.FilterUtil.java

/**
 * /*from ww  w . ja va 2s .  co m*/
 * @param list -  
 * @param object - ?
 * @param <T> - 
 * @return ?
 */
public static <T> T exclude(List<String> list, T object) {
    Class<?> c = object.getClass();

    for (String fieldName : list) {
        Method[] methods = c.getDeclaredMethods();

        for (Method method : methods) {
            if (method.getName().equals(getMethodName(fieldName, "set"))) {
                Class<?> param_class = method.getParameterTypes()[0];
                try {
                    logger.info("Invoking: " + method.getName());
                    method.invoke(object, param_class.cast(null));
                    logger.info("Invoked: " + method.getName());
                } catch (IllegalAccessException e) {
                    logger.warn("IllegalAccessException");
                } catch (InvocationTargetException e) {
                    logger.warn("InvocationTargetException");
                }
            }
        }
    }

    return object;
}

From source file:Main.java

/**
 * Returns an array of all methods in a class with the specified parameter types.
 *
 * The return type is optional, it will not be compared if it is {@code null}.
 * Use {@code void.class} if you want to search for methods returning nothing.
 *//*w w w . java  2 s .  c om*/
public static Method[] findMethodsByExactParameters(Class<?> clazz, Class<?> returnType,
        Class<?>... parameterTypes) {
    List<Method> result = new LinkedList<Method>();
    for (Method method : clazz.getDeclaredMethods()) {
        if (returnType != null && returnType != method.getReturnType())
            continue;

        Class<?>[] methodParameterTypes = method.getParameterTypes();
        if (parameterTypes.length != methodParameterTypes.length)
            continue;

        boolean match = true;
        for (int i = 0; i < parameterTypes.length; i++) {
            if (parameterTypes[i] != methodParameterTypes[i]) {
                match = false;
                break;
            }
        }

        if (!match)
            continue;

        method.setAccessible(true);
        result.add(method);
    }
    return result.toArray(new Method[result.size()]);
}

From source file:com.dragome.callbackevictor.serverside.utils.ReflectionUtils.java

public static Map<String, Object> discoverMethods(final Class<?> pClazz, final Matcher pMatcher,
        final Indexer pIndexer) {

    log.debug("discovering methods on " + pClazz.getName());

    final Map<String, Object> result = new HashMap<String, Object>();

    Class<?> current = pClazz;
    do {/*w  w w.j av  a2 s. c om*/
        final Method[] methods = current.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            final String mname = methods[i].getName();
            if (pMatcher.matches(mname)) {
                pIndexer.put(result, mname, methods[i]);

                log.debug("discovered method " + mname + " -> " + methods[i]);
            }
        }
        current = current.getSuperclass();
    } while (current != null);

    return result;
}

From source file:de.micromata.genome.tpsb.GroovyExceptionInterceptor.java

protected static void collectMethods(Class<?> cls, Map<String, Method> collected) {
    for (Method m : cls.getDeclaredMethods()) {
        if ((m.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC) {
            continue;
        }/*  w ww.  j av  a 2s  .  com*/
        if ((m.getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
            continue;
        }
        if (m.getAnnotation(TpsbIgnore.class) != null) {
            continue;
        }
        if (ignoreMethods.contains(m.getName()) == true) {
            continue;
        }
        if (m.getReturnType().isPrimitive() == true) {
            continue;
        }
        String sm = methodToString(m);
        if (collected.containsKey(sm) == true) {
            continue;
        }
        collected.put(sm, m);
    }
    Class<?> scls = cls.getSuperclass();
    if (scls == Object.class) {
        return;
    }
    collectMethods(scls, collected);
}