I am getting a class cast exception when using the Visitor Patter. I am pretty sure I have coded things correctly for it to work.
I have a collection of organizations that i need to be able to fetch from an Oracle DB. They are all stored on the same table, but are abstracted into different Java classes through hibernate.
The code that is doing the fetching and then using the visitor to get the proper object is:
Code:
CfgOrg brokerOrg = orgDAO.getOrgByOasysId(message.getOasysAcronym());
if(brokerOrg.isBrokerOrg())
{
return brokerOrg.getBrokerOrg().getMpid();
}
The exception (shown below) is thrown when brokerOrg.getBrokerOrg() is called.
Code:
Caused by: java.lang.ClassCastException: com.itp.gt.data.org.generated.CfgOrg$$EnhancerByCGLIB$$36fb9bea
at com.itp.gt.data.org.generated.CfgOrg$$EnhancerByCGLIB$$36fb9bea.getBrokerOrg(<generated>)
To make things simpler, all the visitor functionality has been coded in an abstract class, AbstractOrg, which all the specific org classes extend.
Code:
public abstract class AbstractOrg
{
public abstract CfgOrgHelper accept(CfgOrgHelper visitor);// implemented in subclasses
public boolean isClientOrg(){ return accept(new CfgOrgHelper()).isClientOrg(); }
public boolean isBrokerOrg(){ return accept(new CfgOrgHelper()).isBrokerOrg(); }
public boolean isSystemOrg(){ return accept(new CfgOrgHelper()).isSystemOrg(); }
public boolean isIntermediateOrg(){ return accept(new CfgOrgHelper()).isIntermediateOrg(); }
public boolean isBankOrg(){ return accept(new CfgOrgHelper()).isBankOrg(); }
public boolean isCounterpartyOrg(){ return accept(new CfgOrgHelper()).isCounterpartyOrg(); }
public boolean isFileHubClientOrg(){ return accept(new CfgOrgHelper()).isFileHubClientOrg(); }
public CfgClientOrg getClientOrg(){ return accept(new CfgOrgHelper()).getClientOrg(); }
public CfgBrokerOrg getBrokerOrg(){ return accept(new CfgOrgHelper()).getBrokerOrg(); }
public CfgSystemOrg getSystemOrg(){ return accept(new CfgOrgHelper()).getSystemOrg(); }
public CfgIntermediateOrg getIntermediateOrg(){ return accept(new CfgOrgHelper()).getIntermediateOrg(); }
public CfgBankOrg getBankOrg(){ return accept(new CfgOrgHelper()).getBankOrg(); }
public CfgCounterpartyOrg getCounterpartyOrg(){ return accept(new CfgOrgHelper()).getCptyOrg(); }
public CfgFileHubClientOrg getFileHubClientOrg(){ return accept(new CfgOrgHelper()).getFileHubClientOrg(); }
//============================= Visitor Implementation ===================================
protected class CfgOrgHelper implements CfgOrgVisitor {
private CfgBrokerOrg brokerOrg;
private CfgClientOrg clientOrg;
private CfgSystemOrg systemOrg;
private CfgCounterpartyOrg cptyOrg;
private CfgBankOrg bankOrg;
private CfgIntermediateOrg intermediateOrg;
private CfgFileHubClientOrg fileHubClientOrg;
public CfgBrokerOrg getBrokerOrg() { return brokerOrg; }
public CfgClientOrg getClientOrg() { return clientOrg; }
public CfgSystemOrg getSystemOrg() { return systemOrg; }
public CfgCounterpartyOrg getCptyOrg() { return cptyOrg; }
public CfgBankOrg getBankOrg() { return bankOrg; }
public CfgIntermediateOrg getIntermediateOrg() { return intermediateOrg; }
public CfgFileHubClientOrg getFileHubClientOrg() { return fileHubClientOrg; }
public void visit(CfgOrg org){
throw new RuntimeException("This method has to be implemented in all subclasses!!");
}
public void visit(CfgBrokerOrg broker){ this.brokerOrg = broker; }
public void visit(CfgClientOrg client){ this.clientOrg = client; }
public void visit(CfgCounterpartyOrg cpty){ this.cptyOrg = cpty; }
public void visit(CfgBankOrg bank){ this.bankOrg = bank; }
public void visit(CfgIntermediateOrg intermediate){ this.intermediateOrg = intermediate; }
public void visit(CfgSystemOrg sys){ this.systemOrg = sys; }
public void visit(CfgFileHubClientOrg fileHub){ this.fileHubClientOrg = fileHub; }
public boolean isBrokerOrg(){ return this.brokerOrg != null; }
public boolean isClientOrg(){ return this.clientOrg != null; }
public boolean isSystemOrg(){ return this.systemOrg != null; }
public boolean isIntermediateOrg(){ return this.intermediateOrg != null; }
public boolean isBankOrg(){ return this.bankOrg != null; }
public boolean isCounterpartyOrg(){ return this.cptyOrg != null; }
public boolean isFileHubClientOrg(){ return this.fileHubClientOrg != null; }
}
}
The subclass CfgBrokerOrg's implementaion of accept:
Code:
public CfgOrgHelper accept(CfgOrgHelper visitor)
{
visitor.visit(this);
return visitor;
}
Does anyone see anything incorrect in this implementation. I would greatly appreciate any help resolving this issue. Thank you.