Search in sources :

Example 1 with MutableArgumentValue

use of io.micronaut.core.type.MutableArgumentValue in project micronaut-core by micronaut-projects.

the class AbstractInterceptorChain method getParameters.

@Override
@NonNull
public Map<String, MutableArgumentValue<?>> getParameters() {
    Map<String, MutableArgumentValue<?>> localParameters = this.parameters;
    if (localParameters == null) {
        synchronized (this) {
            // double check
            localParameters = this.parameters;
            if (localParameters == null) {
                Argument[] arguments = getArguments();
                localParameters = new LinkedHashMap<>(arguments.length);
                for (int i = 0; i < arguments.length; i++) {
                    Argument argument = arguments[i];
                    int finalIndex = i;
                    localParameters.put(argument.getName(), new MutableArgumentValue<Object>() {

                        @Override
                        public AnnotationMetadata getAnnotationMetadata() {
                            return argument.getAnnotationMetadata();
                        }

                        @Override
                        public Optional<Argument<?>> getFirstTypeVariable() {
                            return argument.getFirstTypeVariable();
                        }

                        @Override
                        public Argument[] getTypeParameters() {
                            return argument.getTypeParameters();
                        }

                        @Override
                        public Map<String, Argument<?>> getTypeVariables() {
                            return argument.getTypeVariables();
                        }

                        @NonNull
                        @Override
                        public String getName() {
                            return argument.getName();
                        }

                        @NonNull
                        @Override
                        public Class<Object> getType() {
                            return argument.getType();
                        }

                        @Override
                        public boolean equalsType(@Nullable Argument<?> other) {
                            return argument.equalsType(other);
                        }

                        @Override
                        public int typeHashCode() {
                            return argument.typeHashCode();
                        }

                        @Override
                        public Object getValue() {
                            return originalParameters[finalIndex];
                        }

                        @Override
                        public void setValue(Object value) {
                            originalParameters[finalIndex] = value;
                        }
                    });
                }
                localParameters = Collections.unmodifiableMap(localParameters);
                this.parameters = localParameters;
            }
        }
    }
    return localParameters;
}
Also used : Argument(io.micronaut.core.type.Argument) Optional(java.util.Optional) MutableArgumentValue(io.micronaut.core.type.MutableArgumentValue) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) NonNull(io.micronaut.core.annotation.NonNull) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NonNull(io.micronaut.core.annotation.NonNull)

Example 2 with MutableArgumentValue

use of io.micronaut.core.type.MutableArgumentValue in project micronaut-core by micronaut-projects.

the class ArgMutatingInterceptor method intercept.

@Override
public Object intercept(InvocationContext context) {
    Mutating m = context.synthesize(Mutating.class);
    MutableArgumentValue arg = (MutableArgumentValue) context.getParameters().get(m.value());
    if (arg != null) {
        Object value = arg.getValue();
        if (value instanceof Number) {
            arg.setValue(((Number) value).intValue() * 2);
        } else {
            arg.setValue("changed");
        }
    }
    return context.proceed();
}
Also used : MutableArgumentValue(io.micronaut.core.type.MutableArgumentValue)

Example 3 with MutableArgumentValue

use of io.micronaut.core.type.MutableArgumentValue in project micronaut-core by micronaut-projects.

the class ArgMutatingInterceptor method intercept.

@Override
public Object intercept(InvocationContext context) {
    Mutating m = context.synthesize(Mutating.class);
    MutableArgumentValue arg = (MutableArgumentValue) context.getParameters().get(m.value());
    if (arg != null) {
        Object value = arg.getValue();
        if (value instanceof Number) {
            arg.setValue(((Number) value).intValue() * 2);
        } else {
            arg.setValue("changed");
        }
    }
    return context.proceed();
}
Also used : MutableArgumentValue(io.micronaut.core.type.MutableArgumentValue)

Example 4 with MutableArgumentValue

use of io.micronaut.core.type.MutableArgumentValue in project micronaut-core by micronaut-projects.

the class DefaultValidator method validateParameters.

