Package org.apache.sling.models.factory

# Interface ModelFactory

@ProviderType public interface ModelFactory
The ModelFactory instantiates Sling Model classes similar to #adaptTo but will throw an exception in case instantiation fails for some reason.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`canCreateFromAdaptable(@NotNull Object adaptable, @NotNull Class<?> type)`
`<ModelType>
ModelType`
`createModel(@NotNull Object adaptable, @NotNull Class<ModelType> type)`
Instantiates the given Sling Model class from the given adaptable.
`<T> T`
`createModelFromWrappedRequest(@NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request, @NotNull org.apache.sling.api.resource.Resource resource, @NotNull Class<T> targetClass)`
Create a wrapped request object with the specified resource and instantiates the given Sling Model class from that wrapped request.
`<T> T`
`createModelFromWrappedRequest(org.apache.sling.api.SlingHttpServletRequest request, @NotNull org.apache.sling.api.resource.Resource resource, @NotNull Class<T> targetClass)`
Deprecated.
use `createModelFromWrappedRequest(SlingJakartaHttpServletRequest, Resource, Class)` instead
`<T> T`
`exportModel(@NotNull Object model, @NotNull String exporterName, @NotNull Class<T> targetClass, @NotNull Map<String,String> options)`
Export the model object using the defined target class using the named exporter.
`<T> T`
`exportModelForRequest(@NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request, @NotNull String exporterName, @NotNull Class<T> targetClass, @NotNull Map<String,String> options)`
Export the model object registered to the request's resource's type using the defined target class using the named exporter.
`<T> T`
`exportModelForRequest(org.apache.sling.api.SlingHttpServletRequest request, @NotNull String exporterName, @NotNull Class<T> targetClass, @NotNull Map<String,String> options)`
Deprecated.
use `exportModelForRequest(SlingJakartaHttpServletRequest, String, Class, Map)` instead
`<T> T`
`exportModelForResource(@NotNull org.apache.sling.api.resource.Resource resource, @NotNull String exporterName, @NotNull Class<T> targetClass, @NotNull Map<String,String> options)`
Export the model object registered to the resource's type using the defined target class using the named exporter.
`@NotNull Object`
`getModelFromRequest(@NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request)`
Obtain an adapted model class based on the resource type of the request's resource.
`@NotNull Object`
`getModelFromRequest(org.apache.sling.api.SlingHttpServletRequest request)`
Deprecated.
use `getModelFromRequest(SlingJakartaHttpServletRequest)` instead
`@NotNull Object`
`getModelFromResource(@NotNull org.apache.sling.api.resource.Resource resource)`
Obtain an adapted model class based on the resource type of the provided resource.
`<T> T`
`getModelFromWrappedRequest(@NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request, @NotNull org.apache.sling.api.resource.Resource resource, @NotNull Class<T> targetClass)`
Create a wrapped request object with the specified resource and (try to) adapt the request object into the specified class.
`<T> T`
`getModelFromWrappedRequest(org.apache.sling.api.SlingHttpServletRequest request, @NotNull org.apache.sling.api.resource.Resource resource, @NotNull Class<T> targetClass)`
Deprecated.
use `getModelFromWrappedRequest(SlingJakartaHttpServletRequest, Resource, Class)` instead
`boolean`
`isModelAvailableForRequest(@NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request)`
Determine is a model class is available for the request's resource's resource type.
`boolean`
`isModelAvailableForRequest(org.apache.sling.api.SlingHttpServletRequest request)`
Deprecated.
use `isModelAvailableForRequest(SlingJakartaHttpServletRequest)` instead
`boolean`
`isModelAvailableForResource(@NotNull org.apache.sling.api.resource.Resource resource)`
Determine is a model class is available for the resource's resource type.
`boolean`
`isModelClass(@NotNull Class<?> type)`
Checks if a given type can be instantiated though Sling Models.
`boolean`
`isModelClass(@NotNull Object adaptable, @NotNull Class<?> type)`
Deprecated.
Use `isModelClass(Class)` instead!

## Method Details

### createModel

@NotNull <ModelType> ModelType createModel(@NotNull @NotNull Object adaptable, @NotNull @NotNull Class<ModelType> type) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelException
Instantiates the given Sling Model class from the given adaptable.

Type Parameters:
  `ModelType` - Model type
Parameters:
  `adaptable` - the adaptable to use to instantiate the Sling Model Class
  `type` - the class to instantiate
Returns:
  a new instance for the required model (never `null`)
