Java Code Examples for io.micronaut.aop.MethodInvocationContext#getParameterValues()
The following examples show how to use
io.micronaut.aop.MethodInvocationContext#getParameterValues() .
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: DefaultDeleteOneReactiveInterceptor.java From micronaut-data with Apache License 2.0 | 6 votes |
@Override public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) { Object[] parameterValues = context.getParameterValues(); if (parameterValues.length == 1) { Class<Object> rootEntity = (Class<Object>) getRequiredRootEntity(context); Object o = parameterValues[0]; if (o != null) { BatchOperation<Object> batchOperation = getBatchOperation(context, rootEntity, Collections.singletonList(o)); Publisher<Number> publisher = Publishers.map(reactiveOperations.deleteAll(batchOperation), n -> convertNumberArgumentIfNecessary(n, context.getReturnType().asArgument()) ); return Publishers.convertPublisher( publisher, context.getReturnType().getType() ); } else { throw new IllegalArgumentException("Entity to delete cannot be null"); } } else { throw new IllegalStateException("Expected exactly one argument"); } }
Example 2
Source File: DefaultSaveAllInterceptor.java From micronaut-data with Apache License 2.0 | 6 votes |
@Override public Iterable<R> intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, Iterable<R>> context) { Object[] parameterValues = context.getParameterValues(); if (ArrayUtils.isNotEmpty(parameterValues) && parameterValues[0] instanceof Iterable) { //noinspection unchecked Iterable<R> iterable = (Iterable<R>) parameterValues[0]; Iterable<R> rs = operations.persistAll(getBatchOperation(context, iterable)); ReturnType<Iterable<R>> rt = context.getReturnType(); if (!rt.getType().isInstance(rs)) { return ConversionService.SHARED.convert(rs, rt.asArgument()) .orElseThrow(() -> new IllegalStateException("Unsupported iterable return type: " + rs.getClass())); } return rs; } else { throw new IllegalArgumentException("First argument should be an iterable"); } }
Example 3
Source File: DefaultDeleteOneAsyncInterceptor.java From micronaut-data with Apache License 2.0 | 6 votes |
@Override public CompletionStage<Number> intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, CompletionStage<Number>> context) { Object[] parameterValues = context.getParameterValues(); if (parameterValues.length == 1) { Object o = parameterValues[0]; if (o != null) { BatchOperation<Object> batchOperation = getBatchOperation(context, Collections.singletonList(o)); return asyncDatastoreOperations.deleteAll(batchOperation) .thenApply(n -> convertNumberArgumentIfNecessary(n, context.getReturnType().asArgument())); } else { throw new IllegalArgumentException("Entity to delete cannot be null"); } } else { throw new IllegalStateException("Expected exactly one argument"); } }
Example 4
Source File: AbstractQueryInterceptor.java From micronaut-data with Apache License 2.0 | 5 votes |
/** * Validates null arguments ensuring no argument is null unless declared so. * @param context The context */ protected final void validateNullArguments(MethodInvocationContext<T, R> context) { Object[] parameterValues = context.getParameterValues(); for (int i = 0; i < parameterValues.length; i++) { Object o = parameterValues[i]; if (o == null && !context.getArguments()[i].isNullable()) { throw new IllegalArgumentException("Argument [" + context.getArguments()[i].getName() + "] value is null and the method parameter is not declared as nullable"); } } }
Example 5
Source File: DefaultFindByIdReactiveInterceptor.java From micronaut-data with Apache License 2.0 | 5 votes |
@Override public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) { Class<?> rootEntity = getRequiredRootEntity(context); Object id = context.getParameterValues()[0]; if (!(id instanceof Serializable)) { throw new IllegalArgumentException("Entity IDs must be serializable!"); } Publisher<Object> publisher = reactiveOperations.findOne((Class<Object>) rootEntity, (Serializable) id); return Publishers.convertPublisher(publisher, context.getReturnType().getType()); }
Example 6
Source File: DefaultSaveAllReactiveInterceptor.java From micronaut-data with Apache License 2.0 | 5 votes |
@Override public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) { Object[] parameterValues = context.getParameterValues(); if (ArrayUtils.isNotEmpty(parameterValues) && parameterValues[0] instanceof Iterable) { //noinspection unchecked BatchOperation<Object> batchOperation = getBatchOperation(context, (Iterable<Object>) parameterValues[0]); Publisher<Object> publisher = reactiveOperations.persistAll(batchOperation); return Publishers.convertPublisher(publisher, context.getReturnType().getType()); } else { throw new IllegalArgumentException("First argument should be an iterable"); } }
Example 7
Source File: DefaultFindByIdInterceptor.java From micronaut-data with Apache License 2.0 | 5 votes |
@Override public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, Object> context) { Class<?> rootEntity = getRequiredRootEntity(context); Object id = context.getParameterValues()[0]; if (!(id instanceof Serializable)) { throw new IllegalArgumentException("Entity IDs must be serializable!"); } return operations.findOne(rootEntity, (Serializable) id); }
Example 8
Source File: DefaultFindByIdAsyncInterceptor.java From micronaut-data with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public CompletionStage<Object> intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, CompletionStage<Object>> context) { Class<?> rootEntity = getRequiredRootEntity(context); Object id = context.getParameterValues()[0]; if (!(id instanceof Serializable)) { throw new IllegalArgumentException("Entity IDs must be serializable!"); } return asyncDatastoreOperations.findOne((Class<Object>) rootEntity, (Serializable) id); }
Example 9
Source File: DefaultSaveAllAsyncInterceptor.java From micronaut-data with Apache License 2.0 | 5 votes |
@Override public CompletionStage<Iterable<Object>> intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, CompletionStage<Iterable<Object>>> context) { Object[] parameterValues = context.getParameterValues(); if (ArrayUtils.isNotEmpty(parameterValues) && parameterValues[0] instanceof Iterable) { //noinspection unchecked BatchOperation<Object> batchOperation = getBatchOperation(context, (Iterable<Object>) parameterValues[0]); return asyncDatastoreOperations.persistAll(batchOperation); } else { throw new IllegalArgumentException("First argument should be an iterable"); } }
Example 10
Source File: CountSpecificationInterceptor.java From micronaut-data with Apache License 2.0 | 5 votes |
@Override public Number intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Number> context) { final Object parameterValue = context.getParameterValues()[0]; if (parameterValue instanceof Specification) { Specification specification = (Specification) parameterValue; final EntityManager entityManager = jpaOperations.getCurrentEntityManager(); final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class); final Root<?> root = query.from(getRequiredRootEntity(context)); final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder); query.where(predicate); if (query.isDistinct()) { query.select(criteriaBuilder.countDistinct(root)); } else { query.select(criteriaBuilder.count(root)); } query.orderBy(Collections.emptyList()); final TypedQuery<Long> typedQuery = entityManager.createQuery(query); final Long result = typedQuery.getSingleResult(); final ReturnType<Number> rt = context.getReturnType(); final Class<Number> returnType = rt.getType(); if (returnType.isInstance(result)) { return result; } else { return ConversionService.SHARED.convertRequired( result, rt.asArgument() ); } } else { throw new IllegalArgumentException("Argument must be an instance of: " + Specification.class); } }
Example 11
Source File: FindOneSpecificationInterceptor.java From micronaut-data with Apache License 2.0 | 5 votes |
@Override public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) { final Object parameterValue = context.getParameterValues()[0]; if (parameterValue instanceof Specification) { Specification specification = (Specification) parameterValue; final EntityManager entityManager = jpaOperations.getCurrentEntityManager(); final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery<Object> query = criteriaBuilder.createQuery((Class<Object>) getRequiredRootEntity(context)); final Root<Object> root = query.from((Class<Object>) getRequiredRootEntity(context)); final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder); query.where(predicate); query.select(root); final TypedQuery<?> typedQuery = entityManager.createQuery(query); try { final Object result = typedQuery.getSingleResult(); final ReturnType<?> rt = context.getReturnType(); final Class<?> returnType = rt.getType(); if (returnType.isInstance(result)) { return result; } else { return ConversionService.SHARED.convertRequired( result, rt.asArgument() ); } } catch (NoResultException e) { if (context.isNullable()) { return null; } else { throw new EmptyResultDataAccessException(1); } } } else { throw new IllegalArgumentException("Argument must be an instance of: " + Specification.class); } }
Example 12
Source File: DefaultDeleteOneInterceptor.java From micronaut-data with Apache License 2.0 | 4 votes |
@Override public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, Object> context) { Object[] parameterValues = context.getParameterValues(); if (parameterValues.length == 1) { Object o = parameterValues[0]; if (context.hasAnnotation(Query.class)) { PreparedQuery<?, Number> preparedQuery = (PreparedQuery<?, Number>) prepareQuery(methodKey, context); final Class<?> rootEntity = preparedQuery.getRootEntity(); if (rootEntity.isInstance(o)) { final RuntimePersistentEntity<?> entity = operations.getEntity(rootEntity); final RuntimePersistentProperty<?> identity = entity.getIdentity(); if (identity != null) { if (identity instanceof Embedded) { final BeanProperty idProp = identity.getProperty(); final Object idValue = idProp.get(o); if (idValue == null) { throw new IllegalStateException("Cannot delete an entity with null ID: " + o); } preparedQuery.getParameterArray()[0] = idValue; } final Number result = operations.executeDelete(preparedQuery).orElse(0); final Class<Object> returnType = context.getReturnType().getType(); if (returnType.equals(rootEntity)) { if (result.longValue() > 0) { return o; } else { return null; } } else if (Number.class.isAssignableFrom(returnType)) { return ConversionService.SHARED.convertRequired(result, returnType); } else { return result; } } else { throw new IllegalStateException("Cannot delete an entity which defines no ID: " + rootEntity.getName()); } } else { if (o == null) { throw new IllegalArgumentException("Entity to delete cannot be null"); } else { throw new IllegalArgumentException("Entity argument must be an instance of " + rootEntity.getName()); } } } else { BatchOperation<Object> batchOperation = getBatchOperation(context, Collections.singletonList(o)); if (o != null) { operations.deleteAll(batchOperation); } else { throw new IllegalArgumentException("Entity to delete cannot be null"); } } } else { throw new IllegalStateException("Expected exactly one argument"); } return null; }
Example 13
Source File: FindPageSpecificationInterceptor.java From micronaut-data with Apache License 2.0 | 4 votes |
@Override public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) { final Object[] parameterValues = context.getParameterValues(); if (parameterValues.length != 2) { throw new IllegalStateException("Expected exactly 2 arguments to method"); } final Object parameterValue = parameterValues[0]; final Object pageableObject = parameterValues[1]; if (parameterValue instanceof Specification) { Specification specification = (Specification) parameterValue; final EntityManager entityManager = jpaOperations.getCurrentEntityManager(); final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); final CriteriaQuery<Object> query = criteriaBuilder.createQuery((Class<Object>) getRequiredRootEntity(context)); final Root<Object> root = query.from((Class<Object>) getRequiredRootEntity(context)); final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder); query.where(predicate); query.select(root); if (pageableObject instanceof Pageable) { Pageable pageable = (Pageable) pageableObject; final Sort sort = pageable.getSort(); if (sort.isSorted()) { final List<Order> orders = QueryUtils.toOrders(sort, root, criteriaBuilder); query.orderBy(orders); } final TypedQuery<Object> typedQuery = entityManager .createQuery(query); if (pageable.isUnpaged()) { return new PageImpl<>( typedQuery .getResultList() ); } else { typedQuery.setFirstResult((int) pageable.getOffset()); typedQuery.setMaxResults(pageable.getPageSize()); final List<Object> results = typedQuery.getResultList(); final CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class); final Root<?> countRoot = countQuery.from(getRequiredRootEntity(context)); final Predicate countPredicate = specification.toPredicate(root, query, criteriaBuilder); countQuery.where(countPredicate); countQuery.select(criteriaBuilder.count(countRoot)); return new PageImpl<>( results, pageable, entityManager.createQuery(countQuery).getSingleResult() ); } } else { return new PageImpl<>( entityManager .createQuery(query) .getResultList() ); } } else { throw new IllegalArgumentException("Argument must be an instance of: " + Specification.class); } }