io.micronaut.aop.chain.InterceptorChain Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-aop Show documentation
Show all versions of micronaut-aop Show documentation
Core components supporting the Micronaut Framework
/*
* Copyright 2017-2018 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.aop.chain;
import io.micronaut.aop.*;
import io.micronaut.aop.exceptions.UnimplementedAdviceException;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.BeanContext;
import io.micronaut.context.EnvironmentConfigurable;
import io.micronaut.context.annotation.Type;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.convert.value.MutableConvertibleValues;
import io.micronaut.core.order.OrderUtil;
import io.micronaut.core.type.Argument;
import io.micronaut.core.type.MutableArgumentValue;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.inject.ExecutableMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.annotation.Annotation;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
*
* An internal representation of the {@link Interceptor} chain. This class implements {@link InvocationContext} and is
* consumed by the framework itself and should not be used directly in application code.
*
* @param The declaring type
* @param The result of the method call
*
* @author Graeme Rocher
* @since 1.0
*/
@Internal
public class InterceptorChain implements InvocationContext {
private static final Logger LOG = LoggerFactory.getLogger(InterceptorChain.class);
protected final Interceptor[] interceptors;
protected final B target;
protected final ExecutableMethod executionHandle;
protected final MutableConvertibleValues attributes = MutableConvertibleValues.of(new ConcurrentHashMap<>());
protected final Map> parameters = new LinkedHashMap<>();
private int index = 0;
/**
* Constructor.
*
* @param interceptors array of interceptors
* @param target target type
* @param method result method
* @param originalParameters parameters
*/
public InterceptorChain(Interceptor[] interceptors,
B target,
ExecutableMethod method,
Object... originalParameters) {
if (LOG.isTraceEnabled()) {
LOG.trace("Intercepted method [{}] invocation on target: {}", method, target);
}
this.target = target;
this.executionHandle = method;
this.interceptors = new Interceptor[interceptors.length + 1];
System.arraycopy(interceptors, 0, this.interceptors, 0, interceptors.length);
boolean isIntroduction = target instanceof Introduced;
if (isIntroduction) {
this.interceptors[this.interceptors.length - 1] = context -> {
throw new UnimplementedAdviceException(method);
};
} else {
this.interceptors[this.interceptors.length - 1] = context -> method.invoke(target, getParameterValues());
}
Argument[] arguments = method.getArguments();
for (int i = 0; i < arguments.length; i++) {
Argument argument = method.getArguments()[i];
Object value = originalParameters[i];
parameters.put(argument.getName(), MutableArgumentValue.create(argument, value));
}
}
@Override
public AnnotationMetadata getAnnotationMetadata() {
return executionHandle.getAnnotationMetadata();
}
@Override
public MutableConvertibleValues