javaatg

ATG - Override method and return more specific component


I'm trying override some method to let it with more specific return. So I did.

@Override
public StoreProfile getProfile() {
    return (StoreProfile)super.getProfile();
}

However, the method super.getProfile() always return null when it is overrided.

My enviroment has the following specifications.

.\java -version
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b50)
Dynamic Code Evolution Client VM (build 0.2-b02-internal, 19.0-b04-internal, mixed mode)

Edited

With some more tests I discovered this problem isn't a Java issue. My project use ATG 10.0.3, and that problem happens when I try override some method that return a component (and just components) and cast it for a more specific class.


Solution

  • You are seeing an example of the Covariant Return problem, explained well here: https://dzone.com/articles/covariant-return-type-abyssal

    The difference you are seeing with plain Java is that ATG is using JavaBeans. If JavaBeans loads the base class first, it sees both a get and set methods. If JavaBeans loads the subclass first, it sees a getter but no corresponding setter (it doesn't match on type, and it's not in the spec to check baseclasses). Because of this dependency, small changes in your server start-up can cause nulls, and you should always avoid this problem.

    The best solution is to make your subclass's getter have a different name. Based on your example, I would use:

    public StoreProfile getStoreProfile() {
        return (StoreProfile) getProfile();
    }