use of java.lang.reflect.AnnotatedElement in project spring-framework by spring-projects.
the class MultipleComposedAnnotationsOnSingleAnnotatedElementTests method getComposedPlusLocalAnnotationsOnMethod.
@Test
public void getComposedPlusLocalAnnotationsOnMethod() throws Exception {
AnnotatedElement element = getClass().getDeclaredMethod("composedPlusLocalCachesMethod");
assertGetAllMergedAnnotationsBehavior(element);
}
use of java.lang.reflect.AnnotatedElement in project spring-framework by spring-projects.
the class AbstractExpressionEvaluatingCondition method evaluateExpression.
private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext, Class<A> annotationType, ExtensionContext extensionContext) {
AnnotatedElement element = extensionContext.getElement().get();
GenericApplicationContext gac = null;
ApplicationContext applicationContext;
if (loadContext) {
applicationContext = SpringExtension.getApplicationContext(extensionContext);
} else {
gac = new GenericApplicationContext();
gac.refresh();
applicationContext = gac;
}
if (!(applicationContext instanceof ConfigurableApplicationContext)) {
if (logger.isWarnEnabled()) {
String contextType = (applicationContext != null ? applicationContext.getClass().getName() : "null");
logger.warn(String.format("@%s(\"%s\") could not be evaluated on [%s] since the test " + "ApplicationContext [%s] is not a ConfigurableApplicationContext", annotationType.getSimpleName(), expression, element, contextType));
}
return false;
}
ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
BeanExpressionResolver expressionResolver = configurableBeanFactory.getBeanExpressionResolver();
BeanExpressionContext beanExpressionContext = new BeanExpressionContext(configurableBeanFactory, null);
Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression), beanExpressionContext);
if (gac != null) {
gac.close();
}
if (result instanceof Boolean) {
return ((Boolean) result).booleanValue();
} else if (result instanceof String) {
String str = ((String) result).trim().toLowerCase();
if ("true".equals(str)) {
return true;
}
Assert.state("false".equals(str), () -> String.format("@%s(\"%s\") on %s must evaluate to \"true\" or \"false\", not \"%s\"", annotationType.getSimpleName(), expression, element, result));
return false;
} else {
String message = String.format("@%s(\"%s\") on %s must evaluate to a String or a Boolean, not %s", annotationType.getSimpleName(), expression, element, (result != null ? result.getClass().getName() : "null"));
throw new IllegalStateException(message);
}
}
use of java.lang.reflect.AnnotatedElement in project spring-framework by spring-projects.
the class AbstractExpressionEvaluatingCondition method evaluateAnnotation.
/**
* Evaluate the expression configured via the supplied annotation type on
* the {@link AnnotatedElement} for the supplied {@link ExtensionContext}.
*
* @param annotationType the type of annotation to process
* @param expressionExtractor a function that extracts the expression from
* the annotation
* @param reasonExtractor a function that extracts the reason from the
* annotation
* @param loadContextExtractor a function that extracts the {@code loadContext}
* flag from the annotation
* @param enabledOnTrue indicates whether the returned {@code ConditionEvaluationResult}
* should be {@link ConditionEvaluationResult#enabled enabled} if the expression
* evaluates to {@code true}
* @param context the {@code ExtensionContext}
* @return {@link ConditionEvaluationResult#enabled enabled} if the container
* or test should be enabled; otherwise {@link ConditionEvaluationResult#disabled disabled}
*/
protected <A extends Annotation> ConditionEvaluationResult evaluateAnnotation(Class<A> annotationType, Function<A, String> expressionExtractor, Function<A, String> reasonExtractor, Function<A, Boolean> loadContextExtractor, boolean enabledOnTrue, ExtensionContext context) {
AnnotatedElement element = context.getElement().get();
Optional<A> annotation = findMergedAnnotation(element, annotationType);
if (!annotation.isPresent()) {
String reason = String.format("%s is enabled since @%s is not present", element, annotationType.getSimpleName());
if (logger.isDebugEnabled()) {
logger.debug(reason);
}
return ConditionEvaluationResult.enabled(reason);
}
// @formatter:off
String expression = annotation.map(expressionExtractor).map(String::trim).filter(StringUtils::hasLength).orElseThrow(() -> new IllegalStateException(String.format("The expression in @%s on [%s] must not be blank", annotationType.getSimpleName(), element)));
// @formatter:on
boolean loadContext = annotation.map(loadContextExtractor).get();
boolean evaluatedToTrue = evaluateExpression(expression, loadContext, annotationType, context);
if (evaluatedToTrue) {
String adjective = (enabledOnTrue ? "enabled" : "disabled");
String reason = annotation.map(reasonExtractor).filter(StringUtils::hasText).orElseGet(() -> String.format("%s is %s because @%s(\"%s\") evaluated to true", element, adjective, annotationType.getSimpleName(), expression));
if (logger.isInfoEnabled()) {
logger.info(reason);
}
return (enabledOnTrue ? ConditionEvaluationResult.enabled(reason) : ConditionEvaluationResult.disabled(reason));
} else {
String adjective = (enabledOnTrue ? "disabled" : "enabled");
String reason = String.format("%s is %s because @%s(\"%s\") did not evaluate to true", element, adjective, annotationType.getSimpleName(), expression);
if (logger.isDebugEnabled()) {
logger.debug(reason);
}
return (enabledOnTrue ? ConditionEvaluationResult.disabled(reason) : ConditionEvaluationResult.enabled(reason));
}
}
use of java.lang.reflect.AnnotatedElement in project cloudstack by apache.
the class CSJacksonAnnotationIntrospector method findSerializer.
@Override
public Object findSerializer(Annotated a) {
AnnotatedElement ae = a.getAnnotated();
Url an = ae.getAnnotation(Url.class);
if (an == null) {
return null;
}
if (an.type() == String.class) {
return new UriSerializer(an);
} else if (an.type() == List.class) {
return new UrisSerializer(an);
}
throw new UnsupportedOperationException("Unsupported type " + an.type());
}
use of java.lang.reflect.AnnotatedElement in project wildfly by wildfly.
the class WSRefAnnotationProcessor method processRef.
private static void processRef(final DeploymentUnit unit, final String type, final WSRefAnnotationWrapper annotation, final ClassInfo classInfo, final InjectionTarget injectionTarget, final String bindingName) throws DeploymentUnitProcessingException {
final EEModuleDescription moduleDescription = unit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
final AnnotatedElement target = createAnnotatedElement(unit, classInfo, injectionTarget);
final String componentClassName = classInfo.name().toString();
final Map<String, String> bindingMap = new HashMap<String, String>();
boolean isEJB = false;
for (final ComponentDescription componentDescription : moduleDescription.getComponentsByClassName(componentClassName)) {
if (componentDescription instanceof SessionBeanComponentDescription) {
isEJB = true;
bindingMap.put(componentDescription.getComponentName() + "/" + bindingName, bindingName);
}
}
if (!isEJB) {
bindingMap.put(bindingName, bindingName);
}
for (String refKey : bindingMap.keySet()) {
String refName = bindingMap.get(refKey);
ManagedReferenceFactory factory = WebServiceReferences.createWebServiceFactory(unit, type, annotation, target, refName, refKey);
final EEModuleClassDescription classDescription = moduleDescription.addOrGetLocalClassDescription(classInfo.name().toString());
// Create the binding from whence our injection comes.
final InjectionSource serviceRefSource = new FixedInjectionSource(factory, factory);
final BindingConfiguration bindingConfiguration = new BindingConfiguration(refName, serviceRefSource);
classDescription.getBindingConfigurations().add(bindingConfiguration);
// our injection comes from the local lookup, no matter what.
final ResourceInjectionConfiguration injectionConfiguration = injectionTarget != null ? new ResourceInjectionConfiguration(injectionTarget, new LookupInjectionSource(refName)) : null;
if (injectionConfiguration != null) {
classDescription.addResourceInjection(injectionConfiguration);
}
}
}
Aggregations