@NonNull
@Override
public <T> Set<ConstraintViolation<T>> validateParameters(@NonNull T object, @NonNull ExecutableMethod method, @NonNull Collection<MutableArgumentValue<?>> argumentValues, @Nullable Class<?>... groups) {
    ArgumentUtils.requireNonNull("object", object);
    ArgumentUtils.requireNonNull("method", method);
    ArgumentUtils.requireNonNull("parameterValues", argumentValues);
    final Argument[] arguments = method.getArguments();
    final int argLen = arguments.length;
    if (argLen != argumentValues.size()) {
        throw new IllegalArgumentException("The method parameter array must have exactly " + argLen + " elements.");
    }
    DefaultConstraintValidatorContext context = new DefaultConstraintValidatorContext(object, groups);
    Set overallViolations = new HashSet<>(5);
    final Path.Node node = context.addMethodNode(method);
    try {
        @SuppressWarnings("unchecked") final Class<T> rootClass = (Class<T>) object.getClass();
        validateParametersInternal(rootClass, object, argumentValues.stream().map(ArgumentValue::getValue).toArray(), arguments, argLen, context, overallViolations, node);
    } finally {
        context.removeLast();
    }
    // noinspection unchecked
    return Collections.unmodifiableSet(overallViolations);
}
Also used : Path(javax.validation.Path) Argument(io.micronaut.core.type.Argument) MutableArgumentValue(io.micronaut.core.type.MutableArgumentValue) ArgumentValue(io.micronaut.core.type.ArgumentValue) InjectionPoint(io.micronaut.inject.InjectionPoint) Constraint(javax.validation.Constraint) NonNull(io.micronaut.core.annotation.NonNull)

Example 5 with MutableArgumentValue

use of io.micronaut.core.type.MutableArgumentValue in project micronaut-core by micronaut-projects.

the class TraceInterceptor method intercept.

@Override
public Object intercept(InvocationContext context) {
    if (LOG.isTraceEnabled() && context instanceof MethodExecutionHandle) {
        MethodExecutionHandle handle = (MethodExecutionHandle) context;
        Collection<MutableArgumentValue<?>> values = context.getParameters().values();
        LOG.trace("Invoking method {}#{}(..) with arguments {}", context.getTarget().getClass().getName(), handle.getMethodName(), values.stream().map(ArgumentValue::getValue).collect(Collectors.toList()));
    }
    Object result = context.proceed();
    if (LOG.isTraceEnabled() && context instanceof MethodExecutionHandle) {
        MethodExecutionHandle handle = (MethodExecutionHandle) context;
        LOG.trace("Method {}#{}(..) returned result {}", context.getTarget().getClass().getName(), handle.getMethodName(), result);
    }
    return result;
}
Also used : MethodExecutionHandle(io.micronaut.inject.MethodExecutionHandle) MutableArgumentValue(io.micronaut.core.type.MutableArgumentValue) ArgumentValue(io.micronaut.core.type.ArgumentValue) MutableArgumentValue(io.micronaut.core.type.MutableArgumentValue)

Aggregations

MutableArgumentValue (io.micronaut.core.type.MutableArgumentValue)6 Argument (io.micronaut.core.type.Argument)3 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 NonNull (io.micronaut.core.annotation.NonNull)2 ArgumentValue (io.micronaut.core.type.ArgumentValue)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 InterceptedMethod (io.micronaut.aop.InterceptedMethod)1 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 MethodInvocationContext (io.micronaut.aop.MethodInvocationContext)1 BootstrapContextCompatible (io.micronaut.context.annotation.BootstrapContextCompatible)1 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 Internal (io.micronaut.core.annotation.Internal)1 Nullable (io.micronaut.core.annotation.Nullable)1 Publishers (io.micronaut.core.async.publisher.Publishers)1 CompletionAwareSubscriber (io.micronaut.core.async.subscriber.CompletionAwareSubscriber)1 BeanMap (io.micronaut.core.beans.BeanMap)1 Bindable (io.micronaut.core.bind.annotation.Bindable)1