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:com.wrmsr.search.dsl.util.DerivedSuppliers.java

private static void cloneParameterAnnotation(MethodDefinition methodDefinition, int parameterIndex,
        Annotation annotation) throws ReflectiveOperationException {
    Class<?> annotationInterface = getOnlyElement(ImmutableList.copyOf(annotation.getClass().getInterfaces()));
    AnnotationDefinition annotationDefinition = methodDefinition.declareParameterAnnotation(annotationInterface,
            parameterIndex);//from   www  . j a  v  a 2s.c om
    for (java.lang.reflect.Method interfaceMethod : annotationInterface.getDeclaredMethods()) {
        String name = interfaceMethod.getName();
        Object value = interfaceMethod.invoke(annotation);

        // :|
        java.lang.reflect.Method setValueMethod = AnnotationDefinition.class.getDeclaredMethod("setValue",
                String.class, value.getClass());
        setValueMethod.invoke(annotationDefinition, name, value);
    }
}

From source file:com.miandui.utils.runtimePermission.AndPermission.java

private static <T extends Annotation> Method[] findMethodForRequestCode(@NonNull Class<?> source,
        @NonNull Class<T> annotation, int requestCode) {
    List<Method> methods = new ArrayList<>(1);
    for (Method method : source.getDeclaredMethods())
        if (method.isAnnotationPresent(annotation))
            if (isSameRequestCode(method, annotation, requestCode))
                methods.add(method);//from   ww w. jav a  2 s  .co  m
    return methods.toArray(new Method[methods.size()]);
}

From source file:com.hihframework.core.utils.ReflectUtil.java

private static Method[] getMethods(Object obj, int cmpModifier, String prefix) {
    Method[] retMethod = null;//from   w  w  w .  jav  a  2s.  co m

    Class<?> objClass = obj.getClass();
    Method[] methods = objClass.getDeclaredMethods();

    if (methods != null && !(methods.length < 1)) {

        int j = 0;

        for (int i = 0; i < methods.length; i++) {
            int modifier = methods[i].getModifiers();
            if (prefix != null && !prefix.equals("")) {
                if (modifier == cmpModifier && methods[i].getName().startsWith(prefix))
                    j++;
            } else if (modifier == cmpModifier)
                j++;
        }
        retMethod = new Method[j];

        j = 0;
        for (int i = 0; i < methods.length; i++) {
            int modifier = methods[i].getModifiers();
            if (prefix != null && !prefix.equals("")) {
                if (modifier == cmpModifier && methods[i].getName().startsWith(prefix)) {
                    retMethod[j] = methods[i];
                    j++;
                }
            } else if (modifier == cmpModifier)
                retMethod[j] = methods[i];
        }

        return retMethod;
    } else
        return null;
}

From source file:com.hihframework.core.utils.ReflectUtil.java

private static String[] getMethodsName(Object obj, int cmpModifier, String prefix) {
    String[] retValue;//from www.  ja v  a2  s .  c  o m
    Class<?> objClass = obj.getClass();
    Method[] methods = objClass.getDeclaredMethods();

    if (methods != null && !(methods.length < 1)) {
        int j = 0;

        for (int i = 0; i < methods.length; i++) {
            int modifier = methods[i].getModifiers();

            if (prefix != null && !prefix.equals("")) {
                if (modifier == cmpModifier && methods[i].getName().startsWith(prefix))
                    j++;
            } else if (modifier == cmpModifier)
                j++;
        }

        retValue = new String[j];

        j = 0;
        for (int i = 0; i < methods.length; i++) {
            int modifier = methods[i].getModifiers();
            if (prefix != null && !prefix.equals("")) {
                if (modifier == cmpModifier && methods[i].getName().startsWith(prefix)) {
                    retValue[j] = methods[i].getName();
                    j++;
                }
            } else if (modifier == cmpModifier) {
                retValue[j] = methods[i].getName();
                j++;
            }

        }

        return retValue;
    } else
        return null;
}

