Here you can find the source of getAnnotationValue(final A annotation, final Function valueRetriever, final V defaultValue)
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. |
null
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)
//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; } }