Java Code Examples for java.lang.reflect.AnnotatedElement#isAnnotationPresent()

The following examples show how to use java.lang.reflect.AnnotatedElement#isAnnotationPresent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonToStringConverter.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: PropertyLoader.java    From properties with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: StringValidator.java    From jsonstream with Apache License 2.0 6 votes vote down vote up
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());
    }
}
 
Example 4
Source File: StringifyConverter.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
@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;
}
 
Example 5
Source File: ViewTransitionKeeper.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
/**
 * 指定された注釈付けされた要素を利用してこのインスタンスを初期化します。
 * もし{@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;
}
 
Example 6
Source File: AnnotationReader.java    From cxf with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 7
Source File: CompanionDependencyInjector.java    From cuba with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: Allure1Utils.java    From allure-java with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 9
Source File: ExplicitNullableTypeChecker.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: RLPUtils.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: VaadinConnectAccessChecker.java    From flow with Apache License 2.0 5 votes vote down vote up
private boolean entityForbidden(AnnotatedElement entity,
        HttpServletRequest request) {
    return entity.isAnnotationPresent(DenyAll.class) || (!entity
            .isAnnotationPresent(AnonymousAllowed.class)
            && !roleAllowed(entity.getAnnotation(RolesAllowed.class),
                    request));
}
 
Example 12
Source File: PropertyLoader.java    From properties with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: BooleanValidator.java    From jsonstream with Apache License 2.0 4 votes vote down vote up
public BooleanValidator(AnnotatedElement ae) {
    required = ae.isAnnotationPresent(Required.class);
}
 
Example 14
Source File: DeprecatedFieldTest.java    From travelguide with Apache License 2.0 4 votes vote down vote up
private boolean isDeprecated(AnnotatedElement annotated) {
  return annotated.isAnnotationPresent(Deprecated.class);
}
 
Example 15
Source File: SlingObjectInjector.java    From sling-org-apache-sling-models-impl with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 16
Source File: KeycloakContainerFeaturesController.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private boolean isEnableFeature(AnnotatedElement annotatedElement) {
    return (annotatedElement.isAnnotationPresent(EnableFeatures.class) || annotatedElement.isAnnotationPresent(EnableFeature.class));
}
 
Example 17
Source File: AbstractInjectableElement.java    From sling-org-apache-sling-models-impl with Apache License 2.0 4 votes vote down vote up
private static boolean getHasDefaultValue(AnnotatedElement element, InjectAnnotationProcessor2 annotationProcessor) {
    if (annotationProcessor != null) {
        return annotationProcessor.hasDefault();
    }
    return element.isAnnotationPresent(Default.class);
}
 
Example 18
Source File: AnnotatedElementUtils.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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);
}
 
Example 19
Source File: AnnotatedElementUtils.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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));
}
 
Example 20
Source File: AnnotatedElementUtils.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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);
}