Search in sources :

Example 1 with InjectableBean

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();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) FieldCreator(io.quarkus.gizmo.FieldCreator) ClassCreator(io.quarkus.gizmo.ClassCreator) InjectableBean(io.quarkus.arc.InjectableBean) Supplier(java.util.function.Supplier)

Example 2 with InjectableBean

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));
}
Also used : MethodCreator(io.quarkus.gizmo.MethodCreator) InjectableBean(io.quarkus.arc.InjectableBean) ResultHandle(io.quarkus.gizmo.ResultHandle) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle)

Example 3 with InjectableBean

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;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) InjectableBean(io.quarkus.arc.InjectableBean) ContextInstanceHandle(io.quarkus.arc.ContextInstanceHandle) InstanceHandle(io.quarkus.arc.InstanceHandle) ContextInstanceHandle(io.quarkus.arc.ContextInstanceHandle)

Example 4 with InjectableBean

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;
}
Also used : StackFrame(java.lang.StackWalker.StackFrame) InjectableBean(io.quarkus.arc.InjectableBean) ArrayList(java.util.ArrayList) RemovedBean(io.quarkus.arc.RemovedBean)

Example 5 with InjectableBean

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);
}
Also used : InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) InjectableBean(io.quarkus.arc.InjectableBean) ArrayList(java.util.ArrayList) InjectionPointImpl(io.quarkus.arc.impl.CurrentInjectionPointProvider.InjectionPointImpl)

Aggregations

InjectableBean (io.quarkus.arc.InjectableBean)18 ClassCreator (io.quarkus.gizmo.ClassCreator)5 Annotation (java.lang.annotation.Annotation)5 FieldCreator (io.quarkus.gizmo.FieldCreator)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 Supplier (java.util.function.Supplier)4 ArcContainer (io.quarkus.arc.ArcContainer)3 InstanceHandle (io.quarkus.arc.InstanceHandle)3 MethodCreator (io.quarkus.gizmo.MethodCreator)3 ResultHandle (io.quarkus.gizmo.ResultHandle)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 ClassInfo (org.jboss.jandex.ClassInfo)3 Arc (io.quarkus.arc.Arc)2 ContextInstanceHandle (io.quarkus.arc.ContextInstanceHandle)2 AssignableResultHandle (io.quarkus.gizmo.AssignableResultHandle)2 Type (java.lang.reflect.Type)2