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.kelveden.rastajax.core.ResourceClassLoader.java

private List<ResourceClassMethod> loadMethods(final Class<?> candidateResourceClass) {

    final List<ResourceClassMethod> methodsOnResource = new ArrayList<ResourceClassMethod>();

    Method[] methods;/*from  w w w  .  j  a v a2s  .c o m*/
    try {
        methods = candidateResourceClass.getDeclaredMethods();
    } catch (NoClassDefFoundError e) {
        LOGGER.warn(
                "Could not process candidate resource class {} as a class referenced in it could not be found.",
                candidateResourceClass.getName(), e);
        return methodsOnResource;
    }

    for (Method method : methods) {
        if (Modifier.isPublic(method.getModifiers())) {
            final ResourceClassMethod methodOnResource = loadMethod(candidateResourceClass, method);

            if (methodOnResource != null) {
                methodsOnResource.add(methodOnResource);
            }
        }
    }

    return methodsOnResource;
}

From source file:com.gh.bmd.jrt.android.v4.core.DefaultObjectContextRoutineBuilder.java

@Nullable
private static Method getAnnotatedMethod(@Nonnull final Class<?> targetClass, @Nonnull final String name) {

    Method targetMethod = null;// www.ja  v  a2 s . co m

    for (final Method method : targetClass.getMethods()) {

        final Bind annotation = method.getAnnotation(Bind.class);

        if ((annotation != null) && name.equals(annotation.value())) {

            targetMethod = method;
            break;
        }
    }

    if (targetMethod == null) {

        for (final Method method : targetClass.getDeclaredMethods()) {

            final Bind annotation = method.getAnnotation(Bind.class);

            if ((annotation != null) && name.equals(annotation.value())) {

                targetMethod = method;
                break;
            }
        }
    }

    return targetMethod;
}

From source file:me.anon.lib.Views.java

