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.core.util.ArrayUtils;
import io.micronaut.inject.BeanDefinition;
import io.micronaut.inject.FieldInjectionPoint;
import io.micronaut.inject.annotation.AbstractEnvironmentAnnotationMetadata;
import io.micronaut.inject.annotation.DefaultAnnotationMetadata;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Objects;
@Internal
class DefaultFieldInjectionPoint<B, T> implements FieldInjectionPoint<B, T>, EnvironmentConfigurable {
private final BeanDefinition declaringBean;
private final Class declaringType;
private final Class<T> fieldType;
private final String field;
private final AnnotationMetadata annotationMetadata;
private final Argument[] typeArguments;
private Environment environment;
DefaultFieldInjectionPoint(
BeanDefinition declaringBean,
Class declaringType,
Class<T> fieldType,
String field,
@Nullable AnnotationMetadata annotationMetadata,
@Nullable Argument[] typeArguments) {
this.declaringBean = declaringBean;
this.declaringType = declaringType;
this.fieldType = fieldType;
this.field = field;
this.annotationMetadata = initAnnotationMetadata(annotationMetadata);
this.typeArguments = ArrayUtils.isEmpty(typeArguments) ? Argument.ZERO_ARGUMENTS : typeArguments;
}
@Override
public void configure(Environment environment) {
this.environment = environment;
}
@Override
public String toString() {
return fieldType.getSimpleName() + " " + field;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DefaultFieldInjectionPoint<?, ?> that = (DefaultFieldInjectionPoint<?, ?>) o;
return Objects.equals(declaringType, that.declaringType) &&
Objects.equals(fieldType, that.fieldType) &&
Objects.equals(field, that.field);
}
@Override
public int hashCode() {
return Objects.hash(declaringType, fieldType, field);
}
@Override
public AnnotationMetadata getAnnotationMetadata() {
return annotationMetadata;
}
@Override
public String getName() {
return field;
}
@Override
public Field getField() {
return ReflectionUtils.getRequiredField(declaringType, this.field);
}
@Override
public Class<T> getType() {
return fieldType;
}
@Override
public void set(T instance, Object object) {
Field field = getField();
try {
field.setAccessible(true);
field.set(instance, object);
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public Argument<T> asArgument() {
return Argument.of(
fieldType,
field,
annotationMetadata,
typeArguments
);
}
@Override
public BeanDefinition getDeclaringBean() {
return declaringBean;
}
@Override
public boolean requiresReflection() {
return false;
}
@Override
public <T extends Annotation> T synthesize(Class<T> annotationClass) {
return getAnnotationMetadata().synthesize(annotationClass);
}
@Override
public Annotation[] synthesizeAll() {
return getAnnotationMetadata().synthesizeAll();
}
@Override
public Annotation[] synthesizeDeclared() {
return getAnnotationMetadata().synthesizeDeclared();
}
private AnnotationMetadata initAnnotationMetadata(@Nullable AnnotationMetadata annotationMetadata) {
if (annotationMetadata instanceof DefaultAnnotationMetadata) {
return new FieldAnnotationMetadata((DefaultAnnotationMetadata) annotationMetadata);
} else if (annotationMetadata != null) {
return annotationMetadata;
}
return AnnotationMetadata.EMPTY_METADATA;
}
private final class FieldAnnotationMetadata extends AbstractEnvironmentAnnotationMetadata {
FieldAnnotationMetadata(DefaultAnnotationMetadata targetMetadata) {
super(targetMetadata);
}
@Nullable
@Override
protected Environment getEnvironment() {
return environment;
}
}
}