From source file:com.springframework.beans.BeanUtils.java

/**
 * Find a method with the given method name and minimal parameters (best case: none),
 * declared on the given class or one of its superclasses. Will return a public,
 * protected, package access, or private method.
 * <p>Checks {@code Class.getDeclaredMethods}, cascading upwards to all superclasses.
 * @param clazz the class to check/*www. ja v a  2  s . c  om*/
 * @param methodName the name of the method to find
 * @return the Method object, or {@code null} if not found
 * @throws IllegalArgumentException if methods of the given name were found but
 * could not be resolved to a unique method with minimal parameters
 * @see Class#getDeclaredMethods
 */
public static Method findDeclaredMethodWithMinimalParameters(Class<?> clazz, String methodName)
        throws IllegalArgumentException {

    Method targetMethod = findMethodWithMinimalParameters(clazz.getDeclaredMethods(), methodName);
    if (targetMethod == null && clazz.getSuperclass() != null) {
        targetMethod = findDeclaredMethodWithMinimalParameters(clazz.getSuperclass(), methodName);
    }
    return targetMethod;
}

From source file:com.aaplab.android.roboboost.RoboBoost.java

private static ArrayList<Method> getMethodsList(Class<?> classToInspect, boolean includeSuperclassFields) {
    ArrayList<Method> methodsList = new ArrayList<Method>(Arrays.asList(classToInspect.getDeclaredMethods()));
    if (includeSuperclassFields && classToInspect.getSuperclass() != null) {
        methodsList.addAll(getMethodsList(classToInspect.getSuperclass(), includeSuperclassFields));
    }//from w w  w  .ja v a2  s  .c o m
    return methodsList;
}

From source file:com.are.photophone.utils.permissionutils.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    if (isUsingAndroidAnnotations(object)) {
        clazz = clazz.getSuperclass();/* www  .  java2  s  .c o m*/
    }
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(AfterPermissionGranted.class)) {
            // Check for annotated methods with matching request code.
            AfterPermissionGranted ann = method.getAnnotation(AfterPermissionGranted.class);
            if (ann.value() == requestCode) {
                // Method must be void so that we can invoke it
                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException("Cannot execute non-void method " + method.getName());
                }

                try {
                    // Make method accessible if private
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    method.invoke(object);
                } catch (IllegalAccessException e) {
                    Log.e(TAG, "runDefaultMethod:IllegalAccessException", e);
                } catch (InvocationTargetException e) {
                    Log.e(TAG, "runDefaultMethod:InvocationTargetException", e);
                }
            }
        }
    }
}

From source file:org.alfresco.module.org_alfresco_module_rm.api.PublicAPITestUtil.java

/**
 * Get all the classes referenced by the given class, which might be used by an extension. We consider visible
 * methods, constructors, fields and inner classes, as well as superclasses and interfaces extended by the class.
 *
 * @param initialClass The class to analyse.
 * @param consideredClasses Classes that have already been considered, and which should not be considered again. If
 *            the given class has already been considered then an empty set will be returned. This set will be
 *            updated with the given class.
 * @return The set of classes that might be accessible by an extension of this class.
 *///  w w  w  .j  av a  2s .co m
