AdaptTo in aem | Community
Skip to main content
February 23, 2025
Solved

AdaptTo in aem

  • February 23, 2025
  • 1 reply
  • 588 views

Hi All,

 

we can adapt resource to page and resourceresolver to pagemanager but  we can't adapt resource to pagemanger and resolver to page  how does adapter know this?

Thanks,

Keerthi K.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

Hi @kummk123,

This depends on the implementation of specific object and/or AdapterFactory correlated with it. Official documentation describes is like below:

How it works

There are various ways that Adaptable.adaptTo() can be implemented:

  • By the object itself; implementing the method itself and mapping to certain objects.

  • By an AdapterFactory, which can map arbitrary objects.

    The objects must still implement the Adaptable interface and must extend SlingAdaptable (which passes the adaptTo call to a central adapter manager).

    This method allows hooks into the adaptTo mechanism for existing classes, such as Resource.

  • A combination of both.

For the first case, the Java™ docs can state what adaptTo-targets are possible. However, for specific subclasses such as the JCR-based Resource, this statement is often not possible. In the latter case, implementations of AdapterFactory are typically part of the private classes of a bundle and thus not exposed in a client API, nor listed in Java™ docs. Theoretically, it is possible to access all AdapterFactory implementations from the OSGi service runtime and look at their “adaptables” (sources and targets) configurations, but not to map them to each other. In the end, it depends on the internal logic, which must be documented. Hence this reference.


In general if you will try to adapt to a class that for which adapter is not supported you will get a null as a result.

Please find below documentation that describes many details:

Additionally please also check list of available adapter, so you will know what type can be adapt to which object.

Last but not lease you can check full list of adapters on your AEM instance under: /system/console/adapters or /system/console/status-adapters

This might be useful in case to find some custom adapter provided by 3rd party.

Finally you can implement your own adapter in case OOTB are not covering your needs.

1 reply

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
February 23, 2025

Hi @kummk123,

This depends on the implementation of specific object and/or AdapterFactory correlated with it. Official documentation describes is like below:

How it works

There are various ways that Adaptable.adaptTo() can be implemented:

  • By the object itself; implementing the method itself and mapping to certain objects.

  • By an AdapterFactory, which can map arbitrary objects.

    The objects must still implement the Adaptable interface and must extend SlingAdaptable (which passes the adaptTo call to a central adapter manager).

    This method allows hooks into the adaptTo mechanism for existing classes, such as Resource.

  • A combination of both.

For the first case, the Java™ docs can state what adaptTo-targets are possible. However, for specific subclasses such as the JCR-based Resource, this statement is often not possible. In the latter case, implementations of AdapterFactory are typically part of the private classes of a bundle and thus not exposed in a client API, nor listed in Java™ docs. Theoretically, it is possible to access all AdapterFactory implementations from the OSGi service runtime and look at their “adaptables” (sources and targets) configurations, but not to map them to each other. In the end, it depends on the internal logic, which must be documented. Hence this reference.


In general if you will try to adapt to a class that for which adapter is not supported you will get a null as a result.

Please find below documentation that describes many details:

Additionally please also check list of available adapter, so you will know what type can be adapt to which object.

Last but not lease you can check full list of adapters on your AEM instance under: /system/console/adapters or /system/console/status-adapters

This might be useful in case to find some custom adapter provided by 3rd party.

Finally you can implement your own adapter in case OOTB are not covering your needs.