private static void inject(final Object target, Object source, Finder finder) {
    if (target.getClass().getDeclaredFields() != null) {
        ArrayList<Method> methods = new ArrayList<Method>();
        ArrayList<Field> fields = new ArrayList<Field>();
        Class objOrSuper = target.getClass();

        if (!objOrSuper.isAnnotationPresent(Injectable.class)) {
            Log.e("InjectView", "No Injectable annotation for class " + objOrSuper);
            return;
        }/* w ww  . j a  v  a2s  . c om*/

        while (objOrSuper.isAnnotationPresent(Injectable.class)) {
            for (Field field : objOrSuper.getDeclaredFields()) {
                if (field.isAnnotationPresent(InjectView.class) || field.isAnnotationPresent(InjectViews.class)
                        || field.isAnnotationPresent(InjectFragment.class)
                        || field.isAnnotationPresent(OnClick.class)) {
                    fields.add(field);
                }
            }

            for (Method method : objOrSuper.getDeclaredMethods()) {
                if (method.isAnnotationPresent(OnClick.class)) {
                    methods.add(method);
                }
            }

            objOrSuper = objOrSuper.getSuperclass();
        }

        for (Field field : fields) {
            if (field.isAnnotationPresent(InjectView.class)) {
                InjectView a = (InjectView) field.getAnnotation(InjectView.class);

                try {
                    field.setAccessible(true);

                    int id = ((InjectView) a).value();
                    if (id < 1) {
                        String key = ((InjectView) a).id();
                        if (TextUtils.isEmpty(key)) {
                            key = field.getName();
                            key = key.replaceAll("(.)([A-Z])", "$1_$2").toLowerCase(Locale.ENGLISH);
                        }

                        Field idField = R.id.class.getField(key);
                        id = idField.getInt(null);
                    }

                    View v = finder.findById(source, id);

                    if (v != null) {
                        field.set(target, v);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (field.isAnnotationPresent(InjectViews.class)) {
                try {
                    InjectViews annotation = (InjectViews) field.getAnnotation(InjectViews.class);
                    field.setAccessible(true);

                    int[] ids = annotation.value();
                    String[] strIds = annotation.id();
                    Class[] instances = annotation.instances();

                    List<View> views = new ArrayList<View>(ids.length);

                    if (ids.length > 0) {
                        for (int index = 0; index < ids.length; index++) {
                            View v = finder.findById(source, ids[index]);
                            views.add(index, v);
                        }
                    } else if (strIds.length > 0) {
                        for (int index = 0; index < ids.length; index++) {
                            String key = annotation.id()[index];
                            Field idField = R.id.class.getField(key);
                            int id = idField.getInt(null);

                            View v = finder.findById(source, id);
                            views.add(index, v);
                        }
                    } else if (instances.length > 0) {
                        for (int index = 0; index < instances.length; index++) {
                            List<View> v = finder.findByInstance(source, instances[index]);
                            views.addAll(v);
                        }
                    }

                    field.set(target, views);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (field.isAnnotationPresent(InjectFragment.class)) {
                InjectFragment annotation = (InjectFragment) field.getAnnotation(InjectFragment.class);

                try {
                    field.setAccessible(true);

                    int id = ((InjectFragment) annotation).value();
                    Object fragment = null;

                    if (id < 1) {
                        String tag = ((InjectFragment) annotation).tag();

                        fragment = finder.findFragmentByTag(source, tag);
                    } else {
                        fragment = finder.findFragmentById(source, id);
                    }

                    if (fragment != null) {
                        field.set(target, fragment);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (field.isAnnotationPresent(OnClick.class)) {
                OnClick annotation = (OnClick) field.getAnnotation(OnClick.class);

                try {
                    if (field.get(target) != null) {
                        final View view = ((View) field.get(target));

                        if (!TextUtils.isEmpty(annotation.method())) {
                            final String clickName = annotation.method();
                            view.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    try {
                                        Class<?> c = Class.forName(target.getClass().getCanonicalName());
                                        Method m = c.getMethod(clickName, View.class);
                                        m.invoke(target, v);
                                    } catch (Exception e) {
                                        throw new IllegalArgumentException("Method not found " + clickName);
                                    }
                                }
                            });
                        } else {
                            view.setOnClickListener((View.OnClickListener) target);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        for (final Method method : methods) {
            if (method.isAnnotationPresent(OnClick.class)) {
                final OnClick annotation = (OnClick) method.getAnnotation(OnClick.class);
                final String clickName = method.getName();

                try {
                    int id = annotation.value();
                    if (id < 1) {
                        String key = annotation.id();

                        if (TextUtils.isEmpty(key)) {
                            key = clickName;
                            key = key.replaceAll("^(on)?(.*)Click$", "$2");
                            key = key.replaceAll("(.)([A-Z])", "$1_$2").toLowerCase(Locale.ENGLISH);
                        }

                        Field field = R.id.class.getField(key);
                        id = field.getInt(null);
                    }

                    View view = finder.findById(source, id);
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                if (method != null && method.getParameterTypes().length > 0) {
                                    Class<?> paramType = method.getParameterTypes()[0];
                                    method.setAccessible(true);
                                    method.invoke(target, paramType.cast(v));
                                } else if (method != null && method.getParameterTypes().length < 1) {
                                    method.setAccessible(true);
                                    method.invoke(target);
                                } else {
                                    new IllegalArgumentException(
                                            "Failed to find method " + clickName + " with nil or View params")
                                                    .printStackTrace();
                                }
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:com.github.dactiv.common.utils.ReflectionUtils.java

/**
 * //from www. jav a2 s  . co  m
 * ?annotationClass
 * 
 * @param targetClass
 *            Class
 * @param annotationClass
 *            Class
 * 
 * @return List
 */
public static <T extends Annotation> List<T> getAnnotations(Class targetClass, Class annotationClass) {
    Assert.notNull(targetClass, "targetClass?");
    Assert.notNull(annotationClass, "annotationClass?");

    List<T> result = new ArrayList<T>();
    Annotation annotation = targetClass.getAnnotation(annotationClass);
    if (annotation != null) {
        result.add((T) annotation);
    }
    Constructor[] constructors = targetClass.getDeclaredConstructors();
    // ?
    CollectionUtils.addAll(result, getAnnotations(constructors, annotationClass).iterator());

    Field[] fields = targetClass.getDeclaredFields();
    // ?
    CollectionUtils.addAll(result, getAnnotations(fields, annotationClass).iterator());

    Method[] methods = targetClass.getDeclaredMethods();
    // ?
    CollectionUtils.addAll(result, getAnnotations(methods, annotationClass).iterator());

    for (Class<?> superClass = targetClass.getSuperclass(); superClass == null
            || superClass == Object.class; superClass = superClass.getSuperclass()) {
        List<T> temp = getAnnotations(superClass, annotationClass);
        if (CollectionUtils.isNotEmpty(temp)) {
            CollectionUtils.addAll(result, temp.iterator());
        }
    }

    return result;
}

From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java

/**
 * Use any relevant annotations on the supplied class to configure
 * the given job./*  w  w w. ja v a 2  s .co  m*/
 * 
 * @param clazz            the class to reflect for mara-related annotations
 * @param job            the job being configured
 * @throws ToolException   if reflection fails for any reason
 */
public void configureJobFromClass(Class<?> clazz, Job job) throws ToolException {

    configureJobFromAnnotations(job, clazz.getAnnotations(), clazz);

    for (Field field : clazz.getDeclaredFields()) {
        configureJobFromAnnotations(job, field.getAnnotations(), field);
    }
    for (Method method : clazz.getDeclaredMethods()) {
        configureJobFromAnnotations(job, method.getAnnotations(), method);
    }
}

From source file:io.hummer.util.ws.AbstractNode.java

protected boolean isRESTfulService() {
    Class<?> c = getClass();
    do {/*from   w  w  w .  j a  v  a2 s  .c o m*/
        if (c.isAnnotationPresent(Path.class))
            return true;
        for (Method m : c.getDeclaredMethods()) {
            if (m.isAnnotationPresent(Path.class))
                return true;
        }
        c = c.getSuperclass();
    } while (c != null && c != Object.class);
    return false;
}

From source file:com.opensymphony.xwork2.util.finder.DefaultClassFinder.java

public List<Method> findAnnotatedMethods(Class<? extends Annotation> annotation) {
    classesNotLoaded.clear();//from w  ww .  j a  v  a 2  s .c o m
    List<ClassInfo> seen = new ArrayList<ClassInfo>();
    List<Method> methods = new ArrayList<Method>();
    List<Info> infos = getAnnotationInfos(annotation.getName());
    for (Info info : infos) {
        if (info instanceof MethodInfo && !"<init>".equals(info.getName())) {
            MethodInfo methodInfo = (MethodInfo) info;
            ClassInfo classInfo = methodInfo.getDeclaringClass();

            if (seen.contains(classInfo))
                continue;

            seen.add(classInfo);

            try {
                Class clazz = classInfo.get();
                for (Method method : clazz.getDeclaredMethods()) {
                    if (method.isAnnotationPresent(annotation)) {
                        methods.add(method);
                    }
                }
            } catch (Throwable e) {
                LOG.error("Error loading class [{}]", classInfo.getName(), e);
                classesNotLoaded.add(classInfo.getName());
            }
        }
    }
    return methods;
}

From source file:grails.plugin.springsecurity.web.access.intercept.AnnotationFilterInvocationDefinition.java

protected List<InterceptedUrl> findActionRoles(final Class<?> clazz) {
    List<InterceptedUrl> actionRoles = new ArrayList<InterceptedUrl>();
    for (Method method : clazz.getDeclaredMethods()) {
        Annotation annotation = findSecuredAnnotation(method);
        if (annotation != null) {
            Collection<String> values = getValue(annotation);
            if (!values.isEmpty()) {
                actionRoles.add(new InterceptedUrl(grailsUrlConverter.toUrlElement(method.getName()), values,
                        getHttpMethod(annotation)));
            }//from   w  w w .j  a va2  s. c  o m
        }
    }
    return actionRoles;
}

From source file:grails.plugin.springsecurity.web.access.intercept.AnnotationFilterInvocationDefinition.java

protected List<InterceptedUrl> findActionClosures(final Class<?> clazz) {
    List<InterceptedUrl> actionClosures = new ArrayList<InterceptedUrl>();
    for (Method method : clazz.getDeclaredMethods()) {
        grails.plugin.springsecurity.annotation.Secured annotation = method
                .getAnnotation(grails.plugin.springsecurity.annotation.Secured.class);
        if (annotation != null
                && annotation.closure() != grails.plugin.springsecurity.annotation.Secured.class) {
            actionClosures.add(new InterceptedUrl(grailsUrlConverter.toUrlElement(method.getName()),
                    annotation.closure(), getHttpMethod(annotation)));
        }/*from  w ww  . j  av  a2s .c o m*/
    }
    return actionClosures;
}