package io.micronaut.context;
import io.micronaut.context.env.Environment;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.core.type.Argument;
import io.micronaut.inject.BeanDefinition;
import io.micronaut.inject.MethodInjectionPoint;
import io.micronaut.inject.annotation.AbstractEnvironmentAnnotationMetadata;
import edu.umd.cs.findbugs.annotations.Nullable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
@Internal
class DefaultMethodInjectionPoint<B, T> implements MethodInjectionPoint<B, T>, EnvironmentConfigurable {
private final BeanDefinition declaringBean;
private final AnnotationMetadata annotationMetadata;
private final Class<?> declaringType;
private final String methodName;
private final Class[] argTypes;
private final Argument[] arguments;
private Environment environment;
DefaultMethodInjectionPoint(
BeanDefinition declaringBean,
Class<?> declaringType,
String methodName,
@Nullable Argument[] arguments) {
this(declaringBean, declaringType, methodName, arguments, AnnotationMetadata.EMPTY_METADATA);
}
DefaultMethodInjectionPoint(
BeanDefinition declaringBean,
Class<?> declaringType,
String methodName,
@Nullable Argument[] arguments,
@Nullable AnnotationMetadata annotationMetadata) {
Objects.requireNonNull(declaringBean, "Declaring bean cannot be null");
this.declaringType = declaringType;
this.methodName = methodName;
this.arguments = arguments == null ? Argument.ZERO_ARGUMENTS : arguments;
this.argTypes = Argument.toClassArray(arguments);
this.declaringBean = declaringBean;
this.annotationMetadata = initAnnotationMetadata(annotationMetadata);
}
@Override
public String toString() {
String text = Argument.toString(getArguments());
return declaringType.getSimpleName() + "." + methodName + "(" + text + ")";
}
@Override
public void configure(Environment environment) {
this.environment = environment;
}
@Override
public Method getMethod() {
Method method = ReflectionUtils.getMethod(declaringType, methodName, argTypes)
.orElseThrow(() -> ReflectionUtils.newNoSuchMethodError(declaringType, methodName, argTypes));
method.setAccessible(true);
return method;
}
@Override
public String getName() {
return methodName;
}
@Override
public boolean isPreDestroyMethod() {
return annotationMetadata.hasDeclaredAnnotation(PreDestroy.class);
}
@Override
public boolean isPostConstructMethod() {
return annotationMetadata.hasDeclaredAnnotation(PostConstruct.class);
}
@Override
public Object invoke(Object instance, Object... args) {
Method targetMethod = getMethod();
return ReflectionUtils.invokeMethod(instance, targetMethod, args);
}
@Override
public AnnotationMetadata getAnnotationMetadata() {
return annotationMetadata;
}
@Override
public BeanDefinition getDeclaringBean() {
return declaringBean;
}
@Override
public boolean requiresReflection() {
return false;
}
@Override
public Argument<?>[] getArguments() {
return arguments;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DefaultMethodInjectionPoint that = (DefaultMethodInjectionPoint) o;
return Objects.equals(declaringType, that.declaringType) &&
Objects.equals(methodName, that.methodName) &&
Arrays.equals(argTypes, that.argTypes);
}
@Override
public int hashCode() {
int result = Objects.hash(declaringType, methodName);
result = 31 * result + Arrays.hashCode(argTypes);
return result;
}
private AnnotationMetadata initAnnotationMetadata(@Nullable AnnotationMetadata annotationMetadata) {
if (annotationMetadata != AnnotationMetadata.EMPTY_METADATA) {
return new MethodAnnotationMetadata(annotationMetadata);
}
return AnnotationMetadata.EMPTY_METADATA;
}
private final class MethodAnnotationMetadata extends AbstractEnvironmentAnnotationMetadata {
MethodAnnotationMetadata(AnnotationMetadata targetMetadata) {
super(targetMetadata);
}
@Nullable
@Override
protected Environment getEnvironment() {
return environment;
}
}
}