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;
}
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();
}
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();
}
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);
}
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;
}
Aggregations