use of jakarta.enterprise.inject.spi.AnnotatedMethod in project core by weld.
the class QualifierInstance method createValues.
private static Map<String, Object> createValues(final Annotation instance, final MetaAnnotationStore store) {
final Class<? extends Annotation> annotationClass = instance.annotationType();
final QualifierModel<? extends Annotation> model = store.getBindingTypeModel(annotationClass);
if (model.getAnnotatedAnnotation().getMethods().size() == 0) {
return Collections.emptyMap();
}
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
for (final AnnotatedMethod<?> method : model.getAnnotatedAnnotation().getMethods()) {
if (!model.getNonBindingMembers().contains(method)) {
try {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(SetAccessibleAction.of(method.getJavaMember()));
} else {
method.getJavaMember().setAccessible(true);
}
builder.put(method.getJavaMember().getName(), method.getJavaMember().invoke(instance));
} catch (IllegalArgumentException e) {
// it may happen that we are in EAR and have stored the annotation's method from different WAR class loader
// an invocation will then lead to IAE, we can re-try with reflection
Method[] methods;
builder = ImmutableMap.builder();
if (System.getSecurityManager() != null) {
methods = AccessController.doPrivileged(new GetDeclaredMethodsAction(annotationClass));
} else {
methods = annotationClass.getDeclaredMethods();
}
for (Method m : methods) {
if (m.getAnnotation(Nonbinding.class) == null) {
try {
builder.put(m.getName(), m.invoke(instance));
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
throw ResolutionLogger.LOG.cannotCreateQualifierInstanceValues(instance, Formats.formatAsStackTraceElement(method.getJavaMember()), ex);
}
}
}
} catch (InvocationTargetException | IllegalAccessException e) {
throw ResolutionLogger.LOG.cannotCreateQualifierInstanceValues(instance, Formats.formatAsStackTraceElement(method.getJavaMember()), e);
}
}
}
return builder.build();
}
use of jakarta.enterprise.inject.spi.AnnotatedMethod in project core by weld.
the class ObserverMethodConfiguratorImpl method read.
@Override
public ObserverMethodConfigurator<T> read(AnnotatedMethod<?> method) {
checkArgumentNotNull(method);
Set<AnnotatedParameter<?>> eventParameters = method.getParameters().stream().filter((p) -> p.isAnnotationPresent(Observes.class) || p.isAnnotationPresent(ObservesAsync.class)).collect(Collectors.toSet());
checkEventParams(eventParameters, method.getJavaMember());
AnnotatedParameter<?> eventParameter = eventParameters.iterator().next();
Observes observesAnnotation = eventParameter.getAnnotation(Observes.class);
if (observesAnnotation != null) {
reception(observesAnnotation.notifyObserver());
transactionPhase(observesAnnotation.during());
async(false);
} else {
reception(eventParameter.getAnnotation(ObservesAsync.class).notifyObserver());
async(true);
}
Priority priority = method.getAnnotation(Priority.class);
if (priority != null) {
priority(priority.value());
}
beanClass(eventParameter.getDeclaringCallable().getDeclaringType().getJavaClass());
observedType(eventParameter.getBaseType());
qualifiers(Configurators.getQualifiers(eventParameter));
return this;
}
use of jakarta.enterprise.inject.spi.AnnotatedMethod in project hibernate-validator by hibernate.
the class ValidationExtension method determineConstrainedMethods.
private <T> void determineConstrainedMethods(AnnotatedType<T> type, BeanDescriptor beanDescriptor, Set<AnnotatedCallable<? super T>> callables) {
List<Method> overriddenAndImplementedMethods = InheritedMethodsHelper.getAllMethods(type.getJavaClass());
for (AnnotatedMethod<? super T> annotatedMethod : type.getMethods()) {
Method method = annotatedMethod.getJavaMember();
Optional<String> correspondingProperty = getterPropertySelectionStrategyHelper.getProperty(method);
// obtain @ValidateOnExecution from the top-most method in the hierarchy
Method methodForExecutableTypeRetrieval = replaceWithOverriddenOrInterfaceMethod(method, overriddenAndImplementedMethods);
EnumSet<ExecutableType> classLevelExecutableTypes = executableTypesDefinedOnType(methodForExecutableTypeRetrieval.getDeclaringClass());
EnumSet<ExecutableType> memberLevelExecutableType = executableTypesDefinedOnMethod(methodForExecutableTypeRetrieval, correspondingProperty.isPresent());
ExecutableType currentExecutableType = correspondingProperty.isPresent() ? ExecutableType.GETTER_METHODS : ExecutableType.NON_GETTER_METHODS;
// validation occurs
if (veto(classLevelExecutableTypes, memberLevelExecutableType, currentExecutableType)) {
continue;
}
boolean needsValidation;
if (correspondingProperty.isPresent()) {
needsValidation = isGetterConstrained(beanDescriptor, method, correspondingProperty.get());
} else {
needsValidation = isNonGetterConstrained(beanDescriptor, method);
}
if (needsValidation) {
callables.add(annotatedMethod);
}
}
}
use of jakarta.enterprise.inject.spi.AnnotatedMethod in project helidon by oracle.
the class SchedulingCdiExtension method invoke.
void invoke(@Observes @Priority(PLATFORM_AFTER + 4000) @Initialized(ApplicationScoped.class) Object event, BeanManager beanManager) {
ScheduledThreadPoolSupplier scheduledThreadPoolSupplier = ScheduledThreadPoolSupplier.builder().threadNamePrefix(schedulingConfig.get("thread-name-prefix").asString().orElse("scheduled-")).config(schedulingConfig).build();
for (AnnotatedMethod<?> am : methods) {
Class<?> aClass = am.getDeclaringType().getJavaClass();
Bean<?> bean = beans.get(am);
Object beanInstance = lookup(bean, beanManager);
ScheduledExecutorService executorService = scheduledThreadPoolSupplier.get();
executors.add(executorService);
Method method = am.getJavaMember();
if (!method.trySetAccessible()) {
throw new DeploymentException(String.format("Scheduled method %s#%s is not accessible!", method.getDeclaringClass().getName(), method.getName()));
}
if (am.isAnnotationPresent(FixedRate.class) && am.isAnnotationPresent(Scheduled.class)) {
throw new DeploymentException(String.format("Scheduled method %s#%s can have only one scheduling annotation.", method.getDeclaringClass().getName(), method.getName()));
}
Config methodConfig = config.get(aClass.getName() + "." + method.getName() + ".schedule");
if (am.isAnnotationPresent(FixedRate.class)) {
FixedRate annotation = am.getAnnotation(FixedRate.class);
long initialDelay = methodConfig.get("initial-delay").asLong().orElseGet(annotation::initialDelay);
long delay = methodConfig.get("delay").asLong().orElseGet(annotation::value);
TimeUnit timeUnit = methodConfig.get("time-unit").asString().map(TimeUnit::valueOf).orElseGet(annotation::timeUnit);
Task task = Scheduling.fixedRateBuilder().executor(executorService).initialDelay(initialDelay).delay(delay).timeUnit(timeUnit).task(inv -> invokeWithOptionalParam(beanInstance, method, inv)).build();
LOGGER.log(Level.FINE, () -> String.format("Method %s#%s scheduled to be executed %s", aClass.getSimpleName(), method.getName(), task.description()));
} else if (am.isAnnotationPresent(Scheduled.class)) {
Scheduled annotation = am.getAnnotation(Scheduled.class);
String cron = methodConfig.get("cron").asString().orElseGet(() -> resolvePlaceholders(annotation.value(), config));
boolean concurrent = methodConfig.get("concurrent").asBoolean().orElseGet(annotation::concurrentExecution);
Task task = Scheduling.cronBuilder().executor(executorService).concurrentExecution(concurrent).expression(cron).task(inv -> invokeWithOptionalParam(beanInstance, method, inv)).build();
LOGGER.log(Level.FINE, () -> String.format("Method %s#%s scheduled to be executed %s", aClass.getSimpleName(), method.getName(), task.description()));
}
}
}
use of jakarta.enterprise.inject.spi.AnnotatedMethod in project core by weld.
the class InterceptionModelInitializer method initMethodDeclaredEjbInterceptors.
private void initMethodDeclaredEjbInterceptors(AnnotatedMethod<?> method) {
Method javaMethod = method.getJavaMember();
boolean excludeClassInterceptors = method.isAnnotationPresent(interceptorsApi.getExcludeClassInterceptorsAnnotationClass());
if (excludeClassInterceptors) {
builder.addMethodIgnoringGlobalInterceptors(javaMethod);
}
Class<?>[] methodDeclaredInterceptors = interceptorsApi.extractInterceptorClasses(method);
if (methodDeclaredInterceptors != null && methodDeclaredInterceptors.length > 0) {
if (Reflections.isFinal(method.getJavaMember())) {
throw new DeploymentException(BeanLogger.LOG.finalInterceptedBeanMethodNotAllowed(method, methodDeclaredInterceptors[0].getName()));
}
InterceptionType interceptionType = isTimeoutAnnotationPresentOn(method) ? InterceptionType.AROUND_TIMEOUT : InterceptionType.AROUND_INVOKE;
builder.interceptMethod(interceptionType, javaMethod, getMethodDeclaredInterceptorMetadatas(methodDeclaredInterceptors), null);
}
}
Aggregations