private static Set<Class<?>> getReferencedClassesFromClass(Class<?> initialClass,
        Set<Class<?>> consideredClasses) {
    Set<Class<?>> referencedClasses = new HashSet<>();

    if (consideredClasses.add(initialClass)) {
        for (Method method : initialClass.getDeclaredMethods()) {
            if (isVisibleToExtender(method.getModifiers())) {
                referencedClasses.addAll(getClassesFromMethod(method));
            }
        }
        for (Constructor<?> constructor : initialClass.getDeclaredConstructors()) {
            if (isVisibleToExtender(constructor.getModifiers())) {
                referencedClasses.addAll(getClassesFromConstructor(constructor));
            }
        }
        for (Field field : initialClass.getDeclaredFields()) {
            if (isVisibleToExtender(field.getModifiers())) {
                referencedClasses.addAll(getClassesFromField(field));
            }
        }
        for (Class<?> clazz : initialClass.getDeclaredClasses()) {
            if (isVisibleToExtender(clazz.getModifiers())) {
                referencedClasses.addAll(getReferencedClassesFromClass(clazz, consideredClasses));
            }
        }
        if (initialClass.getSuperclass() != null) {
            referencedClasses
                    .addAll(getReferencedClassesFromClass(initialClass.getSuperclass(), consideredClasses));
        }
        for (Class<?> clazz : initialClass.getInterfaces()) {
            referencedClasses.addAll(getReferencedClassesFromClass(clazz, consideredClasses));
        }
    }
    return referencedClasses;
}

From source file:nl.nn.adapterframework.util.ClassUtils.java

private static void appendFieldsAndMethods(StringBuffer result, Object o, String type, Class c) {
    Field fields[] = c.getDeclaredFields();
    Method methods[] = c.getDeclaredMethods();
    result.append(//from   w w  w.  ja  v a  2s.c o  m
            type + " " + c.getName() + " #fields [" + fields.length + "] #methods [" + methods.length + "]");
    if (fields.length > 0 || methods.length > 0) {
        result.append(" {\n");
        for (int i = 0; i < fields.length; i++) {
            Field f = fields[i];
            Object value;
            try {
                f.setAccessible(true);
                value = f.get(o);
            } catch (Exception e) {
                value = "Could not get value: " + ClassUtils.nameOf(e) + ": " + e.getMessage();
            }
            result.append(
                    "  field[" + i + "] " + f.getName() + "(" + f.getType().getName() + "): [" + value + "]\n");
        }
        for (int i = 0; i < methods.length; i++) {
            Method m = methods[i];
            result.append("  method[" + i + "] " + m.getName());
            //            Object value;
            //            try {
            //               m.setAccessible(true);
            //               value=m.invoke(o,null);
            //            } catch (Exception e) {
            //               value="Could not get value: "+ClassUtils.nameOf(e)+": "+e.getMessage();
            //            }
            //            result +=": ["+value+"]\n";
            result.append("\n");
        }
        result.append("}");
    }
    result.append("\n");
}

From source file:la.xiong.mylibrary.EasyPermissions.java

/**
 * Find all methods annotated with {@link } on a given object with the
 * correc requestCode argument./* w  ww.jav a2 s.  c  o  m*/
 * @param object the object with annotated methods.
 * @param requestCode the requestCode passed to the annotation.
 */
private static void runAnnotatedMethods(@NonNull Object object, int requestCode) {
    Class clazz = object.getClass();
    if (isUsingAndroidAnnotations(object)) {
        clazz = clazz.getSuperclass();
    }

    while (clazz != null) {
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.isAnnotationPresent(AfterPermissionGranted.class)) {
                // Check for annotated methods with matching request code.
                AfterPermissionGranted ann = method.getAnnotation(AfterPermissionGranted.class);
                if (ann.value() == requestCode) {
                    // Method must be void so that we can invoke it
                    if (method.getParameterTypes().length > 0) {
                        throw new RuntimeException("Cannot execute method " + method.getName()
                                + " because it is non-void method and/or has input parameters.");
                    }

                    try {
                        // Make method accessible if private
                        if (!method.isAccessible()) {
                            method.setAccessible(true);
                        }
                        method.invoke(object);
                    } catch (IllegalAccessException e) {
                        Log.e(TAG, "runDefaultMethod:IllegalAccessException", e);
                    } catch (InvocationTargetException e) {
                        Log.e(TAG, "runDefaultMethod:InvocationTargetException", e);
                    }
                }
            }
        }

        clazz = clazz.getSuperclass();
    }
}