Hey,
Given
public class Bar implements Foo {
...
}
I want to provide AdapterFactory that returns Bar instance when
resource.adaptTo(Foo.class) is called.
Here is my attempt:
=>8=
@Component(metatype = false, immediate = true)
@Service
@Properties({
@Property(name = SlingConstants.PROPERTY_ADAPTABLE_CLASSES, value =
{"org.apache.sling.api.resource.Resource"}),
@Property(name = SlingConstants.PROPERTY_ADAPTER_CLASSES, value =
{"com.example.Foo"}),
@Property(name = "service.ranking", intValue = -1)//this doesn't seem
to be observed
})
public class MyAdaptToFoo implements AdapterFactory {
@Override
public <AdapterType> AdapterType getAdapter(Object adaptable,
Class<AdapterType> type) {
final Resource resource = (Resource) adaptable;
final Foo foo = resource.adaptTo(Foo.class);//I don't want infinite
recursion here.
if (someCondition) {
final Bar bar = new Bar(foo);
return (AdapterType) bar;
}
return (AdapterType) foo;
}
}
=>8=
first of all, that adapter factory doesn't get called when
resource.adaptTo(Foo.class); is called..
Even if MyAdaptToFoo gets called, I don't want infinite recursion when its
.getAdapter() calls resource.adaptTo(Foo.class).
The reason why I'm doing this is because I have a massive code base that
uses resource.adaptTo(Foo.class).. and I need to augment Foo (with Bar) in
some cases.
Is this possible?