Search code examples
javaosgiabstract-classapache-felix

Calling Abstract classes @Activate method (apache felix)


I have an abstract class that a child class extends. My abstract class has an @Activate method, so does the child class. When OSGi creates my service, it invokes the child class activate method but never the abstract class's activate. Is there any way to force the abstract class's activate to be called by OSGi rather than having the child class manually call the parent activate method?

Here is some code to help elaborate on what I am asking.

@Component(componentAbstract=true, inherit=true)
@Service(value=ISomeInterface)
public abstract class AbstractHello implements ISomeInterface{
    @Activate
    public void activate(){
        System.out.print("Hello ");
    }
}

@Component
@Service(Value=ISomeInterface)
public class World extends AbstractHello{
    @Activate
    public void activate(){
        System.out.println("World!");
    }
}

The result of the code above would be "World!", rather than "Hello World!".

Initially I thought maybe the child activate method name was clobbering the abstract activate method of the same name. The result is the same even if the abstract class's activate method is given a unique name. Is there any way to have OSGi call the abstract class's activate method for me?


Solution

  • The DS annotation processors only look at the concrete class decorated with @Component. Super classes are not examined. Since the annotation processing is done at build time, super types may come from imported packages which are not chosen until runtime.

    Also, the annotation processor generates component description XML from the annotations. So there can only be one activate="methodName" attribute in the XML. If you need the superclass' method called, then you need to call it from the subclass' method.