yangxu998 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

new class ReflectionUtils with following methods #656

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi guys,

at the weekend I watched some videos and slides on ur framework and was 
inspired to contribute.

I have my own little ReflectionHelper class and like to discuss and maybe if it 
will be accepted, to integrate it.

I use reflection on methods and have some little methods:

    /**
     * try to retrieve a special annotation from the method
     * <p/>
     * in case that it is not found, it returns <b>null</b>
     * 
     * @param method
     * @param annotation
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends Annotation> T getAnnotation(final Method method, final Class<T> annotation) {

        Assert.assertTrue(ReflectionUtils.isAnnotationPresent(method, annotation), "annotation " + annotation
                + " should be present");

        return (T) getAnnotation(method.getAnnotations(), annotation);
    }

    /**
     * checks whether a method has this annotation set
     * 
     * @param method
     * @param annotation
     * @return
     */
    public static boolean isAnnotationPresent(final Method method, final Class<? extends Annotation> annotation) {
        return isAnnotationFound(method.getAnnotations(), annotation);
    }

    /**
     * retrieves specific annotation from set of annotations
     * 
     * @param annotations - set of annotations
     * @param annotation - specific
     * @return
     */
    private static Annotation getAnnotation(final Annotation[] annotations, final Class<? extends Annotation> annotation) {
        Annotation result = null;
        for (int i = 0; i < annotations.length && (result == null); i++) {
            if (annotations[i].annotationType() == annotation) {
                return annotations[i];
            }
        }
        return result;
    }

    /**
     * simply checks the existence of a special type of annotation in a set of many
     * 
     * @param annotations - set of annotations
     * @param annotation - specific
     * @return
     */
    private static boolean isAnnotationFound(final Annotation[] annotations,
            final Class<? extends Annotation> annotation) {

        return getAnnotation(annotations, annotation) == null ? false : true;
    }

what are ur thought on this?

Original issue reported on code.google.com by lkw...@googlemail.com on 11 Jul 2011 at 9:30

GoogleCodeExporter commented 9 years ago
What does this add on top of the methods

  public <T extends Annotation> T getAnnotation(Class<T> annotationClass)

and

  public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)

that are already on the Method class?

Original comment by ian.b.ro...@gmail.com on 11 Jul 2011 at 1:44

GoogleCodeExporter commented 9 years ago
nothing, I did not knew that before.

I hoped so, that I reduce my code base through discussion.

thx.

So issue has no relevance anymore.

Original comment by lkw...@googlemail.com on 11 Jul 2011 at 2:51

GoogleCodeExporter commented 9 years ago

Original comment by cgdec...@gmail.com on 11 Jul 2011 at 4:41

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09