Throws:
  `MissingElementsException` - in case no injector was able to inject some required values with the given types
  `InvalidAdaptableException` - in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)
  `ModelClassException` - in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be called
  `PostConstructException` - in case the post-construct method has thrown an exception itself
  `ValidationException` - in case validation could not be performed for some reason (e.g. no validation information available)
  `InvalidModelException` - in case the given model type could not be validated through the model validation

### createModelFromWrappedRequest

@Deprecated(since="1.6.0") @NotNull <T> T createModelFromWrappedRequest(@NotNull org.apache.sling.api.SlingHttpServletRequest request, @NotNull @NotNull org.apache.sling.api.resource.Resource resource, @NotNull @NotNull Class<T> targetClass)
Deprecated.
use `createModelFromWrappedRequest(SlingJakartaHttpServletRequest, Resource, Class)` instead
Create a wrapped request object with the specified resource and instantiates the given Sling Model class from that wrapped request. The wrapped request object will have a fresh set of script bindings so that any injected bindings references have the correct context.

Type Parameters:
  `T` - Model type
Parameters:
  `request` - the current request
  `resource` - the resource to set as the wrapped request's resource
  `targetClass` - the class to instantiate
Returns:
  a new instance for the required model (never `null`)
Throws:
  `MissingElementsException` - in case no injector was able to inject some required values with the given types
  `InvalidAdaptableException` - in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)
  `ModelClassException` - in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be called
  `PostConstructException` - in case the post-construct method has thrown an exception itself
  `ValidationException` - in case validation could not be performed for some reason (e.g. no validation information available)
  `InvalidModelException` - in case the given model type could not be validated through the model validation
Since:
  1.5.0 (Models API Bundle 1.4.4)
See Also:
  - `getModelFromWrappedRequest(SlingHttpServletRequest, Resource, Class)`

### createModelFromWrappedRequest

@NotNull <T> T createModelFromWrappedRequest(@NotNull @NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request, @NotNull @NotNull org.apache.sling.api.resource.Resource resource, @NotNull @NotNull Class<T> targetClass)
Create a wrapped request object with the specified resource and instantiates the given Sling Model class from that wrapped request. The wrapped request object will have a fresh set of script bindings so that any injected bindings references have the correct context.

Type Parameters:
  `T` - Model type
Parameters:
  `request` - the current request
  `resource` - the resource to set as the wrapped request's resource
  `targetClass` - the class to instantiate
Returns:
  a new instance for the required model (never `null`)
Throws:
  `MissingElementsException` - in case no injector was able to inject some required values with the given types
  `InvalidAdaptableException` - in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)
  `ModelClassException` - in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be called
  `PostConstructException` - in case the post-construct method has thrown an exception itself
  `ValidationException` - in case validation could not be performed for some reason (e.g. no validation information available)
  `InvalidModelException` - in case the given model type could not be validated through the model validation
Since:
  1.5.0 (Models API Bundle 1.4.4)
See Also:
  - `getModelFromWrappedRequest(SlingJakartaHttpServletRequest, Resource, Class)`

### canCreateFromAdaptable

boolean canCreateFromAdaptable(@NotNull @NotNull Object adaptable, @NotNull @NotNull Class<?> type)

Parameters:
  `adaptable` - the adaptable to check
  `type` - the class to check
Returns:
  `true` in case the given class can be created from the given adaptable, otherwise `false`

### isModelClass

@Deprecated(since="1.1.0") boolean isModelClass(@NotNull @NotNull Object adaptable, @NotNull @NotNull Class<?> type)
Deprecated.
Use `isModelClass(Class)` instead!

Parameters:
  `adaptable` - the adaptable to check
  `type` - the class to check
Returns:
  false in case no class with the Model annotation adapts to the requested type
See Also:
  - `Model`

### isModelClass

boolean isModelClass(@NotNull @NotNull Class<?> type)
Checks if a given type can be instantiated though Sling Models. This checks that

- there is a class annotated with `Model` which adapts to the given type
- this class is registered as Sling Model (i.e. the package is listed in the "Sling-Model-Packages" header from the bundles manifest and has been picked up already by the bundle listener)

Only if both conditions are fulfilled this method will return `true`.

Parameters:
  `type` - the class to check
Returns:
  `true` in case the given type can be instantiated though Sling Models.

### isModelAvailableForResource

boolean isModelAvailableForResource(@NotNull @NotNull org.apache.sling.api.resource.Resource resource)
Determine is a model class is available for the resource's resource type.

Parameters:
  `resource` - a resource
Returns:
  `true` if a model class is mapped to the resource type

