use of io.quarkus.arc.InjectableBean in project quarkus by quarkusio.
the class BeanGenerator method generateClassBean.
Collection<Resource> generateClassBean(BeanInfo bean, ClassInfo beanClass) {
String baseName;
if (beanClass.enclosingClass() != null) {
baseName = DotNames.simpleName(beanClass.enclosingClass()) + UNDERSCORE + DotNames.simpleName(beanClass);
} else {
baseName = DotNames.simpleName(beanClass);
}
ProviderType providerType = new ProviderType(bean.getProviderType());
String targetPackage = DotNames.packageName(providerType.name());
String generatedName = generatedNameFromTarget(targetPackage, baseName, BEAN_SUFFIX);
beanToGeneratedName.put(bean, generatedName);
if (existingClasses.contains(generatedName)) {
return Collections.emptyList();
}
boolean isApplicationClass = applicationClassPredicate.test(beanClass.name()) || bean.isForceApplicationClass();
ResourceClassOutput classOutput = new ResourceClassOutput(isApplicationClass, name -> name.equals(generatedName) ? SpecialType.BEAN : null, generateSources);
// Foo_Bean implements InjectableBean<T>
ClassCreator beanCreator = ClassCreator.builder().classOutput(classOutput).className(generatedName).interfaces(InjectableBean.class, Supplier.class).build();
// Fields
FieldCreator beanTypes = beanCreator.getFieldCreator(FIELD_NAME_BEAN_TYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
FieldCreator qualifiers = null;
if (!bean.getQualifiers().isEmpty() && !bean.hasDefaultQualifiers()) {
qualifiers = beanCreator.getFieldCreator(FIELD_NAME_QUALIFIERS, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}
if (bean.getScope().isNormal()) {
// For normal scopes a client proxy is generated too
initializeProxy(bean, baseName, beanCreator);
}
FieldCreator stereotypes = null;
if (!bean.getStereotypes().isEmpty()) {
stereotypes = beanCreator.getFieldCreator(FIELD_NAME_STEREOTYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
}
Map<InjectionPointInfo, String> injectionPointToProviderSupplierField = new HashMap<>();
Map<InterceptorInfo, String> interceptorToProviderSupplierField = new HashMap<>();
Map<DecoratorInfo, String> decoratorToProviderSupplierField = new HashMap<>();
initMaps(bean, injectionPointToProviderSupplierField, interceptorToProviderSupplierField, decoratorToProviderSupplierField);
createProviderFields(beanCreator, bean, injectionPointToProviderSupplierField, interceptorToProviderSupplierField, decoratorToProviderSupplierField);
createConstructor(classOutput, beanCreator, bean, injectionPointToProviderSupplierField, interceptorToProviderSupplierField, decoratorToProviderSupplierField, annotationLiterals, reflectionRegistration);
implementGetIdentifier(bean, beanCreator);
implementSupplierGet(beanCreator);
if (bean.hasDestroyLogic()) {
implementDestroy(bean, beanCreator, providerType, injectionPointToProviderSupplierField, isApplicationClass, baseName);
}
implementCreate(classOutput, beanCreator, bean, providerType, baseName, injectionPointToProviderSupplierField, interceptorToProviderSupplierField, decoratorToProviderSupplierField, targetPackage, isApplicationClass);
implementGet(bean, beanCreator, providerType, baseName);
implementGetTypes(beanCreator, beanTypes.getFieldDescriptor());
if (!BuiltinScope.isDefault(bean.getScope())) {
implementGetScope(bean, beanCreator);
}
if (qualifiers != null) {
implementGetQualifiers(bean, beanCreator, qualifiers.getFieldDescriptor());
}
implementIsAlternative(bean, beanCreator);
implementGetPriority(bean, beanCreator);
if (stereotypes != null) {
implementGetStereotypes(bean, beanCreator, stereotypes.getFieldDescriptor());
}
implementGetBeanClass(bean, beanCreator);
implementGetName(bean, beanCreator);
if (bean.isDefaultBean()) {
implementIsDefaultBean(bean, beanCreator);
}
implementIsSuppressed(bean, beanCreator);
implementEquals(bean, beanCreator);
implementHashCode(bean, beanCreator);
beanCreator.close();
return classOutput.getResources();
}
use of io.quarkus.arc.InjectableBean in project quarkus by quarkusio.
the class BeanGenerator method implementEquals.
protected void implementEquals(BeanInfo bean, ClassCreator beanCreator) {
MethodCreator equals = beanCreator.getMethodCreator("equals", boolean.class, Object.class).setModifiers(ACC_PUBLIC);
final ResultHandle obj = equals.getMethodParam(0);
// if (this == obj) {
// return true;
// }
equals.ifReferencesEqual(equals.getThis(), obj).trueBranch().returnValue(equals.load(true));
// if (obj == null) {
// return false;
// }
equals.ifNull(obj).trueBranch().returnValue(equals.load(false));
// if (!(obj instanceof InjectableBean)) {
// return false;
// }
equals.ifFalse(equals.instanceOf(obj, InjectableBean.class)).trueBranch().returnValue(equals.load(false));
// return identifier.equals(((InjectableBean) obj).getIdentifier());
ResultHandle injectableBean = equals.checkCast(obj, InjectableBean.class);
ResultHandle otherIdentifier = equals.invokeInterfaceMethod(MethodDescriptors.GET_IDENTIFIER, injectableBean);
equals.returnValue(equals.invokeVirtualMethod(MethodDescriptors.OBJECT_EQUALS, equals.load(bean.getIdentifier()), otherIdentifier));
}
use of io.quarkus.arc.InjectableBean in project quarkus by quarkusio.
the class HttpSessionContext method get.
@SuppressWarnings("unchecked")
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
HttpServletRequest request = servletRequest();
if (request == null) {
throw new ContextNotActiveException();
}
InjectableBean<T> bean = (InjectableBean<T>) contextual;
ComputingCache<Key, ContextInstanceHandle<?>> contextualInstances = getContextualInstances(request);
if (creationalContext != null) {
return (T) contextualInstances.getValue(new Key(creationalContext, bean.getIdentifier())).get();
} else {
InstanceHandle<T> handle = (InstanceHandle<T>) contextualInstances.getValueIfPresent(new Key(null, bean.getIdentifier()));
return handle != null ? handle.get() : null;
}
}
use of io.quarkus.arc.InjectableBean in project quarkus by quarkusio.
the class ArcContainerImpl method getMatchingBeans.
List<InjectableBean<?>> getMatchingBeans(Resolvable resolvable) {
List<InjectableBean<?>> matching = new ArrayList<>();
for (InjectableBean<?> bean : beans) {
if (matches(bean, resolvable.requiredType, resolvable.qualifiers)) {
matching.add(bean);
}
}
if (matching.isEmpty() && !removedBeans.isEmpty()) {
List<RemovedBean> removedMatching = new ArrayList<>();
for (RemovedBean removedBean : removedBeans) {
if (matches(removedBean.getTypes(), removedBean.getQualifiers(), resolvable.requiredType, resolvable.qualifiers)) {
removedMatching.add(removedBean);
}
}
if (!removedMatching.isEmpty()) {
String separator = "====================";
String msg = "\n%1$s%1$s%1$s%1$s\n" + "CDI: programmatic lookup problem detected\n" + "-----------------------------------------\n" + "At least one bean matched the required type and qualifiers but was marked as unused and removed during build\n\n" + "Stack frame: %5$s\n" + "Required type: %3$s\n" + "Required qualifiers: %4$s\n" + "Removed beans:\n\t- %2$s\n" + "Solutions:\n" + "\t- Application developers can eliminate false positives via the @Unremovable annotation\n" + "\t- Extensions can eliminate false positives via build items, e.g. using the UnremovableBeanBuildItem\n" + "\t- See also https://quarkus.io/guides/cdi-reference#remove_unused_beans\n" + "\t- Enable the DEBUG log level to see the full stack trace\n" + "%1$s%1$s%1$s%1$s\n";
StackWalker walker = StackWalker.getInstance();
StackFrame frame = walker.walk(this::findCaller);
LOGGER.warnf(msg, separator, removedMatching.stream().map(Object::toString).collect(Collectors.joining("\n\t- ")), resolvable.requiredType, Arrays.toString(resolvable.qualifiers), frame != null ? frame : "n/a");
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("\nCDI: programmatic lookup stack trace:\n" + walker.walk(this::collectStack));
}
}
}
return matching;
}
use of io.quarkus.arc.InjectableBean in project quarkus by quarkusio.
the class Instances method listOf.
@SuppressWarnings("unchecked")
public static <T> List<T> listOf(InjectableBean<?> targetBean, Type injectionPointType, Type requiredType, Set<Annotation> requiredQualifiers, CreationalContextImpl<?> creationalContext, Set<Annotation> annotations, Member javaMember, int position) {
List<InjectableBean<?>> beans = resolveBeans(requiredType, requiredQualifiers);
if (beans.isEmpty()) {
return Collections.emptyList();
}
List<T> list = new ArrayList<>(beans.size());
InjectionPoint prev = InjectionPointProvider.set(new InjectionPointImpl(injectionPointType, requiredType, requiredQualifiers, targetBean, annotations, javaMember, position));
try {
for (InjectableBean<?> bean : beans) {
list.add(getBeanInstance((CreationalContextImpl<T>) creationalContext, (InjectableBean<T>) bean));
}
} finally {
InjectionPointProvider.set(prev);
}
return List.copyOf(list);
}
Aggregations