Class BeanContext.AbstractContext

    • Field Detail

      • locale

        protected transient Locale locale
    • Constructor Detail

      • AbstractContext

        protected AbstractContext()
    • Method Detail

      • cloneContext

        protected Object cloneContext()
      • retrieveService

        protected abstract <T> T retrieveService​(Class<T> type)
      • withLocale

        public BeanContext withLocale​(Locale locale)
        Description copied from interface: BeanContext
        Returns a clone of this context with the locale overridden, or this if it already had this locale. All other internal structures of this will be referenced by the copy, too.
        Specified by:
        withLocale in interface BeanContext
        Parameters:
        locale - the locale; if this is null BeanContext.getLocale() will take this from the attributes.
        Returns:
        a context with this locale, otherwise sharing everything else. Might be this.
      • getService

        public <T> T getService​(Class<T> type)
        Description copied from interface: BeanContext
        retrieves a service implementation using the 'sling' script helper
        Specified by:
        getService in interface BeanContext
      • adaptTo

        public <AdapterType> AdapterType adaptTo​(@NotNull
                                                 @NotNull Class<AdapterType> type)
        Description copied from interface: BeanContext

        Adapts to the components Resource, ResourceResolver, SlingHttpServletRequest, SlingHttpServletResponse, Locale, BeanContext itself, a ValueMap for the request, SlingBeans or possibly more if defined in Sling.

        In case of SlingBean we try the basic sling mechanism (possibly calling Sling-Models) and, failing that, try to instantiate and SlingBean.initialize(BeanContext) it ourselves, if it is instantiable and the Sling-Models @Model annotation is not present.

        Cached - multiple calls will always return the same object, except for ValueMap and SlingBeans.

        Specified by:
        adaptTo in interface org.apache.sling.api.adapter.Adaptable
        Specified by:
        adaptTo in interface BeanContext
        Overrides:
        adaptTo in class org.apache.sling.api.adapter.SlingAdaptable
        Parameters:
        type - not null, the type to be adapted to
        Returns:
        the component of type or whatever Sling has adapters for, or null if there is nothing.
        See Also:
        SlingAdaptable.adaptTo(Class)
      • tryToInstantiateSlingBean

        protected <AdapterType> AdapterType tryToInstantiateSlingBean​(Class<AdapterType> type)
        If type is an instantiable SlingBean but not a Sling-Models bean, try to instantiate it. We have to check per reflection whether the Sling-Models @Models annotation is present since we cannot distinguish a failed Sling-Models instantiation (returning null) from not being a Sling-Models bean at all (also returning null), and the Sling-Models ModelFactory cannot be a dependency, here.
      • typeFits

        protected <AdapterType> boolean typeFits​(Class<?> type,
                                                 Class<?> upperbound,
                                                 AdapterType object,
                                                 Class<AdapterType> defaultclass)
        A type fits if it is below some upper bound (we don't want to return something for type, say, Object) and if it is assignable from the real class of the object. If the object is null, we will return null (and pass by the other Sling adaptable mechanisms) if the type is assignable from the default type the object would implement / extend.