### isModelAvailableForRequest

@Deprecated(since="1.6.0") boolean isModelAvailableForRequest(@NotNull org.apache.sling.api.SlingHttpServletRequest request)
Deprecated.
use `isModelAvailableForRequest(SlingJakartaHttpServletRequest)` instead
Determine is a model class is available for the request's resource's resource type.

Parameters:
  `request` - a request
Returns:
  `true` if a model class is mapped to the resource type

### isModelAvailableForRequest

boolean isModelAvailableForRequest(@NotNull @NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request)
Determine is a model class is available for the request's resource's resource type.

Parameters:
  `request` - a request
Returns:
  `true` if a model class is mapped to the resource type

### getModelFromResource

@NotNull @NotNull Object getModelFromResource(@NotNull @NotNull org.apache.sling.api.resource.Resource resource) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelException
Obtain an adapted model class based on the resource type of the provided resource.

Parameters:
  `resource` - a resource
Returns:
  an adapted model object
Throws:
  `MissingElementsException` - in case no injector was able to inject some required values with the given types
  `InvalidAdaptableException` - in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)
  `ModelClassException` - in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be called
  `PostConstructException` - in case the post-construct method has thrown an exception itself
  `ValidationException` - in case validation could not be performed for some reason (e.g. no validation information available)
  `InvalidModelException` - in case the given model type could not be validated through the model validation

### getModelFromRequest

@Deprecated(since="1.6.0") @NotNull @NotNull Object getModelFromRequest(@NotNull org.apache.sling.api.SlingHttpServletRequest request) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelException
Deprecated.
use `getModelFromRequest(SlingJakartaHttpServletRequest)` instead
Obtain an adapted model class based on the resource type of the request's resource.

Parameters:
  `request` - a request
Returns:
  an adapted model object
Throws:
  `MissingElementsException` - in case no injector was able to inject some required values with the given types
  `InvalidAdaptableException` - in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)
  `ModelClassException` - in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be called
  `PostConstructException` - in case the post-construct method has thrown an exception itself
  `ValidationException` - in case validation could not be performed for some reason (e.g. no validation information available)
  `InvalidModelException` - in case the given model type could not be validated through the model validation

### getModelFromRequest

@NotNull @NotNull Object getModelFromRequest(@NotNull @NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelException
Obtain an adapted model class based on the resource type of the request's resource.

Parameters:
  `request` - a request
Returns:
  an adapted model object
Throws:
  `MissingElementsException` - in case no injector was able to inject some required values with the given types
  `InvalidAdaptableException` - in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)
  `ModelClassException` - in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be called
  `PostConstructException` - in case the post-construct method has thrown an exception itself
  `ValidationException` - in case validation could not be performed for some reason (e.g. no validation information available)
  `InvalidModelException` - in case the given model type could not be validated through the model validation

### exportModel

@NotNull <T> T exportModel(@NotNull @NotNull Object model, @NotNull @NotNull String exporterName, @NotNull @NotNull Class<T> targetClass, @NotNull @NotNull Map<String,String> options) throws ExportException, MissingExporterException
Export the model object using the defined target class using the named exporter.

Type Parameters:
  `T` - the target class
Parameters:
  `model` - the model object
  `exporterName` - the exporter name
  `targetClass` - the target class
  `options` - any exporter options
Returns:
  an instance of the target class
Throws:
  `ExportException` - if the export fails
  `MissingExporterException` - if the named exporter can't be found

### exportModelForResource

@NotNull <T> T exportModelForResource(@NotNull @NotNull org.apache.sling.api.resource.Resource resource, @NotNull @NotNull String exporterName, @NotNull @NotNull Class<T> targetClass, @NotNull @NotNull Map<String,String> options) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelException, ExportException, MissingExporterException
Export the model object registered to the resource's type using the defined target class using the named exporter.

Type Parameters:
  `T` - the target class
Parameters:
  `resource` - the resource
  `exporterName` - the exporter name
  `targetClass` - the target class
  `options` - any exporter options
Returns:
  an instance of the target class
Throws:
  `MissingElementsException` - in case no injector was able to inject some required values with the given types
  `InvalidAdaptableException` - in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)
  `ModelClassException` - in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be called
  `PostConstructException` - in case the post-construct method has thrown an exception itself
  `ValidationException` - in case validation could not be performed for some reason (e.g. no validation information available)
  `InvalidModelException` - in case the given model type could not be validated through the model validation
  `ExportException` - if the export fails
  `MissingExporterException` - if the named exporter can't be found

### exportModelForRequest

