Java Reflection Annotation getAnnotationValue(final A annotation, final Function valueRetriever, final V defaultValue)

Here you can find the source of getAnnotationValue(final A annotation, final Function valueRetriever, final V defaultValue)

Description

Returns the value of the given annotation using a Function.

License

Open Source License

Parameter

Parameter Description
A the type of annotation to analyze
V the type of value to return
annotation the annotation to analyze
valueRetriever the retrieval Function to apply if the given Annotation is not null.
defaultValue the default value to return if the annotation is null or if the given Function did not return any value.

Return

the annotation value or null if it is empty or null.

Declaration

public static <A extends Annotation, V> V getAnnotationValue(final A annotation,
        final Function<A, V> valueRetriever, final V defaultValue) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 Red Hat. All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which accompanies this
 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Red Hat - Initial Contribution
 *******************************************************************************/

import java.lang.annotation.Annotation;

import java.util.function.Function;

public class Main {
    /**/*from   w ww  .  j  av  a 2 s .co m*/
     * Returns the value of the given annotation using a Function.
     * 
     * @param <A> the type of annotation to analyze
     * @param <V> the type of value to return
     * @param annotation the annotation to analyze
     * @param valueRetriever the retrieval {@link Function} to apply if the given {@link Annotation}
     *        is not null.
     * @param defaultValue the default value to return if the annotation is null or if the given
     *        Function did not return any value.
     * @return the annotation value or <code>null</code> if it is empty or null.
     */
    public static <A extends Annotation, V> V getAnnotationValue(final A annotation,
            final Function<A, V> valueRetriever, final V defaultValue) {
        if (annotation != null) {
            final V value = valueRetriever.apply(annotation);
            // do not allow empty String (if applicable)
            if (defaultValue.getClass().equals(String.class) && !String.class.cast(value).isEmpty()) {
                return value;
            } else if (!defaultValue.getClass().equals(String.class) && value != null) {
                return value;
            }
        }
        return defaultValue;
    }
}

Related

  1. getAnnotationValue(@Nonnull Class cls, @Nonnull Class annotation, V def)
  2. getAnnotationValue(Annotation annotation, String attributeName)
  3. getAnnotationValue(Annotation annotation, String value, Class expectedType)
  4. getAnnotationValue(Annotation annotation, String valueName)
  5. getAnnotationValue(Class anno, Field field, String paramName)
  6. getAnnotationValue(Object aObj, String aValue)
  7. getAnnotationValue(Object obj, Class annotation, String field)
  8. getAnnotationWithinHierarchy(Class fsmClass, Class aggregateClass)
  9. getAnnotationWithMetaAnnotation(AnnotatedElement annotatedElement, Class metaAnnotationType)