下面列出了java.lang.reflect.AnnotatedElement#isAnnotationPresent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean canConvert ( final Class<?> from, final Class<?> to, final AnnotatedElement annotatedElement )
{
if ( ! ( from.equals ( String.class ) || to.equals ( String.class ) ) )
{
// either one must be string
return false;
}
if ( annotatedElement != null && annotatedElement.isAnnotationPresent ( Stringify.class ) )
{
return true;
}
if ( to.isAnnotationPresent ( Stringify.class ) )
{
return true;
}
return false;
}
/**
* 指定された注釈付けされた要素を利用してこのインスタンスを初期化します。
* もし{@link ViewIdConstraint}が付加されていない場合、このメソッドは直ちに処理を終了します。
* @param annotated {@link ViewIdConstraint}が付加されている(と想定されている)要素
* @return 初期化した場合には<code>true</code>
*/
protected boolean initializeIfNeeded(AnnotatedElement annotated) {
if (!annotated.isAnnotationPresent(ViewIdConstraint.class)) return false;
ViewIdConstraint c = annotated.getAnnotation(ViewIdConstraint.class);
String specified = null;
// 許可パターン
specified = c.allow();
this.allowPattern = "".equals(specified) ?
DEFAULT_ALLOW_PATTERN : createPattern(specified) ;
// 拒否パターン
specified = c.except();
this.exceptPattern = "".equals(specified) ?
DEFAULT_EXCEPT_PATTERN : createPattern(specified);
this.checkRequired = ALWAYS_CHECK || (this.allowPattern != null || this.exceptPattern != null);
scope = AVAILABLE_SCOPE.contains(c.scope()) ? c.scope() : Controllers.SCOPE_SESSION;
return true;
}
public StringValidator(AnnotatedElement ae) {
required = ae.isAnnotationPresent(Required.class);
minLength = ae.isAnnotationPresent(MinLength.class)
? ae.getAnnotation(MinLength.class).value() : null;
maxLength = ae.isAnnotationPresent(MaxLength.class)
? ae.getAnnotation(MaxLength.class).value() : null;
pattern = ae.isAnnotationPresent(Pattern.class)
? java.util.regex.Pattern.compile(ae.getAnnotation(Pattern.class).value()) : null;
enums = ae.isAnnotationPresent(EnumString.class)
? new HashSet<String>(Arrays.asList(ae.getAnnotation(EnumString.class).value())) : null;
Format[] formatAnnos = ae.getAnnotationsByType(Format.class);
formats = new StringFormat[formatAnnos.length];
for (int i=0; i<formatAnnos.length; i++) {
formats[i] = newStringFormat(formatAnnos[i].value());
}
}
/**
* Convert given value to specified type. If given element annotated with {@link Use} annotation
* use {@link #getConverterForElementWithUseAnnotation(AnnotatedElement)} converter, otherwise
* if element has collection type convert collection and finally try to convert element
* using registered converters.
*/
protected Object convertValue(AnnotatedElement element, String value) {
Class<?> type = getValueType(element);
Type genericType = getValueGenericType(element);
try {
if (element.isAnnotationPresent(Use.class)) {
Converter converter = getConverterForElementWithUseAnnotation(element);
return converter.convert(value);
}
if (Collection.class.isAssignableFrom(type)) {
return manager.convert(type, getCollectionElementType(genericType), value);
}
return manager.convert(type, value);
} catch (Exception e) {
throw new PropertyLoaderException(String.format(
"Can't convert value <%s> to type <%s>", value, type), e);
}
}
@Override
public boolean canConvert ( final Class<?> from, final Class<?> to, final AnnotatedElement annotatedElement )
{
if ( !to.equals ( String.class ) )
{
return false;
}
if ( from.isAnnotationPresent ( JSON.class ) )
{
return true;
}
if ( annotatedElement != null && annotatedElement.isAnnotationPresent ( JSON.class ) )
{
return true;
}
return false;
}
/**
* Get the default value for given element. Annotation {@link Property} should
* be present.
*/
protected String getPropertyDefaultValue(AnnotatedElement element) {
if (element.isAnnotationPresent(DefaultValue.class)) {
return element.getAnnotation(DefaultValue.class).value();
}
return null;
}
private boolean entityForbidden(AnnotatedElement entity,
HttpServletRequest request) {
return entity.isAnnotationPresent(DenyAll.class) || (!entity
.isAnnotationPresent(AnonymousAllowed.class)
&& !roleAllowed(entity.getAnnotation(RolesAllowed.class),
request));
}
static RLPDecoder getAnnotatedRLPDecoder(AnnotatedElement element) {
if (!element.isAnnotationPresent(RLPDecoding.class)) {
return null;
}
Class<? extends RLPDecoder> decoder = element.getAnnotation(RLPDecoding.class).value();
if (decoder == RLPDecoder.None.class) {
return null;
}
return newInstance(decoder);
}
/**
* Validates the given value for the given expected method return value
* type.
*
* @param value
* the value to validate
* @param annotatedElement
* the entity to be type checked
* @return error message when the value is null while the expected type does
* not explicitly allow null, or null meaning the value is OK.
*/
public String checkValueForAnnotatedElement(Object value,
AnnotatedElement annotatedElement) {
if (annotatedElement.isAnnotationPresent(Nullable.class)) {
return null;
}
if (annotatedElement instanceof Method) {
return checkValueForType(value,
((Method) annotatedElement).getGenericReturnType());
}
return null;
}
@SafeVarargs
private static boolean isAnnotationPresent(AnnotatedElement element, // NOPMD
Class<? extends Annotation>... annotations) {
for (Class<?> annotation : annotations) {
if (annotation != null && element.isAnnotationPresent(annotation.asSubclass(Annotation.class))) {
return true;
}
}
return false;
}
private static <T extends Annotation> List<Label> getLabels(final AnnotatedElement element,
final Class<T> annotation,
final Function<T, List<Label>> extractor) {
return element.isAnnotationPresent(annotation)
? extractor.apply(element.getAnnotation(annotation))
: Collections.emptyList();
}
private Class injectionAnnotation(AnnotatedElement element) {
if (element.isAnnotationPresent(Named.class))
return Named.class;
else if (element.isAnnotationPresent(Resource.class))
return Resource.class;
else if (element.isAnnotationPresent(Inject.class))
return Inject.class;
else
return null;
}
private boolean isEnableFeature(AnnotatedElement annotatedElement) {
return (annotatedElement.isAnnotationPresent(EnableFeatures.class) || annotatedElement.isAnnotationPresent(EnableFeature.class));
}
public BooleanValidator(AnnotatedElement ae) {
required = ae.isAnnotationPresent(Required.class);
}
private boolean isDeprecated(AnnotatedElement annotated) {
return annotated.isAnnotationPresent(Deprecated.class);
}
@Override
public Object getValue(final @NotNull Object adaptable, final String name, final @NotNull Type type, final @NotNull AnnotatedElement element,
final @NotNull DisposalCallbackRegistry callbackRegistry) {
// only class types are supported
if (!(type instanceof Class<?>)) {
return null;
}
Class<?> requestedClass = (Class<?>) type;
// validate input
if (adaptable instanceof SlingHttpServletRequest) {
SlingHttpServletRequest request = (SlingHttpServletRequest) adaptable;
if (requestedClass.equals(ResourceResolver.class)) {
return request.getResourceResolver();
}
if (requestedClass.equals(Resource.class) && element.isAnnotationPresent(SlingObject.class)) {
return request.getResource();
}
if (requestedClass.equals(SlingHttpServletRequest.class) || requestedClass.equals(HttpServletRequest.class)) {
return request;
}
if (requestedClass.equals(SlingHttpServletResponse.class)
|| requestedClass.equals(HttpServletResponse.class)) {
return getSlingHttpServletResponse(request);
}
if (requestedClass.equals(SlingScriptHelper.class)) {
return getSlingScriptHelper(request);
}
} else if (adaptable instanceof ResourceResolver) {
ResourceResolver resourceResolver = (ResourceResolver) adaptable;
if (requestedClass.equals(ResourceResolver.class)) {
return resourceResolver;
}
} else if (adaptable instanceof Resource) {
Resource resource = (Resource) adaptable;
if (requestedClass.equals(ResourceResolver.class)) {
return resource.getResourceResolver();
}
if (requestedClass.equals(Resource.class) && element.isAnnotationPresent(SlingObject.class)) {
return resource;
}
}
return null;
}
private static boolean getHasDefaultValue(AnnotatedElement element, InjectAnnotationProcessor2 annotationProcessor) {
if (annotationProcessor != null) {
return annotationProcessor.hasDefault();
}
return element.isAnnotationPresent(Default.class);
}
/**
* Determine if an annotation of the specified {@code annotationType}
* is <em>present</em> on the supplied {@link AnnotatedElement} or
* within the annotation hierarchy <em>above</em> the specified element.
* <p>If this method returns {@code true}, then {@link #getMergedAnnotationAttributes}
* will return a non-null value.
* <p>This method follows <em>get semantics</em> as described in the
* {@linkplain AnnotatedElementUtils class-level javadoc}.
* @param element the annotated element
* @param annotationType the annotation type to find
* @return {@code true} if a matching annotation is present
* @since 4.2.3
* @see #hasAnnotation(AnnotatedElement, Class)
*/
public static boolean isAnnotated(AnnotatedElement element, Class<? extends Annotation> annotationType) {
// Shortcut: directly present on the element, with no merging needed?
if (AnnotationFilter.PLAIN.matches(annotationType) ||
AnnotationsScanner.hasPlainJavaAnnotationsOnly(element)) {
return element.isAnnotationPresent(annotationType);
}
// Exhaustive retrieval of merged annotations...
return getAnnotations(element).isPresent(annotationType);
}
/**
* Determine if an annotation of the specified {@code annotationType}
* is <em>available</em> on the supplied {@link AnnotatedElement} or
* within the annotation hierarchy <em>above</em> the specified element.
* <p>If this method returns {@code true}, then {@link #findMergedAnnotationAttributes}
* will return a non-null value.
* <p>This method follows <em>find semantics</em> as described in the
* {@linkplain AnnotatedElementUtils class-level javadoc}.
* @param element the annotated element
* @param annotationType the annotation type to find
* @return {@code true} if a matching annotation is present
* @since 4.3
* @see #isAnnotated(AnnotatedElement, Class)
*/
public static boolean hasAnnotation(AnnotatedElement element, Class<? extends Annotation> annotationType) {
// Shortcut: directly present on the element, with no merging needed?
if (AnnotationFilter.PLAIN.matches(annotationType) ||
AnnotationsScanner.hasPlainJavaAnnotationsOnly(element)) {
return element.isAnnotationPresent(annotationType);
}
// Exhaustive retrieval of merged annotations...
return findAnnotations(element).isPresent(annotationType);
}
/**
* Determine if an annotation of the specified {@code annotationType}
* is <em>available</em> on the supplied {@link AnnotatedElement} or
* within the annotation hierarchy <em>above</em> the specified element.
* <p>If this method returns {@code true}, then {@link #findMergedAnnotationAttributes}
* will return a non-null value.
* <p>This method follows <em>find semantics</em> as described in the
* {@linkplain AnnotatedElementUtils class-level javadoc}.
* @param element the annotated element
* @param annotationType the annotation type to find
* @return {@code true} if a matching annotation is present
* @since 4.3
* @see #isAnnotated(AnnotatedElement, Class)
*/
public static boolean hasAnnotation(AnnotatedElement element, Class<? extends Annotation> annotationType) {
// Shortcut: directly present on the element, with no processing needed?
if (element.isAnnotationPresent(annotationType)) {
return true;
}
return Boolean.TRUE.equals(searchWithFindSemantics(element, annotationType, null, alwaysTrueAnnotationProcessor));
}