@Deprecated(since="1.6.0") @NotNull <T> T exportModelForRequest(@NotNull org.apache.sling.api.SlingHttpServletRequest request, @NotNull @NotNull String exporterName, @NotNull @NotNull Class<T> targetClass, @NotNull @NotNull Map<String,String> options) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelException, ExportException, MissingExporterException
Deprecated.
use `exportModelForRequest(SlingJakartaHttpServletRequest, String, Class, Map)` instead
Export the model object registered to the request's resource's type using the defined target class using the named exporter.

Type Parameters:
  `T` - the target class
Parameters:
  `request` - the request
  `exporterName` - the exporter name
  `targetClass` - the target class
  `options` - any exporter options
Returns:
  an instance of the target class
Throws:
  `MissingElementsException` - in case no injector was able to inject some required values with the given types
  `InvalidAdaptableException` - in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)
  `ModelClassException` - in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be called
  `PostConstructException` - in case the post-construct method has thrown an exception itself
  `ValidationException` - in case validation could not be performed for some reason (e.g. no validation information available)
  `InvalidModelException` - in case the given model type could not be validated through the model validation
  `ExportException` - if the export fails
  `MissingExporterException` - if the named exporter can't be found

### exportModelForRequest

@NotNull <T> T exportModelForRequest(@NotNull @NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request, @NotNull @NotNull String exporterName, @NotNull @NotNull Class<T> targetClass, @NotNull @NotNull Map<String,String> options) throws MissingElementsException, InvalidAdaptableException, ModelClassException, PostConstructException, ValidationException, InvalidModelException, ExportException, MissingExporterException
Export the model object registered to the request's resource's type using the defined target class using the named exporter.

Type Parameters:
  `T` - the target class
Parameters:
  `request` - the request
  `exporterName` - the exporter name
  `targetClass` - the target class
  `options` - any exporter options
Returns:
  an instance of the target class
Throws:
  `MissingElementsException` - in case no injector was able to inject some required values with the given types
  `InvalidAdaptableException` - in case the given class cannot be instantiated from the given adaptable (different adaptable on the model annotation)
  `ModelClassException` - in case the model could not be instantiated because model annotation was missing, reflection failed, no valid constructor was found, model was not registered as adapter factory yet, or post-construct could not be called
  `PostConstructException` - in case the post-construct method has thrown an exception itself
  `ValidationException` - in case validation could not be performed for some reason (e.g. no validation information available)
  `InvalidModelException` - in case the given model type could not be validated through the model validation
  `ExportException` - if the export fails
  `MissingExporterException` - if the named exporter can't be found

### getModelFromWrappedRequest

@Deprecated(since="1.6.0") @Nullable <T> T getModelFromWrappedRequest(@NotNull org.apache.sling.api.SlingHttpServletRequest request, @NotNull @NotNull org.apache.sling.api.resource.Resource resource, @NotNull @NotNull Class<T> targetClass)
Deprecated.
use `getModelFromWrappedRequest(SlingJakartaHttpServletRequest, Resource, Class)` instead
Create a wrapped request object with the specified resource and (try to) adapt the request object into the specified class. The wrapped request object will have a fresh set of script bindings so that any injected bindings references have the correct context. Consider using `createModelFromWrappedRequest(SlingHttpServletRequest, Resource, Class)` instead to get exceptions propagated which may occur when trying to create the model.

Type Parameters:
  `T` - the target adapter class
Parameters:
  `request` - the current request
  `resource` - the resource to set as the wrapped request's resource
  `targetClass` - the target adapter class
Returns:
  an instance of the target class or null if the adaptation could not be done
Since:
  1.4.0 (Models API Bundle 1.3.6)

### getModelFromWrappedRequest

@Nullable <T> T getModelFromWrappedRequest(@NotNull @NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request, @NotNull @NotNull org.apache.sling.api.resource.Resource resource, @NotNull @NotNull Class<T> targetClass)
Create a wrapped request object with the specified resource and (try to) adapt the request object into the specified class. The wrapped request object will have a fresh set of script bindings so that any injected bindings references have the correct context. Consider using `createModelFromWrappedRequest(SlingHttpServletRequest, Resource, Class)` instead to get exceptions propagated which may occur when trying to create the model.

Type Parameters:
  `T` - the target adapter class
Parameters:
  `request` - the current request
  `resource` - the resource to set as the wrapped request's resource
  `targetClass` - the target adapter class
Returns:
  an instance of the target class or null if the adaptation could not be done
Since:
  1.4.0 (Models API Bundle 1.3.6)