use of io.micronaut.inject.ast.MemberElement in project micronaut-core by micronaut-projects.
the class TestBeanElementVisitor method visitBeanElement.
@Override
public BeanElement visitBeanElement(BeanElement beanElement, VisitorContext visitorContext) {
Element producingElement = beanElement.getProducingElement();
if (producingElement instanceof MemberElement) {
producingElement = ((MemberElement) producingElement).getDeclaringType();
}
final String name = producingElement.getName();
if (name.startsWith("testbe")) {
theBeanElement = beanElement;
}
if (name.equals("testbe2.Excluded")) {
// tests bean veto
return null;
}
return beanElement;
}
use of io.micronaut.inject.ast.MemberElement in project micronaut-core by micronaut-projects.
the class AbstractBeanDefinitionBuilder method produceBeans.
@Override
public <E extends MemberElement> BeanElementBuilder produceBeans(ElementQuery<E> methodsOrFields, Consumer<BeanElementBuilder> childBeanBuilder) {
methodsOrFields = methodsOrFields.onlyConcrete().onlyInstance().modifiers((modifiers) -> modifiers.contains(ElementModifier.PUBLIC));
final List<E> enclosedElements = this.beanType.getEnclosedElements(methodsOrFields);
for (E enclosedElement : enclosedElements) {
if (enclosedElement instanceof FieldElement) {
FieldElement fe = (FieldElement) enclosedElement;
final ClassElement type = fe.getGenericField().getType();
if (type.isPublic() && !type.isPrimitive()) {
return addChildBean(fe, childBeanBuilder);
}
}
if (enclosedElement instanceof MethodElement && !(enclosedElement instanceof ConstructorElement)) {
MethodElement me = (MethodElement) enclosedElement;
final ClassElement type = me.getGenericReturnType().getType();
if (type.isPublic() && !type.isPrimitive()) {
return addChildBean(me, childBeanBuilder);
}
}
}
return this;
}
use of io.micronaut.inject.ast.MemberElement in project micronaut-core by micronaut-projects.
the class AbstractBeanDefinitionBuilder method build.
/**
* Build the bean definition writer.
* @return The writer, possibly null if it wasn't possible to build it
*/
@Nullable
public BeanDefinitionWriter build() {
if (exposedTypes != null) {
final AnnotationClassValue[] annotationClassValues = Arrays.stream(exposedTypes).map(ce -> new AnnotationClassValue<>(ce.getName())).toArray(AnnotationClassValue[]::new);
annotate(Bean.class, (builder) -> builder.member("typed", annotationClassValues));
}
final BeanDefinitionWriter beanDefinitionWriter = createBeanDefinitionWriter();
if (typeArguments != null) {
beanDefinitionWriter.visitTypeArguments(this.typeArguments);
}
if (constructorElement == null) {
constructorElement = initConstructor(beanType);
}
if (constructorElement == null) {
visitorContext.fail("Cannot create associated bean with no accessible primary constructor. Consider supply the constructor with createWith(..)", originatingElement);
return null;
} else {
beanDefinitionWriter.visitBeanDefinitionConstructor(constructorElement, !constructorElement.isPublic(), visitorContext);
}
Map<ClassElement, List<MemberElement>> sortedInjections = new LinkedHashMap<>();
List<MemberElement> allInjected = new ArrayList<>();
allInjected.addAll(injectedFields);
allInjected.addAll(injectedMethods);
allInjected.sort(SORTER);
for (MemberElement memberElement : allInjected) {
final List<MemberElement> list = sortedInjections.computeIfAbsent(memberElement.getDeclaringType(), classElement -> new ArrayList<>());
list.add(memberElement);
}
for (List<MemberElement> members : sortedInjections.values()) {
members.sort((o1, o2) -> {
if (o1 instanceof FieldElement && o2 instanceof MethodElement) {
return 1;
} else if (o1 instanceof MethodElement && o1 instanceof FieldElement) {
return -1;
}
return 0;
});
}
for (List<MemberElement> list : sortedInjections.values()) {
for (MemberElement memberElement : list) {
if (memberElement instanceof FieldElement) {
InternalBeanElementField ibf = (InternalBeanElementField) memberElement;
ibf.<InternalBeanElementField>with((element) -> visitField(beanDefinitionWriter, element, element));
} else {
InternalBeanElementMethod ibm = (InternalBeanElementMethod) memberElement;
ibm.<InternalBeanElementMethod>with((element) -> beanDefinitionWriter.visitMethodInjectionPoint(ibm.getDeclaringType(), ibm, ibm.isRequiresReflection(), visitorContext));
}
}
}
for (BeanMethodElement executableMethod : executableMethods) {
beanDefinitionWriter.visitExecutableMethod(beanType, executableMethod, visitorContext);
}
for (BeanMethodElement postConstructMethod : postConstructMethods) {
if (postConstructMethod.getDeclaringType().equals(beanType)) {
beanDefinitionWriter.visitPostConstructMethod(beanType, postConstructMethod, ((InternalBeanElementMethod) postConstructMethod).isRequiresReflection(), visitorContext);
}
}
for (BeanMethodElement preDestroyMethod : preDestroyMethods) {
if (preDestroyMethod.getDeclaringType().equals(beanType)) {
beanDefinitionWriter.visitPreDestroyMethod(beanType, preDestroyMethod, ((InternalBeanElementMethod) preDestroyMethod).isRequiresReflection(), visitorContext);
}
}
beanDefinitionWriter.visitBeanDefinitionEnd();
return beanDefinitionWriter;
}
Aggregations