JDBI3 Returning a Parameterized Class

112 views Asked by At

I have an abstract EntryPoint class that looks like this:

public abstract class EntryPoint<T extends Tag> implements Serializable {

    public EntryPoint(){}

    protected ArrayList<T> tiedTags;

    public abstract ArrayList<T> getTiedTags();
    public abstract void setTiedTags(List<T> tiedTags);

}

I then have subclasses of this EntryPoint class that each accept only one kind of Tag. For instance an SNMPEntryPoint only stores SNMPTags. The subclasses provide implementations of the abstract methods:

public class SNMPEntryPoint extends EntryPoint<SNMPTag> implements Serializable {

    //Default Constructor for Serialization
    public SNMPEntryPoint(){}

    @Override
    public ArrayList<SNMPTag> getTiedTags(){ return tiedTags; }

    //etc

}

Is there a means to create a JDBI query that returns a List of parameterized classes? At the moment, this is what I'm doing:

public List<SNMPEntryPoint> getSNMPEntryPoints(){
    try(Handle handle = daoFactory.getDataSourceController().open()) {
        return handle.createQuery("SELECT * FROM dbo.EntryPoints WHERE Active = 1 AND SiteID = :sID AND Protocol = 'SNMP'")
                .mapToBean(SNMPEntryPoint.class)
                .list();
    }
    catch(Exception e){
        if(sysconfig.getVerbose()){ e.printStackTrace(); }
    }
    return null;
}

But this feels like a raw use of the class. So I guess my question is, if I use this code to generate SNMPEntryPoints and then call getTiedTags or setTiedTags on those EntryPoints, will that work? Am I breaking any coding conventions by creating my SNMPEntryPoint objects in this way?

1

There are 1 answers

0
TheFunk On BEST ANSWER

Just wanted to close this out. My classes weren't actually parameterized. As you can see above they extend the parameterized abstract class however SNMPEntryPoint doesn't actually take any parameters, so there's no need to do anything differently from what I'm doing above.

HOWEVER, if you do need to return a parameterized generic from JDBI you can do so using the details found here.